Skip to content

Commit 7638624

Browse files
committed
JS refactoring
1 parent 08e5de5 commit 7638624

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,68 +52,80 @@
5252

5353
class Field {
5454
constructor(el) {
55-
const action = el.data('then') || el.data('action') || ''
55+
this.el = el
56+
const action_name = this.evaluateAction()
57+
const result = this.evaluateCondition()
58+
this.condition = result.condition
59+
this.condition_arg = result.condition_arg
60+
this.evaluateTarget(action_name)
61+
}
62+
63+
apply() {
64+
if (this.condition(this.el, this.condition_arg)) {
65+
if (this.else_reverse_action) this.else_reverse_action(this.target, this.else_action_arg)
66+
this.action(this.target, this.action_arg)
67+
}
68+
else {
69+
if (this.reverse_action) this.reverse_action(this.target, this.action_arg)
70+
if (this.else_action) this.else_action(this.target, this.else_action_arg)
71+
}
72+
}
73+
74+
evaluateAction() {
75+
const action = this.el.data('then') || this.el.data('action') || ''
5676
const action_name = action.split(' ', 1)[0]
57-
const else_action = el.data('else') || ''
77+
const else_action = this.el.data('else') || ''
5878
const else_action_name = else_action.split(' ', 1)[0]
5979

60-
this.el = el
6180
this.action = ACTIONS[action_name]
6281
this.action_arg = action.substring(action.indexOf(' ') + 1)
6382
this.reverse_action = REVERSE_ACTIONS[action_name]
6483
this.else_action = ACTIONS[else_action_name]
6584
this.else_action_arg = else_action.substring(else_action.indexOf(' ') + 1)
6685
this.else_reverse_action = REVERSE_ACTIONS[else_action_name]
67-
this.condition = CONDITIONS[el.data('if')]
68-
if (!this.condition && el.data('eq')) {
69-
[this.condition, this.condition_arg] = [CONDITIONS['eq'], el.data('eq')]
70-
}
71-
if (!this.condition && el.data('not')) {
72-
[this.condition, this.condition_arg] = [CONDITIONS['not'], el.data('not')]
73-
}
74-
if (!this.condition && el.data('match')) {
75-
[this.condition, this.condition_arg] = [CONDITIONS['match'], new RegExp(el.data('match'))]
76-
}
77-
if (!this.condition && el.data('mismatch')) {
78-
[this.condition, this.condition_arg] = [CONDITIONS['mismatch'], new RegExp(el.data('mismatch'))]
79-
}
80-
this.custom_function = el.data('function')
81-
if (!this.condition && this.custom_function) {
82-
this.condition = window[this.custom_function]
83-
if (!this.condition) {
84-
el.attr('data-df-errors', 'custom function not found')
86+
87+
return action_name
88+
}
89+
90+
evaluateCondition() {
91+
let value = CONDITIONS[this.el.data('if')?.trim()]
92+
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) }
97+
98+
this.custom_function = this.el.data('function')
99+
if (this.custom_function) {
100+
value = window[this.custom_function]
101+
if (value) return { condition: value }
102+
else {
103+
this.el.attr('data-df-errors', 'custom function not found')
85104
console.warn(`activeadmin_dynamic_fields custom function not found: ${this.custom_function}`)
86105
}
87106
}
88107

89-
// closest find for has many associations
90-
if (el.data('target')) this.target = el.closest('fieldset').find(el.data('target'))
91-
else if (el.data('gtarget')) this.target = $(el.data('gtarget'))
92-
if (action_name == 'callback') this.target = el
108+
return {}
93109
}
94110

95-
apply(el) {
96-
if (this.condition(el, this.condition_arg)) {
97-
if (this.else_reverse_action) this.else_reverse_action(this.target, this.else_action_arg)
98-
this.action(this.target, this.action_arg)
99-
}
100-
else {
101-
if (this.reverse_action) this.reverse_action(this.target, this.action_arg)
102-
if (this.else_action) this.else_action(this.target, this.else_action_arg)
103-
}
111+
evaluateTarget(action_name) {
112+
// closest find for has many associations
113+
if (this.el.data('target')) this.target = this.el.closest('fieldset').find(this.el.data('target'))
114+
else if (this.el.data('gtarget')) this.target = $(this.el.data('gtarget'))
115+
if (action_name == 'callback') this.target = this.el
104116
}
105117

106-
is_valid() {
118+
isValid() {
107119
if (!this.condition) return false
108120
if (!this.action && !this.custom_function) return false
109121

110122
return true
111123
}
112124

113125
setup() {
114-
if (!this.is_valid()) return
115-
if (this.el.data('if') != 'changed') this.apply(this.el)
116-
this.el.on('change', () => this.apply(this.el))
126+
if (!this.isValid()) return
127+
if (this.el.data('if') != 'changed') this.apply()
128+
this.el.on('change', () => this.apply())
117129
}
118130
}
119131

0 commit comments

Comments
 (0)