| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/dom-attr", |
| 5 |
"dojo/dom-class", |
| 6 |
"dojo/dom-geometry", |
| 7 |
"dojo/i18n", |
| 8 |
"dojo/keys", |
| 9 |
"dojo/_base/lang", |
| 10 |
"dojo/on", |
| 11 |
"dojo/sniff", |
| 12 |
"./_FormSelectWidget", |
| 13 |
"../_HasDropDown", |
| 14 |
"../DropDownMenu", |
| 15 |
"../MenuItem", |
| 16 |
"../MenuSeparator", |
| 17 |
"../Tooltip", |
| 18 |
"../_KeyNavMixin", |
| 19 |
"../registry", |
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
var _SelectMenu = declare("dijit.form._SelectMenu", DropDownMenu, { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 45 |
this.own(on(this.domNode, "selectstart", function(evt){ |
| 46 |
evt.preventDefault(); |
| 47 |
evt.stopPropagation(); |
| 48 |
})); |
| 49 |
}, |
| 50 |
|
| 51 |
focus: function(){ |
| 52 |
|
| 53 |
|
| 54 |
var found = false, |
| 55 |
val = this.parentWidget.value; |
| 56 |
if(lang.isArray(val)){ |
| 57 |
val = val[val.length - 1]; |
| 58 |
} |
| 59 |
if(val){ |
| 60 |
array.forEach(this.parentWidget._getChildren(), function(child){ |
| 61 |
if(child.option && (val === child.option.value)){ |
| 62 |
found = true; |
| 63 |
this.focusChild(child, false); |
| 64 |
} |
| 65 |
}, this); |
| 66 |
} |
| 67 |
if(!found){ |
| 68 |
this.inherited(arguments); |
| 69 |
} |
| 70 |
} |
| 71 |
}); |
| 72 |
|
| 73 |
var Select = declare("dijit.form.Select" + (has("dojo-bidi") ? "_NoBidi" : ""), [_FormSelectWidget, _HasDropDown, _KeyNavMixin], { |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
baseClass: "dijitSelect dijitValidationTextBox", |
| 79 |
|
| 80 |
templateString: template, |
| 81 |
|
| 82 |
_buttonInputDisabled: has("ie") ? "disabled" : "", |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
required: false, |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
state: "", |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
message: "", |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
tooltipPosition: [], |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
emptyLabel: " ", |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
_isLoaded: false, |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
_childrenLoaded: false, |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
labelType: "html", |
| 116 |
|
| 117 |
_fillContent: function(){ |
| 118 |
|
| 119 |
|
| 120 |
this.inherited(arguments); |
| 121 |
|
| 122 |
if(this.options.length && !this.value && this.srcNodeRef){ |
| 123 |
var si = this.srcNodeRef.selectedIndex || 0; |
| 124 |
this._set("value", this.options[si >= 0 ? si : 0].value); |
| 125 |
} |
| 126 |
|
| 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( option){ |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
if(!option.value && !option.label){ |
| 136 |
|
| 137 |
return new MenuSeparator({ownerDocument: this.ownerDocument}); |
| 138 |
}else{ |
| 139 |
|
| 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, '&').replace(/</g, '<') : |
| 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( option){ |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 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 |
|
| 176 |
|
| 177 |
if(!this.disabled && this.focusNode.focus){ |
| 178 |
try{ |
| 179 |
this.focusNode.focus(); |
| 180 |
}catch(e){ |
| 181 |
|
| 182 |
} |
| 183 |
} |
| 184 |
}, |
| 185 |
|
| 186 |
focusChild: function( widget){ |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if(widget){ |
| 194 |
this.set('value', widget.option); |
| 195 |
} |
| 196 |
}, |
| 197 |
|
| 198 |
_getFirst: function(){ |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
var children = this._getChildren(); |
| 204 |
return children.length ? children[0] : null; |
| 205 |
}, |
| 206 |
|
| 207 |
_getLast: function(){ |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
var children = this._getChildren(); |
| 213 |
return children.length ? children[children.length-1] : null; |
| 214 |
}, |
| 215 |
|
| 216 |
childSelector: function( node){ |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
var node = registry.byNode(node); |
| 222 |
return node && node.getParent() == this.dropDown; |
| 223 |
}, |
| 224 |
|
| 225 |
onKeyboardSearch: function( item, evt, searchString, numMatches){ |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if(item){ |
| 232 |
this.focusChild(item); |
| 233 |
} |
| 234 |
}, |
| 235 |
|
| 236 |
_loadChildren: function( loadMenuItems){ |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
if(loadMenuItems === true){ |
| 245 |
|
| 246 |
|
| 247 |
|
| 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 |
|
| 256 |
|
| 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 |
|
| 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(); |
| 288 |
}, |
| 289 |
|
| 290 |
_setValueAttr: function(value){ |
| 291 |
this.inherited(arguments); |
| 292 |
domAttr.set(this.valueNode, "value", this.get("value")); |
| 293 |
this._refreshState(); |
| 294 |
}, |
| 295 |
|
| 296 |
_setNameAttr: "valueNode", |
| 297 |
|
| 298 |
_setDisabledAttr: function( value){ |
| 299 |
this.inherited(arguments); |
| 300 |
this._refreshState(); |
| 301 |
}, |
| 302 |
|
| 303 |
_setRequiredAttr: function( value){ |
| 304 |
this._set("required", value); |
| 305 |
this.focusNode.setAttribute("aria-required", value); |
| 306 |
this._refreshState(); |
| 307 |
}, |
| 308 |
|
| 309 |
_setOptionsAttr: function( options){ |
| 310 |
this._isLoaded = false; |
| 311 |
this._set('options', options); |
| 312 |
}, |
| 313 |
|
| 314 |
_setDisplay: function( newDisplay){ |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
var lbl = (this.labelType === 'text' ? (newDisplay || '') |
| 319 |
.replace(/&/g, '&').replace(/</g, '<') : |
| 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( isFocused){ |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 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( ){ |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
return (!this.required || this.value === 0 || !(/^\s*$/.test(this.value || ""))); |
| 350 |
}, |
| 351 |
|
| 352 |
reset: function(){ |
| 353 |
|
| 354 |
|
| 355 |
this.inherited(arguments); |
| 356 |
Tooltip.hide(this.domNode); |
| 357 |
this._refreshState(); |
| 358 |
}, |
| 359 |
|
| 360 |
postMixInProperties: function(){ |
| 361 |
|
| 362 |
|
| 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 |
|
| 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 |
|
| 379 |
|
| 380 |
var keyNavCodes = this._keyNavCodes; |
| 381 |
delete keyNavCodes[keys.LEFT_ARROW]; |
| 382 |
delete keyNavCodes[keys.RIGHT_ARROW]; |
| 383 |
}, |
| 384 |
|
| 385 |
_setStyleAttr: function( 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( loadCallback){ |
| 395 |
|
| 396 |
|
| 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); |
| 408 |
this.inherited(arguments); |
| 409 |
}, |
| 410 |
|
| 411 |
_onFocus: function(){ |
| 412 |
this.validate(true); |
| 413 |
|
| 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( newDisplay){ |
| 426 |
this.inherited(arguments); |
| 427 |
this.applyTextDir(this.containerNode); |
| 428 |
} |
| 429 |
}); |
| 430 |
} |
| 431 |
|
| 432 |
Select._Menu = _SelectMenu; |
| 433 |
|
| 434 |
|
| 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 |
}); |