4 * `Polymer.IronMenuBehavior` implements accessible menu behavior.
6 * @demo demo/index.html
7 * @polymerBehavior Polymer.IronMenuBehavior
9 Polymer.IronMenuBehaviorImpl = {
14 * Returns the currently focused item.
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`
39 '_updateMultiselectable(multi)'
44 'keydown': '_onKeydown'
51 'enter': '_onEnterKey',
52 'shift+tab:keydown': '_onShiftTabDown'
55 _updateMultiselectable: function(multi) {
57 this.setAttribute('aria-multiselectable', 'true');
59 this.removeAttribute('aria-multiselectable');
63 _onShiftTabDown: function() {
66 Polymer.IronMenuBehaviorImpl._shiftTabPressed = true;
68 oldTabIndex = this.getAttribute('tabindex');
70 this.setAttribute('tabindex', '-1');
72 this.async(function() {
73 this.setAttribute('tabindex', oldTabIndex);
74 Polymer.IronMenuBehaviorImpl._shiftTabPressed = false;
75 // Note: polymer/polymer#1305
79 _applySelection: function(item, isSelected) {
81 item.setAttribute('aria-selected', 'true');
83 item.removeAttribute('aria-selected');
86 Polymer.IronSelectableBehavior._applySelection.apply(this, arguments);
89 _focusedItemChanged: function(focusedItem, old) {
90 old && old.setAttribute('tabindex', '-1');
92 focusedItem.setAttribute('tabindex', '0');
97 select: function(value) {
98 if (this._defaultFocusAsync) {
99 this.cancelAsync(this._defaultFocusAsync);
100 this._defaultFocusAsync = null;
102 var item = this._valueToItem(value);
103 if (item && item.hasAttribute('disabled')) return;
104 this._setFocusedItem(item);
105 Polymer.IronMultiSelectableBehaviorImpl.select.apply(this, arguments);
108 _onFocus: function(event) {
109 if (Polymer.IronMenuBehaviorImpl._shiftTabPressed) {
112 // do not focus the menu itself
114 // clear the cached focus item
115 this._setFocusedItem(null);
116 this._defaultFocusAsync = this.async(function() {
117 // focus the selected item when the menu receives focus, or the first item
118 // if no item is selected
119 var selectedItem = this.multi ? (this.selectedItems && this.selectedItems[0]) : this.selectedItem;
121 this._setFocusedItem(selectedItem);
123 this._setFocusedItem(this.items[0]);
125 // async 100ms to wait for `select` to get called from `_itemActivate`
129 _onUpKey: function() {
130 // up and down arrows moves the focus
131 this._focusPrevious();
134 _onDownKey: function() {
138 _onEscKey: function() {
139 // esc blurs the control
140 this.focusedItem.blur();
143 _onEnterKey: function(event) {
144 // enter activates the item unless it is disabled
145 this._activateFocused(event.detail.keyboardEvent);
148 _onKeydown: function(event) {
149 if (this.keyboardEventMatchesKeys(event, 'up down esc enter')) {
153 // all other keys focus the menu item starting with that character
154 this._focusWithKeyboardEvent(event);
157 _focusWithKeyboardEvent: function(event) {
158 for (var i = 0, item; item = this.items[i]; i++) {
159 var attr = this.attrForItemTitle || 'textContent';
160 var title = item[attr] || item.getAttribute(attr);
161 if (title && title.trim().charAt(0).toLowerCase() === String.fromCharCode(event.keyCode).toLowerCase()) {
162 this._setFocusedItem(item);
168 _activateFocused: function(event) {
169 if (!this.focusedItem.hasAttribute('disabled')) {
170 this._activateHandler(event);
174 _focusPrevious: function() {
175 var length = this.items.length;
176 var index = (Number(this.indexOf(this.focusedItem)) - 1 + length) % length;
177 this._setFocusedItem(this.items[index]);
180 _focusNext: function() {
181 var index = (Number(this.indexOf(this.focusedItem)) + 1) % this.items.length;
182 this._setFocusedItem(this.items[index]);
187 Polymer.IronMenuBehaviorImpl._shiftTabPressed = false;
189 /** @polymerBehavior Polymer.IronMenuBehavior */
190 Polymer.IronMenuBehavior = [
191 Polymer.IronMultiSelectableBehavior,
192 Polymer.IronA11yKeysBehavior,
193 Polymer.IronMenuBehaviorImpl