|
1 | 1 | (function () { |
2 | 2 | 'use strict' |
3 | 3 |
|
| 4 | + // noinspection JSUnusedGlobalSymbols |
4 | 5 | const ACTIONS = { |
5 | 6 | addClass: (el, name) => el.addClass(name), |
6 | 7 | addStyle: (el, extra_style) => { |
|
11 | 12 | } |
12 | 13 | }, |
13 | 14 | callback: (el, name) => { |
14 | | - if (window[name]) window[name](el.data('args')) |
| 15 | + const cb_function = window.hasOwnProperty(name) ? window[name] : null |
| 16 | + if (typeof cb_function === 'function') cb_function(el.data('args')) |
15 | 17 | else { |
16 | 18 | el.attr('data-df-errors', 'callback function not found') |
17 | 19 | console.warn(`activeadmin_dynamic_fields callback function not found: ${name}`) |
|
21 | 23 | hide: el => el.hide(), |
22 | 24 | setText: (el, text) => el.text(text), |
23 | 25 | setValue: (el, value) => { |
24 | | - if (el.attr('type') == 'checkbox') el.prop('checked', value == '1') |
| 26 | + if (el.attr('type') === 'checkbox') el.prop('checked', value === '1') |
25 | 27 | else el.val(value) |
26 | 28 | el.trigger('change') |
27 | 29 | }, |
28 | 30 | slide: el => el.slideUp() |
29 | 31 | } |
30 | 32 |
|
| 33 | + // noinspection EqualityComparisonWithCoercionJS, JSUnusedGlobalSymbols |
31 | 34 | const CONDITIONS = { |
32 | 35 | blank: el => el.val().length === 0 || !el.val().trim(), |
33 | 36 | changed: _el => true, |
|
90 | 93 | evaluateCondition() { |
91 | 94 | let value = CONDITIONS[this.el.data('if')?.trim()] |
92 | 95 | if (value) return { condition: value } |
93 | | - if (value = this.el.data('eq')) return { condition: CONDITIONS['eq'], condition_arg: value } |
94 | | - if (value = this.el.data('not')) return { condition: CONDITIONS['not'], condition_arg: value } |
95 | | - if (value = this.el.data('match')) return { condition: CONDITIONS['match'], condition_arg: new RegExp(value) } |
96 | | - if (value = this.el.data('mismatch')) return { condition: CONDITIONS['mismatch'], condition_arg: new RegExp(value) } |
| 96 | + |
| 97 | + value = this.el.data('eq') |
| 98 | + if (value) return { condition: CONDITIONS['eq'], condition_arg: value } |
| 99 | + |
| 100 | + value = this.el.data('not') |
| 101 | + if (value) return { condition: CONDITIONS['not'], condition_arg: value } |
| 102 | + |
| 103 | + value = this.el.data('match') |
| 104 | + if (value) return { condition: CONDITIONS['match'], condition_arg: new RegExp(value) } |
| 105 | + |
| 106 | + value = this.el.data('mismatch') |
| 107 | + if (value) return { condition: CONDITIONS['mismatch'], condition_arg: new RegExp(value) } |
97 | 108 |
|
98 | 109 | this.custom_function = this.el.data('function') |
99 | 110 | if (this.custom_function) { |
|
112 | 123 | // closest find for has many associations |
113 | 124 | if (this.el.data('target')) this.target = this.el.closest('fieldset').find(this.el.data('target')) |
114 | 125 | else if (this.el.data('gtarget')) this.target = $(this.el.data('gtarget')) |
115 | | - if (action_name == 'callback') this.target = this.el |
| 126 | + if (action_name === 'callback') this.target = this.el |
116 | 127 | } |
117 | 128 |
|
118 | 129 | isValid() { |
119 | 130 | if (!this.condition) return false |
120 | | - if (!this.action && !this.custom_function) return false |
121 | | - |
122 | | - return true |
| 131 | + return (this.action || this.custom_function) |
123 | 132 | } |
124 | 133 |
|
125 | 134 | setup() { |
126 | 135 | if (!this.isValid()) return |
127 | | - if (this.el.data('if') != 'changed') this.apply() |
| 136 | + if (this.el.data('if') !== 'changed') this.apply() |
128 | 137 | this.el.on('change', () => this.apply()) |
129 | 138 | } |
130 | 139 | } |
131 | 140 |
|
132 | | - // Inline update - must be called binded on the editing element |
| 141 | + // Inline update - must be called bound on the editing element |
133 | 142 | function dfUpdateField() { |
134 | | - if ($(this).data('loading') != '1') { |
| 143 | + if ($(this).data('loading') !== '1') { |
135 | 144 | $(this).data('loading', '1'); |
136 | 145 | let _this = $(this); |
137 | 146 | let type = $(this).data('field-type'); |
138 | 147 | let new_value; |
139 | | - if (type == 'boolean') new_value = !$(this).data('field-value'); |
140 | | - else if (type == 'select') new_value = $(this).val(); |
| 148 | + if (type === 'boolean') new_value = !$(this).data('field-value'); |
| 149 | + else if (type === 'select') new_value = $(this).val(); |
141 | 150 | else new_value = $(this).text(); |
142 | 151 | let data = {}; |
143 | 152 | data[$(this).data('field')] = new_value; |
|
146 | 155 | data: { data: data }, |
147 | 156 | method: 'POST', |
148 | 157 | url: $(this).data('save-url'), |
149 | | - complete: function (req, status) { |
| 158 | + complete: function (_req, _status) { |
150 | 159 | $(this).data('loading', '0'); |
151 | 160 | }, |
152 | | - success: function (data, status, req) { |
153 | | - if (data.status == 'error') { |
| 161 | + success: function (data, _status, _req) { |
| 162 | + if (data.status === 'error') { |
154 | 163 | if ($(this).data('show-errors')) { |
155 | 164 | let result = ''; |
156 | 165 | let message = data.message; |
157 | 166 | for (let key in message) { |
158 | | - if (typeof (message[key]) === 'object') { |
| 167 | + if (message.hasOwnProperty(key) && typeof (message[key]) === 'object') { |
159 | 168 | if (result) result += ' - '; |
160 | 169 | result += key + ': ' + message[key].join('; '); |
161 | 170 | } |
|
205 | 214 | $('.active_admin [data-df-dialog]').on('click', function (event) { |
206 | 215 | event.preventDefault() |
207 | 216 | $(this).blur() |
208 | | - if ($('#df-dialog').data('loading') != '1') { |
209 | | - $('#df-dialog').data('loading', '1') |
210 | | - if ($('#df-dialog').length == 0) $('body').append('<div id="df-dialog"></div>') |
| 217 | + const df_dialog = $('#df-dialog') |
| 218 | + |
| 219 | + if (df_dialog.data('loading') !== '1') { |
| 220 | + df_dialog.data('loading', '1') |
| 221 | + if (df_dialog.length === 0) $('body').append('<div id="df-dialog"></div>') |
211 | 222 | let title = $(this).attr('title') |
212 | 223 | $.ajax({ |
213 | 224 | url: $(this).attr('href'), |
214 | | - complete: function (req, status) { |
| 225 | + complete: function (_req, _status) { |
215 | 226 | $('#df-dialog').data('loading', '0') |
216 | 227 | }, |
217 | | - success: function (data, status, req) { |
218 | | - if (title) $('#df-dialog').attr('title', title) |
219 | | - $('#df-dialog').html(data) |
220 | | - $('#df-dialog').dialog({ modal: true }) |
221 | | - }, |
| 228 | + success: function (data, _status, _req) { |
| 229 | + const dialog = $('#df-dialog') |
| 230 | + if (title) dialog.attr('title', title) |
| 231 | + dialog.html(data) |
| 232 | + dialog.dialog({ modal: true }) |
| 233 | + } |
222 | 234 | }) |
223 | 235 | } |
224 | 236 | }) |
|
231 | 243 | $(this).data('field-value', $(this).text()) |
232 | 244 | let fnUpdate = $.proxy(dfUpdateField, $(this)) |
233 | 245 | $(this).on('blur', function () { |
234 | | - if ($(this).data('field-value') != $(this).text()) fnUpdate() |
| 246 | + if ($(this).data('field-value') !== $(this).text()) fnUpdate() |
235 | 247 | }) |
236 | 248 | }) |
237 | 249 | $('[data-field][data-field-type="select"][data-save-url]').each(function () { |
|
0 commit comments