Skip to content

Commit 815341f

Browse files
committed
chore: Small JS refactoring
Address ESLint offenses.
1 parent 6202e5d commit 815341f

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ Actions:
6363
+ **hide**: hides elements (ex. `"data-then": "hide", "data-target": ".errors"`)
6464
+ **slide**: hides elements (using sliding)
6565
+ **fade**: hides elements (using fading)
66-
+ **addClass**: adds classes (ex. `"data-then": "addClass red"`)
67-
+ **addStyle**: adds some styles (ex. `"data-then": "addStyle color: #fb1; font-size: 12px"`)
68-
+ **setText**: set the text of an element (ex. `"data-then": "setText A sample text"`)
69-
+ **setValue**: set the value of an input element (ex. `"data-then": "setValue A sample value"`)
66+
+ **addClass**: adds classes (ex. `"data-then": "addClass", "data-args": "red"`)
67+
+ **addStyle**: adds some styles (ex. `"data-then": "addStyle", "data-args": "color: #fb1; font-size: 12px"`)
68+
+ **setText**: set the text of an element (ex. `"data-then": "setText", "data-args": "A sample text"`)
69+
+ **setValue**: set the value of an input element (ex. `"data-then": "setValue", "data-args": "A sample value"`)
7070
+ **callback**: call a function (with arguments: **data-args**) (ex. `"data-then": "callback a_fun"`)
71+
- **data-args**: arguments passed to the triggered action (or to the callback function)
7172
- **data-else**: action to trigger when the condition check is not true
72-
- **data-args**: arguments passed to the callback function
73+
- **data-else-args**: arguments passed to the triggered else action
7374

7475
Targets:
7576

@@ -98,14 +99,14 @@ end
9899
- Add 3 classes (*first*, *second*, *third*) if a checkbox is not checked, else add "forth" class:
99100

100101
```rb
101-
data = { if: 'not_checked', then: 'addClass first second third', target: '.grp1', else: 'addClass forth' }
102+
data = { if: 'not_checked', then: 'addClass', args: 'first second third', target: '.grp1', else: 'addClass', 'else-args': 'forth' }
102103
f.input :published, input_html: { data: data }
103104
```
104105

105106
- Set another field value if a string field is blank:
106107

107108
```rb
108-
f.input :title, input_html: { data: { if: 'blank', then: 'setValue 10', target: '#article_position' } }
109+
f.input :title, input_html: { data: { if: 'blank', then: 'setValue', args: '10', target: '#article_position' } }
109110
```
110111

111112
- Use a custom function for conditional check (*title_not_empty()* must be available on global scope) (with alternative syntax for data attributes):

app/assets/javascripts/activeadmin/dynamic_fields.js

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable no-undef */
1+
/* global $ */
22
(function () {
33
'use strict'
44

@@ -13,8 +13,9 @@
1313
}
1414
},
1515
callback: (el, name) => {
16-
const cb_function = window.hasOwnProperty(name) ? window[name] : null
17-
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'))
1819
else {
1920
el.attr('data-df-errors', 'callback function not found')
2021
console.warn(`activeadmin_dynamic_fields callback function not found: ${name}`)
@@ -34,7 +35,7 @@
3435
// noinspection EqualityComparisonWithCoercionJS, JSUnusedGlobalSymbols
3536
const CONDITIONS = {
3637
blank: el => el.val().length === 0 || !el.val().trim(),
37-
changed: _el => true,
38+
changed: () => true,
3839
checked: el => el.is(':checked'),
3940
eq: (el, value) => el.val() == value,
4041
match: (el, regexp) => regexp.test(el.val()),
@@ -102,34 +103,47 @@
102103
}
103104

104105
evaluateCondition() {
105-
let value
106-
if (value = this.el.data('if')) {
106+
let value = this.el.data('if')
107+
if (value) {
107108
if (REGEXP_NOT.test(value)) value = 'not_' + value.replace(REGEXP_NOT, '')
109+
108110
return { condition: CONDITIONS[value] }
109111
}
110-
if (value = this.el.data('eq')) {
112+
113+
value = this.el.data('eq')
114+
if (value) {
111115
if (REGEXP_NOT.test(value)) {
112116
return { condition: CONDITIONS['not'], condition_arg: value.replace(REGEXP_NOT, '') }
113117
}
118+
114119
return { condition: CONDITIONS['eq'], condition_arg: value }
115120
}
116-
if (value = this.el.data('not')) {
121+
122+
value = this.el.data('not')
123+
if (value) {
117124
if (REGEXP_NOT.test(value)) {
118125
return { condition: CONDITIONS['eq'], condition_arg: value.replace(REGEXP_NOT, '') }
119126
}
127+
120128
return { condition: CONDITIONS['not'], condition_arg: value }
121129
}
122-
if (value = this.el.data('match')) {
130+
131+
value = this.el.data('match')
132+
if (value) {
123133
return { condition: CONDITIONS['match'], condition_arg: new RegExp(value) }
124134
}
125-
if (value = this.el.data('mismatch')) {
135+
136+
value = this.el.data('mismatch')
137+
if (value) {
126138
return { condition: CONDITIONS['mismatch'], condition_arg: new RegExp(value) }
127139
}
128140

129141
this.custom_function = this.el.data('function')
130142
if (this.custom_function) {
131143
value = window[this.custom_function]
132-
if (value) return { condition: value }
144+
if (value) {
145+
return { condition: value }
146+
}
133147
else {
134148
this.el.attr('data-df-errors', 'custom function not found')
135149
console.warn(`activeadmin_dynamic_fields custom function not found: ${this.custom_function}`)
@@ -175,16 +189,17 @@
175189
data: { data: data },
176190
method: 'POST',
177191
url: $(this).data('save-url'),
178-
complete: function (_req, _status) {
192+
complete: function () {
179193
$(this).data('loading', '0');
180194
},
181-
success: function (data, _status, _req) {
195+
success: function (data) {
182196
if (data.status === 'error') {
183197
if ($(this).data('show-errors')) {
184198
let result = '';
185199
let message = data.message;
186200
for (let key in message) {
187-
if (message.hasOwnProperty(key) && typeof (message[key]) === 'object') {
201+
const keyAvailable = Object.prototype.hasOwnProperty.call(message, key)
202+
if (keyAvailable && typeof (message[key]) === 'object') {
188203
if (result) result += ' - ';
189204
result += key + ': ' + message[key].join('; ');
190205
}
@@ -241,10 +256,10 @@
241256
let title = $(this).attr('title')
242257
$.ajax({
243258
url: $(this).attr('href'),
244-
complete: function (_req, _status) {
259+
complete: function () {
245260
$('#df-dialog').data('loading', '0')
246261
},
247-
success: function (data, _status, _req) {
262+
success: function (data) {
248263
const dialog = $('#df-dialog')
249264
if (title) dialog.attr('title', title)
250265
dialog.html(data)

0 commit comments

Comments
 (0)