-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathoverflow_menu.js
222 lines (178 loc) · 5.99 KB
/
overflow_menu.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.ui.OverflowMenu');
goog.require('goog.asserts');
goog.require('shaka.ads.Utils');
goog.require('shaka.log');
goog.require('shaka.ui.Controls');
goog.require('shaka.ui.Element');
goog.require('shaka.ui.Enums');
goog.require('shaka.ui.Locales');
goog.require('shaka.ui.Localization');
goog.require('shaka.ui.Utils');
goog.require('shaka.util.Dom');
goog.require('shaka.util.Iterables');
/**
* @extends {shaka.ui.Element}
* @final
* @export
*/
shaka.ui.OverflowMenu = class extends shaka.ui.Element {
/**
* @param {!HTMLElement} parent
* @param {!shaka.ui.Controls} controls
*/
constructor(parent, controls) {
super(parent, controls);
/** @private {!shaka.extern.UIConfiguration} */
this.config_ = this.controls.getConfig();
/** @private {HTMLElement} */
this.controlsContainer_ = this.controls.getControlsContainer();
/** @private {!Array<shaka.extern.IUIElement>} */
this.children_ = [];
this.addOverflowMenuButton_();
this.addOverflowMenu_();
this.createChildren_();
this.eventManager.listen(
this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
this.updateAriaLabel_();
});
this.eventManager.listen(
this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
this.updateAriaLabel_();
});
this.eventManager.listen(
this.adManager, shaka.ads.Utils.AD_STARTED, () => {
if (this.ad && this.ad.isLinear()) {
shaka.ui.Utils.setDisplay(this.overflowMenuButton_, false);
}
});
this.eventManager.listen(
this.adManager, shaka.ads.Utils.AD_STOPPED, () => {
shaka.ui.Utils.setDisplay(this.overflowMenuButton_, true);
});
this.eventManager.listen(
this.controls, 'submenuopen', () => {
// Hide the main overflow menu if one of the sub menus has
// been opened.
shaka.ui.Utils.setDisplay(this.overflowMenu_, false);
});
this.eventManager.listen(
this.overflowMenu_, 'touchstart', (event) => {
this.controls.setLastTouchEventTime(Date.now());
event.stopPropagation();
});
this.eventManager.listen(this.overflowMenuButton_, 'click', () => {
this.onOverflowMenuButtonClick_();
});
this.updateAriaLabel_();
if (this.ad && this.ad.isLinear()) {
// There was already an ad.
shaka.ui.Utils.setDisplay(this.overflowMenuButton_, false);
}
}
/** @override */
release() {
this.controlsContainer_ = null;
for (const element of this.children_) {
element.release();
}
this.children_ = [];
super.release();
}
/**
* @param {string} name
* @param {!shaka.extern.IUIElement.Factory} factory
* @export
*/
static registerElement(name, factory) {
shaka.ui.OverflowMenu.elementNamesToFactories_.set(name, factory);
}
/**
* @private
*/
addOverflowMenu_() {
/** @private {!HTMLElement} */
this.overflowMenu_ = shaka.util.Dom.createHTMLElement('div');
this.overflowMenu_.classList.add('shaka-overflow-menu');
this.overflowMenu_.classList.add('shaka-no-propagation');
this.overflowMenu_.classList.add('shaka-show-controls-on-mouse-over');
this.overflowMenu_.classList.add('shaka-hidden');
this.controlsContainer_.appendChild(this.overflowMenu_);
}
/**
* @private
*/
addOverflowMenuButton_() {
/** @private {!HTMLButtonElement} */
this.overflowMenuButton_ = shaka.util.Dom.createButton();
this.overflowMenuButton_.classList.add('shaka-overflow-menu-button');
this.overflowMenuButton_.classList.add('shaka-no-propagation');
this.overflowMenuButton_.classList.add('material-icons-round');
this.overflowMenuButton_.classList.add('shaka-tooltip');
this.overflowMenuButton_.textContent =
shaka.ui.Enums.MaterialDesignIcons.OPEN_OVERFLOW;
this.parent.appendChild(this.overflowMenuButton_);
}
/**
* @private
*/
createChildren_() {
for (const name of this.config_.overflowMenuButtons) {
if (shaka.ui.OverflowMenu.elementNamesToFactories_.get(name)) {
const factory =
shaka.ui.OverflowMenu.elementNamesToFactories_.get(name);
goog.asserts.assert(this.controls, 'Controls should not be null!');
this.children_.push(factory.create(this.overflowMenu_, this.controls));
} else {
shaka.log.alwaysWarn('Unrecognized overflow menu element requested:',
name);
}
}
}
/** @private */
onOverflowMenuButtonClick_() {
if (this.controls.anySettingsMenusAreOpen()) {
this.controls.hideSettingsMenus();
} else {
shaka.ui.Utils.setDisplay(this.overflowMenu_, true);
this.controls.computeOpacity();
// If overflow menu has currently visible buttons, focus on the
// first one, when the menu opens.
const isDisplayed =
(element) => element.classList.contains('shaka-hidden') == false;
const Iterables = shaka.util.Iterables;
if (Iterables.some(this.overflowMenu_.childNodes, isDisplayed)) {
// Focus on the first visible child of the overflow menu
const visibleElements =
Iterables.filter(this.overflowMenu_.childNodes, isDisplayed);
/** @type {!HTMLElement} */ (visibleElements[0]).focus();
}
}
}
/**
* @private
*/
updateAriaLabel_() {
const LocIds = shaka.ui.Locales.Ids;
this.overflowMenuButton_.ariaLabel =
this.localization.resolve(LocIds.MORE_SETTINGS);
}
};
/**
* @implements {shaka.extern.IUIElement.Factory}
* @final
*/
shaka.ui.OverflowMenu.Factory = class {
/** @override */
create(rootElement, controls) {
return new shaka.ui.OverflowMenu(rootElement, controls);
}
};
shaka.ui.Controls.registerElement(
'overflow_menu', new shaka.ui.OverflowMenu.Factory());
/** @private {!Map<string, !shaka.extern.IUIElement.Factory>} */
shaka.ui.OverflowMenu.elementNamesToFactories_ = new Map();