forked from ableplayer/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapters.js
More file actions
243 lines (213 loc) · 7.92 KB
/
chapters.js
File metadata and controls
243 lines (213 loc) · 7.92 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
(function ($) {
AblePlayer.prototype.populateChaptersDiv = function() {
var headingLevel, headingType, headingId, $chaptersHeading,
$chaptersList;
if ($('#' + this.chaptersDivLocation)) {
this.$chaptersDiv = $('#' + this.chaptersDivLocation);
this.$chaptersDiv.addClass('able-chapters-div');
// 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.tt.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, buttonId, hasDefault,
getClickFunction, $clickedItem, $chaptersList, thisChapterIndex;
thisObj = this;
if (!this.$chaptersNav) {
return false;
}
if (typeof this.useChapterTimes === 'undefined') {
if (this.seekbarScope === 'chapter' && this.selectedChapters.cues.length) {
this.useChapterTimes = true;
}
else {
this.useChapterTimes = 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');
thisChapterIndex = $chaptersList.index($clickedItem);
$chaptersList.removeClass('able-current-chapter').attr('aria-selected','');
$clickedItem.addClass('able-current-chapter').attr('aria-selected','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-selected','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-selected','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, chapterLabel;
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').attr('aria-selected','');
this.$chaptersDiv.find('ul').find('li').eq(thisChapterIndex)
.addClass('able-current-chapter').attr('aria-selected','true');
}
}
}
};
AblePlayer.prototype.getChapterDuration = function () {
// called if this.seekbarScope === 'chapter'
// get duration of the current chapter
var videoDuration, lastChapterIndex, chapterEnd;
if (typeof this.currentChapter === 'undefined') {
return 0;
}
videoDuration = this.getDuration();
lastChapterIndex = this.selectedChapters.cues.length-1;
if (this.selectedChapters.cues[lastChapterIndex] == this.currentChapter) {
// this is the last chapter
if (this.currentChapter.end !== videoDuration) {
// chapter ends before or after video ends, adjust chapter end to match video end
chapterEnd = videoDuration;
this.currentChapter.end = videoDuration;
}
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;
}
var videoDuration = this.getDuration();
var videoElapsed = this.getElapsed();
if (videoElapsed > this.currentChapter.start) {
return videoElapsed - 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;
}
};
})(jQuery);