Debian Codegraph @

dijit/form/Select.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.forEach
3 "dojo/_base/declare", // declare
4 "dojo/dom-attr", // domAttr.set
5 "dojo/dom-class", // domClass.add domClass.remove domClass.toggle
6 "dojo/dom-geometry", // domGeometry.setMarginBox
7 "dojo/i18n", // i18n.getLocalization
8 "dojo/keys",
9 "dojo/_base/lang", // lang.hitch
10 "dojo/on",
11 "dojo/sniff", // has("ie")
12 "./_FormSelectWidget",
13 "../_HasDropDown",
14 "../DropDownMenu",
15 "../MenuItem",
16 "../MenuSeparator",
17 "../Tooltip",
18 "../_KeyNavMixin",
19 "../registry", // registry.byNode
20 "dojo/text!./templates/Select.html",
21 "dojo/i18n!./nls/validate"
22 ], function(array, declare, domAttr, domClass, domGeometry, i18n, keys, lang, on, has,
23 _FormSelectWidget, _HasDropDown, DropDownMenu, MenuItem, MenuSeparator, Tooltip, _KeyNavMixin, registry, template){
24
25 // module:
26 // dijit/form/Select
27
28 var _SelectMenu = declare("dijit.form._SelectMenu", DropDownMenu, {
29 // summary:
30 // An internally-used menu for dropdown that allows us a vertical scrollbar
31
32 // Override Menu.autoFocus setting so that opening a Select highlights the current value.
33 autoFocus: true,
34
35 buildRendering: function(){
36 this.inherited(arguments);
37
38 this.domNode.setAttribute("role", "listbox");
39 },
40
41 postCreate: function(){
42 this.inherited(arguments);
43
44 // stop mousemove from selecting text on IE to be consistent with other browsers
45 this.own(on(this.domNode, "selectstart", function(evt){
46 evt.preventDefault();
47 evt.stopPropagation();
48 }));
49 },
50
51 focus: function(){
52 // summary:
53 // Overridden so that the previously selected value will be focused instead of only the first item
54 var found = false,
55 val = this.parentWidget.value;
56 if(lang.isArray(val)){
57 val = val[val.length - 1];
58 }
59 if(val){ // if focus selected
60 array.forEach(this.parentWidget._getChildren(), function(child){
61 if(child.option && (val === child.option.value)){ // find menu item widget with this value
62 found = true;
63 this.focusChild(child, false); // focus previous selection
64 }
65 }, this);
66 }
67 if(!found){
68 this.inherited(arguments); // focus first item by default
69 }
70 }
71 });
72
73 var Select = declare("dijit.form.Select" + (has("dojo-bidi") ? "_NoBidi" : ""), [_FormSelectWidget, _HasDropDown, _KeyNavMixin], {
74 // summary:
75 // This is a "styleable" select box - it is basically a DropDownButton which
76 // can take a `<select>` as its input.
77
78 baseClass: "dijitSelect dijitValidationTextBox",
79
80 templateString: template,
81
82 _buttonInputDisabled: has("ie") ? "disabled" : "", // allows IE to disallow focus, but Firefox cannot be disabled for mousedown events
83
84 // required: Boolean
85 // Can be true or false, default is false.
86 required: false,
87
88 // state: [readonly] String
89 // "Incomplete" if this select is required but unset (i.e. blank value), "" otherwise
90 state: "",
91
92 // message: String
93 // Currently displayed error/prompt message
94 message: "",
95
96 // tooltipPosition: String[]
97 // See description of `dijit/Tooltip.defaultPosition` for details on this parameter.
98 tooltipPosition: [],
99
100 // emptyLabel: string
101 // What to display in an "empty" dropdown
102 emptyLabel: "&#160;", // &nbsp;
103
104 // _isLoaded: Boolean
105 // Whether or not we have been loaded
106 _isLoaded: false,
107
108 // _childrenLoaded: Boolean
109 // Whether or not our children have been loaded
110 _childrenLoaded: false,
111
112 // labelType: String
113 // Specifies how to interpret the labelAttr in the data store items.
114 // Can be "html" or "text".
115 labelType: "html",
116
117 _fillContent: function(){
118 // summary:
119 // Set the value to be the first, or the selected index
120 this.inherited(arguments);
121 // set value from selected option
122 if(this.options.length && !this.value && this.srcNodeRef){
123 var si = this.srcNodeRef.selectedIndex || 0; // || 0 needed for when srcNodeRef is not a SELECT
124 this._set("value", this.options[si >= 0 ? si : 0].value);
125 }
126 // Create the dropDown widget
127 this.dropDown = new _SelectMenu({ id: this.id + "_menu", parentWidget: this });
128 domClass.add(this.dropDown.domNode, this.baseClass.replace(/\s+|$/g, "Menu "));
129 },
130
131 _getMenuItemForOption: function(/*_FormSelectWidget.__SelectOption*/ option){
132 // summary:
133 // For the given option, return the menu item that should be
134 // used to display it. This can be overridden as needed
135 if(!option.value && !option.label){
136 // We are a separator (no label set for it)
137 return new MenuSeparator({ownerDocument: this.ownerDocument});
138 }else{
139 // Just a regular menu option
140 var click = lang.hitch(this, "_setValueAttr", option);
141 var item = new MenuItem({
142 option: option,
143 label: (this.labelType === 'text' ? (option.label || '').toString()
144 .replace(/&/g, '&amp;').replace(/</g, '&lt;') :
145 option.label) || this.emptyLabel,
146 onClick: click,
147 ownerDocument: this.ownerDocument,
148 dir: this.dir,
149 textDir: this.textDir,
150 disabled: option.disabled || false
151 });
152 item.focusNode.setAttribute("role", "option");
153 return item;
154 }
155 },
156
157 _addOptionItem: function(/*_FormSelectWidget.__SelectOption*/ option){
158 // summary:
159 // For the given option, add an option to our dropdown.
160 // If the option doesn't have a value, then a separator is added
161 // in that place.
162 if(this.dropDown){
163 this.dropDown.addChild(this._getMenuItemForOption(option));
164 }
165 },
166
167 _getChildren: function(){
168 if(!this.dropDown){
169 return [];
170 }
171 return this.dropDown.getChildren();
172 },
173
174 focus: function(){
175 // Override _KeyNavMixin::focus(), which calls focusFirstChild().
176 // We just want the standard form widget behavior.
177 if(!this.disabled && this.focusNode.focus){
178 try{
179 this.focusNode.focus();
180 }catch(e){
181 /*squelch errors from hidden nodes*/
182 }
183 }
184 },
185
186 focusChild: function(/*dijit/_WidgetBase*/ widget){
187 // summary:
188 // Sets the value to the given option, used during search by letter.
189 // widget:
190 // Reference to option's widget
191 // tags:
192 // protected
193 if(widget){
194 this.set('value', widget.option);
195 }
196 },
197
198 _getFirst: function(){
199 // summary:
200 // Returns the first child widget.
201 // tags:
202 // abstract extension
203 var children = this._getChildren();
204 return children.length ? children[0] : null;
205 },
206
207 _getLast: function(){
208 // summary:
209 // Returns the last child widget.
210 // tags:
211 // abstract extension
212 var children = this._getChildren();
213 return children.length ? children[children.length-1] : null;
214 },
215
216 childSelector: function(/*DOMNode*/ node){
217 // Implement _KeyNavMixin.childSelector, to identify focusable child nodes.
218 // If we allowed a dojo/query dependency from this module this could more simply be a string "> *"
219 // instead of this function.
220
221 var node = registry.byNode(node);
222 return node && node.getParent() == this.dropDown;
223 },
224
225 onKeyboardSearch: function(/*dijit/_WidgetBase*/ item, /*Event*/ evt, /*String*/ searchString, /*Number*/ numMatches){
226 // summary:
227 // When a key is pressed that matches a child item,
228 // this method is called so that a widget can take appropriate action is necessary.
229 // tags:
230 // protected
231 if(item){
232 this.focusChild(item);
233 }
234 },
235
236 _loadChildren: function(/*Boolean*/ loadMenuItems){
237 // summary:
238 // Resets the menu and the length attribute of the button - and
239 // ensures that the label is appropriately set.
240 // loadMenuItems: Boolean
241 // actually loads the child menu items - we only do this when we are
242 // populating for showing the dropdown.
243
244 if(loadMenuItems === true){
245 // this.inherited destroys this.dropDown's child widgets (MenuItems).
246 // Avoid this.dropDown (Menu widget) having a pointer to a destroyed widget (which will cause
247 // issues later in _setSelected). (see #10296)
248 if(this.dropDown){
249 delete this.dropDown.focusedChild;
250 this.focusedChild = null;
251 }
252 if(this.options.length){
253 this.inherited(arguments);
254 }else{
255 // Drop down menu is blank but add one blank entry just so something appears on the screen
256 // to let users know that they are no choices (mimicing native select behavior)
257 array.forEach(this._getChildren(), function(child){
258 child.destroyRecursive();
259 });
260 var item = new MenuItem({
261 ownerDocument: this.ownerDocument,
262 label: this.emptyLabel
263 });
264 this.dropDown.addChild(item);
265 }
266 }else{
267 this._updateSelection();
268 }
269
270 this._isLoaded = false;
271 this._childrenLoaded = true;
272
273 if(!this._loadingStore){
274 // Don't call this if we are loading - since we will handle it later
275 this._setValueAttr(this.value, false);
276 }
277 },
278
279 _refreshState: function(){
280 if(this._started){
281 this.validate(this.focused);
282 }
283 },
284
285 startup: function(){
286 this.inherited(arguments);
287 this._refreshState(); // after all _set* methods have run
288 },
289
290 _setValueAttr: function(value){
291 this.inherited(arguments);
292 domAttr.set(this.valueNode, "value", this.get("value"));
293 this._refreshState(); // to update this.state
294 },
295
296 _setNameAttr: "valueNode",
297
298 _setDisabledAttr: function(/*Boolean*/ value){
299 this.inherited(arguments);
300 this._refreshState(); // to update this.state
301 },
302
303 _setRequiredAttr: function(/*Boolean*/ value){
304 this._set("required", value);
305 this.focusNode.setAttribute("aria-required", value);
306 this._refreshState(); // to update this.state
307 },
308
309 _setOptionsAttr: function(/*Array*/ options){
310 this._isLoaded = false;
311 this._set('options', options);
312 },
313
314 _setDisplay: function(/*String*/ newDisplay){
315 // summary:
316 // sets the display for the given value (or values)
317
318 var lbl = (this.labelType === 'text' ? (newDisplay || '')
319 .replace(/&/g, '&amp;').replace(/</g, '&lt;') :
320 newDisplay) || this.emptyLabel;
321 this.containerNode.innerHTML = '<span role="option" aria-selected="true" class="dijitReset dijitInline ' + this.baseClass.replace(/\s+|$/g, "Label ") + '">' + lbl + '</span>';
322 },
323
324 validate: function(/*Boolean*/ isFocused){
325 // summary:
326 // Called by oninit, onblur, and onkeypress, and whenever required/disabled state changes
327 // description:
328 // Show missing or invalid messages if appropriate, and highlight textbox field.
329 // Used when a select is initially set to no value and the user is required to
330 // set the value.
331
332 var isValid = this.disabled || this.isValid(isFocused);
333 this._set("state", isValid ? "" : (this._hasBeenBlurred ? "Error" : "Incomplete"));
334 this.focusNode.setAttribute("aria-invalid", isValid ? "false" : "true");
335 var message = isValid ? "" : this._missingMsg;
336 if(message && this.focused && this._hasBeenBlurred){
337 Tooltip.show(message, this.domNode, this.tooltipPosition, !this.isLeftToRight());
338 }else{
339 Tooltip.hide(this.domNode);
340 }
341 this._set("message", message);
342 return isValid;
343 },
344
345 isValid: function(/*Boolean*/ /*===== isFocused =====*/){
346 // summary:
347 // Whether or not this is a valid value. The only way a Select
348 // can be invalid is when it's required but nothing is selected.
349 return (!this.required || this.value === 0 || !(/^\s*$/.test(this.value || ""))); // handle value is null or undefined
350 },
351
352 reset: function(){
353 // summary:
354 // Overridden so that the state will be cleared.
355 this.inherited(arguments);
356 Tooltip.hide(this.domNode);
357 this._refreshState(); // to update this.state
358 },
359
360 postMixInProperties: function(){
361 // summary:
362 // set the missing message
363 this.inherited(arguments);
364 this._missingMsg = i18n.getLocalization("dijit.form", "validate", this.lang).missingMessage;
365 },
366
367 postCreate: function(){
368 this.inherited(arguments);
369
370 // stop mousemove from selecting text on IE to be consistent with other browsers
371 this.own(on(this.domNode, "selectstart", function(evt){
372 evt.preventDefault();
373 evt.stopPropagation();
374 }));
375
376 this.domNode.setAttribute("aria-expanded", "false");
377
378 // Prevent _KeyNavMixin from calling stopPropagation() on left and right arrow keys, thus breaking
379 // navigation when Select inside Toolbar.
380 var keyNavCodes = this._keyNavCodes;
381 delete keyNavCodes[keys.LEFT_ARROW];
382 delete keyNavCodes[keys.RIGHT_ARROW];
383 },
384
385 _setStyleAttr: function(/*String||Object*/ value){
386 this.inherited(arguments);
387 domClass.toggle(this.domNode, this.baseClass.replace(/\s+|$/g, "FixedWidth "), !!this.domNode.style.width);
388 },
389
390 isLoaded: function(){
391 return this._isLoaded;
392 },
393
394 loadDropDown: function(/*Function*/ loadCallback){
395 // summary:
396 // populates the menu
397 this._loadChildren(true);
398 this._isLoaded = true;
399 loadCallback();
400 },
401
402 destroy: function(preserveDom){
403 if(this.dropDown && !this.dropDown._destroyed){
404 this.dropDown.destroyRecursive(preserveDom);
405 delete this.dropDown;
406 }
407 Tooltip.hide(this.domNode); // in case Select (or enclosing Dialog) destroyed while tooltip shown
408 this.inherited(arguments);
409 },
410
411 _onFocus: function(){
412 this.validate(true); // show tooltip if second focus of required tooltip, but no selection
413 // Note: not calling superclass _onFocus() to avoid _KeyNavMixin::_onFocus() setting tabIndex --> -1
414 },
415
416 _onBlur: function(){
417 Tooltip.hide(this.domNode);
418 this.inherited(arguments);
419 this.validate(false);
420 }
421 });
422
423 if(has("dojo-bidi")){
424 Select = declare("dijit.form.Select", Select, {
425 _setDisplay: function(/*String*/ newDisplay){
426 this.inherited(arguments);
427 this.applyTextDir(this.containerNode);
428 }
429 });
430 }
431
432 Select._Menu = _SelectMenu; // for monkey patching
433
434 // generic event helper to ensure the dropdown items are loaded before the real event handler is called
435 function _onEventAfterLoad(method){
436 return function(evt){
437 if(!this._isLoaded){
438 this.loadDropDown(lang.hitch(this, method, evt));
439 }else{
440 this.inherited(method, arguments);
441 }
442 };
443 }
444 Select.prototype._onContainerKeydown = _onEventAfterLoad("_onContainerKeydown");
445 Select.prototype._onContainerKeypress = _onEventAfterLoad("_onContainerKeypress");
446
447 return Select;
448 });