1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
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 { MDCMenuSurfaceFoundation } from '@material/menu-surface/foundation';
|
27 | import { cssClasses, DefaultFocusState, numbers, strings } from './constants';
|
28 | var MDCMenuFoundation = (function (_super) {
|
29 | __extends(MDCMenuFoundation, _super);
|
30 | function MDCMenuFoundation(adapter) {
|
31 | var _this = _super.call(this, __assign(__assign({}, MDCMenuFoundation.defaultAdapter), adapter)) || this;
|
32 | _this.closeAnimationEndTimerId = 0;
|
33 | _this.defaultFocusState = DefaultFocusState.LIST_ROOT;
|
34 | _this.selectedIndex = -1;
|
35 | return _this;
|
36 | }
|
37 | Object.defineProperty(MDCMenuFoundation, "cssClasses", {
|
38 | get: function () {
|
39 | return cssClasses;
|
40 | },
|
41 | enumerable: false,
|
42 | configurable: true
|
43 | });
|
44 | Object.defineProperty(MDCMenuFoundation, "strings", {
|
45 | get: function () {
|
46 | return strings;
|
47 | },
|
48 | enumerable: false,
|
49 | configurable: true
|
50 | });
|
51 | Object.defineProperty(MDCMenuFoundation, "numbers", {
|
52 | get: function () {
|
53 | return numbers;
|
54 | },
|
55 | enumerable: false,
|
56 | configurable: true
|
57 | });
|
58 | Object.defineProperty(MDCMenuFoundation, "defaultAdapter", {
|
59 | |
60 |
|
61 |
|
62 | get: function () {
|
63 |
|
64 | return {
|
65 | addClassToElementAtIndex: function () { return undefined; },
|
66 | removeClassFromElementAtIndex: function () { return undefined; },
|
67 | addAttributeToElementAtIndex: function () { return undefined; },
|
68 | removeAttributeFromElementAtIndex: function () { return undefined; },
|
69 | getAttributeFromElementAtIndex: function () { return null; },
|
70 | elementContainsClass: function () { return false; },
|
71 | closeSurface: function () { return undefined; },
|
72 | getElementIndex: function () { return -1; },
|
73 | notifySelected: function () { return undefined; },
|
74 | getMenuItemCount: function () { return 0; },
|
75 | focusItemAtIndex: function () { return undefined; },
|
76 | focusListRoot: function () { return undefined; },
|
77 | getSelectedSiblingOfItemAtIndex: function () { return -1; },
|
78 | isSelectableItemAtIndex: function () { return false; },
|
79 | };
|
80 |
|
81 | },
|
82 | enumerable: false,
|
83 | configurable: true
|
84 | });
|
85 | MDCMenuFoundation.prototype.destroy = function () {
|
86 | if (this.closeAnimationEndTimerId) {
|
87 | clearTimeout(this.closeAnimationEndTimerId);
|
88 | }
|
89 | this.adapter.closeSurface();
|
90 | };
|
91 | MDCMenuFoundation.prototype.handleKeydown = function (evt) {
|
92 | var key = evt.key, keyCode = evt.keyCode;
|
93 | var isTab = key === 'Tab' || keyCode === 9;
|
94 | if (isTab) {
|
95 | this.adapter.closeSurface( true);
|
96 | }
|
97 | };
|
98 | MDCMenuFoundation.prototype.handleItemAction = function (listItem) {
|
99 | var _this = this;
|
100 | var index = this.adapter.getElementIndex(listItem);
|
101 | if (index < 0) {
|
102 | return;
|
103 | }
|
104 | this.adapter.notifySelected({ index: index });
|
105 | var skipRestoreFocus = this.adapter.getAttributeFromElementAtIndex(index, strings.SKIP_RESTORE_FOCUS) === 'true';
|
106 | this.adapter.closeSurface(skipRestoreFocus);
|
107 |
|
108 | this.closeAnimationEndTimerId = setTimeout(function () {
|
109 |
|
110 | var recomputedIndex = _this.adapter.getElementIndex(listItem);
|
111 | if (recomputedIndex >= 0 &&
|
112 | _this.adapter.isSelectableItemAtIndex(recomputedIndex)) {
|
113 | _this.setSelectedIndex(recomputedIndex);
|
114 | }
|
115 | }, MDCMenuSurfaceFoundation.numbers.TRANSITION_CLOSE_DURATION);
|
116 | };
|
117 | MDCMenuFoundation.prototype.handleMenuSurfaceOpened = function () {
|
118 | switch (this.defaultFocusState) {
|
119 | case DefaultFocusState.FIRST_ITEM:
|
120 | this.adapter.focusItemAtIndex(0);
|
121 | break;
|
122 | case DefaultFocusState.LAST_ITEM:
|
123 | this.adapter.focusItemAtIndex(this.adapter.getMenuItemCount() - 1);
|
124 | break;
|
125 | case DefaultFocusState.NONE:
|
126 |
|
127 | break;
|
128 | default:
|
129 | this.adapter.focusListRoot();
|
130 | break;
|
131 | }
|
132 | };
|
133 | |
134 |
|
135 |
|
136 |
|
137 |
|
138 | MDCMenuFoundation.prototype.setDefaultFocusState = function (focusState) {
|
139 | this.defaultFocusState = focusState;
|
140 | };
|
141 |
|
142 | MDCMenuFoundation.prototype.getSelectedIndex = function () {
|
143 | return this.selectedIndex;
|
144 | };
|
145 | |
146 |
|
147 |
|
148 |
|
149 | MDCMenuFoundation.prototype.setSelectedIndex = function (index) {
|
150 | this.validatedIndex(index);
|
151 | if (!this.adapter.isSelectableItemAtIndex(index)) {
|
152 | throw new Error('MDCMenuFoundation: No selection group at specified index.');
|
153 | }
|
154 | var prevSelectedIndex = this.adapter.getSelectedSiblingOfItemAtIndex(index);
|
155 | if (prevSelectedIndex >= 0) {
|
156 | this.adapter.removeAttributeFromElementAtIndex(prevSelectedIndex, strings.ARIA_CHECKED_ATTR);
|
157 | this.adapter.removeClassFromElementAtIndex(prevSelectedIndex, cssClasses.MENU_SELECTED_LIST_ITEM);
|
158 | }
|
159 | this.adapter.addClassToElementAtIndex(index, cssClasses.MENU_SELECTED_LIST_ITEM);
|
160 | this.adapter.addAttributeToElementAtIndex(index, strings.ARIA_CHECKED_ATTR, 'true');
|
161 | this.selectedIndex = index;
|
162 | };
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 | MDCMenuFoundation.prototype.setEnabled = function (index, isEnabled) {
|
169 | this.validatedIndex(index);
|
170 | if (isEnabled) {
|
171 | this.adapter.removeClassFromElementAtIndex(index, listCssClasses.LIST_ITEM_DISABLED_CLASS);
|
172 | this.adapter.addAttributeToElementAtIndex(index, strings.ARIA_DISABLED_ATTR, 'false');
|
173 | }
|
174 | else {
|
175 | this.adapter.addClassToElementAtIndex(index, listCssClasses.LIST_ITEM_DISABLED_CLASS);
|
176 | this.adapter.addAttributeToElementAtIndex(index, strings.ARIA_DISABLED_ATTR, 'true');
|
177 | }
|
178 | };
|
179 | MDCMenuFoundation.prototype.validatedIndex = function (index) {
|
180 | var menuSize = this.adapter.getMenuItemCount();
|
181 | var isIndexInRange = index >= 0 && index < menuSize;
|
182 | if (!isIndexInRange) {
|
183 | throw new Error('MDCMenuFoundation: No list item at specified index.');
|
184 | }
|
185 | };
|
186 | return MDCMenuFoundation;
|
187 | }(MDCFoundation));
|
188 | export { MDCMenuFoundation };
|
189 |
|
190 | export default MDCMenuFoundation;
|
191 |
|
\ | No newline at end of file |