-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdynamic_fields.js
274 lines (251 loc) · 9.51 KB
/
dynamic_fields.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
(function () {
'use strict'
// noinspection JSUnusedGlobalSymbols
const ACTIONS = {
addClass: (el, name) => el.addClass(name),
addStyle: (el, extra_style) => {
let style = (el.attr('style') || '').trim()
if (!style.includes(extra_style)) {
if (style) style = style.replace(/;$/, '') + '; ' // ensure style ends with ;
el.attr('style', `${style}${extra_style}`)
}
},
callback: (el, name) => {
const cb_function = window.hasOwnProperty(name) ? window[name] : null
if (typeof cb_function === 'function') cb_function(el.data('args'))
else {
el.attr('data-df-errors', 'callback function not found')
console.warn(`activeadmin_dynamic_fields callback function not found: ${name}`)
}
},
fade: el => el.fadeOut(),
hide: el => el.hide(),
setText: (el, text) => el.text(text),
setValue: (el, value) => {
if (el.attr('type') === 'checkbox') el.prop('checked', value === '1')
else el.val(value)
el.trigger('change')
},
slide: el => el.slideUp()
}
// noinspection EqualityComparisonWithCoercionJS, JSUnusedGlobalSymbols
const CONDITIONS = {
blank: el => el.val().length === 0 || !el.val().trim(),
changed: _el => true,
checked: el => el.is(':checked'),
eq: (el, value) => el.val() == value,
match: (el, regexp) => regexp.test(el.val()),
mismatch: (el, regexp) => !regexp.test(el.val()),
not: (el, value) => el.val() != value,
not_blank: el => !CONDITIONS.blank(el),
not_checked: el => !CONDITIONS.checked(el)
}
const REVERSE_ACTIONS = {
addClass: (el, name) => el.removeClass(name),
addStyle: (el, extra_style) => {
if(el.attr('style')) el.attr('style', el.attr('style').replace(extra_style, ''))
},
fade: el => el.fadeIn(),
hide: el => el.show(),
slide: el => el.slideDown()
}
const REGEXP_NOT = /^!\s*/
class Field {
constructor(el) {
this.el = el
const action_name = this.evaluateAction()
const result = this.evaluateCondition()
this.condition = result.condition
this.condition_arg = result.condition_arg
this.evaluateTarget(action_name)
}
apply() {
if (this.condition(this.el, this.condition_arg)) {
if (this.else_reverse_action) this.else_reverse_action(this.target, this.else_action_arg)
this.action(this.target, this.action_arg)
}
else {
if (this.reverse_action) this.reverse_action(this.target, this.action_arg)
if (this.else_action) this.else_action(this.target, this.else_action_arg)
}
}
evaluateAction() {
const dataAction = this.el.data('then') || this.el.data('action') || ''
const actionName = dataAction.split(' ', 1)[0]
const dataElse = this.el.data('else') || ''
const elseActionName = dataElse.split(' ', 1)[0]
this.action = ACTIONS[actionName]
if (actionName == 'callback') {
this.action_arg = dataAction.substring(dataAction.indexOf(' ') + 1)
} else {
this.action_arg = this.el.data('args') || dataAction.substring(dataAction.indexOf(' ') + 1)
}
this.reverse_action = REVERSE_ACTIONS[actionName]
this.else_action = ACTIONS[elseActionName]
if (elseActionName == 'callback') {
this.else_action_arg = dataElse.substring(dataElse.indexOf(' ') + 1)
} else {
this.else_action_arg = this.el.data('else-args') || dataElse.substring(dataElse.indexOf(' ') + 1)
}
this.else_reverse_action = REVERSE_ACTIONS[elseActionName]
return actionName
}
evaluateCondition() {
let value
if (value = this.el.data('if')) {
if (REGEXP_NOT.test(value)) value = 'not_' + value.replace(REGEXP_NOT, '')
return { condition: CONDITIONS[value] }
}
if (value = this.el.data('eq')) {
if (REGEXP_NOT.test(value)) {
return { condition: CONDITIONS['not'], condition_arg: value.replace(REGEXP_NOT, '') }
}
return { condition: CONDITIONS['eq'], condition_arg: value }
}
if (value = this.el.data('not')) {
if (REGEXP_NOT.test(value)) {
return { condition: CONDITIONS['eq'], condition_arg: value.replace(REGEXP_NOT, '') }
}
return { condition: CONDITIONS['not'], condition_arg: value }
}
if (value = this.el.data('match')) {
return { condition: CONDITIONS['match'], condition_arg: new RegExp(value) }
}
if (value = this.el.data('mismatch')) {
return { condition: CONDITIONS['mismatch'], condition_arg: new RegExp(value) }
}
this.custom_function = this.el.data('function')
if (this.custom_function) {
value = window[this.custom_function]
if (value) return { condition: value }
else {
this.el.attr('data-df-errors', 'custom function not found')
console.warn(`activeadmin_dynamic_fields custom function not found: ${this.custom_function}`)
}
}
return {}
}
evaluateTarget(action_name) {
// closest find for has many associations
if (this.el.data('target')) this.target = this.el.closest('fieldset').find(this.el.data('target'))
else if (this.el.data('gtarget')) this.target = $(this.el.data('gtarget'))
if (action_name === 'callback') this.target = this.el
}
isValid() {
if (!this.condition) return false
return (this.action || this.custom_function)
}
setup() {
if (!this.isValid()) return
if (this.el.data('if') !== 'changed') this.apply()
this.el.on('change', () => this.apply())
}
}
// Inline update - must be called bound on the editing element
function dfUpdateField() {
if ($(this).data('loading') !== '1') {
$(this).data('loading', '1');
let _this = $(this);
let type = $(this).data('field-type');
let new_value;
if (type === 'boolean') new_value = !$(this).data('field-value');
else if (type === 'select') new_value = $(this).val();
else new_value = $(this).text();
let data = {};
data[$(this).data('field')] = new_value;
$.ajax({
context: _this,
data: { data: data },
method: 'POST',
url: $(this).data('save-url'),
complete: function (_req, _status) {
$(this).data('loading', '0');
},
success: function (data, _status, _req) {
if (data.status === 'error') {
if ($(this).data('show-errors')) {
let result = '';
let message = data.message;
for (let key in message) {
if (message.hasOwnProperty(key) && typeof (message[key]) === 'object') {
if (result) result += ' - ';
result += key + ': ' + message[key].join('; ');
}
}
if (result) alert(result);
}
}
else {
$(this).data('field-value', new_value);
if ($(this).data('content')) {
let old_text = $(this).text();
let old_class = $(this).attr('class');
let content = $($(this).data('content'));
$(this).text(content.text());
$(this).attr('class', content.attr('class'));
content.text(old_text);
content.attr('class', old_class);
$(this).data('content', content);
}
}
}
});
}
}
function dfInit() {
// Setup dynamic fields
const selectors = '.active_admin .input [data-if], .active_admin .input [data-eq], .active_admin .input [data-not], .active_admin .input [data-match], .active_admin .input [data-mismatch], .active_admin .input [data-function]'
$(selectors).each(function () {
new Field($(this)).setup()
})
// Setup dynamic fields for associations
$('.active_admin .has_many_container').on('has_many_add:after', () => {
$(selectors).each(function () {
new Field($(this)).setup()
})
})
// Set dialog icon link
$('.active_admin [data-df-icon]').each(function () {
$(this).append(' »')
})
// Open content in dialog
$('.active_admin [data-df-dialog]').on('click', function (event) {
event.preventDefault()
$(this).blur()
const df_dialog = $('#df-dialog')
if (df_dialog.data('loading') !== '1') {
df_dialog.data('loading', '1')
if (df_dialog.length === 0) $('body').append('<div id="df-dialog"></div>')
let title = $(this).attr('title')
$.ajax({
url: $(this).attr('href'),
complete: function (_req, _status) {
$('#df-dialog').data('loading', '0')
},
success: function (data, _status, _req) {
const dialog = $('#df-dialog')
if (title) dialog.attr('title', title)
dialog.html(data)
dialog.dialog({ modal: true })
}
})
}
})
// Inline editing
$('[data-field][data-field-type="boolean"][data-save-url]').each(function () {
$(this).on('click', $.proxy(dfUpdateField, $(this)))
})
$('[data-field][data-field-type="string"][data-save-url]').each(function () {
$(this).data('field-value', $(this).text())
let fnUpdate = $.proxy(dfUpdateField, $(this))
$(this).on('blur', function () {
if ($(this).data('field-value') !== $(this).text()) fnUpdate()
})
})
$('[data-field][data-field-type="select"][data-save-url]').each(function () {
$(this).on('change', $.proxy(dfUpdateField, $(this)))
})
}
$(document).ready(dfInit)
$(document).on('turbolinks:load', dfInit)
})()