Skip to content

Commit 0663a81

Browse files
committed
Add Field class
1 parent 0b287bd commit 0663a81

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,51 +33,58 @@
3333
slide: el => el.slideDown()
3434
}
3535

36-
function dfEvalCondition(el) {
37-
let condition = CONDITIONS[el.data('if')]
38-
let condition_arg
36+
class Field {
37+
constructor(el) {
38+
const action = (el.data('then') || el.data('action') || '')
39+
const action_name = action.split(' ', 1)[0]
40+
41+
this.el = el
42+
this.action = ACTIONS[action_name]
43+
this.action_arg = action.substring(action.indexOf(' ') + 1)
44+
this.reverse_action = REVERSE_ACTIONS[action_name]
45+
this.condition = CONDITIONS[el.data('if')]
46+
if (!this.condition && el.data('eq')) {
47+
[this.condition, this.condition_arg] = [CONDITIONS['eq'], el.data('eq')]
48+
}
49+
if (!this.condition && el.data('not')) {
50+
[this.condition, this.condition_arg] = [CONDITIONS['not'], el.data('not')]
51+
}
52+
this.custom_function = el.data('function')
53+
if (!this.condition && this.custom_function) {
54+
this.condition = window[this.custom_function]
55+
if (!this.condition) {
56+
el.attr('data-df-errors', 'custom function not found')
57+
console.warn(`activeadmin_dynamic_fields custom function not found: ${this.custom_function}`)
58+
}
59+
}
3960

40-
if(!condition && el.data('eq')) {
41-
condition = CONDITIONS['eq']
42-
condition_arg = el.data('eq')
61+
// closest find for has many associations
62+
if (el.data('target')) this.target = el.closest('fieldset').find(el.data('target'))
63+
else if (el.data('gtarget')) this.target = $(el.data('gtarget'))
64+
if (action_name == 'callback') this.target = el
4365
}
44-
if(!condition && el.data('not')) {
45-
condition = CONDITIONS['not']
46-
condition_arg = el.data('not')
47-
}
48-
if(!condition && el.data('function')) {
49-
condition = window[el.data('function')]
50-
if(!condition) {
51-
el.attr('data-df-errors', 'custom function not found')
52-
console.warn(`activeadmin_dynamic_fields custom function not found: ${el.data('function')}`)
66+
67+
apply(el) {
68+
if (this.condition(el, this.condition_arg)) {
69+
this.action(this.target, this.action_arg)
70+
}
71+
else {
72+
if (this.reverse_action) this.reverse_action(this.target, this.action_arg)
5373
}
5474
}
5575

56-
return [condition, condition_arg]
57-
}
76+
is_valid() {
77+
if (!this.condition) return false
78+
if (!this.action && !this.custom_function) return false
5879

59-
function dfInitField(el) {
60-
const [condition, condition_arg] = dfEvalCondition(el)
61-
const action_name = (el.data('then') || el.data('action') || '').substr(0, 8)
62-
const action = ACTIONS[action_name]
63-
const arg = (el.data('then') || el.data('action') || '').substr(9)
64-
const reverse_action = REVERSE_ACTIONS[action_name]
65-
if (typeof condition === 'undefined') return
66-
if (typeof action === 'undefined' && !el.data('function')) return
67-
68-
// closest find for has many associations
69-
let target
70-
if (el.data('target')) target = el.closest('fieldset').find(el.data('target'))
71-
else if (el.data('gtarget')) target = $(el.data('gtarget'))
72-
if (action_name == 'callback') target = el
73-
74-
if (condition(el, condition_arg) && el.data('if') != 'changed') action(target, arg)
75-
else if (reverse_action) reverse_action(target, arg)
76-
77-
el.on('change', () => {
78-
if (condition(el, condition_arg)) action(target, arg)
79-
else if (reverse_action) reverse_action(target, arg)
80-
})
80+
return true
81+
}
82+
83+
setup() {
84+
if (!this.is_valid()) return
85+
if (this.el.data('if') != 'changed') this.apply(this.el)
86+
this.el.on('change', () => this.apply(this.el))
87+
}
8188
}
8289

8390
// Set the value of an element
@@ -144,14 +151,14 @@
144151
// Setup dynamic fields
145152
const selectors = '.active_admin .input [data-if], .active_admin .input [data-eq], .active_admin .input [data-not], .active_admin .input [data-function]'
146153
$(selectors).each(function () {
147-
dfInitField($(this))
154+
new Field($(this)).setup()
148155
})
149156

150157
// Setup dynamic fields for associations
151158
$('.active_admin .has_many_container').on('has_many_add:after', () => {
152159
$(selectors).each(function () {
153-
dfInitField($(this))
154-
})
160+
new Field($(this)).setup()
161+
})
155162
})
156163

157164
// Set dialog icon link

0 commit comments

Comments
 (0)