-
Notifications
You must be signed in to change notification settings - Fork 26
/
ampersand-select-view.js
executable file
·447 lines (380 loc) · 15.6 KB
/
ampersand-select-view.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* $AMPERSAND_VERSION */
var domify = require('domify');
var dom = require('ampersand-dom');
var matches = require('matches-selector');
var View = require('ampersand-view');
var createOption = function(value, text, disabled) {
var node = document.createElement('option');
//Set to empty-string if undefined or null, but not if 0, false, etc
if (value === null || value === undefined) value = '';
if (disabled) node.disabled = true;
node.textContent = text;
node.value = value;
return node;
};
var createOptgroup = function(text) {
var node = document.createElement('optgroup');
node.label = text;
return node;
};
module.exports = View.extend({
//Replaceable with anything with label, message-container, message-text data-hooks and a <select>
template: [
'<label class="select">',
'<span data-hook="label"></span>',
'<select></select>',
'<span data-hook="message-container" class="message message-below message-error">',
'<p data-hook="message-text"></p>',
'</span>',
'</label>'
].join('\n'),
initialize: function(opts) {
opts = opts || {};
if (typeof opts.name !== 'string') throw new Error('SelectView requires a name property.');
this.name = opts.name;
if (opts.groupOptions) {
if (opts.options) {
throw new Error('Don\'t provide ampersand-select-view with both options and groupOptions properties, as option will be generated with values from groupOptions.');
}
// Create the options array from the groupOptions property, containing
// only the values that will result in an <option> element
this.groupOptions = opts.groupOptions;
opts.options = [];
opts.groupOptions.forEach(function(optgroup) {
optgroup.options.forEach(function(option) {
opts.options.push(option);
}.bind(this));
}.bind(this));
}
if (!Array.isArray(opts.options) && !opts.options.isCollection) {
throw new Error('SelectView requires select options.');
}
this.options = opts.options;
if (this.options.isCollection) {
this.idAttribute = opts.idAttribute || this.options.mainIndex || 'id';
this.textAttribute = opts.textAttribute || 'text';
this.disabledAttribute = opts.disabledAttribute;
}
this.el = opts.el;
this.label = opts.label || '';
this.parent = opts.parent || this.parent;
if (opts.template) this.template = opts.template;
this.unselectedText = opts.unselectedText;
this.startingValue = opts.value;
this.yieldModel = (opts.yieldModel === false) ? false : true;
this.tabindex = opts.tabindex || '0';
this.eagerValidate = opts.eagerValidate;
this.required = opts.required || false;
this.validClass = opts.validClass || 'input-valid';
this.invalidClass = opts.invalidClass || 'input-invalid';
if (opts.requiredMessage === undefined) {
this.requiredMessage = 'Selection required';
} else {
this.requiredMessage = opts.requiredMessage;
}
this.onChange = this.onChange.bind(this);
this.startingValue = this.setValue(opts.value, this.eagerValidate ? false : true, true);
if (opts.beforeSubmit) this.beforeSubmit = opts.beforeSubmit;
if (opts.autoRender) this.autoRender = opts.autoRender;
},
render: function() {
var elDom,
labelEl;
if (this.rendered) return;
elDom = domify(this.template);
if (!this.el) this.el = elDom;
else this.el.appendChild(elDom);
labelEl = this.el.querySelector('[data-hook~=label]');
if (labelEl) {
labelEl.textContent = this.label;
this.label = labelEl;
} else {
delete this.label;
}
if (this.el.tagName === 'SELECT') {
this.select = this.el;
} else {
this.select = this.el.querySelector('select');
}
if (!this.select) throw new Error('no select found in template');
if (matches(this.el, 'select')) this.select = this.el;
if (this.select) this.select.setAttribute('name', this.name);
if (this.select) this.select.setAttribute('tabindex', this.tabindex);
this.bindDOMEvents();
this.renderOptions();
this.updateSelectedOption();
if (this.options.isCollection) {
this.options.on('add remove reset', function() {
var setValueFirstModel = function() {
if (!this.options.length) return;
if (this.yieldModel) this.setValue(this.options.models[0]);
else this.setValue(this.options.models[0][this.idAttribute]);
}.bind(this);
this.renderOptions();
if (this.hasOptionByValue(this.value)) this.updateSelectedOption();
else setValueFirstModel();
}.bind(this));
}
this.validate(this.eagerValidate ? false : true, true);
return this;
},
onChange: function() {
var value = this.select.options[this.select.selectedIndex].value;
if (this.options.isCollection && this.yieldModel) {
value = this.getModelForId(value);
}
this.setValue(value);
},
/**
* Finds a model in the options collection provided an ID
* @param {any} id
* @return {State}
* @throws {RangeError} If model not found
*/
getModelForId: function(id) {
return this.options.filter(function(model) {
// intentionally coerce for '1' == 1
return model[this.idAttribute] == id;
}.bind(this))[0];
},
bindDOMEvents: function() {
this.select.addEventListener('change', this.onChange, false);
},
renderOptions: function() {
if (!this.select) return;
this.select.innerHTML = '';
if (this.unselectedText !== undefined) {
this.select.appendChild(
createOption(null, this.unselectedText)
);
}
if (this.groupOptions) {
this.groupOptions.forEach(function(optgroup) {
// this.groupOptions is an array of Objects representing <optgroup> elements
var optGroupElement = createOptgroup(optgroup.groupName);
// Loop over the <options> from that <optgroup>
optgroup.options.forEach(function(option) {
// Add the <option>s to the <optgroup>
optGroupElement.appendChild(
createOption(
this.getOptionValue(option),
this.getOptionText(option),
this.getOptionDisabled(option)
)
);
}.bind(this));
// Add the <optgroup> to the <select>
this.select.appendChild(optGroupElement);
}.bind(this));
} else {
this.options.forEach(function(option) {
// Create and add the <option> to the <select>
this.select.appendChild(
createOption(
this.getOptionValue(option),
this.getOptionText(option),
this.getOptionDisabled(option)
)
);
}.bind(this));
}
},
/**
* Updates the <select> control to set the select option when the option
* has changed programatically (i.e. not through direct user selection)
* @return {SelectView} this
* @throws {Error} If no option exists for this.value
*/
updateSelectedOption: function() {
var lookupValue = this.value;
if (lookupValue === null || lookupValue === undefined || lookupValue === '') {
if (this.unselectedText !== undefined || (!this.startingValue && !this.rendered)) {
this.select.selectedIndex = 0;
return this;
} else if (!this.options.length && this.value === null) {
return this;
}
}
// Pull out the id if it's a model
if (this.options.isCollection && this.yieldModel) {
lookupValue = lookupValue && lookupValue[this.idAttribute];
}
if (lookupValue || lookupValue === 0 || lookupValue === null) {
if (lookupValue === null) lookupValue = ''; // DOM sees only '' empty value
for (var i = this.select.options.length; i--; i) {
if (this.select.options[i].value == lookupValue) {
this.select.selectedIndex = i;
return this;
}
}
}
// failed to match any
throw new Error('no option exists for value: ' + lookupValue);
},
remove: function() {
if (this.el && this.el.parentNode) this.el.parentNode.removeChild(this.el);
this.el.removeEventListener('change', this.onChange, false);
},
/**
* Sets control to unselectedText option, or user specified option with `null`
* value
* @return {SelectView} this
*/
clear: function() {
this.setValue(null, true);
return this;
},
/**
* Sets the selected option and view value to the original option value provided
* during construction
* @return {SelectView} this
*/
reset: function() {
return this.setValue(this.startingValue, true);
},
setValue: function(value, skipValidationMessage, init) {
var option, model, nullValid;
// enforce the <select> control to contain only a singular falsy
// value (excluding 0), because browsers will set the select.value
// in each case to be the empty string
if (value === null || value === undefined || value === '') {
this.value = null;
// test if null is a valid option
if (this.unselectedText !== undefined) {
nullValid = true;
} else {
nullValid = this.hasOptionByValue(null);
}
// empty value requested to be set. This may be because the field is just
// initializing. If initializing and `null` isn't in the honored option set
// set the select to the 0-th index
if (init && this.options.length && !nullValid) {
// no initial value passed, set initial value to first item in set
if (this.options.isCollection) {
model = this.options.models[0];
this.value = this.yieldModel ? model : model[this.idAttribute];
} else {
if (Array.isArray(this.options[0])) {
this.value = this.options[0][0];
} else {
this.value = this.options[0];
}
}
}
} else {
// Ensure corresponding option exists before assigning value
option = this.getOptionByValue(value);
this.value = Array.isArray(option) ? option[0] : option;
}
this.validate(skipValidationMessage);
if (this.select) this.updateSelectedOption();
if (this.parent && typeof this.parent.update === 'function') this.parent.update(this);
return this.value;
},
validate: function(skipValidationMessage) {
if (!this.required) {
// selected option always known to be in option set,
// thus field is always valid if not required
this.valid = true;
if (this.select) this.toggleMessage(skipValidationMessage);
return this.valid;
}
if (this.required && !this.value && this.value !== 0) {
this.valid = false;
if (this.select) this.toggleMessage(skipValidationMessage, this.requiredMessage);
} else {
this.valid = true;
if (this.select) this.toggleMessage(skipValidationMessage);
}
return this.valid;
},
/**
* Called by FormView on submit
* @return {SelectView} this
*/
beforeSubmit: function() {
if (this.select) this.setValue(this.select.options[this.select.selectedIndex].value);
},
/**
* Gets the option corresponding to provided value.
* @param {*} string, state, or model
* @return {*} string, array, state, or model
*/
getOptionByValue: function(value) {
var model;
if (this.options.isCollection) {
// find value in collection, error if no model found
if (this.options.indexOf(value) === -1) model = this.getModelForId(value);
else model = value;
if (!model) throw new Error('model or model idAttribute not found in options collection');
return this.yieldModel ? model : model[this.idAttribute];
} else if (Array.isArray(this.options)) {
// find value value in options array
// find option, formatted [['val', 'text'], ...]
if (this.options.length && Array.isArray(this.options[0])) {
for (var i = this.options.length - 1; i >= 0; i--) {
if (this.options[i][0] == value) return this.options[i];
}
}
// find option, formatted ['valAndText', ...] format
if (this.options.length && this.options.indexOf(value) !== -1) return value;
throw new Error('value not in set of provided options');
}
throw new Error('select option set invalid');
},
/**
* Tests if option set has an option corresponding to the provided value
* @param {*} value
* @return {Boolean}
*/
hasOptionByValue: function(value) {
try {
this.getOptionByValue(value);
return true;
} catch (err) {
return false;
}
},
getOptionValue: function(option) {
if (Array.isArray(option)) return option[0];
if (this.options.isCollection) return option[this.idAttribute];
return option;
},
getOptionText: function(option) {
if (Array.isArray(option)) return option[1];
if (this.options.isCollection) {
if (this.textAttribute && option[this.textAttribute] !== undefined) {
return option[this.textAttribute];
}
}
return option;
},
getOptionDisabled: function(option) {
if (Array.isArray(option)) return option[2];
if (this.options.isCollection && this.disabledAttribute) return option[this.disabledAttribute];
return false;
},
toggleMessage: function(hide, message) {
var mContainer = this.el.querySelector('[data-hook~=message-container]'),
mText = this.el.querySelector('[data-hook~=message-text]');
if (!mContainer || !mText) return;
if (hide) {
dom.hide(mContainer);
mText.textContent = '';
dom.removeClass(this.el, this.validClass);
dom.removeClass(this.el, this.invalidClass);
return;
}
if (message) {
dom.show(mContainer);
mText.textContent = message;
dom.addClass(this.el, this.invalidClass);
dom.removeClass(this.el, this.validClass);
} else {
dom.hide(mContainer);
mText.textContent = '';
dom.addClass(this.el, this.validClass);
dom.removeClass(this.el, this.invalidClass);
}
}
});