PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Test04/www/bower_components/iron-menu-behavior/iron-menu-behavior.html

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