-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathSelectBoxes.js
332 lines (297 loc) · 9.58 KB
/
SelectBoxes.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import _ from 'lodash';
import { componentValueTypes, getComponentSavedTypes, boolValue, getComponent } from '../../utils/utils';
import RadioComponent from '../radio/Radio';
export default class SelectBoxesComponent extends RadioComponent {
static schema(...extend) {
return RadioComponent.schema({
type: 'selectboxes',
label: 'Select Boxes',
key: 'selectBoxes',
inline: false
}, ...extend);
}
static get builderInfo() {
return {
title: 'Select Boxes',
group: 'basic',
icon: 'plus-square',
weight: 60,
documentation: '/userguide/form-building/form-components#select-box',
schema: SelectBoxesComponent.schema()
};
}
static get serverConditionSettings() {
return SelectBoxesComponent.conditionOperatorsSettings;
}
static get conditionOperatorsSettings() {
return {
...super.conditionOperatorsSettings,
valueComponent(classComp) {
const isValuesSrc = !classComp.dataSrc || classComp.dataSrc === 'values';
return isValuesSrc
? {
type: 'select',
dataSrc: 'custom',
valueProperty: 'value',
dataType: 'string',
data: {
custom: `values = ${classComp && classComp.values ? JSON.stringify(classComp.values) : []}`
},
}
: {
...classComp,
dataType: 'string',
type: 'select',
}
}
};
}
static savedValueTypes(schema) {
return getComponentSavedTypes(schema) || [componentValueTypes.object];
}
constructor(...args) {
super(...args);
}
init() {
super.init();
this.component.inputType = 'checkbox';
}
get defaultSchema() {
return SelectBoxesComponent.schema();
}
get inputInfo() {
const info = super.elementInfo();
info.attr.name += '[]';
info.attr.type = 'checkbox';
info.attr.class = 'form-check-input';
return info;
}
get emptyValue() {
return this.component.values.reduce((prev, value) => {
if (value.value) {
prev[value.value] = false;
}
return prev;
}, {});
}
get defaultValue() {
let defaultValue = this.emptyValue;
if (!_.isEmpty(this.component.defaultValue)) {
defaultValue = this.component.defaultValue;
}
if (this.component.customDefaultValue && !this.options.preview) {
defaultValue = this.evaluate(
this.component.customDefaultValue,
{ value: '' },
'value'
);
}
return defaultValue;
}
/**
* Only empty if the values are all false.
* @param {any} value - The value to check if empty.
* @returns {boolean} - If the value is empty.
*/
isEmpty(value = this.dataValue) {
let empty = true;
for (const key in value) {
if (value.hasOwnProperty(key) && value[key]) {
empty = false;
break;
}
}
return empty;
}
getValue() {
if (this.viewOnly || !this.refs.input || !this.refs.input.length) {
return this.dataValue;
}
const value = {};
_.each(this.refs.input, (input) => {
value[input.value] = !!input.checked;
});
return value;
}
/**
* Normalize values coming into updateValue.
* @param {any} value - The value to normalize.
* @returns {*} - The normalized value
*/
normalizeValue(value) {
value = value || {};
if (typeof value !== 'object') {
if (typeof value === 'string') {
value = {
[value]: true
};
}
else {
value = {};
}
}
if (Array.isArray(value)) {
_.each(value, (val) => {
value[val] = true;
});
}
const checkedValues = _.keys(_.pickBy(value, (val) => val));
if (this.isSelectURL && this.templateData && _.every(checkedValues, (val) => this.templateData[val])) {
const submission = this.root.submission;
if (!submission.metadata.selectData) {
submission.metadata.selectData = {};
}
const selectData = [];
checkedValues.forEach((value) => selectData.push(this.templateData[value]));
_.set(submission.metadata.selectData, this.path, selectData);
}
// Ensure that for dataSrc == 'values' that there are not any other superfluous values.
if (this.component.dataSrc === 'values') {
for (const key in value) {
if (!this.component.values.find((val) => val.value === key)) {
delete value[key];
}
}
}
return value;
}
/**
* Set the value of this component.
* @param {any} value - The value to set.
* @param {any} flags - Flags to apply to this update.
* @returns {boolean} - If the value has changed.
*/
setValue(value, flags = {}) {
const changed = this.updateValue(value, flags);
value = this.dataValue;
if (this.isHtmlRenderMode()) {
if (changed) {
this.redraw();
}
}
else {
_.each(this.refs.input, (input) => {
if (_.isUndefined(value[input.value])) {
value[input.value] = false;
}
input.checked = !!value[input.value];
});
}
return changed;
}
getValueAsString(value, options = {}) {
if (!value) {
return '';
}
if (this.isSelectURL) {
if (options.modalPreview || this.options.readOnly || this.inDataTable) {
const checkedItems = _.keys(_.pickBy(value, (val) => val));
if (this.selectData?.length === checkedItems.length) {
return this.selectData.map(item => this.itemTemplate(item)).join(', ');
} else if (this.loadedOptions?.length) {
return this.loadedOptions.filter((option) => value[option.value]).map((option) => option.label).join(', ');
}
}
return _(value).pickBy((val) => val).keys().join(', ');
}
return _(this.component.values || [])
.filter((v) => value[v.value])
.map('label')
.join(', ');
}
setSelectedClasses() {
if (this.refs.wrapper) {
//add/remove selected option class
const value = this.dataValue;
const valuesKeys = Object.keys(value);
this.refs.wrapper.forEach((wrapper, index) => {
let key = valuesKeys[index];
const input = this.refs.input[index];
if (input?.value.toString() !== key) {
key = valuesKeys.find((k) => input?.value.toString() === k);
}
const isChecked = value[key];
if ((isChecked && key) || (this.isSelectURL && !this.shouldLoad && this.listData && _.findIndex(this.selectData, this.listData[index]) !== -1)) {
//add class to container when selected
this.addClass(wrapper, this.optionSelectedClass);
//change "checked" attribute
input.setAttribute('checked', 'true');
}
else if (!isChecked && key) {
this.removeClass(wrapper, this.optionSelectedClass);
input.removeAttribute('checked');
}
});
}
}
setInputsDisabled(value, onlyUnchecked) {
if (this.refs.input) {
this.refs.input.forEach(item => {
if (onlyUnchecked && !item.checked || !onlyUnchecked) {
item.disabled = value;
}
});
}
}
checkComponentValidity(data, dirty, rowData, options, errors = []) {
const minCount = this.component.validate.minSelectedCount;
const maxCount = this.component.validate.maxSelectedCount;
if (!this.shouldSkipValidation(data, rowData, options)) {
const isValid = this.isValid(data, dirty);
if ((maxCount || minCount)) {
const count = Object.keys(this.validationValue).reduce((total, key) => {
if (this.validationValue[key]) {
total++;
}
return total;
}, 0);
// Disable the rest of inputs if the max amount is already checked
if (maxCount && count >= maxCount) {
this.setInputsDisabled(true, true);
}
else if (maxCount && !this.shouldDisabled) {
this.setInputsDisabled(false);
}
if (!isValid && maxCount && count > maxCount) {
const message = this.t(
this.component.maxSelectedCountMessage || 'You may only select up to {{maxCount}} items',
{ maxCount }
);
this.errors.push({ message });
this.setCustomValidity(message, dirty);
return false;
}
else if (!isValid && minCount && count < minCount) {
this.setInputsDisabled(false);
const message = this.t(
this.component.minSelectedCountMessage || 'You must select at least {{minCount}} items',
{ minCount }
);
this.errors.push({ message });
this.setCustomValidity(message, dirty);
return false;
}
}
}
return super.checkComponentValidity(data, dirty, rowData, options, errors);
}
setCustomValidity(messages, dirty, external) {
if (this.options.building && _.find(messages, {ruleName: 'invalidValueProperty'})) {
setTimeout(() => {
this.root && getComponent(this.root.components, 'valueProperty').setCustomValidity(messages, dirty);
}, 0);
return super.setCustomValidity(_.filter(messages, (message) => message.ruleName !=='invalidValueProperty'), dirty, external);
} else {
return super.setCustomValidity(messages, dirty, external);
};
}
validateValueAvailability(setting, value) {
if (!boolValue(setting) || !value) {
return true;
}
const values = this.component.dataSrc === 'values' ? this.component.values : this.loadedOptions;
const availableValueKeys = (values || []).map(({ value: optionValue }) => optionValue);
const valueKeys = Object.keys(value);
return valueKeys.every((key) => availableValueKeys.includes(key));
}
}