|
52 | 52 |
|
53 | 53 | class Field { |
54 | 54 | 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') || '' |
56 | 76 | const action_name = action.split(' ', 1)[0] |
57 | | - const else_action = el.data('else') || '' |
| 77 | + const else_action = this.el.data('else') || '' |
58 | 78 | const else_action_name = else_action.split(' ', 1)[0] |
59 | 79 |
|
60 | | - this.el = el |
61 | 80 | this.action = ACTIONS[action_name] |
62 | 81 | this.action_arg = action.substring(action.indexOf(' ') + 1) |
63 | 82 | this.reverse_action = REVERSE_ACTIONS[action_name] |
64 | 83 | this.else_action = ACTIONS[else_action_name] |
65 | 84 | this.else_action_arg = else_action.substring(else_action.indexOf(' ') + 1) |
66 | 85 | 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') |
85 | 104 | console.warn(`activeadmin_dynamic_fields custom function not found: ${this.custom_function}`) |
86 | 105 | } |
87 | 106 | } |
88 | 107 |
|
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 {} |
93 | 109 | } |
94 | 110 |
|
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 |
104 | 116 | } |
105 | 117 |
|
106 | | - is_valid() { |
| 118 | + isValid() { |
107 | 119 | if (!this.condition) return false |
108 | 120 | if (!this.action && !this.custom_function) return false |
109 | 121 |
|
110 | 122 | return true |
111 | 123 | } |
112 | 124 |
|
113 | 125 | 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()) |
117 | 129 | } |
118 | 130 | } |
119 | 131 |
|
|
0 commit comments