|
12 | 12 | }, |
13 | 13 | fade: el => el.fadeOut(), |
14 | 14 | hide: el => el.hide(), |
15 | | - setValue: (el, value) => dfSetValue(el, value), |
| 15 | + setValue: (el, value) => { |
| 16 | + if (el.attr('type') == 'checkbox') el.prop('checked', value == '1') |
| 17 | + else el.val(value) |
| 18 | + el.trigger('change') |
| 19 | + }, |
16 | 20 | slide: el => el.slideUp() |
17 | 21 | } |
18 | 22 |
|
|
33 | 37 | slide: el => el.slideDown() |
34 | 38 | } |
35 | 39 |
|
36 | | - function dfEvalCondition(el) { |
37 | | - let condition = CONDITIONS[el.data('if')] |
38 | | - let condition_arg |
| 40 | + class Field { |
| 41 | + constructor(el) { |
| 42 | + const action = el.data('then') || el.data('action') || '' |
| 43 | + const action_name = action.split(' ', 1)[0] |
| 44 | + const else_action = el.data('else') || '' |
| 45 | + const else_action_name = else_action.split(' ', 1)[0] |
| 46 | + |
| 47 | + this.el = el |
| 48 | + this.action = ACTIONS[action_name] |
| 49 | + this.action_arg = action.substring(action.indexOf(' ') + 1) |
| 50 | + this.reverse_action = REVERSE_ACTIONS[action_name] |
| 51 | + this.else_action = ACTIONS[else_action_name] |
| 52 | + this.else_action_arg = else_action.substring(else_action.indexOf(' ') + 1) |
| 53 | + this.else_reverse_action = REVERSE_ACTIONS[else_action_name] |
| 54 | + this.condition = CONDITIONS[el.data('if')] |
| 55 | + if (!this.condition && el.data('eq')) { |
| 56 | + [this.condition, this.condition_arg] = [CONDITIONS['eq'], el.data('eq')] |
| 57 | + } |
| 58 | + if (!this.condition && el.data('not')) { |
| 59 | + [this.condition, this.condition_arg] = [CONDITIONS['not'], el.data('not')] |
| 60 | + } |
| 61 | + this.custom_function = el.data('function') |
| 62 | + if (!this.condition && this.custom_function) { |
| 63 | + this.condition = window[this.custom_function] |
| 64 | + if (!this.condition) { |
| 65 | + el.attr('data-df-errors', 'custom function not found') |
| 66 | + console.warn(`activeadmin_dynamic_fields custom function not found: ${this.custom_function}`) |
| 67 | + } |
| 68 | + } |
39 | 69 |
|
40 | | - if(!condition && el.data('eq')) { |
41 | | - condition = CONDITIONS['eq'] |
42 | | - condition_arg = el.data('eq') |
43 | | - } |
44 | | - if(!condition && el.data('not')) { |
45 | | - condition = CONDITIONS['not'] |
46 | | - condition_arg = el.data('not') |
| 70 | + // closest find for has many associations |
| 71 | + if (el.data('target')) this.target = el.closest('fieldset').find(el.data('target')) |
| 72 | + else if (el.data('gtarget')) this.target = $(el.data('gtarget')) |
| 73 | + if (action_name == 'callback') this.target = el |
47 | 74 | } |
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')}`) |
| 75 | + |
| 76 | + apply(el) { |
| 77 | + if (this.condition(el, this.condition_arg)) { |
| 78 | + if (this.else_reverse_action) this.else_reverse_action(this.target, this.else_action_arg) |
| 79 | + this.action(this.target, this.action_arg) |
| 80 | + } |
| 81 | + else { |
| 82 | + if (this.reverse_action) this.reverse_action(this.target, this.action_arg) |
| 83 | + if (this.else_action) this.else_action(this.target, this.else_action_arg) |
53 | 84 | } |
54 | 85 | } |
55 | 86 |
|
56 | | - return [condition, condition_arg] |
57 | | - } |
| 87 | + is_valid() { |
| 88 | + if (!this.condition) return false |
| 89 | + if (!this.action && !this.custom_function) return false |
58 | 90 |
|
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 | | - }) |
81 | | - } |
| 91 | + return true |
| 92 | + } |
82 | 93 |
|
83 | | - // Set the value of an element |
84 | | - function dfSetValue(el, val) { |
85 | | - if (el.attr('type') == 'checkbox') el.prop('checked', val == '1') |
86 | | - else el.val(val) |
87 | | - el.trigger('change') |
| 94 | + setup() { |
| 95 | + if (!this.is_valid()) return |
| 96 | + if (this.el.data('if') != 'changed') this.apply(this.el) |
| 97 | + this.el.on('change', () => this.apply(this.el)) |
| 98 | + } |
88 | 99 | } |
89 | 100 |
|
90 | 101 | // Inline update - must be called binded on the editing element |
|
144 | 155 | // Setup dynamic fields |
145 | 156 | const selectors = '.active_admin .input [data-if], .active_admin .input [data-eq], .active_admin .input [data-not], .active_admin .input [data-function]' |
146 | 157 | $(selectors).each(function () { |
147 | | - dfInitField($(this)) |
| 158 | + new Field($(this)).setup() |
148 | 159 | }) |
149 | 160 |
|
150 | 161 | // Setup dynamic fields for associations |
151 | 162 | $('.active_admin .has_many_container').on('has_many_add:after', () => { |
152 | 163 | $(selectors).each(function () { |
153 | | - dfInitField($(this)) |
154 | | - }) |
| 164 | + new Field($(this)).setup() |
| 165 | + }) |
155 | 166 | }) |
156 | 167 |
|
157 | 168 | // Set dialog icon link |
|
0 commit comments