forked from material-components/material-components-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoundation.js
179 lines (157 loc) · 5.73 KB
/
foundation.js
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
58
59
60
61
62
63
64
65
66
67
68
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @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 MDCFoundation from '@material/base/foundation';
import {MDCMenuAdapter} from './adapter';
import {cssClasses, strings} from './constants';
import {MDCMenuSurfaceFoundation} from '@material/menu-surface/foundation';
import MDCListFoundation from '@material/list/foundation';
/**
* @extends {MDCFoundation<!MDCMenuAdapter>}
*/
class MDCMenuFoundation extends MDCFoundation {
/** @return enum{cssClasses} */
static get cssClasses() {
return cssClasses;
}
/** @return enum{strings} */
static get strings() {
return strings;
}
/**
* {@see MDCMenuAdapter} for typing information on parameters and return
* types.
* @return {!MDCMenuAdapter}
*/
static get defaultAdapter() {
return /** @type {!MDCMenuAdapter} */ ({
addClassToElementAtIndex: () => {},
removeClassFromElementAtIndex: () => {},
addAttributeToElementAtIndex: () => {},
removeAttributeFromElementAtIndex: () => {},
elementContainsClass: () => {},
closeSurface: () => {},
getElementIndex: () => {},
getParentElement: () => {},
getSelectedElementIndex: () => {},
notifySelected: () => {},
});
}
/** @param {!MDCMenuAdapter} adapter */
constructor(adapter) {
super(Object.assign(MDCMenuFoundation.defaultAdapter, adapter));
/** @type {number} */
this.closeAnimationEndTimerId_ = 0;
}
destroy() {
if (this.closeAnimationEndTimerId_) {
clearTimeout(this.closeAnimationEndTimerId_);
}
this.adapter_.closeSurface();
}
/**
* Handler function for the keydown events.
* @param {!Event} evt
*/
handleKeydown(evt) {
const {key, keyCode} = evt;
const isTab = key === 'Tab' || keyCode === 9;
if (isTab) {
this.adapter_.closeSurface();
}
}
/**
* @param {!HTMLElement} listItem
*/
handleItemAction(listItem) {
const index = this.adapter_.getElementIndex(listItem);
if (index < 0) {
return;
}
this.adapter_.notifySelected({index});
this.adapter_.closeSurface();
// Wait for the menu to close before adding/removing classes that affect styles.
this.closeAnimationEndTimerId_ = setTimeout(() => {
const selectionGroup = this.getSelectionGroup_(listItem);
if (selectionGroup !== null) {
this.handleSelectionGroup_(/** @type {!HTMLElement} */ (selectionGroup), index);
}
}, MDCMenuSurfaceFoundation.numbers.TRANSITION_CLOSE_DURATION);
}
/**
* Handles toggling the selected classes in a selection group when a
* selection is made.
* @param {!HTMLElement} selectionGroup
* @param {number} index The selected index value
* @private
*/
handleSelectionGroup_(selectionGroup, index) {
// De-select the previous selection in this group.
const selectedIndex = this.adapter_.getSelectedElementIndex(selectionGroup);
if (selectedIndex >= 0) {
this.adapter_.removeAttributeFromElementAtIndex(selectedIndex, strings.ARIA_SELECTED_ATTR);
this.adapter_.removeClassFromElementAtIndex(selectedIndex, cssClasses.MENU_SELECTED_LIST_ITEM);
}
// Select the new list item in this group.
this.adapter_.addClassToElementAtIndex(index, cssClasses.MENU_SELECTED_LIST_ITEM);
this.adapter_.addAttributeToElementAtIndex(index, strings.ARIA_SELECTED_ATTR, 'true');
}
/**
* Returns the parent selection group of an element if one exists.
* @param listItem
* @return {?HTMLElement} parent selection group element or null.
* @private
*/
getSelectionGroup_(listItem) {
let parent = this.adapter_.getParentElement(listItem);
let isGroup = this.adapter_.elementContainsClass(parent, cssClasses.MENU_SELECTION_GROUP);
// Iterate through ancestors until we find the group or get to the list.
while (!isGroup && !this.adapter_.elementContainsClass(parent, MDCListFoundation.cssClasses.ROOT)) {
parent = this.adapter_.getParentElement(parent);
isGroup = this.adapter_.elementContainsClass(parent, cssClasses.MENU_SELECTION_GROUP);
}
if (isGroup) {
return parent;
} else {
return null;
}
}
/**
* Find the first ancestor with the mdc-list-item class.
* @param {?HTMLElement} target
* @return {?HTMLElement}
* @private
*/
getListItem_(target) {
let isListItem = this.adapter_.elementContainsClass(target, MDCListFoundation.cssClasses.LIST_ITEM_CLASS);
while (!isListItem) {
target = this.adapter_.getParentElement(target);
if (target) {
isListItem = this.adapter_.elementContainsClass(target, MDCListFoundation.cssClasses.LIST_ITEM_CLASS);
} else { // target has no parent element.
return null;
}
}
return target;
}
}
export {MDCMenuFoundation};