-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathtext_selection.js
230 lines (183 loc) · 6.65 KB
/
text_selection.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
222
223
224
225
226
227
228
229
230
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.ui.TextSelection');
goog.require('shaka.ui.Controls');
goog.require('shaka.ui.Enums');
goog.require('shaka.ui.LanguageUtils');
goog.require('shaka.ui.Locales');
goog.require('shaka.ui.Localization');
goog.require('shaka.ui.OverflowMenu');
goog.require('shaka.ui.SettingsMenu');
goog.require('shaka.ui.Utils');
goog.require('shaka.util.Dom');
goog.require('shaka.util.FakeEvent');
goog.requireType('shaka.ui.Controls');
/**
* @extends {shaka.ui.SettingsMenu}
* @final
* @export
*/
shaka.ui.TextSelection = class extends shaka.ui.SettingsMenu {
/**
* @param {!HTMLElement} parent
* @param {!shaka.ui.Controls} controls
*/
constructor(parent, controls) {
super(parent,
controls, shaka.ui.Enums.MaterialDesignIcons.CLOSED_CAPTIONS);
this.button.classList.add('shaka-caption-button');
this.button.classList.add('shaka-tooltip-status');
this.menu.classList.add('shaka-text-languages');
if (this.player && this.player.isTextTrackVisible()) {
this.button.ariaPressed = 'true';
} else {
this.button.ariaPressed = 'false';
}
this.addOffOption_();
this.eventManager.listen(
this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
this.updateLocalizedStrings_();
// If captions/subtitles are off, this string needs localization.
// TODO: is there a more efficient way of updating just the strings
// we need instead of running the whole language update?
this.updateTextLanguages_();
});
this.eventManager.listen(
this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
this.updateLocalizedStrings_();
// If captions/subtitles are off, this string needs localization.
// TODO: is there a more efficient way of updating just the strings
// we need instead of running the whole language update?
this.updateTextLanguages_();
});
this.eventManager.listen(this.player, 'loading', () => {
this.onTracksChanged_();
});
this.eventManager.listen(this.player, 'texttrackvisibility', () => {
this.onCaptionStateChange_();
this.updateTextLanguages_();
});
this.eventManager.listen(this.player, 'textchanged', () => {
this.updateTextLanguages_();
});
this.eventManager.listen(this.player, 'trackschanged', () => {
this.onTracksChanged_();
});
// Initialize caption state with a fake event.
this.onCaptionStateChange_();
// Set up all the strings in the user's preferred language.
this.updateLocalizedStrings_();
this.updateTextLanguages_();
this.onTracksChanged_();
}
/**
* @private
*/
addOffOption_() {
const off = shaka.util.Dom.createButton();
off.ariaSelected = 'true';
this.menu.appendChild(off);
off.appendChild(shaka.ui.Utils.checkmarkIcon());
/** @private {!HTMLElement} */
this.captionsOffSpan_ = shaka.util.Dom.createHTMLElement('span');
this.captionsOffSpan_.classList.add('shaka-auto-span');
off.appendChild(this.captionsOffSpan_);
}
/** @private */
onCaptionStateChange_() {
if (this.player.isTextTrackVisible()) {
this.icon.textContent =
shaka.ui.Enums.MaterialDesignIcons.CLOSED_CAPTIONS;
this.button.ariaPressed = 'true';
} else {
this.icon.textContent =
shaka.ui.Enums.MaterialDesignIcons.CLOSED_CAPTIONS_OFF;
this.button.ariaPressed = 'false';
}
this.controls.dispatchEvent(
new shaka.util.FakeEvent('captionselectionupdated'));
}
/** @private */
updateTextLanguages_() {
const tracks = this.player.getTextTracks();
shaka.ui.LanguageUtils.updateTextTracks(tracks, this.menu,
(track) => this.onTextTrackSelected_(track),
// Don't mark current text language as chosen unless captions are
// enabled
this.player.isTextTrackVisible(),
this.currentSelection,
this.localization,
this.controls.getConfig().textTrackLabelFormat);
// Add the Off button
const offButton = shaka.util.Dom.createButton();
offButton.classList.add('shaka-turn-captions-off-button');
this.eventManager.listen(offButton, 'click', () => {
this.player.setTextTrackVisibility(false);
this.updateTextLanguages_();
});
offButton.appendChild(this.captionsOffSpan_);
this.menu.appendChild(offButton);
if (!this.player.isTextTrackVisible()) {
offButton.ariaSelected = 'true';
offButton.appendChild(shaka.ui.Utils.checkmarkIcon());
this.captionsOffSpan_.classList.add('shaka-chosen-item');
this.currentSelection.textContent =
this.localization.resolve(shaka.ui.Locales.Ids.OFF);
}
this.button.setAttribute('shaka-status', this.currentSelection.textContent);
shaka.ui.Utils.focusOnTheChosenItem(this.menu);
this.controls.dispatchEvent(
new shaka.util.FakeEvent('captionselectionupdated'));
}
/**
* @param {!shaka.extern.Track} track
* @return {!Promise}
* @private
*/
async onTextTrackSelected_(track) {
// setTextTrackVisibility should be called after selectTextTrack.
// selectTextTrack sets a text stream, and setTextTrackVisibility(true)
// will set a text stream if it isn't already set. Consequently, reversing
// the order of these calls makes two languages display simultaneously
// if captions are turned off -> on in a different language.
this.player.selectTextTrack(track);
await this.player.setTextTrackVisibility(true);
}
/**
* @private
*/
updateLocalizedStrings_() {
const LocIds = shaka.ui.Locales.Ids;
this.button.ariaLabel = this.localization.resolve(LocIds.CAPTIONS);
this.backButton.ariaLabel = this.localization.resolve(LocIds.BACK);
this.nameSpan.textContent =
this.localization.resolve(LocIds.CAPTIONS);
this.backSpan.textContent =
this.localization.resolve(LocIds.CAPTIONS);
this.captionsOffSpan_.textContent =
this.localization.resolve(LocIds.OFF);
}
/** @private */
onTracksChanged_() {
const hasText = this.player.getTextTracks().length > 0;
shaka.ui.Utils.setDisplay(this.button, hasText);
this.updateTextLanguages_();
}
};
/**
* @implements {shaka.extern.IUIElement.Factory}
* @final
*/
shaka.ui.TextSelection.Factory = class {
/** @override */
create(rootElement, controls) {
return new shaka.ui.TextSelection(rootElement, controls);
}
};
shaka.ui.OverflowMenu.registerElement(
'captions', new shaka.ui.TextSelection.Factory());
shaka.ui.Controls.registerElement(
'captions', new shaka.ui.TextSelection.Factory());