Skip to content

Commit fc8e708

Browse files
authored
Merge pull request #32 from blocknotes/js/minor-refactoring
Small JS refactoring
2 parents d70fb2b + 5a993c3 commit fc8e708

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
/.rspec_failures
55
/.rubocop-*
66
/Gemfile.lock
7+
/eslint.config.mjs
8+
/package-lock.json
79
/gemfiles/.bundle/config
810

911
/_misc/
1012
/coverage/
13+
/node_modules/
1114
/spec/dummy/db/*.sqlite3*
1215
/spec/dummy/log/
1316
/spec/dummy/storage/

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* global $ */
12
(function () {
23
'use strict'
34

@@ -12,8 +13,9 @@
1213
}
1314
},
1415
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'))
1719
else {
1820
el.attr('data-df-errors', 'callback function not found')
1921
console.warn(`activeadmin_dynamic_fields callback function not found: ${name}`)
@@ -33,7 +35,7 @@
3335
// noinspection EqualityComparisonWithCoercionJS, JSUnusedGlobalSymbols
3436
const CONDITIONS = {
3537
blank: el => el.val().length === 0 || !el.val().trim(),
36-
changed: _el => true,
38+
changed: () => true,
3739
checked: el => el.is(':checked'),
3840
eq: (el, value) => el.val() == value,
3941
match: (el, regexp) => regexp.test(el.val()),
@@ -101,34 +103,47 @@
101103
}
102104

103105
evaluateCondition() {
104-
let value
105-
if (value = this.el.data('if')) {
106+
let value = this.el.data('if')
107+
if (value) {
106108
if (REGEXP_NOT.test(value)) value = 'not_' + value.replace(REGEXP_NOT, '')
109+
107110
return { condition: CONDITIONS[value] }
108111
}
109-
if (value = this.el.data('eq')) {
112+
113+
value = this.el.data('eq')
114+
if (value) {
110115
if (REGEXP_NOT.test(value)) {
111116
return { condition: CONDITIONS['not'], condition_arg: value.replace(REGEXP_NOT, '') }
112117
}
118+
113119
return { condition: CONDITIONS['eq'], condition_arg: value }
114120
}
115-
if (value = this.el.data('not')) {
121+
122+
value = this.el.data('not')
123+
if (value) {
116124
if (REGEXP_NOT.test(value)) {
117125
return { condition: CONDITIONS['eq'], condition_arg: value.replace(REGEXP_NOT, '') }
118126
}
127+
119128
return { condition: CONDITIONS['not'], condition_arg: value }
120129
}
121-
if (value = this.el.data('match')) {
130+
131+
value = this.el.data('match')
132+
if (value) {
122133
return { condition: CONDITIONS['match'], condition_arg: new RegExp(value) }
123134
}
124-
if (value = this.el.data('mismatch')) {
135+
136+
value = this.el.data('mismatch')
137+
if (value) {
125138
return { condition: CONDITIONS['mismatch'], condition_arg: new RegExp(value) }
126139
}
127140

128141
this.custom_function = this.el.data('function')
129142
if (this.custom_function) {
130143
value = window[this.custom_function]
131-
if (value) return { condition: value }
144+
if (value) {
145+
return { condition: value }
146+
}
132147
else {
133148
this.el.attr('data-df-errors', 'custom function not found')
134149
console.warn(`activeadmin_dynamic_fields custom function not found: ${this.custom_function}`)
@@ -174,16 +189,17 @@
174189
data: { data: data },
175190
method: 'POST',
176191
url: $(this).data('save-url'),
177-
complete: function (_req, _status) {
192+
complete: function () {
178193
$(this).data('loading', '0');
179194
},
180-
success: function (data, _status, _req) {
195+
success: function (data) {
181196
if (data.status === 'error') {
182197
if ($(this).data('show-errors')) {
183198
let result = '';
184199
let message = data.message;
185200
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') {
187203
if (result) result += ' - ';
188204
result += key + ': ' + message[key].join('; ');
189205
}
@@ -240,10 +256,10 @@
240256
let title = $(this).attr('title')
241257
$.ajax({
242258
url: $(this).attr('href'),
243-
complete: function (_req, _status) {
259+
complete: function () {
244260
$('#df-dialog').data('loading', '0')
245261
},
246-
success: function (data, _status, _req) {
262+
success: function (data) {
247263
const dialog = $('#df-dialog')
248264
if (title) dialog.attr('title', title)
249265
dialog.html(data)

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,10 @@
99
"files": [
1010
"app/**/*",
1111
"index.js"
12-
]
12+
],
13+
"devDependencies": {
14+
"@eslint/js": "^9.22.0",
15+
"eslint": "^9.22.0",
16+
"globals": "^16.0.0"
17+
}
1318
}

0 commit comments

Comments
 (0)