-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
Copy pathchapters-button.js
224 lines (187 loc) · 5.6 KB
/
chapters-button.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
/**
* @file chapters-button.js
*/
import TextTrackButton from './text-track-button.js';
import Component from '../../component.js';
import ChaptersTrackMenuItem from './chapters-track-menu-item.js';
import {toTitleCase} from '../../utils/str.js';
/** @import Player from '../../player' */
/** @import Menu from '../../menu/menu' */
/** @import TextTrack from '../../tracks/text-track' */
/** @import TextTrackMenuItem from '../text-track-controls/text-track-menu-item' */
/**
* The button component for toggling and selecting chapters
* Chapters act much differently than other text tracks
* Cues are navigation vs. other tracks of alternative languages
*
* @extends TextTrackButton
*/
class ChaptersButton extends TextTrackButton {
/**
* Creates an instance of this class.
*
* @param {Player} player
* The `Player` that this class should be attached to.
*
* @param {Object} [options]
* The key/value store of player options.
*
* @param {Function} [ready]
* The function to call when this function is ready.
*/
constructor(player, options, ready) {
super(player, options, ready);
this.setIcon('chapters');
this.selectCurrentItem_ = () => {
this.items.forEach(item => {
item.selected(this.track_.activeCues[0] === item.cue);
});
};
}
/**
* Builds the default DOM `className`.
*
* @return {string}
* The DOM `className` for this object.
*/
buildCSSClass() {
return `vjs-chapters-button ${super.buildCSSClass()}`;
}
buildWrapperCSSClass() {
return `vjs-chapters-button ${super.buildWrapperCSSClass()}`;
}
/**
* Update the menu based on the current state of its items.
*
* @param {Event} [event]
* An event that triggered this function to run.
*
* @listens TextTrackList#addtrack
* @listens TextTrackList#removetrack
* @listens TextTrackList#change
*/
update(event) {
if (event && event.track && event.track.kind !== 'chapters') {
return;
}
const track = this.findChaptersTrack();
if (track !== this.track_) {
this.setTrack(track);
super.update();
} else if (!this.items || (track && track.cues && track.cues.length !== this.items.length)) {
// Update the menu initially or if the number of cues has changed since set
super.update();
}
}
/**
* Set the currently selected track for the chapters button.
*
* @param {TextTrack} track
* The new track to select. Nothing will change if this is the currently selected
* track.
*/
setTrack(track) {
if (this.track_ === track) {
return;
}
if (!this.updateHandler_) {
this.updateHandler_ = this.update.bind(this);
}
// here this.track_ refers to the old track instance
if (this.track_) {
const remoteTextTrackEl = this.player_.remoteTextTrackEls().getTrackElementByTrack_(this.track_);
if (remoteTextTrackEl) {
remoteTextTrackEl.removeEventListener('load', this.updateHandler_);
}
this.track_.removeEventListener('cuechange', this.selectCurrentItem_);
this.track_ = null;
}
this.track_ = track;
// here this.track_ refers to the new track instance
if (this.track_) {
this.track_.mode = 'hidden';
const remoteTextTrackEl = this.player_.remoteTextTrackEls().getTrackElementByTrack_(this.track_);
if (remoteTextTrackEl) {
remoteTextTrackEl.addEventListener('load', this.updateHandler_);
}
this.track_.addEventListener('cuechange', this.selectCurrentItem_);
}
}
/**
* Find the track object that is currently in use by this ChaptersButton
*
* @return {TextTrack|undefined}
* The current track or undefined if none was found.
*/
findChaptersTrack() {
const tracks = this.player_.textTracks() || [];
for (let i = tracks.length - 1; i >= 0; i--) {
// We will always choose the last track as our chaptersTrack
const track = tracks[i];
if (track.kind === this.kind_) {
return track;
}
}
}
/**
* Get the caption for the ChaptersButton based on the track label. This will also
* use the current tracks localized kind as a fallback if a label does not exist.
*
* @return {string}
* The tracks current label or the localized track kind.
*/
getMenuCaption() {
if (this.track_ && this.track_.label) {
return this.track_.label;
}
return this.localize(toTitleCase(this.kind_));
}
/**
* Create menu from chapter track
*
* @return {Menu}
* New menu for the chapter buttons
*/
createMenu() {
this.options_.title = this.getMenuCaption();
return super.createMenu();
}
/**
* Create a menu item for each text track
*
* @return {TextTrackMenuItem[]}
* Array of menu items
*/
createItems() {
const items = [];
if (!this.track_) {
return items;
}
const cues = this.track_.cues;
if (!cues) {
return items;
}
for (let i = 0, l = cues.length; i < l; i++) {
const cue = cues[i];
const mi = new ChaptersTrackMenuItem(this.player_, { track: this.track_, cue });
items.push(mi);
}
return items;
}
}
/**
* `kind` of TextTrack to look for to associate it with this menu.
*
* @type {string}
* @private
*/
ChaptersButton.prototype.kind_ = 'chapters';
/**
* The text that should display over the `ChaptersButton`s controls. Added for localization.
*
* @type {string}
* @protected
*/
ChaptersButton.prototype.controlText_ = 'Chapters';
Component.registerComponent('ChaptersButton', ChaptersButton);
export default ChaptersButton;