| 1 | define([ |
| 2 | "dojo/_base/array", // array.forEach |
| 3 | "dojo/_base/declare", // declare |
| 4 | "dojo/dom-attr", // domAttr.set |
| 5 | "dojo/keys", // keys.END keys.HOME, keys.LEFT_ARROW etc. |
| 6 | "dojo/_base/lang", // lang.hitch |
| 7 | "dojo/on", |
| 8 | "dijit/registry", |
| 9 | "dijit/_FocusMixin" // to make _onBlur() work |
| 10 | ], function(array, declare, domAttr, keys, lang, on, registry, _FocusMixin){ |
| 11 | |
| 12 | // module: |
| 13 | // dijit/_KeyNavMixin |
| 14 | |
| 15 | return declare("dijit._KeyNavMixin", _FocusMixin, { |
| 16 | // summary: |
| 17 | // A mixin to allow arrow key and letter key navigation of child or descendant widgets. |
| 18 | // It can be used by dijit/_Container based widgets with a flat list of children, |
| 19 | // or more complex widgets like dijit/Tree. |
| 20 | // |
| 21 | // To use this mixin, the subclass must: |
| 22 | // |
| 23 | // - Implement _getNext(), _getFirst(), _getLast(), _onLeftArrow(), _onRightArrow() |
| 24 | // _onDownArrow(), _onUpArrow() methods to handle home/end/left/right/up/down keystrokes. |
| 25 | // Next and previous in this context refer to a linear ordering of the descendants used |
| 26 | // by letter key search. |
| 27 | // - Set all descendants' initial tabIndex to "-1"; both initial descendants and any |
| 28 | // descendants added later, by for example addChild() |
| 29 | // - Define childSelector to a function or string that identifies focusable descendant widgets |
| 30 | // |
| 31 | // Also, child widgets must implement a focus() method. |
| 32 | |
| 33 | /*===== |
| 34 | // focusedChild: [protected readonly] Widget |
| 35 | // The currently focused child widget, or null if there isn't one |
| 36 | focusedChild: null, |
| 37 | |
| 38 | // _keyNavCodes: Object |
| 39 | // Hash mapping key code (arrow keys and home/end key) to functions to handle those keys. |
| 40 | // Usually not used directly, as subclasses can instead override _onLeftArrow() etc. |
| 41 | _keyNavCodes: {}, |
| 42 | =====*/ |
| 43 | |
| 44 | // tabIndex: String |
| 45 | // Tab index of the container; same as HTML tabIndex attribute. |
| 46 | // Note then when user tabs into the container, focus is immediately |
| 47 | // moved to the first item in the container. |
| 48 | tabIndex: "0", |
| 49 | |
| 50 | // childSelector: [protected abstract] Function||String |
| 51 | // Selector (passed to on.selector()) used to identify what to treat as a child widget. Used to monitor |
| 52 | // focus events and set this.focusedChild. Must be set by implementing class. If this is a string |
| 53 | // (ex: "> *") then the implementing class must require dojo/query. |
| 54 | childSelector: null, |
| 55 | |
| 56 | postCreate: function(){ |
| 57 | this.inherited(arguments); |
| 58 | |
| 59 | // Set tabIndex on this.domNode. Will be automatic after #7381 is fixed. |
| 60 | domAttr.set(this.domNode, "tabIndex", this.tabIndex); |
| 61 | |
| 62 | if(!this._keyNavCodes){ |
| 63 | var keyCodes = this._keyNavCodes = {}; |
| 64 | keyCodes[keys.HOME] = lang.hitch(this, "focusFirstChild"); |
| 65 | keyCodes[keys.END] = lang.hitch(this, "focusLastChild"); |
| 66 | keyCodes[this.isLeftToRight() ? keys.LEFT_ARROW : keys.RIGHT_ARROW] = lang.hitch(this, "_onLeftArrow"); |
| 67 | keyCodes[this.isLeftToRight() ? keys.RIGHT_ARROW : keys.LEFT_ARROW] = lang.hitch(this, "_onRightArrow"); |
| 68 | keyCodes[keys.UP_ARROW] = lang.hitch(this, "_onUpArrow"); |
| 69 | keyCodes[keys.DOWN_ARROW] = lang.hitch(this, "_onDownArrow"); |
| 70 | } |
| 71 | |
| 72 | var self = this, |
| 73 | childSelector = typeof this.childSelector == "string" |
| 74 | ? this.childSelector |
| 75 | : lang.hitch(this, "childSelector"); |
| 76 | this.own( |
| 77 | on(this.domNode, "keypress", lang.hitch(this, "_onContainerKeypress")), |
| 78 | on(this.domNode, "keydown", lang.hitch(this, "_onContainerKeydown")), |
| 79 | on(this.domNode, "focus", lang.hitch(this, "_onContainerFocus")), |
| 80 | on(this.containerNode, on.selector(childSelector, "focusin"), function(evt){ |
| 81 | self._onChildFocus(registry.getEnclosingWidget(this), evt); |
| 82 | }) |
| 83 | ); |
| 84 | }, |
| 85 | |
| 86 | _onLeftArrow: function(){ |
| 87 | // summary: |
| 88 | // Called on left arrow key, or right arrow key if widget is in RTL mode. |
| 89 | // Should go back to the previous child in horizontal container widgets like Toolbar. |
| 90 | // tags: |
| 91 | // extension |
| 92 | }, |
| 93 | |
| 94 | _onRightArrow: function(){ |
| 95 | // summary: |
| 96 | // Called on right arrow key, or left arrow key if widget is in RTL mode. |
| 97 | // Should go to the next child in horizontal container widgets like Toolbar. |
| 98 | // tags: |
| 99 | // extension |
| 100 | }, |
| 101 | |
| 102 | _onUpArrow: function(){ |
| 103 | // summary: |
| 104 | // Called on up arrow key. Should go to the previous child in vertical container widgets like Menu. |
| 105 | // tags: |
| 106 | // extension |
| 107 | }, |
| 108 | |
| 109 | _onDownArrow: function(){ |
| 110 | // summary: |
| 111 | // Called on down arrow key. Should go to the next child in vertical container widgets like Menu. |
| 112 | // tags: |
| 113 | // extension |
| 114 | }, |
| 115 | |
| 116 | focus: function(){ |
| 117 | // summary: |
| 118 | // Default focus() implementation: focus the first child. |
| 119 | this.focusFirstChild(); |
| 120 | }, |
| 121 | |
| 122 | _getFirstFocusableChild: function(){ |
| 123 | // summary: |
| 124 | // Returns first child that can be focused. |
| 125 | |
| 126 | // Leverage _getNextFocusableChild() to skip disabled children |
| 127 | return this._getNextFocusableChild(null, 1); // dijit/_WidgetBase |
| 128 | }, |
| 129 | |
| 130 | _getLastFocusableChild: function(){ |
| 131 | // summary: |
| 132 | // Returns last child that can be focused. |
| 133 | |
| 134 | // Leverage _getNextFocusableChild() to skip disabled children |
| 135 | return this._getNextFocusableChild(null, -1); // dijit/_WidgetBase |
| 136 | }, |
| 137 | |
| 138 | focusFirstChild: function(){ |
| 139 | // summary: |
| 140 | // Focus the first focusable child in the container. |
| 141 | // tags: |
| 142 | // protected |
| 143 | |
| 144 | this.focusChild(this._getFirstFocusableChild()); |
| 145 | }, |
| 146 | |
| 147 | focusLastChild: function(){ |
| 148 | // summary: |
| 149 | // Focus the last focusable child in the container. |
| 150 | // tags: |
| 151 | // protected |
| 152 | |
| 153 | this.focusChild(this._getLastFocusableChild()); |
| 154 | }, |
| 155 | |
| 156 | focusChild: function(/*dijit/_WidgetBase*/ widget, /*Boolean*/ last){ |
| 157 | // summary: |
| 158 | // Focus specified child widget. |
| 159 | // widget: |
| 160 | // Reference to container's child widget |
| 161 | // last: |
| 162 | // If true and if widget has multiple focusable nodes, focus the |
| 163 | // last one instead of the first one |
| 164 | // tags: |
| 165 | // protected |
| 166 | |
| 167 | if(!widget){ |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | if(this.focusedChild && widget !== this.focusedChild){ |
| 172 | this._onChildBlur(this.focusedChild); // used to be used by _MenuBase |
| 173 | } |
| 174 | widget.set("tabIndex", this.tabIndex); // for IE focus outline to appear, must set tabIndex before focus |
| 175 | widget.focus(last ? "end" : "start"); |
| 176 | |
| 177 | // Don't set focusedChild here, because the focus event should trigger a call to _onChildFocus(), which will |
| 178 | // set it. More importantly, _onChildFocus(), which may be executed asynchronously (after this function |
| 179 | // returns) needs to know the old focusedChild to set its tabIndex to -1. |
| 180 | }, |
| 181 | |
| 182 | _onContainerFocus: function(evt){ |
| 183 | // summary: |
| 184 | // Handler for when the container itself gets focus. |
| 185 | // description: |
| 186 | // Initially the container itself has a tabIndex, but when it gets |
| 187 | // focus, switch focus to first child. |
| 188 | // |
| 189 | // TODO for 2.0 (or earlier): Instead of having the container tabbable, always maintain a single child |
| 190 | // widget as tabbable, Requires code in startup(), addChild(), and removeChild(). |
| 191 | // That would avoid various issues like #17347. |
| 192 | // tags: |
| 193 | // private |
| 194 | |
| 195 | // Note that we can't use _onFocus() because switching focus from the |
| 196 | // _onFocus() handler confuses the focus.js code |
| 197 | // (because it causes _onFocusNode() to be called recursively). |
| 198 | // Also, _onFocus() would fire when focus went directly to a child widget due to mouse click. |
| 199 | |
| 200 | // Ignore spurious focus events: |
| 201 | // 1. focus on a child widget bubbles on FF |
| 202 | // 2. on IE, clicking the scrollbar of a select dropdown moves focus from the focused child item to me |
| 203 | if(evt.target !== this.domNode || this.focusedChild){ |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | this.focus(); |
| 208 | }, |
| 209 | |
| 210 | _onFocus: function(){ |
| 211 | // When the container gets focus by being tabbed into, or a descendant gets focus by being clicked, |
| 212 | // set the container's tabIndex to -1 (don't remove as that breaks Safari 4) so that tab or shift-tab |
| 213 | // will go to the fields after/before the container, rather than the container itself |
| 214 | domAttr.set(this.domNode, "tabIndex", "-1"); |
| 215 | |
| 216 | this.inherited(arguments); |
| 217 | }, |
| 218 | |
| 219 | _onBlur: function(evt){ |
| 220 | // When focus is moved away the container, and its descendant (popup) widgets, |
| 221 | // then restore the container's tabIndex so that user can tab to it again. |
| 222 | // Note that using _onBlur() so that this doesn't happen when focus is shifted |
| 223 | // to one of my child widgets (typically a popup) |
| 224 | |
| 225 | // TODO: for 2.0 consider changing this to blur whenever the container blurs, to be truthful that there is |
| 226 | // no focused child at that time. |
| 227 | |
| 228 | domAttr.set(this.domNode, "tabIndex", this.tabIndex); |
| 229 | if(this.focusedChild){ |
| 230 | this.focusedChild.set("tabIndex", "-1"); |
| 231 | this.lastFocusedChild = this.focusedChild; |
| 232 | this._set("focusedChild", null); |
| 233 | } |
| 234 | this.inherited(arguments); |
| 235 | }, |
| 236 | |
| 237 | _onChildFocus: function(/*dijit/_WidgetBase*/ child){ |
| 238 | // summary: |
| 239 | // Called when a child widget gets focus, either by user clicking |
| 240 | // it, or programatically by arrow key handling code. |
| 241 | // description: |
| 242 | // It marks that the current node is the selected one, and the previously |
| 243 | // selected node no longer is. |
| 244 | |
| 245 | if(child && child != this.focusedChild){ |
| 246 | if(this.focusedChild && !this.focusedChild._destroyed){ |
| 247 | // mark that the previously focusable node is no longer focusable |
| 248 | this.focusedChild.set("tabIndex", "-1"); |
| 249 | } |
| 250 | |
| 251 | // mark that the new node is the currently selected one |
| 252 | child.set("tabIndex", this.tabIndex); |
| 253 | this.lastFocused = child; // back-compat for Tree, remove for 2.0 |
| 254 | this._set("focusedChild", child); |
| 255 | } |
| 256 | }, |
| 257 | |
| 258 | _searchString: "", |
| 259 | // multiCharSearchDuration: Number |
| 260 | // If multiple characters are typed where each keystroke happens within |
| 261 | // multiCharSearchDuration of the previous keystroke, |
| 262 | // search for nodes matching all the keystrokes. |
| 263 | // |
| 264 | // For example, typing "ab" will search for entries starting with |
| 265 | // "ab" unless the delay between "a" and "b" is greater than multiCharSearchDuration. |
| 266 | multiCharSearchDuration: 1000, |
| 267 | |
| 268 | onKeyboardSearch: function(/*dijit/_WidgetBase*/ item, /*Event*/ evt, /*String*/ searchString, /*Number*/ numMatches){ |
| 269 | // summary: |
| 270 | // When a key is pressed that matches a child item, |
| 271 | // this method is called so that a widget can take appropriate action is necessary. |
| 272 | // tags: |
| 273 | // protected |
| 274 | if(item){ |
| 275 | this.focusChild(item); |
| 276 | } |
| 277 | }, |
| 278 | |
| 279 | _keyboardSearchCompare: function(/*dijit/_WidgetBase*/ item, /*String*/ searchString){ |
| 280 | // summary: |
| 281 | // Compares the searchString to the widget's text label, returning: |
| 282 | // |
| 283 | // * -1: a high priority match and stop searching |
| 284 | // * 0: not a match |
| 285 | // * 1: a match but keep looking for a higher priority match |
| 286 | // tags: |
| 287 | // private |
| 288 | |
| 289 | var element = item.domNode, |
| 290 | text = item.label || (element.focusNode ? element.focusNode.label : '') || element.innerText || element.textContent || "", |
| 291 | currentString = text.replace(/^\s+/, '').substr(0, searchString.length).toLowerCase(); |
| 292 | |
| 293 | return (!!searchString.length && currentString == searchString) ? -1 : 0; // stop searching after first match by default |
| 294 | }, |
| 295 | |
| 296 | _onContainerKeydown: function(evt){ |
| 297 | // summary: |
| 298 | // When a key is pressed, if it's an arrow key etc. then it's handled here. |
| 299 | // tags: |
| 300 | // private |
| 301 | |
| 302 | var func = this._keyNavCodes[evt.keyCode]; |
| 303 | if(func){ |
| 304 | func(evt, this.focusedChild); |
| 305 | evt.stopPropagation(); |
| 306 | evt.preventDefault(); |
| 307 | this._searchString = ''; // so a DOWN_ARROW b doesn't search for ab |
| 308 | }else if(evt.keyCode == keys.SPACE && this._searchTimer && !(evt.ctrlKey || evt.altKey || evt.metaKey)){ |
| 309 | evt.stopImmediatePropagation(); // stop a11yclick and _HasDropdown from seeing SPACE if we're doing keyboard searching |
| 310 | evt.preventDefault(); // stop IE from scrolling, and most browsers (except FF) from sending keypress |
| 311 | this._keyboardSearch(evt, ' '); |
| 312 | } |
| 313 | }, |
| 314 | |
| 315 | _onContainerKeypress: function(evt){ |
| 316 | // summary: |
| 317 | // When a printable key is pressed, it's handled here, searching by letter. |
| 318 | // tags: |
| 319 | // private |
| 320 | |
| 321 | // Ignore: |
| 322 | // - duplicate events on firefox (ex: arrow key that will be handled by keydown handler) |
| 323 | // - control sequences like CMD-Q. |
| 324 | // - the SPACE key (only occurs on FF) |
| 325 | // |
| 326 | // Note: if there's no search in progress, then SPACE should be ignored. If there is a search |
| 327 | // in progress, then SPACE is handled in _onContainerKeyDown. |
| 328 | if(evt.charCode <= keys.SPACE || evt.ctrlKey || evt.altKey || evt.metaKey){ |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | evt.preventDefault(); |
| 333 | evt.stopPropagation(); |
| 334 | |
| 335 | this._keyboardSearch(evt, String.fromCharCode(evt.charCode).toLowerCase()); |
| 336 | }, |
| 337 | |
| 338 | _keyboardSearch: function(/*Event*/ evt, /*String*/ keyChar){ |
| 339 | // summary: |
| 340 | // Perform a search of the widget's options based on the user's keyboard activity |
| 341 | // description: |
| 342 | // Called on keypress (and sometimes keydown), searches through this widget's children |
| 343 | // looking for items that match the user's typed search string. Multiple characters |
| 344 | // typed within 1 sec of each other are combined for multicharacter searching. |
| 345 | // tags: |
| 346 | // private |
| 347 | var |
| 348 | matchedItem = null, |
| 349 | searchString, |
| 350 | numMatches = 0, |
| 351 | search = lang.hitch(this, function(){ |
| 352 | if(this._searchTimer){ |
| 353 | this._searchTimer.remove(); |
| 354 | } |
| 355 | this._searchString += keyChar; |
| 356 | var allSameLetter = /^(.)\1*$/.test(this._searchString); |
| 357 | var searchLen = allSameLetter ? 1 : this._searchString.length; |
| 358 | searchString = this._searchString.substr(0, searchLen); |
| 359 | // commented out code block to search again if the multichar search fails after a smaller timeout |
| 360 | //this._searchTimer = this.defer(function(){ // this is the "failure" timeout |
| 361 | // this._typingSlowly = true; // if the search fails, then treat as a full timeout |
| 362 | // this._searchTimer = this.defer(function(){ // this is the "success" timeout |
| 363 | // this._searchTimer = null; |
| 364 | // this._searchString = ''; |
| 365 | // }, this.multiCharSearchDuration >> 1); |
| 366 | //}, this.multiCharSearchDuration >> 1); |
| 367 | this._searchTimer = this.defer(function(){ // this is the "success" timeout |
| 368 | this._searchTimer = null; |
| 369 | this._searchString = ''; |
| 370 | }, this.multiCharSearchDuration); |
| 371 | var currentItem = this.focusedChild || null; |
| 372 | if(searchLen == 1 || !currentItem){ |
| 373 | currentItem = this._getNextFocusableChild(currentItem, 1); // skip current |
| 374 | if(!currentItem){ |
| 375 | return; |
| 376 | } // no items |
| 377 | } |
| 378 | var stop = currentItem; |
| 379 | do{ |
| 380 | var rc = this._keyboardSearchCompare(currentItem, searchString); |
| 381 | if(!!rc && numMatches++ == 0){ |
| 382 | matchedItem = currentItem; |
| 383 | } |
| 384 | if(rc == -1){ // priority match |
| 385 | numMatches = -1; |
| 386 | break; |
| 387 | } |
| 388 | currentItem = this._getNextFocusableChild(currentItem, 1); |
| 389 | }while(currentItem && currentItem != stop); |
| 390 | // commented out code block to search again if the multichar search fails after a smaller timeout |
| 391 | //if(!numMatches && (this._typingSlowly || searchLen == 1)){ |
| 392 | // this._searchString = ''; |
| 393 | // if(searchLen > 1){ |
| 394 | // // if no matches and they're typing slowly, then go back to first letter searching |
| 395 | // search(); |
| 396 | // } |
| 397 | //} |
| 398 | }); |
| 399 | |
| 400 | search(); |
| 401 | // commented out code block to search again if the multichar search fails after a smaller timeout |
| 402 | //this._typingSlowly = false; |
| 403 | this.onKeyboardSearch(matchedItem, evt, searchString, numMatches); |
| 404 | }, |
| 405 | |
| 406 | _onChildBlur: function(/*dijit/_WidgetBase*/ /*===== widget =====*/){ |
| 407 | // summary: |
| 408 | // Called when focus leaves a child widget to go |
| 409 | // to a sibling widget. |
| 410 | // Used to be used by MenuBase.js (remove for 2.0) |
| 411 | // tags: |
| 412 | // protected |
| 413 | }, |
| 414 | |
| 415 | _getNextFocusableChild: function(child, dir){ |
| 416 | // summary: |
| 417 | // Returns the next or previous focusable descendant, compared to "child". |
| 418 | // Implements and extends _KeyNavMixin._getNextFocusableChild() for a _Container. |
| 419 | // child: Widget |
| 420 | // The current widget |
| 421 | // dir: Integer |
| 422 | // - 1 = after |
| 423 | // - -1 = before |
| 424 | // tags: |
| 425 | // abstract extension |
| 426 | |
| 427 | var wrappedValue = child; |
| 428 | do{ |
| 429 | if(!child){ |
| 430 | child = this[dir > 0 ? "_getFirst" : "_getLast"](); |
| 431 | if(!child){ break; } |
| 432 | }else{ |
| 433 | child = this._getNext(child, dir); |
| 434 | } |
| 435 | if(child != null && child != wrappedValue && child.isFocusable()){ |
| 436 | return child; // dijit/_WidgetBase |
| 437 | } |
| 438 | }while(child != wrappedValue); |
| 439 | // no focusable child found |
| 440 | return null; // dijit/_WidgetBase |
| 441 | }, |
| 442 | |
| 443 | _getFirst: function(){ |
| 444 | // summary: |
| 445 | // Returns the first child. |
| 446 | // tags: |
| 447 | // abstract extension |
| 448 | |
| 449 | return null; // dijit/_WidgetBase |
| 450 | }, |
| 451 | |
| 452 | _getLast: function(){ |
| 453 | // summary: |
| 454 | // Returns the last descendant. |
| 455 | // tags: |
| 456 | // abstract extension |
| 457 | |
| 458 | return null; // dijit/_WidgetBase |
| 459 | }, |
| 460 | |
| 461 | _getNext: function(child, dir){ |
| 462 | // summary: |
| 463 | // Returns the next descendant, compared to "child". |
| 464 | // child: Widget |
| 465 | // The current widget |
| 466 | // dir: Integer |
| 467 | // - 1 = after |
| 468 | // - -1 = before |
| 469 | // tags: |
| 470 | // abstract extension |
| 471 | |
| 472 | if(child){ |
| 473 | child = child.domNode; |
| 474 | while(child){ |
| 475 | child = child[dir < 0 ? "previousSibling" : "nextSibling"]; |
| 476 | if(child && "getAttribute" in child){ |
| 477 | var w = registry.byNode(child); |
| 478 | if(w){ |
| 479 | return w; // dijit/_WidgetBase |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | return null; // dijit/_WidgetBase |
| 485 | } |
| 486 | }); |
| 487 | }); |