| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /**
- * @license
- * Copyright 2018 Google Inc.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- import { __assign, __extends } from "tslib";
- import { MDCFoundation } from '@material/base/foundation';
- import { cssClasses as listCssClasses } from '@material/list/constants';
- import { cssClasses, DefaultFocusState, numbers, strings } from './constants';
- /** MDC Menu Foundation */
- var MDCMenuFoundation = /** @class */ (function (_super) {
- __extends(MDCMenuFoundation, _super);
- function MDCMenuFoundation(adapter) {
- var _this = _super.call(this, __assign(__assign({}, MDCMenuFoundation.defaultAdapter), adapter)) || this;
- _this.defaultFocusState = DefaultFocusState.LIST_ROOT;
- _this.selectedIndex = -1;
- return _this;
- }
- Object.defineProperty(MDCMenuFoundation, "cssClasses", {
- get: function () {
- return cssClasses;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(MDCMenuFoundation, "strings", {
- get: function () {
- return strings;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(MDCMenuFoundation, "numbers", {
- get: function () {
- return numbers;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(MDCMenuFoundation, "defaultAdapter", {
- /**
- * @see {@link MDCMenuAdapter} for typing information on parameters and return types.
- */
- get: function () {
- // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
- return {
- addClassToElementAtIndex: function () { return undefined; },
- removeClassFromElementAtIndex: function () { return undefined; },
- addAttributeToElementAtIndex: function () { return undefined; },
- removeAttributeFromElementAtIndex: function () { return undefined; },
- getAttributeFromElementAtIndex: function () { return null; },
- elementContainsClass: function () { return false; },
- closeSurface: function () { return undefined; },
- getElementIndex: function () { return -1; },
- notifySelected: function () { return undefined; },
- getMenuItemCount: function () { return 0; },
- focusItemAtIndex: function () { return undefined; },
- focusListRoot: function () { return undefined; },
- getSelectedSiblingOfItemAtIndex: function () { return -1; },
- isSelectableItemAtIndex: function () { return false; },
- };
- // tslint:enable:object-literal-sort-keys
- },
- enumerable: false,
- configurable: true
- });
- MDCMenuFoundation.prototype.destroy = function () {
- this.adapter.closeSurface();
- };
- MDCMenuFoundation.prototype.handleKeydown = function (evt) {
- var key = evt.key, keyCode = evt.keyCode;
- var isTab = key === 'Tab' || keyCode === 9;
- if (isTab) {
- this.adapter.closeSurface(/** skipRestoreFocus */ true);
- }
- };
- MDCMenuFoundation.prototype.handleItemAction = function (listItem) {
- var index = this.adapter.getElementIndex(listItem);
- if (index < 0) {
- return;
- }
- this.adapter.notifySelected({ index: index });
- var skipRestoreFocus = this.adapter.getAttributeFromElementAtIndex(index, strings.SKIP_RESTORE_FOCUS) === 'true';
- this.adapter.closeSurface(skipRestoreFocus);
- if (this.adapter.isSelectableItemAtIndex(index)) {
- this.setSelectedIndex(index);
- }
- };
- MDCMenuFoundation.prototype.handleMenuSurfaceOpened = function () {
- switch (this.defaultFocusState) {
- case DefaultFocusState.FIRST_ITEM:
- this.adapter.focusItemAtIndex(0);
- break;
- case DefaultFocusState.LAST_ITEM:
- this.adapter.focusItemAtIndex(this.adapter.getMenuItemCount() - 1);
- break;
- case DefaultFocusState.NONE:
- // Do nothing.
- break;
- default:
- this.adapter.focusListRoot();
- break;
- }
- };
- /**
- * Sets default focus state where the menu should focus every time when menu
- * is opened. Focuses the list root (`DefaultFocusState.LIST_ROOT`) element by
- * default.
- */
- MDCMenuFoundation.prototype.setDefaultFocusState = function (focusState) {
- this.defaultFocusState = focusState;
- };
- /** @return Index of the currently selected list item within the menu. */
- MDCMenuFoundation.prototype.getSelectedIndex = function () {
- return this.selectedIndex;
- };
- /**
- * Selects the list item at `index` within the menu.
- * @param index Index of list item within the menu.
- */
- MDCMenuFoundation.prototype.setSelectedIndex = function (index) {
- this.validatedIndex(index);
- if (!this.adapter.isSelectableItemAtIndex(index)) {
- throw new Error('MDCMenuFoundation: No selection group at specified index.');
- }
- var prevSelectedIndex = this.adapter.getSelectedSiblingOfItemAtIndex(index);
- if (prevSelectedIndex >= 0) {
- this.adapter.removeAttributeFromElementAtIndex(prevSelectedIndex, strings.ARIA_CHECKED_ATTR);
- this.adapter.removeClassFromElementAtIndex(prevSelectedIndex, cssClasses.MENU_SELECTED_LIST_ITEM);
- }
- this.adapter.addClassToElementAtIndex(index, cssClasses.MENU_SELECTED_LIST_ITEM);
- this.adapter.addAttributeToElementAtIndex(index, strings.ARIA_CHECKED_ATTR, 'true');
- this.selectedIndex = index;
- };
- /**
- * Sets the enabled state to isEnabled for the menu item at the given index.
- * @param index Index of the menu item
- * @param isEnabled The desired enabled state of the menu item.
- */
- MDCMenuFoundation.prototype.setEnabled = function (index, isEnabled) {
- this.validatedIndex(index);
- if (isEnabled) {
- this.adapter.removeClassFromElementAtIndex(index, listCssClasses.LIST_ITEM_DISABLED_CLASS);
- this.adapter.addAttributeToElementAtIndex(index, strings.ARIA_DISABLED_ATTR, 'false');
- }
- else {
- this.adapter.addClassToElementAtIndex(index, listCssClasses.LIST_ITEM_DISABLED_CLASS);
- this.adapter.addAttributeToElementAtIndex(index, strings.ARIA_DISABLED_ATTR, 'true');
- }
- };
- MDCMenuFoundation.prototype.validatedIndex = function (index) {
- var menuSize = this.adapter.getMenuItemCount();
- var isIndexInRange = index >= 0 && index < menuSize;
- if (!isIndexInRange) {
- throw new Error('MDCMenuFoundation: No list item at specified index.');
- }
- };
- return MDCMenuFoundation;
- }(MDCFoundation));
- export { MDCMenuFoundation };
- // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
- export default MDCMenuFoundation;
- //# sourceMappingURL=foundation.js.map
|