-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathchapters.js
More file actions
264 lines (230 loc) · 8.18 KB
/
chapters.js
File metadata and controls
264 lines (230 loc) · 8.18 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import $ from 'jquery';
function addChaptersFunctions(AblePlayer) {
AblePlayer.prototype.populateChaptersDiv = function() {
var headingLevel, headingType, headingId, $chaptersHeading;
if ( ! this.chaptersDivLocation ) {
return;
}
if ($('#' + this.chaptersDivLocation)) {
this.$chaptersDiv = $('#' + this.chaptersDivLocation);
this.$chaptersDiv.addClass('able-chapters-div');
// empty content from previous build before starting fresh
this.$chaptersDiv.empty();
// add optional header
if (this.chaptersTitle) {
headingLevel = this.getNextHeadingLevel(this.$chaptersDiv);
headingType = 'h' + headingLevel.toString();
headingId = this.mediaId + '-chapters-heading';
$chaptersHeading = $('<' + headingType + '>', {
'class': 'able-chapters-heading',
'id': headingId
}).text(this.chaptersTitle);
this.$chaptersDiv.append($chaptersHeading);
}
this.$chaptersNav = $('<nav>');
if (this.chaptersTitle) {
this.$chaptersNav.attr( 'aria-labelledby', headingId );
} else {
this.$chaptersNav.attr( 'aria-label', this.translate( 'chapters', 'Chapters' ) );
}
this.$chaptersDiv.append(this.$chaptersNav);
// populate this.$chaptersNav with a list of chapters
this.updateChaptersList();
}
};
AblePlayer.prototype.updateChaptersList = function() {
var thisObj, cues, $chaptersList, c, thisChapter,
$chapterItem, $chapterButton, hasDefault,
getClickFunction, $clickedItem;
thisObj = this;
// TODO: Update this so it can change the chapters popup menu
// currently it only works if chapters are in an external container
if (!this.$chaptersNav) {
return false;
}
if (typeof this.useChapterTimes === 'undefined') {
this.useChapterTimes = (this.seekbarScope === 'chapter' && this.selectedChapters.cues.length) ? true : false;
}
if (this.useChapterTimes) {
cues = this.selectedChapters.cues;
} else if (this.chapters.length >= 1) {
cues = this.chapters[0].cues;
} else {
cues = [];
}
if (cues.length > 0) {
$chaptersList = $('<ul>');
for (c = 0; c < cues.length; c++) {
thisChapter = c;
$chapterItem = $('<li></li>');
$chapterButton = $('<button>',{
'type': 'button',
'val': thisChapter
}).text(this.flattenCueForCaption(cues[thisChapter]));
// add event listeners
getClickFunction = function (time) {
return function () {
thisObj.seekTrigger = 'chapter';
$clickedItem = $(this).closest('li');
$chaptersList = $(this).closest('ul').find('li');
$chaptersList.removeClass('able-current-chapter')
.children('button').removeAttr('aria-current');
$clickedItem.addClass('able-current-chapter')
.children('button').attr('aria-current','true');
// Need to updateChapter before seeking to it
// Otherwise seekBar is redrawn with wrong chapterDuration and/or chapterTime
thisObj.updateChapter(time);
thisObj.seekTo(time);
}
};
$chapterButton.on('click',getClickFunction(cues[thisChapter].start)); // works with Enter too
$chapterButton.on('focus',function() {
$(this).closest('ul').find('li').removeClass('able-focus');
$(this).closest('li').addClass('able-focus');
});
$chapterItem.on('hover',function() {
$(this).closest('ul').find('li').removeClass('able-focus');
$(this).addClass('able-focus');
});
$chapterItem.on('mouseleave',function() {
$(this).removeClass('able-focus');
});
$chapterButton.on('blur',function() {
$(this).closest('li').removeClass('able-focus');
});
// put it all together
$chapterItem.append($chapterButton);
$chaptersList.append($chapterItem);
if (this.defaultChapter === cues[thisChapter].id) {
$chapterButton.attr('aria-current','true').parent('li').addClass('able-current-chapter');
this.currentChapter = cues[thisChapter];
hasDefault = true;
}
}
if (!hasDefault) {
// select the first chapter
this.currentChapter = cues[0];
$chaptersList.find('button').first().attr('aria-current','true')
.parent('li').addClass('able-current-chapter');
}
this.$chaptersNav.html($chaptersList);
}
return false;
};
AblePlayer.prototype.seekToChapter = function(chapterId) {
// step through chapters looking for matching ID
var i=0;
while (i < this.selectedChapters.cues.length) {
if (this.selectedChapters.cues[i].id == chapterId) {
// found the target chapter! Seek to it
this.seekTo(this.selectedChapters.cues[i].start);
this.updateChapter(this.selectedChapters.cues[i].start);
break;
}
i++;
}
};
AblePlayer.prototype.updateChapter = function (now) {
// as time-synced chapters change during playback, track changes in current chapter
if (typeof this.selectedChapters === 'undefined') {
return;
}
var chapters, i, thisChapterIndex;
chapters = this.selectedChapters.cues;
for (i = 0; i < chapters.length; i++) {
if ((chapters[i].start <= now) && (chapters[i].end > now)) {
thisChapterIndex = i;
break;
}
}
if (typeof thisChapterIndex !== 'undefined') {
if (this.currentChapter !== chapters[thisChapterIndex]) {
// this is a new chapter
this.currentChapter = chapters[thisChapterIndex];
if (this.useChapterTimes) {
this.chapterDuration = this.getChapterDuration();
this.seekIntervalCalculated = false; // will be recalculated in setSeekInterval()
}
if (typeof this.$chaptersDiv !== 'undefined') {
// chapters are listed in an external container
this.$chaptersDiv.find('ul').find('li')
.removeClass('able-current-chapter')
.children('button').removeAttr('aria-current');
this.$chaptersDiv.find('ul').find('li').eq(thisChapterIndex)
.addClass('able-current-chapter')
.children('button').attr('aria-current','true');
}
}
}
};
AblePlayer.prototype.getChapterDuration = function () {
// called if this.seekbarScope === 'chapter'
// get duration of the current chapter
var lastChapterIndex, chapterEnd;
if (typeof this.currentChapter === 'undefined') {
return 0;
}
if (typeof this.duration === 'undefined') {
return 0;
}
lastChapterIndex = this.selectedChapters.cues.length-1;
if (this.selectedChapters.cues[lastChapterIndex] == this.currentChapter) {
// this is the last chapter
if (this.currentChapter.end !== this.duration) {
// chapter ends before or after video ends, adjust chapter end to match video end
chapterEnd = this.duration;
this.currentChapter.end = this.duration;
} else {
chapterEnd = this.currentChapter.end;
}
} else { // this is not the last chapter
chapterEnd = this.currentChapter.end;
}
return chapterEnd - this.currentChapter.start;
};
AblePlayer.prototype.getChapterElapsed = function () {
// called if this.seekbarScope === 'chapter'
// get current elapsed time, relative to the current chapter duration
if (typeof this.currentChapter === 'undefined') {
return 0;
}
if (this.elapsed > this.currentChapter.start) {
return this.elapsed - this.currentChapter.start;
} else {
return 0;
}
};
AblePlayer.prototype.convertChapterTimeToVideoTime = function (chapterTime) {
// chapterTime is the time within the current chapter
// return the same time, relative to the entire video
if (typeof this.currentChapter !== 'undefined') {
var newTime = this.currentChapter.start + chapterTime;
if (newTime > this.currentChapter.end) {
return this.currentChapter.end;
} else {
return newTime;
}
} else {
return chapterTime;
}
};
AblePlayer.prototype.getChapterClickFunction = function (time) {
// Returns the function used when a chapter is clicked in the chapters menu.
var thisObj = this;
return function () {
thisObj.seekTrigger = 'chapter';
thisObj.seekTo(time);
// stopgap to prevent spacebar in Firefox from reopening popup
// immediately after closing it (used in handleChapters())
thisObj.hidingPopup = true;
thisObj.chaptersPopup.hide();
// Ensure stopgap gets cancelled if handleChapters() isn't called
// e.g., if user triggered button with Enter or mouse click, not spacebar
setTimeout(function() {
thisObj.hidingPopup = false;
}, 100);
thisObj.$chaptersButton.trigger('focus');
}
};
}
export default addChaptersFunctions;