|
| 1 | +/* global $ */ |
1 | 2 | (function () {
|
2 | 3 | 'use strict'
|
3 | 4 |
|
|
12 | 13 | }
|
13 | 14 | },
|
14 | 15 | callback: (el, name) => {
|
15 |
| - const cb_function = window.hasOwnProperty(name) ? window[name] : null |
16 |
| - if (typeof cb_function === 'function') cb_function(el.data('args')) |
| 16 | + const callbackDefined = Object.prototype.hasOwnProperty.call(window, name) |
| 17 | + const callbackFunction = callbackDefined ? window[name] : null |
| 18 | + if (typeof callbackFunction === 'function') callbackFunction(el.data('args')) |
17 | 19 | else {
|
18 | 20 | el.attr('data-df-errors', 'callback function not found')
|
19 | 21 | console.warn(`activeadmin_dynamic_fields callback function not found: ${name}`)
|
|
33 | 35 | // noinspection EqualityComparisonWithCoercionJS, JSUnusedGlobalSymbols
|
34 | 36 | const CONDITIONS = {
|
35 | 37 | blank: el => el.val().length === 0 || !el.val().trim(),
|
36 |
| - changed: _el => true, |
| 38 | + changed: () => true, |
37 | 39 | checked: el => el.is(':checked'),
|
38 | 40 | eq: (el, value) => el.val() == value,
|
39 | 41 | match: (el, regexp) => regexp.test(el.val()),
|
|
101 | 103 | }
|
102 | 104 |
|
103 | 105 | evaluateCondition() {
|
104 |
| - let value |
105 |
| - if (value = this.el.data('if')) { |
| 106 | + let value = this.el.data('if') |
| 107 | + if (value) { |
106 | 108 | if (REGEXP_NOT.test(value)) value = 'not_' + value.replace(REGEXP_NOT, '')
|
| 109 | + |
107 | 110 | return { condition: CONDITIONS[value] }
|
108 | 111 | }
|
109 |
| - if (value = this.el.data('eq')) { |
| 112 | + |
| 113 | + value = this.el.data('eq') |
| 114 | + if (value) { |
110 | 115 | if (REGEXP_NOT.test(value)) {
|
111 | 116 | return { condition: CONDITIONS['not'], condition_arg: value.replace(REGEXP_NOT, '') }
|
112 | 117 | }
|
| 118 | + |
113 | 119 | return { condition: CONDITIONS['eq'], condition_arg: value }
|
114 | 120 | }
|
115 |
| - if (value = this.el.data('not')) { |
| 121 | + |
| 122 | + value = this.el.data('not') |
| 123 | + if (value) { |
116 | 124 | if (REGEXP_NOT.test(value)) {
|
117 | 125 | return { condition: CONDITIONS['eq'], condition_arg: value.replace(REGEXP_NOT, '') }
|
118 | 126 | }
|
| 127 | + |
119 | 128 | return { condition: CONDITIONS['not'], condition_arg: value }
|
120 | 129 | }
|
121 |
| - if (value = this.el.data('match')) { |
| 130 | + |
| 131 | + value = this.el.data('match') |
| 132 | + if (value) { |
122 | 133 | return { condition: CONDITIONS['match'], condition_arg: new RegExp(value) }
|
123 | 134 | }
|
124 |
| - if (value = this.el.data('mismatch')) { |
| 135 | + |
| 136 | + value = this.el.data('mismatch') |
| 137 | + if (value) { |
125 | 138 | return { condition: CONDITIONS['mismatch'], condition_arg: new RegExp(value) }
|
126 | 139 | }
|
127 | 140 |
|
128 | 141 | this.custom_function = this.el.data('function')
|
129 | 142 | if (this.custom_function) {
|
130 | 143 | value = window[this.custom_function]
|
131 |
| - if (value) return { condition: value } |
| 144 | + if (value) { |
| 145 | + return { condition: value } |
| 146 | + } |
132 | 147 | else {
|
133 | 148 | this.el.attr('data-df-errors', 'custom function not found')
|
134 | 149 | console.warn(`activeadmin_dynamic_fields custom function not found: ${this.custom_function}`)
|
|
174 | 189 | data: { data: data },
|
175 | 190 | method: 'POST',
|
176 | 191 | url: $(this).data('save-url'),
|
177 |
| - complete: function (_req, _status) { |
| 192 | + complete: function () { |
178 | 193 | $(this).data('loading', '0');
|
179 | 194 | },
|
180 |
| - success: function (data, _status, _req) { |
| 195 | + success: function (data) { |
181 | 196 | if (data.status === 'error') {
|
182 | 197 | if ($(this).data('show-errors')) {
|
183 | 198 | let result = '';
|
184 | 199 | let message = data.message;
|
185 | 200 | for (let key in message) {
|
186 |
| - if (message.hasOwnProperty(key) && typeof (message[key]) === 'object') { |
| 201 | + const keyAvailable = Object.prototype.hasOwnProperty.call(message, key) |
| 202 | + if (keyAvailable && typeof (message[key]) === 'object') { |
187 | 203 | if (result) result += ' - ';
|
188 | 204 | result += key + ': ' + message[key].join('; ');
|
189 | 205 | }
|
|
240 | 256 | let title = $(this).attr('title')
|
241 | 257 | $.ajax({
|
242 | 258 | url: $(this).attr('href'),
|
243 |
| - complete: function (_req, _status) { |
| 259 | + complete: function () { |
244 | 260 | $('#df-dialog').data('loading', '0')
|
245 | 261 | },
|
246 |
| - success: function (data, _status, _req) { |
| 262 | + success: function (data) { |
247 | 263 | const dialog = $('#df-dialog')
|
248 | 264 | if (title) dialog.attr('title', title)
|
249 | 265 | dialog.html(data)
|
|
0 commit comments