| OLD | NEW |
| (Empty) | |
| 1 |
| 2 |
| 3 Polymer.IronMenuBehavior = Polymer.IronMultiSelectableBehavior.concat({ |
| 4 |
| 5 properties: { |
| 6 |
| 7 /** |
| 8 * Returns the currently focused item. |
| 9 * |
| 10 * @attribute focusedItem |
| 11 * @type Object |
| 12 */ |
| 13 focusedItem: { |
| 14 observer: '_focusedItemChanged', |
| 15 readOnly: true, |
| 16 type: Object |
| 17 }, |
| 18 |
| 19 /** |
| 20 * The attribute to use on menu items to look up the item title. Typing th
e first |
| 21 * letter of an item when the menu is open focuses that item. If unset, `t
extContent` |
| 22 * will be used. |
| 23 * |
| 24 * @attribute attrForItemTitle |
| 25 * @type String |
| 26 */ |
| 27 attrForItemTitle: { |
| 28 type: String |
| 29 } |
| 30 |
| 31 }, |
| 32 |
| 33 observers: [ |
| 34 '_selectedItemsChanged(selectedItems)', |
| 35 '_selectedItemChanged(selectedItem)' |
| 36 ], |
| 37 |
| 38 hostAttributes: { |
| 39 'role': 'menu', |
| 40 'tabindex': '0' |
| 41 }, |
| 42 |
| 43 listeners: { |
| 44 'focus': '_onFocus', |
| 45 'keydown': '_onKeydown' |
| 46 }, |
| 47 |
| 48 _focusedItemChanged: function(focusedItem, old) { |
| 49 old && old.setAttribute('tabindex', '-1'); |
| 50 if (focusedItem) { |
| 51 focusedItem.setAttribute('tabindex', '0'); |
| 52 focusedItem.focus(); |
| 53 } |
| 54 }, |
| 55 |
| 56 _selectedItemsChanged: function(selectedItems) { |
| 57 this._setFocusedItem(selectedItems[0]); |
| 58 }, |
| 59 |
| 60 _selectedItemChanged: function(selectedItem) { |
| 61 this._setFocusedItem(selectedItem); |
| 62 }, |
| 63 |
| 64 _onFocus: function(event) { |
| 65 // clear the cached focus item |
| 66 this._setFocusedItem(null); |
| 67 // focus the selected item when the menu receives focus, or the first item |
| 68 // if no item is selected |
| 69 var selectedItem = this.multi ? (this.selectedItems && this.selectedItems[
0]) : this.selectedItem; |
| 70 if (selectedItem) { |
| 71 this._setFocusedItem(selectedItem); |
| 72 } else { |
| 73 this._setFocusedItem(this.items[0]); |
| 74 } |
| 75 }, |
| 76 |
| 77 _onKeydown: function(event) { |
| 78 // FIXME want to define these somewhere, core-a11y-keys? |
| 79 var DOWN = 40; |
| 80 var UP = 38; |
| 81 var ESC = 27; |
| 82 var ENTER = 13; |
| 83 if (event.keyCode === DOWN) { |
| 84 // up and down arrows moves the focus |
| 85 this._focusNext(); |
| 86 } else if (event.keyCode === UP) { |
| 87 this._focusPrevious(); |
| 88 } else if (event.keyCode === ESC) { |
| 89 // esc blurs the control |
| 90 this.focusedItem.blur(); |
| 91 } else if (event.keyCode === ENTER) { |
| 92 // enter activates the item unless it is disabled |
| 93 if (!this.focusedItem.hasAttribute('disabled')) { |
| 94 this._activateHandler(event); |
| 95 } |
| 96 } else { |
| 97 // all other keys focus the menu item starting with that character |
| 98 for (var i = 0, item; item = this.items[i]; i++) { |
| 99 var attr = this.attrForItemTitle || 'textContent'; |
| 100 var title = item[attr] || item.getAttribute(attr); |
| 101 if (title && title.trim().charAt(0).toLowerCase() === String.fromCharC
ode(event.keyCode).toLowerCase()) { |
| 102 this._setFocusedItem(item); |
| 103 break; |
| 104 } |
| 105 } |
| 106 } |
| 107 }, |
| 108 |
| 109 _focusPrevious: function() { |
| 110 var length = this.items.length; |
| 111 var index = (Number(this.indexOf(this.focusedItem)) - 1 + length) % length
; |
| 112 this._setFocusedItem(this.items[index]); |
| 113 }, |
| 114 |
| 115 _focusNext: function() { |
| 116 var index = (Number(this.indexOf(this.focusedItem)) + 1) % this.items.leng
th; |
| 117 this._setFocusedItem(this.items[index]); |
| 118 } |
| 119 |
| 120 }); |
| 121 |
| OLD | NEW |