OLD | NEW |
1 | 1 |
2 | 2 |
3 Polymer.IronMenuBehavior = Polymer.IronMultiSelectableBehavior.concat({ | 3 /** |
| 4 * `Polymer.IronMenuBehavior` implements accessible menu behavior. |
| 5 * |
| 6 * @demo demo/index.html |
| 7 * @polymerBehavior Polymer.IronMenuBehavior |
| 8 */ |
| 9 Polymer.IronMenuBehaviorImpl = { |
4 | 10 |
5 properties: { | 11 properties: { |
6 | 12 |
7 /** | 13 /** |
8 * Returns the currently focused item. | 14 * Returns the currently focused item. |
9 * | 15 * |
10 * @attribute focusedItem | 16 * @attribute focusedItem |
11 * @type Object | 17 * @type Object |
12 */ | 18 */ |
13 focusedItem: { | 19 focusedItem: { |
14 observer: '_focusedItemChanged', | 20 observer: '_focusedItemChanged', |
15 readOnly: true, | 21 readOnly: true, |
16 type: Object | 22 type: Object |
17 }, | 23 }, |
18 | 24 |
19 /** | 25 /** |
20 * The attribute to use on menu items to look up the item title. Typing th
e first | 26 * 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` | 27 * letter of an item when the menu is open focuses that item. If unset, `t
extContent` |
22 * will be used. | 28 * will be used. |
23 * | 29 * |
24 * @attribute attrForItemTitle | 30 * @attribute attrForItemTitle |
25 * @type String | 31 * @type String |
26 */ | 32 */ |
27 attrForItemTitle: { | 33 attrForItemTitle: { |
28 type: String | 34 type: String |
29 } | 35 } |
30 | |
31 }, | 36 }, |
32 | 37 |
33 observers: [ | |
34 '_selectedItemsChanged(selectedItems)', | |
35 '_selectedItemChanged(selectedItem)' | |
36 ], | |
37 | |
38 hostAttributes: { | 38 hostAttributes: { |
39 'role': 'menu', | 39 'role': 'menu', |
40 'tabindex': '0' | 40 'tabindex': '0' |
41 }, | 41 }, |
42 | 42 |
| 43 observers: [ |
| 44 '_updateMultiselectable(multi)' |
| 45 ], |
| 46 |
43 listeners: { | 47 listeners: { |
44 'focus': '_onFocus', | 48 'focus': '_onFocus', |
45 'keydown': '_onKeydown' | 49 'keydown': '_onKeydown' |
46 }, | 50 }, |
47 | 51 |
| 52 keyBindings: { |
| 53 'up': '_onUpKey', |
| 54 'down': '_onDownKey', |
| 55 'esc': '_onEscKey', |
| 56 'enter': '_onEnterKey', |
| 57 'shift+tab:keydown': '_onShiftTabDown' |
| 58 }, |
| 59 |
| 60 _updateMultiselectable: function(multi) { |
| 61 if (multi) { |
| 62 this.setAttribute('aria-multiselectable', 'true'); |
| 63 } else { |
| 64 this.removeAttribute('aria-multiselectable'); |
| 65 } |
| 66 }, |
| 67 |
| 68 _onShiftTabDown: function() { |
| 69 var oldTabIndex; |
| 70 |
| 71 Polymer.IronMenuBehaviorImpl._shiftTabPressed = true; |
| 72 |
| 73 oldTabIndex = this.getAttribute('tabindex'); |
| 74 |
| 75 this.setAttribute('tabindex', '-1'); |
| 76 |
| 77 this.async(function() { |
| 78 this.setAttribute('tabindex', oldTabIndex); |
| 79 Polymer.IronMenuBehaviorImpl._shiftTabPressed = false; |
| 80 // Note: polymer/polymer#1305 |
| 81 }, 1); |
| 82 }, |
| 83 |
| 84 _applySelection: function(item, isSelected) { |
| 85 if (isSelected) { |
| 86 item.setAttribute('aria-selected', 'true'); |
| 87 } else { |
| 88 item.removeAttribute('aria-selected'); |
| 89 } |
| 90 |
| 91 Polymer.IronSelectableBehavior._applySelection.apply(this, arguments); |
| 92 }, |
| 93 |
48 _focusedItemChanged: function(focusedItem, old) { | 94 _focusedItemChanged: function(focusedItem, old) { |
49 old && old.setAttribute('tabindex', '-1'); | 95 old && old.setAttribute('tabindex', '-1'); |
50 if (focusedItem) { | 96 if (focusedItem) { |
51 focusedItem.setAttribute('tabindex', '0'); | 97 focusedItem.setAttribute('tabindex', '0'); |
52 focusedItem.focus(); | 98 focusedItem.focus(); |
53 } | 99 } |
54 }, | 100 }, |
55 | 101 |
56 _selectedItemsChanged: function(selectedItems) { | 102 select: function(value) { |
57 this._setFocusedItem(selectedItems[0]); | 103 if (this._defaultFocusAsync) { |
58 }, | 104 this.cancelAsync(this._defaultFocusAsync); |
59 | 105 this._defaultFocusAsync = null; |
60 _selectedItemChanged: function(selectedItem) { | 106 } |
61 this._setFocusedItem(selectedItem); | 107 var item = this._valueToItem(value); |
| 108 this._setFocusedItem(item); |
| 109 Polymer.IronMultiSelectableBehaviorImpl.select.apply(this, arguments); |
62 }, | 110 }, |
63 | 111 |
64 _onFocus: function(event) { | 112 _onFocus: function(event) { |
| 113 if (Polymer.IronMenuBehaviorImpl._shiftTabPressed) { |
| 114 return; |
| 115 } |
| 116 // do not focus the menu itself |
| 117 this.blur(); |
65 // clear the cached focus item | 118 // clear the cached focus item |
66 this._setFocusedItem(null); | 119 this._setFocusedItem(null); |
67 // focus the selected item when the menu receives focus, or the first item | 120 this._defaultFocusAsync = this.async(function() { |
68 // if no item is selected | 121 // focus the selected item when the menu receives focus, or the first it
em |
69 var selectedItem = this.multi ? (this.selectedItems && this.selectedItems[
0]) : this.selectedItem; | 122 // if no item is selected |
70 if (selectedItem) { | 123 var selectedItem = this.multi ? (this.selectedItems && this.selectedItem
s[0]) : this.selectedItem; |
71 this._setFocusedItem(selectedItem); | 124 if (selectedItem) { |
72 } else { | 125 this._setFocusedItem(selectedItem); |
73 this._setFocusedItem(this.items[0]); | 126 } else { |
| 127 this._setFocusedItem(this.items[0]); |
| 128 } |
| 129 // async 100ms to wait for `select` to get called from `_itemActivate` |
| 130 }, 100); |
| 131 }, |
| 132 |
| 133 _onUpKey: function() { |
| 134 // up and down arrows moves the focus |
| 135 this._focusPrevious(); |
| 136 }, |
| 137 |
| 138 _onDownKey: function() { |
| 139 this._focusNext(); |
| 140 }, |
| 141 |
| 142 _onEscKey: function() { |
| 143 // esc blurs the control |
| 144 this.focusedItem.blur(); |
| 145 }, |
| 146 |
| 147 _onEnterKey: function(event) { |
| 148 // enter activates the item unless it is disabled |
| 149 this._activateFocused(event.detail.keyboardEvent); |
| 150 }, |
| 151 |
| 152 _onKeydown: function(event) { |
| 153 if (this.keyboardEventMatchesKeys(event, 'up down esc enter')) { |
| 154 return; |
| 155 } |
| 156 |
| 157 // all other keys focus the menu item starting with that character |
| 158 this._focusWithKeyboardEvent(event); |
| 159 }, |
| 160 |
| 161 _focusWithKeyboardEvent: function(event) { |
| 162 for (var i = 0, item; item = this.items[i]; i++) { |
| 163 var attr = this.attrForItemTitle || 'textContent'; |
| 164 var title = item[attr] || item.getAttribute(attr); |
| 165 if (title && title.trim().charAt(0).toLowerCase() === String.fromCharCod
e(event.keyCode).toLowerCase()) { |
| 166 this._setFocusedItem(item); |
| 167 break; |
| 168 } |
74 } | 169 } |
75 }, | 170 }, |
76 | 171 |
77 _onKeydown: function(event) { | 172 _activateFocused: function(event) { |
78 // FIXME want to define these somewhere, core-a11y-keys? | 173 if (!this.focusedItem.hasAttribute('disabled')) { |
79 var DOWN = 40; | 174 this._activateHandler(event); |
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 } | 175 } |
107 }, | 176 }, |
108 | 177 |
109 _focusPrevious: function() { | 178 _focusPrevious: function() { |
110 var length = this.items.length; | 179 var length = this.items.length; |
111 var index = (Number(this.indexOf(this.focusedItem)) - 1 + length) % length
; | 180 var index = (Number(this.indexOf(this.focusedItem)) - 1 + length) % length
; |
112 this._setFocusedItem(this.items[index]); | 181 this._setFocusedItem(this.items[index]); |
113 }, | 182 }, |
114 | 183 |
115 _focusNext: function() { | 184 _focusNext: function() { |
116 var index = (Number(this.indexOf(this.focusedItem)) + 1) % this.items.leng
th; | 185 var index = (Number(this.indexOf(this.focusedItem)) + 1) % this.items.leng
th; |
117 this._setFocusedItem(this.items[index]); | 186 this._setFocusedItem(this.items[index]); |
118 } | 187 } |
119 | 188 |
120 }); | 189 }; |
121 | 190 |
| 191 Polymer.IronMenuBehaviorImpl._shiftTabPressed = false; |
| 192 |
| 193 /** @polymerBehavior Polymer.IronMenuBehavior */ |
| 194 Polymer.IronMenuBehavior = [ |
| 195 Polymer.IronMultiSelectableBehavior, |
| 196 Polymer.IronA11yKeysBehavior, |
| 197 Polymer.IronMenuBehaviorImpl |
| 198 ]; |
| 199 |
OLD | NEW |