foundation.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @license
  3. * Copyright 2018 Google Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. import { __assign, __extends } from "tslib";
  24. import { MDCFoundation } from '@material/base/foundation';
  25. import { cssClasses as listCssClasses } from '@material/list/constants';
  26. import { cssClasses, DefaultFocusState, numbers, strings } from './constants';
  27. /** MDC Menu Foundation */
  28. var MDCMenuFoundation = /** @class */ (function (_super) {
  29. __extends(MDCMenuFoundation, _super);
  30. function MDCMenuFoundation(adapter) {
  31. var _this = _super.call(this, __assign(__assign({}, MDCMenuFoundation.defaultAdapter), adapter)) || this;
  32. _this.defaultFocusState = DefaultFocusState.LIST_ROOT;
  33. _this.selectedIndex = -1;
  34. return _this;
  35. }
  36. Object.defineProperty(MDCMenuFoundation, "cssClasses", {
  37. get: function () {
  38. return cssClasses;
  39. },
  40. enumerable: false,
  41. configurable: true
  42. });
  43. Object.defineProperty(MDCMenuFoundation, "strings", {
  44. get: function () {
  45. return strings;
  46. },
  47. enumerable: false,
  48. configurable: true
  49. });
  50. Object.defineProperty(MDCMenuFoundation, "numbers", {
  51. get: function () {
  52. return numbers;
  53. },
  54. enumerable: false,
  55. configurable: true
  56. });
  57. Object.defineProperty(MDCMenuFoundation, "defaultAdapter", {
  58. /**
  59. * @see {@link MDCMenuAdapter} for typing information on parameters and return types.
  60. */
  61. get: function () {
  62. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  63. return {
  64. addClassToElementAtIndex: function () { return undefined; },
  65. removeClassFromElementAtIndex: function () { return undefined; },
  66. addAttributeToElementAtIndex: function () { return undefined; },
  67. removeAttributeFromElementAtIndex: function () { return undefined; },
  68. getAttributeFromElementAtIndex: function () { return null; },
  69. elementContainsClass: function () { return false; },
  70. closeSurface: function () { return undefined; },
  71. getElementIndex: function () { return -1; },
  72. notifySelected: function () { return undefined; },
  73. getMenuItemCount: function () { return 0; },
  74. focusItemAtIndex: function () { return undefined; },
  75. focusListRoot: function () { return undefined; },
  76. getSelectedSiblingOfItemAtIndex: function () { return -1; },
  77. isSelectableItemAtIndex: function () { return false; },
  78. };
  79. // tslint:enable:object-literal-sort-keys
  80. },
  81. enumerable: false,
  82. configurable: true
  83. });
  84. MDCMenuFoundation.prototype.destroy = function () {
  85. this.adapter.closeSurface();
  86. };
  87. MDCMenuFoundation.prototype.handleKeydown = function (evt) {
  88. var key = evt.key, keyCode = evt.keyCode;
  89. var isTab = key === 'Tab' || keyCode === 9;
  90. if (isTab) {
  91. this.adapter.closeSurface(/** skipRestoreFocus */ true);
  92. }
  93. };
  94. MDCMenuFoundation.prototype.handleItemAction = function (listItem) {
  95. var index = this.adapter.getElementIndex(listItem);
  96. if (index < 0) {
  97. return;
  98. }
  99. this.adapter.notifySelected({ index: index });
  100. var skipRestoreFocus = this.adapter.getAttributeFromElementAtIndex(index, strings.SKIP_RESTORE_FOCUS) === 'true';
  101. this.adapter.closeSurface(skipRestoreFocus);
  102. if (this.adapter.isSelectableItemAtIndex(index)) {
  103. this.setSelectedIndex(index);
  104. }
  105. };
  106. MDCMenuFoundation.prototype.handleMenuSurfaceOpened = function () {
  107. switch (this.defaultFocusState) {
  108. case DefaultFocusState.FIRST_ITEM:
  109. this.adapter.focusItemAtIndex(0);
  110. break;
  111. case DefaultFocusState.LAST_ITEM:
  112. this.adapter.focusItemAtIndex(this.adapter.getMenuItemCount() - 1);
  113. break;
  114. case DefaultFocusState.NONE:
  115. // Do nothing.
  116. break;
  117. default:
  118. this.adapter.focusListRoot();
  119. break;
  120. }
  121. };
  122. /**
  123. * Sets default focus state where the menu should focus every time when menu
  124. * is opened. Focuses the list root (`DefaultFocusState.LIST_ROOT`) element by
  125. * default.
  126. */
  127. MDCMenuFoundation.prototype.setDefaultFocusState = function (focusState) {
  128. this.defaultFocusState = focusState;
  129. };
  130. /** @return Index of the currently selected list item within the menu. */
  131. MDCMenuFoundation.prototype.getSelectedIndex = function () {
  132. return this.selectedIndex;
  133. };
  134. /**
  135. * Selects the list item at `index` within the menu.
  136. * @param index Index of list item within the menu.
  137. */
  138. MDCMenuFoundation.prototype.setSelectedIndex = function (index) {
  139. this.validatedIndex(index);
  140. if (!this.adapter.isSelectableItemAtIndex(index)) {
  141. throw new Error('MDCMenuFoundation: No selection group at specified index.');
  142. }
  143. var prevSelectedIndex = this.adapter.getSelectedSiblingOfItemAtIndex(index);
  144. if (prevSelectedIndex >= 0) {
  145. this.adapter.removeAttributeFromElementAtIndex(prevSelectedIndex, strings.ARIA_CHECKED_ATTR);
  146. this.adapter.removeClassFromElementAtIndex(prevSelectedIndex, cssClasses.MENU_SELECTED_LIST_ITEM);
  147. }
  148. this.adapter.addClassToElementAtIndex(index, cssClasses.MENU_SELECTED_LIST_ITEM);
  149. this.adapter.addAttributeToElementAtIndex(index, strings.ARIA_CHECKED_ATTR, 'true');
  150. this.selectedIndex = index;
  151. };
  152. /**
  153. * Sets the enabled state to isEnabled for the menu item at the given index.
  154. * @param index Index of the menu item
  155. * @param isEnabled The desired enabled state of the menu item.
  156. */
  157. MDCMenuFoundation.prototype.setEnabled = function (index, isEnabled) {
  158. this.validatedIndex(index);
  159. if (isEnabled) {
  160. this.adapter.removeClassFromElementAtIndex(index, listCssClasses.LIST_ITEM_DISABLED_CLASS);
  161. this.adapter.addAttributeToElementAtIndex(index, strings.ARIA_DISABLED_ATTR, 'false');
  162. }
  163. else {
  164. this.adapter.addClassToElementAtIndex(index, listCssClasses.LIST_ITEM_DISABLED_CLASS);
  165. this.adapter.addAttributeToElementAtIndex(index, strings.ARIA_DISABLED_ATTR, 'true');
  166. }
  167. };
  168. MDCMenuFoundation.prototype.validatedIndex = function (index) {
  169. var menuSize = this.adapter.getMenuItemCount();
  170. var isIndexInRange = index >= 0 && index < menuSize;
  171. if (!isIndexInRange) {
  172. throw new Error('MDCMenuFoundation: No list item at specified index.');
  173. }
  174. };
  175. return MDCMenuFoundation;
  176. }(MDCFoundation));
  177. export { MDCMenuFoundation };
  178. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  179. export default MDCMenuFoundation;
  180. //# sourceMappingURL=foundation.js.map