-
Notifications
You must be signed in to change notification settings - Fork 60
/
Select.js
507 lines (469 loc) · 20.9 KB
/
Select.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* Copyright 2012 Amadeus s.a.s.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var Aria = require("../../Aria");
var ariaWidgetsFormDropDownListTrait = require("./DropDownListTrait");
var ariaUtilsString = require("../../utils/String");
var ariaWidgetsControllersSelectController = require("../controllers/SelectController");
var ariaUtilsDom = require("../../utils/Dom");
var ariaWidgetsFormSelectStyle = require("./SelectStyle.tpl.css");
var ariaWidgetsFormListListStyle = require("./list/ListStyle.tpl.css");
var ariaWidgetsContainerDivStyle = require("../container/DivStyle.tpl.css");
var ariaWidgetsFormDropDownInput = require("./DropDownInput");
var ariaCoreBrowser = require("../../core/Browser");
/**
* Select widget allows the use of a simple HTML select element or an AT skin version. Class for both versions of Select -
* simpleHtml and AT skin.
*/
module.exports = Aria.classDefinition({
$classpath : "aria.widgets.form.Select",
$extends : ariaWidgetsFormDropDownInput,
$css : [ariaWidgetsFormSelectStyle, ariaWidgetsFormListListStyle, ariaWidgetsContainerDivStyle],
/**
* @param {aria.widgets.CfgBeans:SelectCfg} cfg the widget configuration
* @param {aria.templates.TemplateCtxt} ctxt template context
* @param {Number} lineNumber Line number corresponding in the .tpl file where the widget is created
*/
$constructor : function (cfg, ctxt, lineNumber) {
this.$DropDownInput.constructor.call(this, cfg, ctxt, lineNumber);
var skinObj = this._skinObj;
if (!skinObj.simpleHTML) {
// the controller is needed only for the skin version
var controller = new ariaWidgetsControllersSelectController();
this.controller = controller;
controller.setListOptions(cfg.options);
} else {
// remove any dropdown icon, has this is handled by the select
skinObj.iconsRight = [];
}
this._minInputMarkupWidth = 8; // minimum width for a Select?
this._defaultInputMarkupWidth = 170; // default input width
this._customTabIndexProvided = true;
/**
* DOM reference of the select field.
* @private
* @type HTMLElement
*/
this._selectField = null;
/**
* Whether the width of the popup can be smaller than the field, when configured to be so. If false, the
* popupWidth property will be overridden when it is smaller than the field width
* @type Boolean
* @protected
*/
this._freePopupWidth = false;
},
$destructor : function () {
// the controller is disposed in DropDownInput
this.$DropDownInput.$destructor.call(this);
this._selectField = null;
},
$statics : {
// ERROR MESSAGE:
WIDGET_OPTIONS_INVALID_VALUE : "%1Bound value stored in the data model is not a valid option value for the select widget."
},
$prototype : {
/**
* Skinnable class to use for this widget.
* @protected
* @type String
*/
_skinnableClass : "Select",
/**
* Prototype init method called at prototype creation time Allows to store class-level objects that are shared
* by all instances
* @param {Object} p the prototype object being built
*/
$init : function (p) {
var src = ariaWidgetsFormDropDownListTrait.prototype;
for (var key in src) {
if (src.hasOwnProperty(key) && !p.hasOwnProperty(key)) {
// copy methods which are not already on this object (this avoids copying $classpath and
// $destructor)
p[key] = src[key];
}
}
},
/**
* Used to check that the value for the widget when it is initialised, is set.
*/
_checkCfgConsistency : function () {
this.$DropDownInput._checkCfgConsistency.call(this);
this._checkValue();
},
/**
* Set the focus on the select field.
*/
focus : function () {
var field = this.getSelectField();
field.focus();
},
/**
* React to controller report, by displaying the text given in the report, opening/closing the popup and
* updating the value in the data model (if specified in the report and not blocked by stopValueProp option).
* @param {aria.widgets.controllers.reports.ControllerReport} report
* @param {Object} arg Optional parameters
* @protected
*/
_reactToControllerReport : function (report, options) {
if (report) {
var text = report.text;
var value = report.value;
var stopValueProp = options ? options.stopValueProp : false;
if (text != null) {
var domElt = this.getSelectField();
domElt.innerHTML = ariaUtilsString.escapeHTML(text) + ' ';
// the at the end of the label is useful to make sure there is always something in the line
// so that the height does not change
}
var openDropdown = report.displayDropDown;
if (typeof value != 'undefined' && stopValueProp !== true) {
this._updateValue(true);
}
if (this._cfg) {
if (openDropdown === true && !this._dropdownPopup) {
this._openDropdown();
} else if (openDropdown === false && this._dropdownPopup) {
this._closeDropdown();
}
}
report.$dispose();
}
},
/**
* Checks if the widget value is undefined (only when widget is initialised) If it is then sets the value as the
* first value in the select options If there is already a value set then checks the value set is a valid value
* contained in the select options If it isn't then logs an error that the widget value is different from any
* values in the widgets options.
*/
_checkValue : function () {
if (!this._cfg.options.length) {
return;
}
if (typeof this._cfg.value != 'undefined') {
for (var i = 0; i < this._cfg.options.length; i++) {
if (this._cfg.options[i].value === this._cfg.value) {
return;
}
}
this.$logError(this.WIDGET_OPTIONS_INVALID_VALUE);
} else {
this.setProperty("value", this._cfg.options[0].value);
}
},
/**
* Internal method to set the _state property from the _cfg description
* @protected
*/
_setState : function () {
if (this._cfg.disabled) {
this._state = "disabled";
} else if (this._cfg.readOnly) {
this._state = "readOnly";
} else {
var state;
if (this._cfg.mandatory) {
state = "mandatory";
} else {
state = "normal";
}
if (this._cfg.formatError || this._cfg.error) {
state += "Error";
}
if (this._hasFocus || this._keepFocus) {
state += "Focused";
}
this._state = state;
}
},
/**
* Internal method to handle the blur event
* @protected
* @param {aria.DomEvent} event Blur event
*/
_dom_onblur : function () {
this._hasFocus = false;
this._updateState();
if (this._cfg.formatError && this._cfg.validationEvent === 'onBlur') {// show
// errortip on blur used for debug purposes
this._validationPopupShow();
} else { // dispose of error tip
this._validationPopupHide();
if (this._cfg.directOnBlurValidation) {
if (this._cfg.bind) {
var bind = this._cfg.bind.value;
if (bind) {
var dataholder = bind.inside;
var name = bind.to;
var groups = this._cfg.validationGroups;
aria.utils.Data.validateValue(dataholder, name, null, groups, 'onblur');
}
}
}
}
this._updateValue(true);
},
/**
* Internal method to handle the focus event
* @protected
* @param {aria.DomEvent} event Focus event
*/
_dom_onfocus : function () {
this._hasFocus = true;
if (this._cfg) {
if (this._cfg.validationEvent === 'onFocus'
&& ((this._cfg.formatError && this._cfg.formatErrorMessages.length) || (this._cfg.error && this._cfg.errorMessages.length))) {
this._validationPopupShow();
}
}
this._updateState();
},
/**
* Internal method to handle the mousedown event
* @protected
* @param {aria.DomEvent} event Mouse down event
*/
_dom_onmousedown : function (evt) {
var target = evt.target;
var inputDomElt = this._getInputMarkupDomElt();
if (this.controller && ariaUtilsDom.isAncestor(target, inputDomElt)) {
this._toggleDropdown();
evt.preventDefault(); // prevent the selection of the text when clicking
}
},
/**
* Internal method to handle the click event
* @protected
* @param {aria.DomEvent} event Click event
*/
_dom_onclick : function () {
this._updateValue(false);
},
/**
* Internal method to handle the change event
* @protected
* @param {aria.DomEvent} event Change event
*/
_dom_onchange : function () {
this._updateValue(false);
},
/**
* Internal method to handle the keypress event
* @protected
* @param {aria.DomEvent} event Key press event
*/
_dom_onkeypress : function (event) {
var domEvent = aria.DomEvent;
if (event.keyCode === domEvent.KC_ENTER) {
this._updateValue(false);
}
this.$DropDownInput._dom_onkeypress.call(this, event);
},
/**
* Returns an array of html elements on which it is possible to click without closing the popup. This array is
* passed in the popup configuration as the ignoreClicksOn property. For the select widget, it returns an array
* containing both the dropdown icon and the input markup dom element (returned by _getInputMarkupDomElt).
* @return {Array} array of HTML elements
* @protected
*/
_getPopupIgnoreClicksOnDomElts : function () {
var res = this.$DropDownInput._getPopupIgnoreClicksOnDomElts.call(this);
res.push(this._getInputMarkupDomElt());
return res;
},
/**
* Copies the value from the widget to the data model.
* @param {Boolean} includeController whether to copy the value from the controller to the data model.
* @protected
*/
_updateValue : function (includeController) {
var hasChanged = null;
if (this._skinObj.simpleHTML) {
hasChanged = this.setProperty("value", this.getSelectField().value);
} else if (includeController) {
var controller = this.controller;
var dataModel = controller.getDataModel();
hasChanged = this.setProperty("value", dataModel.value);
}
// PTR05634154: setProperty can dispose the widget
// (that's why we are checking this._cfg)
if (this._cfg) {
if (hasChanged != null) {
this.changeProperty("error", false);
if (!(this._cfg.formatError && this._cfg.formatErrorMessages.length)
|| (this._cfg.error && this._cfg.errorMessages.length)) {
this._validationPopupHide();
}
if (this._cfg.onchange) {
this.evalCallback(this._cfg.onchange);
}
}
}
},
/**
* Internal method to override to initialize a widget (e.g. to listen to DOM events)
* @param {HTMLElement} elt the Input markup DOM elt - never null
* @protected
* @override
*/
_initInputMarkup : function (elt) {
this.$InputWithFrame._initInputMarkup.call(this, elt);
this._selectField = this._frame.getChild(0);
},
/**
* Creates the markup for the select for both the simple HTML and AT skin versions.
* @param {aria.templates.MarkupWriter} out Markup writer
* @protected
*/
_inputWithFrameMarkup : function (out) {
var cfg = this._cfg;
var width = this._frame.innerWidth;
var disabledOrReadonly = cfg.disabled || cfg.readOnly;
var tabIndex = disabledOrReadonly ? '' : ' tabindex="' + this._calculateTabIndex() + '"';
if (this._skinObj.simpleHTML) {
var stringUtils = ariaUtilsString;
var options = cfg.options;
var selectedValue = cfg.value;
/*
* The _ariaInput attribute is present so that pressing ENTER on this widget raises the onSubmit event
* of the fieldset:
*/
var html = ['<select', Aria.testMode ? ' id="' + this._domId + '_input"' : '',
(width > 0) ? ' style="width: ' + width + 'px;" ' : '', tabIndex,
disabledOrReadonly ? ' disabled="disabled"' : '', ' _ariaInput="1">'];
for (var i = 0, l = options.length; i < l; i++) {
// string cast, otherwise encoding will fail
var optValue = '' + options[i].value;
html.push('<option value="', stringUtils.encodeForQuotedHTMLAttribute(optValue), '"', optValue == selectedValue
? ' selected="selected"'
: '', '>', stringUtils.escapeHTML(options[i].label), '</option>');
}
html.push('</select>');
out.write(html.join(''));
} else {
var report = this.controller.checkValue(cfg.value);
var text = report.text;
report.$dispose();
// The _ariaInput attribute is present so that pressing ENTER on this widget raises the onSubmit event
// of the fieldset:
out.write(['<span', Aria.testMode ? ' id="' + this._domId + '_input"' : '', ' class="xSelect" style="',
(width > 0) ? 'width:' + width + 'px;' : '', '"', tabIndex, ' _ariaInput="1">',
ariaUtilsString.escapeHTML(text), ' </span>'].join(''));
// the at the end of the label is useful to make sure there is always something in the line so
// that the height does not change
}
},
/**
* Internal method called when the value property that the select is bound to has changed.
* @param {String} propertyName the property name
* @param {Object} newValue the new value
* @param {Object} oldValue the old property value
* @protected
*/
_onBoundPropertyChange : function (propertyName, newValue, oldValue) {
if (propertyName === 'value') {
this._checkValue();// checks the value changed in the data model is a valid value from the select
// options
if (this.controller) {
var report = this.controller.checkValue(newValue);
this._reactToControllerReport(report, {
stopValueProp : true
});
} else {
this._selectValue(newValue);
}
} else if (propertyName === 'mandatory') {
this._updateState();
} else if (propertyName === 'readOnly' || propertyName === 'disabled') {
var selectField = this.getSelectField();
var disabledOrReadonly = this.getProperty("disabled") || this.getProperty("readOnly");
if (this._skinObj.simpleHTML) {
selectField.disabled = disabledOrReadonly ? "disabled" : "";
}
var tabIndex = disabledOrReadonly ? -1 : this._calculateTabIndex();
selectField.tabIndex = tabIndex;
this._updateState();
} else if (propertyName === 'options') {
if (this.controller) {
var selectValue = this.controller.getDataModel().value;
this.controller.setListOptions(newValue);
var report = this.controller.checkValue(selectValue);
this._reactToControllerReport(report, {
stopValueProp : false
});
} else {
// markup for the options
var optionsMarkup = [];
var stringUtils = ariaUtilsString;
for (var i = 0, l = newValue.length; i < l; i++) {
// string cast, otherwise encoding will fail
var optValue = '' + newValue[i].value;
optionsMarkup.push('<option value="', stringUtils.encodeForQuotedHTMLAttribute(optValue), '">', stringUtils.escapeHTML(newValue[i].label), '</option>');
}
var selectField = this.getSelectField();
var currentValue = selectField.value;
// update the options list
var optionsListString = optionsMarkup.join('');
if (ariaCoreBrowser.isIE9 || ariaCoreBrowser.isIE8 || ariaCoreBrowser.isIE7) {
// innerHTML replacing in IE truncates the first element and breaks the whole select...
selectField.innerHTML = '';
var helperDiv = Aria.$window.document.createElement('div');
helperDiv.innerHTML = '<select>' + optionsListString + '</select>';
for (var j = 0, options = helperDiv.children[0].children; j < options.length; j++) {
selectField.appendChild(options[j]);
}
} else {
selectField.innerHTML = optionsListString;
}
this._selectValue(currentValue);
}
} else if (propertyName === 'formatError' || propertyName === 'formatErrorMessages'
|| propertyName === 'error' || propertyName === 'errorMessages') {
this._cfg[propertyName] = newValue;
this._updateState();
} else {
this.$DropDownInput._onBoundPropertyChange.apply(this, arguments);
}
},
/**
* Set the 'selected' attribut on each options to true or false depending on the value
* @param {String} value The value to compare
* @private
*/
_selectValue : function(value) {
var selectField = this.getSelectField();
var options = selectField.options;
for (var i = 0; i < options.length; i++) {
var option = options[i];
if (option.value === value) {
option.selected = true;
break;
}
}
},
/**
* Returns the select element.
* @return {HTMLElement}
*/
getSelectField : function () {
if (!this._selectField && !this._initDone) {
this.getDom();
}
return this._selectField;
},
getTextInputField : function () {
return this.getSelectField();
}
}
});