| 1 |
define([ |
| 2 |
"dojo/aspect", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/dom-attr", |
| 5 |
"dojo/keys", |
| 6 |
"dojo/_base/lang", |
| 7 |
"dojo/query", |
| 8 |
"dojo/regexp", |
| 9 |
"dojo/sniff", |
| 10 |
"./DataList", |
| 11 |
"./_TextBoxMixin", |
| 12 |
"./_SearchMixin" |
| 13 |
], function(aspect, declare, domAttr, keys, lang, query, regexp, has, DataList, _TextBoxMixin, SearchMixin){ |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
var AutoCompleterMixin = declare("dijit.form._AutoCompleterMixin", SearchMixin, { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
item: null, |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
autoComplete: true, |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
highlightMatch: "first", |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
labelAttr: "", |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
labelType: "text", |
| 58 |
|
| 59 |
|
| 60 |
maxHeight: -1, |
| 61 |
|
| 62 |
|
| 63 |
_stopClickEvents: false, |
| 64 |
|
| 65 |
_getCaretPos: function( element){ |
| 66 |
|
| 67 |
var pos = 0; |
| 68 |
if(typeof(element.selectionStart) == "number"){ |
| 69 |
|
| 70 |
pos = element.selectionStart; |
| 71 |
}else if(has("ie")){ |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
var tr = element.ownerDocument.selection.createRange().duplicate(); |
| 77 |
var ntr = element.createTextRange(); |
| 78 |
tr.move("character", 0); |
| 79 |
ntr.move("character", 0); |
| 80 |
try{ |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
ntr.setEndPoint("EndToEnd", tr); |
| 85 |
pos = String(ntr.text).replace(/\r/g, "").length; |
| 86 |
}catch(e){ |
| 87 |
|
| 88 |
} |
| 89 |
} |
| 90 |
return pos; |
| 91 |
}, |
| 92 |
|
| 93 |
_setCaretPos: function( element, location){ |
| 94 |
location = parseInt(location); |
| 95 |
_TextBoxMixin.selectInputText(element, location, location); |
| 96 |
}, |
| 97 |
|
| 98 |
_setDisabledAttr: function( value){ |
| 99 |
|
| 100 |
|
| 101 |
this.inherited(arguments); |
| 102 |
this.domNode.setAttribute("aria-disabled", value ? "true" : "false"); |
| 103 |
}, |
| 104 |
|
| 105 |
_onKey: function( evt){ |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if(evt.charCode >= 32){ |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
var key = evt.charCode || evt.keyCode; |
| 114 |
|
| 115 |
|
| 116 |
if(key == keys.ALT || key == keys.CTRL || key == keys.META || key == keys.SHIFT){ |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
var pw = this.dropDown; |
| 121 |
var highlighted = null; |
| 122 |
this._abortQuery(); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
this.inherited(arguments); |
| 132 |
|
| 133 |
if(evt.altKey || evt.ctrlKey || evt.metaKey){ |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
if(this._opened){ |
| 138 |
highlighted = pw.getHighlightedOption(); |
| 139 |
} |
| 140 |
switch(key){ |
| 141 |
case keys.PAGE_DOWN: |
| 142 |
case keys.DOWN_ARROW: |
| 143 |
case keys.PAGE_UP: |
| 144 |
case keys.UP_ARROW: |
| 145 |
|
| 146 |
|
| 147 |
if(this._opened){ |
| 148 |
this._announceOption(highlighted); |
| 149 |
} |
| 150 |
evt.stopPropagation(); |
| 151 |
evt.preventDefault(); |
| 152 |
break; |
| 153 |
|
| 154 |
case keys.ENTER: |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if(highlighted){ |
| 159 |
|
| 160 |
if(highlighted == pw.nextButton){ |
| 161 |
this._nextSearch(1); |
| 162 |
|
| 163 |
evt.stopPropagation(); |
| 164 |
evt.preventDefault(); |
| 165 |
break; |
| 166 |
}else if(highlighted == pw.previousButton){ |
| 167 |
this._nextSearch(-1); |
| 168 |
|
| 169 |
evt.stopPropagation(); |
| 170 |
evt.preventDefault(); |
| 171 |
break; |
| 172 |
} |
| 173 |
|
| 174 |
evt.stopPropagation(); |
| 175 |
evt.preventDefault(); |
| 176 |
}else{ |
| 177 |
|
| 178 |
this._setBlurValue(); |
| 179 |
this._setCaretPos(this.focusNode, this.focusNode.value.length); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
case keys.TAB: |
| 184 |
var newvalue = this.get('displayedValue'); |
| 185 |
|
| 186 |
|
| 187 |
if(pw && (newvalue == pw._messages["previousMessage"] || newvalue == pw._messages["nextMessage"])){ |
| 188 |
break; |
| 189 |
} |
| 190 |
if(highlighted){ |
| 191 |
this._selectOption(highlighted); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
case keys.ESCAPE: |
| 196 |
if(this._opened){ |
| 197 |
this._lastQuery = null; |
| 198 |
this.closeDropDown(); |
| 199 |
} |
| 200 |
break; |
| 201 |
} |
| 202 |
}, |
| 203 |
|
| 204 |
_autoCompleteText: function( text){ |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
var fn = this.focusNode; |
| 213 |
|
| 214 |
|
| 215 |
_TextBoxMixin.selectInputText(fn, fn.value.length); |
| 216 |
|
| 217 |
var caseFilter = this.ignoreCase ? 'toLowerCase' : 'substr'; |
| 218 |
if(text[caseFilter](0).indexOf(this.focusNode.value[caseFilter](0)) == 0){ |
| 219 |
var cpos = this.autoComplete ? this._getCaretPos(fn) : fn.value.length; |
| 220 |
|
| 221 |
if((cpos + 1) > fn.value.length){ |
| 222 |
|
| 223 |
|
| 224 |
fn.value = text; |
| 225 |
|
| 226 |
_TextBoxMixin.selectInputText(fn, cpos); |
| 227 |
} |
| 228 |
}else{ |
| 229 |
|
| 230 |
fn.value = text; |
| 231 |
_TextBoxMixin.selectInputText(fn); |
| 232 |
} |
| 233 |
}, |
| 234 |
|
| 235 |
_openResultList: function( results, query, options){ |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
var wasSelected = this.dropDown.getHighlightedOption(); |
| 243 |
this.dropDown.clearResultList(); |
| 244 |
if(!results.length && options.start == 0){ |
| 245 |
this.closeDropDown(); |
| 246 |
return; |
| 247 |
} |
| 248 |
this._nextSearch = this.dropDown.onPage = lang.hitch(this, function(direction){ |
| 249 |
results.nextPage(direction !== -1); |
| 250 |
this.focus(); |
| 251 |
}); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
this.dropDown.createOptions( |
| 260 |
results, |
| 261 |
options, |
| 262 |
lang.hitch(this, "_getMenuLabelFromItem") |
| 263 |
); |
| 264 |
|
| 265 |
|
| 266 |
this._showResultList(); |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if("direction" in options){ |
| 272 |
if(options.direction){ |
| 273 |
this.dropDown.highlightFirstOption(); |
| 274 |
}else if(!options.direction){ |
| 275 |
this.dropDown.highlightLastOption(); |
| 276 |
} |
| 277 |
if(wasSelected){ |
| 278 |
this._announceOption(this.dropDown.getHighlightedOption()); |
| 279 |
} |
| 280 |
}else if(this.autoComplete && !this._prev_key_backspace |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
&& !/^[*]+$/.test(query[this.searchAttr].toString())){ |
| 286 |
this._announceOption(this.dropDown.containerNode.firstChild.nextSibling); |
| 287 |
} |
| 288 |
}, |
| 289 |
|
| 290 |
_showResultList: function(){ |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
this.closeDropDown(true); |
| 295 |
this.openDropDown(); |
| 296 |
this.domNode.setAttribute("aria-expanded", "true"); |
| 297 |
}, |
| 298 |
|
| 299 |
loadDropDown: function( ){ |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
this._startSearchAll(); |
| 304 |
}, |
| 305 |
|
| 306 |
isLoaded: function(){ |
| 307 |
|
| 308 |
|
| 309 |
return false; |
| 310 |
}, |
| 311 |
|
| 312 |
closeDropDown: function(){ |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
this._abortQuery(); |
| 317 |
if(this._opened){ |
| 318 |
this.inherited(arguments); |
| 319 |
this.domNode.setAttribute("aria-expanded", "false"); |
| 320 |
} |
| 321 |
}, |
| 322 |
|
| 323 |
_setBlurValue: function(){ |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
var newvalue = this.get('displayedValue'); |
| 330 |
var pw = this.dropDown; |
| 331 |
if(pw && (newvalue == pw._messages["previousMessage"] || newvalue == pw._messages["nextMessage"])){ |
| 332 |
this._setValueAttr(this._lastValueReported, true); |
| 333 |
}else if(typeof this.item == "undefined"){ |
| 334 |
|
| 335 |
this.item = null; |
| 336 |
this.set('displayedValue', newvalue); |
| 337 |
}else{ |
| 338 |
if(this.value != this._lastValueReported){ |
| 339 |
this._handleOnChange(this.value, true); |
| 340 |
} |
| 341 |
this._refreshState(); |
| 342 |
} |
| 343 |
|
| 344 |
this.focusNode.removeAttribute("aria-activedescendant"); |
| 345 |
}, |
| 346 |
|
| 347 |
_setItemAttr: function( item, priorityChange, displayedValue){ |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
var value = ''; |
| 357 |
if(item){ |
| 358 |
if(!displayedValue){ |
| 359 |
displayedValue = this.store._oldAPI ? |
| 360 |
this.store.getValue(item, this.searchAttr) : item[this.searchAttr]; |
| 361 |
} |
| 362 |
value = this._getValueField() != this.searchAttr ? this.store.getIdentity(item) : displayedValue; |
| 363 |
} |
| 364 |
this.set('value', value, priorityChange, displayedValue, item); |
| 365 |
}, |
| 366 |
|
| 367 |
_announceOption: function( node){ |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if(!node){ |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
var newValue; |
| 378 |
if(node == this.dropDown.nextButton || |
| 379 |
node == this.dropDown.previousButton){ |
| 380 |
newValue = node.innerHTML; |
| 381 |
this.item = undefined; |
| 382 |
this.value = ''; |
| 383 |
}else{ |
| 384 |
var item = this.dropDown.items[node.getAttribute("item")]; |
| 385 |
newValue = (this.store._oldAPI ? |
| 386 |
this.store.getValue(item, this.searchAttr) : item[this.searchAttr]).toString(); |
| 387 |
this.set('item', item, false, newValue); |
| 388 |
} |
| 389 |
|
| 390 |
this.focusNode.value = this.focusNode.value.substring(0, this._lastInput.length); |
| 391 |
|
| 392 |
this.focusNode.setAttribute("aria-activedescendant", domAttr.get(node, "id")); |
| 393 |
|
| 394 |
this._autoCompleteText(newValue); |
| 395 |
}, |
| 396 |
|
| 397 |
_selectOption: function( target){ |
| 398 |
|
| 399 |
|
| 400 |
this.closeDropDown(); |
| 401 |
if(target){ |
| 402 |
this._announceOption(target); |
| 403 |
} |
| 404 |
this._setCaretPos(this.focusNode, this.focusNode.value.length); |
| 405 |
this._handleOnChange(this.value, true); |
| 406 |
|
| 407 |
|
| 408 |
this.focusNode.removeAttribute("aria-activedescendant"); |
| 409 |
}, |
| 410 |
|
| 411 |
_startSearchAll: function(){ |
| 412 |
this._startSearch(''); |
| 413 |
}, |
| 414 |
|
| 415 |
_startSearchFromInput: function(){ |
| 416 |
this.item = undefined; |
| 417 |
this.inherited(arguments); |
| 418 |
}, |
| 419 |
|
| 420 |
_startSearch: function( key){ |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
if(!this.dropDown){ |
| 425 |
var popupId = this.id + "_popup", |
| 426 |
dropDownConstructor = lang.isString(this.dropDownClass) ? |
| 427 |
lang.getObject(this.dropDownClass, false) : this.dropDownClass; |
| 428 |
this.dropDown = new dropDownConstructor({ |
| 429 |
onChange: lang.hitch(this, this._selectOption), |
| 430 |
id: popupId, |
| 431 |
dir: this.dir, |
| 432 |
textDir: this.textDir |
| 433 |
}); |
| 434 |
} |
| 435 |
this._lastInput = key; |
| 436 |
this.inherited(arguments); |
| 437 |
}, |
| 438 |
|
| 439 |
_getValueField: function(){ |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
return this.searchAttr; |
| 444 |
}, |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
postMixInProperties: function(){ |
| 449 |
this.inherited(arguments); |
| 450 |
if(!this.store && this.srcNodeRef){ |
| 451 |
var srcNodeRef = this.srcNodeRef; |
| 452 |
|
| 453 |
this.store = new DataList({}, srcNodeRef); |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
if(!("value" in this.params)){ |
| 462 |
var item = (this.item = this.store.fetchSelectedItem()); |
| 463 |
if(item){ |
| 464 |
var valueField = this._getValueField(); |
| 465 |
|
| 466 |
this.value = this.store._oldAPI ? this.store.getValue(item, valueField) : item[valueField]; |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
}, |
| 471 |
|
| 472 |
postCreate: function(){ |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
var label = query('label[for="' + this.id + '"]'); |
| 480 |
if(label.length){ |
| 481 |
if(!label[0].id){ |
| 482 |
label[0].id = this.id + "_label"; |
| 483 |
} |
| 484 |
this.domNode.setAttribute("aria-labelledby", label[0].id); |
| 485 |
|
| 486 |
} |
| 487 |
this.inherited(arguments); |
| 488 |
aspect.after(this, "onSearch", lang.hitch(this, "_openResultList"), true); |
| 489 |
}, |
| 490 |
|
| 491 |
_getMenuLabelFromItem: function( item){ |
| 492 |
var label = this.labelFunc(item, this.store), |
| 493 |
labelType = this.labelType; |
| 494 |
|
| 495 |
if(this.highlightMatch != "none" && this.labelType == "text" && this._lastInput){ |
| 496 |
label = this.doHighlight(label, this._lastInput); |
| 497 |
labelType = "html"; |
| 498 |
} |
| 499 |
return {html: labelType == "html", label: label}; |
| 500 |
}, |
| 501 |
|
| 502 |
doHighlight: function( label, find){ |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
var |
| 511 |
|
| 512 |
modifiers = (this.ignoreCase ? "i" : "") + (this.highlightMatch == "all" ? "g" : ""), |
| 513 |
i = this.queryExpr.indexOf("${0}"); |
| 514 |
find = regexp.escapeString(find); |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
return this._escapeHtml(label.replace( |
| 520 |
new RegExp((i == 0 ? "^" : "") + "(" + find + ")" + (i == (this.queryExpr.length - 4) ? "$" : ""), modifiers), |
| 521 |
'\uFFFF$1\uFFFF')).replace( |
| 522 |
/\uFFFF([^\uFFFF]+)\uFFFF/g, '<span class="dijitComboBoxHighlightMatch">$1</span>' |
| 523 |
); |
| 524 |
}, |
| 525 |
|
| 526 |
_escapeHtml: function( str){ |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
str = String(str).replace(/&/gm, "&").replace(/</gm, "<") |
| 531 |
.replace(/>/gm, ">").replace(/"/gm, """); |
| 532 |
return str; |
| 533 |
}, |
| 534 |
|
| 535 |
reset: function(){ |
| 536 |
|
| 537 |
|
| 538 |
this.item = null; |
| 539 |
this.inherited(arguments); |
| 540 |
}, |
| 541 |
|
| 542 |
labelFunc: function(item, store){ |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
return (store._oldAPI ? store.getValue(item, this.labelAttr || this.searchAttr) : |
| 558 |
item[this.labelAttr || this.searchAttr]).toString(); |
| 559 |
}, |
| 560 |
|
| 561 |
_setValueAttr: function( value, priorityChange, displayedValue, item){ |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
this._set("item", item || null); |
| 567 |
if(value == null ){ |
| 568 |
value = ''; |
| 569 |
} |
| 570 |
this.inherited(arguments); |
| 571 |
} |
| 572 |
}); |
| 573 |
|
| 574 |
if(has("dojo-bidi")){ |
| 575 |
AutoCompleterMixin.extend({ |
| 576 |
_setTextDirAttr: function( textDir){ |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
this.inherited(arguments); |
| 585 |
|
| 586 |
if(this.dropDown){ |
| 587 |
this.dropDown._set("textDir", textDir); |
| 588 |
} |
| 589 |
} |
| 590 |
}); |
| 591 |
} |
| 592 |
|
| 593 |
return AutoCompleterMixin; |
| 594 |
}); |