Blame view

bower_components/iron-menu-behavior/iron-menu-behavior.html 9.27 KB
73bcce88   luigser   COMPONENTS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  <!--
  @license
  Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
  This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
  The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
  The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
  Code distributed by Google as part of the polymer project is also
  subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
  -->
  
  <link rel="import" href="../polymer/polymer.html">
  <link rel="import" href="../iron-selector/iron-multi-selectable.html">
  <link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
  
  <script>
  
    /**
     * `Polymer.IronMenuBehavior` implements accessible menu behavior.
     *
     * @demo demo/index.html
     * @polymerBehavior Polymer.IronMenuBehavior
     */
    Polymer.IronMenuBehaviorImpl = {
  
      properties: {
  
        /**
         * Returns the currently focused item.
         * @type {?Object}
         */
        focusedItem: {
          observer: '_focusedItemChanged',
          readOnly: true,
          type: Object
        },
  
        /**
         * The attribute to use on menu items to look up the item title. Typing the first
         * letter of an item when the menu is open focuses that item. If unset, `textContent`
         * will be used.
         */
        attrForItemTitle: {
          type: String
        }
      },
  
      hostAttributes: {
        'role': 'menu',
        'tabindex': '0'
      },
  
      observers: [
        '_updateMultiselectable(multi)'
      ],
  
      listeners: {
        'focus': '_onFocus',
e619a3b0   Luigi Serra   Controllet cross ...
58
59
        'keydown': '_onKeydown',
        'iron-items-changed': '_onIronItemsChanged'
73bcce88   luigser   COMPONENTS
60
61
62
63
64
65
      },
  
      keyBindings: {
        'up': '_onUpKey',
        'down': '_onDownKey',
        'esc': '_onEscKey',
73bcce88   luigser   COMPONENTS
66
67
68
        'shift+tab:keydown': '_onShiftTabDown'
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
      attached: function() {
        this._resetTabindices();
      },
  
      /**
       * Selects the given value. If the `multi` property is true, then the selected state of the
       * `value` will be toggled; otherwise the `value` will be selected.
       *
       * @param {string} value the value to select.
       */
      select: function(value) {
        if (this._defaultFocusAsync) {
          this.cancelAsync(this._defaultFocusAsync);
          this._defaultFocusAsync = null;
        }
        var item = this._valueToItem(value);
        if (item && item.hasAttribute('disabled')) return;
        this._setFocusedItem(item);
        Polymer.IronMultiSelectableBehaviorImpl.select.apply(this, arguments);
      },
  
      /**
       * Resets all tabindex attributes to the appropriate value based on the
       * current selection state. The appropriate value is `0` (focusable) for
       * the default selected item, and `-1` (not keyboard focusable) for all
       * other items.
       */
      _resetTabindices: function() {
        var selectedItem = this.multi ? (this.selectedItems && this.selectedItems[0]) : this.selectedItem;
  
        this.items.forEach(function(item) {
          item.setAttribute('tabindex', item === selectedItem ? '0' : '-1');
        }, this);
      },
  
      /**
       * Sets appropriate ARIA based on whether or not the menu is meant to be
       * multi-selectable.
       *
       * @param {boolean} multi True if the menu should be multi-selectable.
       */
73bcce88   luigser   COMPONENTS
110
111
112
113
114
115
116
117
      _updateMultiselectable: function(multi) {
        if (multi) {
          this.setAttribute('aria-multiselectable', 'true');
        } else {
          this.removeAttribute('aria-multiselectable');
        }
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
118
119
120
121
122
123
124
125
126
127
      /**
       * Given a KeyboardEvent, this method will focus the appropriate item in the
       * menu (if there is a relevant item, and it is possible to focus it).
       *
       * @param {KeyboardEvent} event A KeyboardEvent.
       */
      _focusWithKeyboardEvent: function(event) {
        for (var i = 0, item; item = this.items[i]; i++) {
          var attr = this.attrForItemTitle || 'textContent';
          var title = item[attr] || item.getAttribute(attr);
a1a3bc73   Luigi Serra   graphs updates
128
  
e619a3b0   Luigi Serra   Controllet cross ...
129
130
131
132
133
134
          if (title && title.trim().charAt(0).toLowerCase() === String.fromCharCode(event.keyCode).toLowerCase()) {
            this._setFocusedItem(item);
            break;
          }
        }
      },
73bcce88   luigser   COMPONENTS
135
  
e619a3b0   Luigi Serra   Controllet cross ...
136
137
138
139
140
141
142
143
144
      /**
       * Focuses the previous item (relative to the currently focused item) in the
       * menu.
       */
      _focusPrevious: function() {
        var length = this.items.length;
        var index = (Number(this.indexOf(this.focusedItem)) - 1 + length) % length;
        this._setFocusedItem(this.items[index]);
      },
73bcce88   luigser   COMPONENTS
145
  
e619a3b0   Luigi Serra   Controllet cross ...
146
147
148
149
150
151
152
      /**
       * Focuses the next item (relative to the currently focused item) in the
       * menu.
       */
      _focusNext: function() {
        var index = (Number(this.indexOf(this.focusedItem)) + 1) % this.items.length;
        this._setFocusedItem(this.items[index]);
73bcce88   luigser   COMPONENTS
153
154
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
155
156
157
158
159
160
161
162
      /**
       * Mutates items in the menu based on provided selection details, so that
       * all items correctly reflect selection state.
       *
       * @param {Element} item An item in the menu.
       * @param {boolean} isSelected True if the item should be shown in a
       * selected state, otherwise false.
       */
73bcce88   luigser   COMPONENTS
163
164
165
166
167
168
      _applySelection: function(item, isSelected) {
        if (isSelected) {
          item.setAttribute('aria-selected', 'true');
        } else {
          item.removeAttribute('aria-selected');
        }
73bcce88   luigser   COMPONENTS
169
170
171
        Polymer.IronSelectableBehavior._applySelection.apply(this, arguments);
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
172
173
174
175
176
177
178
179
      /**
       * Discretely updates tabindex values among menu items as the focused item
       * changes.
       *
       * @param {Element} focusedItem The element that is currently focused.
       * @param {?Element} old The last element that was considered focused, if
       * applicable.
       */
73bcce88   luigser   COMPONENTS
180
181
182
183
184
185
186
187
      _focusedItemChanged: function(focusedItem, old) {
        old && old.setAttribute('tabindex', '-1');
        if (focusedItem) {
          focusedItem.setAttribute('tabindex', '0');
          focusedItem.focus();
        }
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
      /**
       * A handler that responds to mutation changes related to the list of items
       * in the menu.
       *
       * @param {CustomEvent} event An event containing mutation records as its
       * detail.
       */
      _onIronItemsChanged: function(event) {
        var mutations = event.detail;
        var mutation;
        var index;
  
        for (index = 0; index < mutations.length; ++index) {
          mutation = mutations[index];
  
          if (mutation.addedNodes.length) {
            this._resetTabindices();
            break;
          }
73bcce88   luigser   COMPONENTS
207
        }
73bcce88   luigser   COMPONENTS
208
209
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
210
211
212
213
214
215
      /**
       * Handler that is called when a shift+tab keypress is detected by the menu.
       *
       * @param {CustomEvent} event A key combination event.
       */
      _onShiftTabDown: function(event) {
a1a3bc73   Luigi Serra   graphs updates
216
        var oldTabIndex = this.getAttribute('tabindex');
e619a3b0   Luigi Serra   Controllet cross ...
217
218
219
  
        Polymer.IronMenuBehaviorImpl._shiftTabPressed = true;
  
a1a3bc73   Luigi Serra   graphs updates
220
        this._setFocusedItem(null);
e619a3b0   Luigi Serra   Controllet cross ...
221
222
223
224
225
226
  
        this.setAttribute('tabindex', '-1');
  
        this.async(function() {
          this.setAttribute('tabindex', oldTabIndex);
          Polymer.IronMenuBehaviorImpl._shiftTabPressed = false;
a1a3bc73   Luigi Serra   graphs updates
227
          // NOTE(cdata): polymer/polymer#1305
e619a3b0   Luigi Serra   Controllet cross ...
228
229
230
231
232
233
234
235
        }, 1);
      },
  
      /**
       * Handler that is called when the menu receives focus.
       *
       * @param {FocusEvent} event A focus event.
       */
73bcce88   luigser   COMPONENTS
236
237
      _onFocus: function(event) {
        if (Polymer.IronMenuBehaviorImpl._shiftTabPressed) {
a1a3bc73   Luigi Serra   graphs updates
238
          // do not focus the menu itself
73bcce88   luigser   COMPONENTS
239
240
          return;
        }
a1a3bc73   Luigi Serra   graphs updates
241
  
73bcce88   luigser   COMPONENTS
242
        this.blur();
a1a3bc73   Luigi Serra   graphs updates
243
  
73bcce88   luigser   COMPONENTS
244
        // clear the cached focus item
73bcce88   luigser   COMPONENTS
245
246
247
248
        this._defaultFocusAsync = this.async(function() {
          // focus the selected item when the menu receives focus, or the first item
          // if no item is selected
          var selectedItem = this.multi ? (this.selectedItems && this.selectedItems[0]) : this.selectedItem;
a1a3bc73   Luigi Serra   graphs updates
249
250
251
  
          this._setFocusedItem(null);
  
73bcce88   luigser   COMPONENTS
252
253
254
255
256
          if (selectedItem) {
            this._setFocusedItem(selectedItem);
          } else {
            this._setFocusedItem(this.items[0]);
          }
a1a3bc73   Luigi Serra   graphs updates
257
258
        // async 1ms to wait for `select` to get called from `_itemActivate`
        }, 1);
73bcce88   luigser   COMPONENTS
259
260
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
261
262
263
264
265
266
      /**
       * Handler that is called when the up key is pressed.
       *
       * @param {CustomEvent} event A key combination event.
       */
      _onUpKey: function(event) {
73bcce88   luigser   COMPONENTS
267
268
269
270
        // up and down arrows moves the focus
        this._focusPrevious();
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
271
272
273
274
275
276
      /**
       * Handler that is called when the down key is pressed.
       *
       * @param {CustomEvent} event A key combination event.
       */
      _onDownKey: function(event) {
73bcce88   luigser   COMPONENTS
277
278
279
        this._focusNext();
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
280
281
282
283
284
285
      /**
       * Handler that is called when the esc key is pressed.
       *
       * @param {CustomEvent} event A key combination event.
       */
      _onEscKey: function(event) {
73bcce88   luigser   COMPONENTS
286
287
288
289
        // esc blurs the control
        this.focusedItem.blur();
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
290
291
292
293
294
      /**
       * Handler that is called when a keydown event is detected.
       *
       * @param {KeyboardEvent} event A keyboard event.
       */
73bcce88   luigser   COMPONENTS
295
      _onKeydown: function(event) {
a1a3bc73   Luigi Serra   graphs updates
296
297
298
        if (!this.keyboardEventMatchesKeys(event, 'up down esc')) {
          // all other keys focus the menu item starting with that character
          this._focusWithKeyboardEvent(event);
73bcce88   luigser   COMPONENTS
299
        }
a1a3bc73   Luigi Serra   graphs updates
300
301
        event.stopPropagation();
      },
73bcce88   luigser   COMPONENTS
302
  
a1a3bc73   Luigi Serra   graphs updates
303
304
305
306
      // override _activateHandler
      _activateHandler: function(event) {
        Polymer.IronSelectableBehavior._activateHandler.call(this, event);
        event.stopPropagation();
73bcce88   luigser   COMPONENTS
307
      }
73bcce88   luigser   COMPONENTS
308
309
310
311
312
313
314
315
316
317
318
319
    };
  
    Polymer.IronMenuBehaviorImpl._shiftTabPressed = false;
  
    /** @polymerBehavior Polymer.IronMenuBehavior */
    Polymer.IronMenuBehavior = [
      Polymer.IronMultiSelectableBehavior,
      Polymer.IronA11yKeysBehavior,
      Polymer.IronMenuBehaviorImpl
    ];
  
  </script>