Skip to content

Commit d7c4fe6

Browse files
author
Mattia Roccoberton
committed
Minor JS refactoring
1 parent ffd58f5 commit d7c4fe6

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(function () {
22
'use strict'
33

4+
// noinspection JSUnusedGlobalSymbols
45
const ACTIONS = {
56
addClass: (el, name) => el.addClass(name),
67
addStyle: (el, extra_style) => {
@@ -11,7 +12,8 @@
1112
}
1213
},
1314
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'))
1517
else {
1618
el.attr('data-df-errors', 'callback function not found')
1719
console.warn(`activeadmin_dynamic_fields callback function not found: ${name}`)
@@ -21,13 +23,14 @@
2123
hide: el => el.hide(),
2224
setText: (el, text) => el.text(text),
2325
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')
2527
else el.val(value)
2628
el.trigger('change')
2729
},
2830
slide: el => el.slideUp()
2931
}
3032

33+
// noinspection EqualityComparisonWithCoercionJS, JSUnusedGlobalSymbols
3134
const CONDITIONS = {
3235
blank: el => el.val().length === 0 || !el.val().trim(),
3336
changed: _el => true,
@@ -90,10 +93,18 @@
9093
evaluateCondition() {
9194
let value = CONDITIONS[this.el.data('if')?.trim()]
9295
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) }
97108

98109
this.custom_function = this.el.data('function')
99110
if (this.custom_function) {
@@ -112,32 +123,30 @@
112123
// closest find for has many associations
113124
if (this.el.data('target')) this.target = this.el.closest('fieldset').find(this.el.data('target'))
114125
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
116127
}
117128

118129
isValid() {
119130
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)
123132
}
124133

125134
setup() {
126135
if (!this.isValid()) return
127-
if (this.el.data('if') != 'changed') this.apply()
136+
if (this.el.data('if') !== 'changed') this.apply()
128137
this.el.on('change', () => this.apply())
129138
}
130139
}
131140

132-
// Inline update - must be called binded on the editing element
141+
// Inline update - must be called bound on the editing element
133142
function dfUpdateField() {
134-
if ($(this).data('loading') != '1') {
143+
if ($(this).data('loading') !== '1') {
135144
$(this).data('loading', '1');
136145
let _this = $(this);
137146
let type = $(this).data('field-type');
138147
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();
141150
else new_value = $(this).text();
142151
let data = {};
143152
data[$(this).data('field')] = new_value;
@@ -146,16 +155,16 @@
146155
data: { data: data },
147156
method: 'POST',
148157
url: $(this).data('save-url'),
149-
complete: function (req, status) {
158+
complete: function (_req, _status) {
150159
$(this).data('loading', '0');
151160
},
152-
success: function (data, status, req) {
153-
if (data.status == 'error') {
161+
success: function (data, _status, _req) {
162+
if (data.status === 'error') {
154163
if ($(this).data('show-errors')) {
155164
let result = '';
156165
let message = data.message;
157166
for (let key in message) {
158-
if (typeof (message[key]) === 'object') {
167+
if (message.hasOwnProperty(key) && typeof (message[key]) === 'object') {
159168
if (result) result += ' - ';
160169
result += key + ': ' + message[key].join('; ');
161170
}
@@ -205,20 +214,23 @@
205214
$('.active_admin [data-df-dialog]').on('click', function (event) {
206215
event.preventDefault()
207216
$(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>')
211222
let title = $(this).attr('title')
212223
$.ajax({
213224
url: $(this).attr('href'),
214-
complete: function (req, status) {
225+
complete: function (_req, _status) {
215226
$('#df-dialog').data('loading', '0')
216227
},
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+
}
222234
})
223235
}
224236
})
@@ -231,7 +243,7 @@
231243
$(this).data('field-value', $(this).text())
232244
let fnUpdate = $.proxy(dfUpdateField, $(this))
233245
$(this).on('blur', function () {
234-
if ($(this).data('field-value') != $(this).text()) fnUpdate()
246+
if ($(this).data('field-value') !== $(this).text()) fnUpdate()
235247
})
236248
})
237249
$('[data-field][data-field-type="select"][data-save-url]').each(function () {

0 commit comments

Comments
 (0)