2 * `Polymer.IronMenuBehavior` implements accessible menu behavior.
4 * @demo demo/index.html
5 * @polymerBehavior Polymer.IronMenuBehavior
7 Polymer.IronMenuBehaviorImpl = {
12 * Returns the currently focused item.
14 * @attribute focusedItem
18 observer: '_focusedItemChanged',
24 * The attribute to use on menu items to look up the item title. Typing the first
25 * letter of an item when the menu is open focuses that item. If unset, `textContent`
28 * @attribute attrForItemTitle
42 '_updateMultiselectable(multi)'
47 'keydown': '_onKeydown'
54 'enter': '_onEnterKey',
55 'shift+tab:keydown': '_onShiftTabDown'
58 _updateMultiselectable: function(multi) {
60 this.setAttribute('aria-multiselectable', 'true');
62 this.removeAttribute('aria-multiselectable');
66 _onShiftTabDown: function() {
69 Polymer.IronMenuBehaviorImpl._shiftTabPressed = true;
71 oldTabIndex = this.getAttribute('tabindex');
73 this.setAttribute('tabindex', '-1');
75 this.async(function() {
76 this.setAttribute('tabindex', oldTabIndex);
77 Polymer.IronMenuBehaviorImpl._shiftTabPressed = false;
78 // Note: polymer/polymer#1305
82 _applySelection: function(item, isSelected) {
84 item.setAttribute('aria-selected', 'true');
86 item.removeAttribute('aria-selected');
89 Polymer.IronSelectableBehavior._applySelection.apply(this, arguments);
92 _focusedItemChanged: function(focusedItem, old) {
93 old && old.setAttribute('tabindex', '-1');
95 focusedItem.setAttribute('tabindex', '0');
100 select: function(value) {
101 if (this._defaultFocusAsync) {
102 this.cancelAsync(this._defaultFocusAsync);
103 this._defaultFocusAsync = null;
105 var item = this._valueToItem(value);
106 this._setFocusedItem(item);
107 Polymer.IronMultiSelectableBehaviorImpl.select.apply(this, arguments);
110 _onFocus: function(event) {
111 if (Polymer.IronMenuBehaviorImpl._shiftTabPressed) {
114 // do not focus the menu itself
116 // clear the cached focus item
117 this._setFocusedItem(null);
118 this._defaultFocusAsync = this.async(function() {
119 // focus the selected item when the menu receives focus, or the first item
120 // if no item is selected
121 var selectedItem = this.multi ? (this.selectedItems && this.selectedItems[0]) : this.selectedItem;
123 this._setFocusedItem(selectedItem);
125 this._setFocusedItem(this.items[0]);
127 // async 100ms to wait for `select` to get called from `_itemActivate`
131 _onUpKey: function() {
132 // up and down arrows moves the focus
133 this._focusPrevious();
136 _onDownKey: function() {
140 _onEscKey: function() {
141 // esc blurs the control
142 this.focusedItem.blur();
145 _onEnterKey: function(event) {
146 // enter activates the item unless it is disabled
147 this._activateFocused(event.detail.keyboardEvent);
150 _onKeydown: function(event) {
151 if (this.keyboardEventMatchesKeys(event, 'up down esc enter')) {
155 // all other keys focus the menu item starting with that character
156 this._focusWithKeyboardEvent(event);
159 _focusWithKeyboardEvent: function(event) {
160 for (var i = 0, item; item = this.items[i]; i++) {
161 var attr = this.attrForItemTitle || 'textContent';
162 var title = item[attr] || item.getAttribute(attr);
163 if (title && title.trim().charAt(0).toLowerCase() === String.fromCharCode(event.keyCode).toLowerCase()) {
164 this._setFocusedItem(item);
170 _activateFocused: function(event) {
171 if (!this.focusedItem.hasAttribute('disabled')) {
172 this._activateHandler(event);
176 _focusPrevious: function() {
177 var length = this.items.length;
178 var index = (Number(this.indexOf(this.focusedItem)) - 1 + length) % length;
179 this._setFocusedItem(this.items[index]);
182 _focusNext: function() {
183 var index = (Number(this.indexOf(this.focusedItem)) + 1) % this.items.length;
184 this._setFocusedItem(this.items[index]);
189 Polymer.IronMenuBehaviorImpl._shiftTabPressed = false;
191 /** @polymerBehavior Polymer.IronMenuBehavior */
192 Polymer.IronMenuBehavior = [
193 Polymer.IronMultiSelectableBehavior,
194 Polymer.IronA11yKeysBehavior,
195 Polymer.IronMenuBehaviorImpl