-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFormUtil.js
More file actions
243 lines (232 loc) · 9.94 KB
/
FormUtil.js
File metadata and controls
243 lines (232 loc) · 9.94 KB
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
(function ($) {
"use strict";
/** @external jQuery */
window.Mapbender = Mapbender || {};
Mapbender.DataManager = Mapbender.DataManager || {};
/**
* Utility class for form operations
*/
class FormUtil {
/**
* @param {(HTMLElement|jQuery)} form
* @param {bool} [filterSubmit]
* @return {Object}
*/
extractValues(form, filterSubmit) {
const values = {};
const radioMap = {};
const $allNamedInputs = $(':input[name]', form);
$allNamedInputs.get().forEach(function(input) {
const type = input.type;
if (filterSubmit && (input.readOnly)) {
// Treat read-only input as "unmapped" and omit it when
// running storage.
return;
}
let value;
switch (type) {
case 'radio':
// Radio inputs repeat with the same name. Do not evaluate them individually. Evaluate the
// whole group.
if (radioMap[input.name]) {
// already done
return;
}
value = $allNamedInputs.filter('[type="radio"][name="' + input.name + '"]:checked').val();
radioMap[input.name] = true;
break;
case 'checkbox':
value = input.checked && input.value;
break;
case 'select-multiple':
const separator = $(input).attr('data-multiselect-separator') || ',';
/** @var {Array<String>|null} valueList */
const valueList = $(input).val();
value = valueList && valueList.join(separator) || null;
break;
default:
value = input.value;
// Date special: if date is not required and empty, convert empty string to null
// This fixes errors saving empty string into SQL DATE columns. OTOH this
// means non-required date fields can only map to nullable columns.
if (value === '' && !input.required && input.type === 'date' || $(input).is('.js-datepicker')) {
value = null;
}
break;
}
values[input.name] = value;
});
return values;
}
/**
* @param {(HTMLElement|jQuery)} form
* @param {Object} values
*/
setValues(form, values) {
const valueKeys = Object.keys(values);
for (let i = 0; i < valueKeys.length; ++i) {
const inputName = valueKeys[i];
let value = values[inputName];
const $input = $(':input[name="' + inputName + '"]', form);
if (!$input.length) {
continue;
}
switch ($input.get(0).type) {
case 'select-multiple':
if (!Array.isArray(value)) {
const separator = $input.attr('data-multiselect-separator') || ',';
value = (value || '').split(separator);
}
$input.val(value);
break;
case 'radio':
const $check = $input.filter(function() {
return this.value === value;
});
$check.prop('checked', true);
break;
case 'checkbox':
// Legacy fun time: database may contain stringified booleans "false" or even "off"
value = !!value && (value !== 'false') && (value !== 'off');
$input.prop('checked', value);
break;
default:
$input.val(value);
break;
}
switch ($input.get(0).type) {
case 'text':
$input.trigger('input.colorpicker').trigger('change.colorpicker');
break;
case 'select':
case 'select-multiple':
$input.trigger('change.select2');
break;
default:
break;
}
}
}
/**
* @param {(HTMLElement|jQuery)} form
* @return {boolean}
*/
validateForm(form) {
const self = this;
const invalidInputs = $(':input[name]', form).get().filter(function(input) {
return !self.validateInput(input);
});
// If there are erros, switch tab container (if any) to reveal the first affected input
const $firstInvalid = invalidInputs.length && $(invalidInputs[0]);
const $tabElement = $firstInvalid && $firstInvalid.closest('.ui-tabs');
if ($tabElement && $tabElement.length) {
const tabIndex = $firstInvalid.closest('.ui-tabs-panel').index('.ui-tabs-panel');
$tabElement.tabs({active: tabIndex});
}
if ($firstInvalid) {
$firstInvalid.focus();
}
return !invalidInputs.length;
}
/**
* @param {HTMLElement} input
* @return {boolean}
*/
validateInput(input) {
const $input = $(input);
if ($input.attr('type') === 'radio') {
// Individual radio buttons cannot be invalid and cannot be validated
return true;
}
// NOTE: hidden, disabled and read-only inputs must be explicitly excluded
// from jQuery validation. They always come back as invalid, even without
// any required / pattern constraints
// see https://stackoverflow.com/questions/51534473/jquery-validate-not-working-on-hidden-input
const isValid =
($input.is(':valid') || input.type === 'hidden' || input.readOnly || input.disabled)
&& this.validateCustom_($input)
;
this.markValidationState($input, isValid);
if (!isValid) {
const self = this;
// Re-validate once on change, to make error message disappear
$input.one('change input', function() {
self.validateInput(input);
});
}
return isValid;
}
/**
* @param {jQuery} $input
* @param {boolean} isValid
*/
markValidationState($input, isValid) {
const $formGroup = $input.closest('.mb-3');
$formGroup.toggleClass('has-error', !isValid);
$formGroup.toggleClass('has-success', isValid);
/** @todo: ensure message container is always present in the right place */
let $messageContainer = $('.invalid-feedback', $formGroup);
const invalidMessage = $input.attr('data-custom-validation-message');
if (!isValid && invalidMessage && $input.attr('type') !== 'checkbox') {
if (!$messageContainer.length) {
$messageContainer = $(document.createElement('div')).addClass('help-block invalid-feedback');
$formGroup.append($messageContainer);
}
$messageContainer.text(invalidMessage || '');
}
$messageContainer.toggle(!isValid);
}
/**
* @param {jQuery} $input
*/
copyToClipboard($input) {
let text = $input.val() || '';
if (text && Array.isArray(text)) {
// select[multple]
const separator = $input.attr('data-multiselect-separator') || ',';
/** @var {Array<String>|null} valueList */
text = text.join(separator) || '';
}
if (window.clipboardData && window.clipboardData.setData) {
/** @see https://caniuse.com/mdn-api_clipboardevent_clipboarddata */
return clipboardData.setData("Text", text);
} else {
const textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy"); // Security exception may be thrown by some browsers.
} catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return false;
} finally {
document.body.removeChild(textarea);
}
}
}
/**
* @param {jQuery} $input
* @return {boolean}
* @private
*/
validateCustom_($input) {
/** @todo: use a more reasonable data key than "warn" */
const validationCallback = $input.data('warn');
if (validationCallback) {
const value = $input.val();
// Legacy quirk: pass null instead of empty string to validation callback
if (value === '') {
return !!validationCallback(null);
} else {
return !!validationCallback(value);
}
} else {
return true;
}
}
}
// Export as singleton instance for backward compatibility
Mapbender.DataManager.FormUtil = new FormUtil();
}(jQuery));