-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoLinks.js
325 lines (241 loc) · 10.6 KB
/
VideoLinks.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Make sure we're only running once.
// The "if" bracket closes at end of file.
if(typeof VideoLinksIsRunning == 'undefined'){
var VideoLinksIsRunning = true;
$(document).ready(function(){
// Declaring semi-global variables for later use.
var video = $('.video');
var vidWrappers = $('.video-wrapper');
var time;
var linkTimer = [];
var linkBeingShown = [];
// hideLinkAfter and linkOptions can be defined on the HTML page. Set defaults below.
if (typeof hideLinkAfter == 'undefined'){
var hideLinkAfter = 5; // Seconds
}
console.log('VideoLinks.js working');
// Mark each video and set of controls with a class that will let us
// handle each of them separately.
// Numbering from 1 to make things easier for course creators.
video.each(function(index){ $(this).addClass('for-video-' + (index + 1)); });
vidWrappers.each(function(index){ $(this).addClass('for-video-' + (index + 1)); });
video.each(function(vidnumber){
var thisVid = $(this);
setUpLists(vidnumber);
// Check to see whether the video is ready before continuing.
var waitForVid = setInterval(function(){
try {
var state = thisVid.data('video-player-state');
if(typeof state.videoPlayer !== 'undefined'){
if (state.videoPlayer.isCued()){
console.log('video data loaded');
if (typeof linkOptions == 'undefined'){
linkOptions = setLinkOptions();
}
else{
linkOptions = checkLinkOptions(linkOptions);
}
// We're positioning links based on the video.
vidWrappers.addClass('link-positioner');
setUpListeners(state);
mainLoop(state, vidnumber);
clearInterval(waitForVid);
}
}
}
catch(err){
console.log('waiting for video to be ready');
}
}, 100);
});
// Take the simple list in our HTML and make it FABULOUS
function setUpLists(vidnumber){
// Let's copy the links to the appropriate location so we can position them there.
var vidlinks = $('#vidlinks-static-' + (vidnumber+1))
.clone()
.prop('id', 'vidlinks-live-' + (vidnumber+1));
vidlinks.appendTo('.video-wrapper.for-video-' + (vidnumber+1));
linkTimer[vidnumber] = [];
// Each link needs a little bit added to it, to keep the author view simple.
// This preps the links that we're going to display on the video.
$('#vidlinks-live-' + (vidnumber+1)).children().each(function(index){
var thisLinkBox = $(this);
var thisLink = $(this).find('a');
// Give the link a class and a unique ID
thisLinkBox.addClass('vidlink_' + linkOptions.location);
thisLinkBox.attr('id','link-card-live-' + index);
// Give the images a class for styling purporses.
thisLink.find('img').addClass('vidlinkicon');
// Make all the links open in new pages.
thisLink.attr('target', '_blank');
// Style all the links
thisLink.addClass('link-text-live');
// Screen readers should skip these links. Rarely (but not never) an issue.
thisLinkBox.attr('aria-hidden','true');
// Build the link timer from the divs.
var tempTimer = {
'time': hmsToTime(thisLinkBox.attr('data-time')),
'shown': false
};
linkTimer[vidnumber].push(tempTimer);
});
// This preps the ones that are visible all the time.
$('#vidlinks-static-' + (vidnumber+1)).children().each(function(index){
var thisLinkBox = $(this);
var thisLink = $(this).find('a');
// Give the link a class and a unique ID
thisLinkBox.addClass('vidlink-static');
thisLinkBox.attr('id','link-card-static-' + index);
// Remove the images.
thisLink.find('img').remove();
});
// Finish making the unordered list.
$('#vidlinks-static-' + (vidnumber+1) + ' .vidlink-static').wrapAll('<ul></ul>');
linkTimer[vidnumber].sort(timeCompare); // Uses a custom function to sort by time.
console.log(linkTimer[vidnumber]);
}
// Set up listeners for the live links.
function setUpListeners(state){
// If they click on one of the live links, pause the video.
$('.link-text-live').on('click tap', function(){
state.videoPlayer.pause();
});
}
function setLinkOptions(){
return {
'effect': 'slide',
'hide': {'direction':'down'},
'show': {'direction':'down'},
'speed': 500,
'location': 'bl'
};
}
function checkLinkOptions(linkOptions){
if (typeof linkOptions.effect == 'undefined'){
linkOptions.effect = 'slide';
}
if (typeof linkOptions.location == 'undefined'){
linkOptions.location = 'bl';
}
if (linkOptions.location == 'bl' || linkOptions.location == 'br'){
linkOptions.show = {'direction':'down'};
linkOptions.hide = {'direction':'down'};
}
else if (linkOptions.location == 'tl' || linkOptions.location == 'tr'){
linkOptions.show = {'direction':'up'};
linkOptions.hide = {'direction':'up'};
}
if (typeof linkOptions.speed == 'undefined'){
linkOptions.speed = 500;
}
return linkOptions;
}
// Every 500 ms, check to see whether we're going to show a new link.
function mainLoop(state, vidnumber){
var timeChecker = setInterval(function(){
try{
state.videoPlayer.update(); // Forced update of time. Required for Safari.
}
catch(err){
// If this fails, shut down this loop.
// it's probably because we moved to a new tab.
clearInterval(timeChecker);
}
time = state.videoPlayer.currentTime;
// If we should be showing a link:
if(currentLink(time, vidnumber) != -1){
// ...and there's something being shown,
if(linkBeingShown[vidnumber]){
// but it's not the one that should be shown,
if(currentLink(time, vidnumber) != currentLinkShown(vidnumber)){
// then hide it.
hideLink(currentLinkShown(vidnumber), vidnumber);
}
}
// ...and there's nothing being shown,
else{
// then show the one we should be showing.
showLink(currentLink(time, vidnumber), vidnumber);
}
// If we should NOT be showing a link,
}else{
// ...and one is showing, hide it.
if(currentLinkShown(vidnumber) != -1){
hideLink(currentLinkShown(vidnumber), vidnumber);
}
}
}, 500);
}
// Show the link on the video. While we're at it, bold the one in the list too.
function showLink(n, vidnumber){
console.log('showing link ' + n + ' for video ' + (vidnumber+1));
$('#vidlinks-live-' + (vidnumber+1) +' #link-card-live-' + n )
.show(linkOptions.effect, linkOptions.show, linkOptions.speed);
$('#vidlinks-static-' + (vidnumber+1) +' #link-card-static-' + n )
.children()
.addClass('boldlink');
linkTimer[vidnumber][n].shown = true;
linkBeingShown[vidnumber] = true;
}
// Hide the link on the video and un-bold the one on the list.
function hideLink(n, vidnumber){
console.log('hiding link ' + n + ' for video ' + (vidnumber+1));
$('#vidlinks-live-' + (vidnumber+1) +' #link-card-live-' + n )
.hide(linkOptions.effect, linkOptions.show, linkOptions.speed);
$('#vidlinks-static-' + (vidnumber+1) +' #link-card-static-' + n )
.children()
.removeClass('boldlink');
linkTimer[vidnumber][n].shown = false;
linkBeingShown[vidnumber] = false;
}
// Which link SHOULD we be showing right now? Return -1 if none.
// If we should be showing several, returns the first one.
function currentLink(t, vidnumber){
var linkNumber = -1;
for(var i=0; i < linkTimer[vidnumber].length; i++){
if(t >= linkTimer[vidnumber][i].time && t < (linkTimer[vidnumber][i].time + hideLinkAfter)){
linkNumber = i;
break;
}
}
return linkNumber;
}
// Which link are we ACTUALLY showing right now? Return -1 if none.
// If we're showing several, returns the first one.
function currentLinkShown(vidnumber){
var linkNumber = -1;
for(var i=0; i < linkTimer[vidnumber].length; i++){
if(linkTimer[vidnumber][i].shown){
linkNumber = i;
break;
}
}
return linkNumber;
}
// This is a sorting function for my timer.
function timeCompare(a,b){
if (a.time < b.time)
return -1;
if (a.time > b.time)
return 1;
return 0;
}
// Converts hh:mm:ss to a number of seconds for time-based problems.
// If it's passed a number, it just spits that back out as seconds.
function hmsToTime(hms){
hms = hms.toString();
var hmsArray = hms.split(':');
var time = 0;
if(hmsArray.length == 3){
time = 3600*parseInt(hmsArray[0]) + 60*parseInt(hmsArray[1]) + Number(hmsArray[2]);
}
else if(hmsArray.length == 2){
time = 60*parseInt(hmsArray[0]) + Number(hmsArray[1]);
}
else if(hmsArray.length == 1){
time = Number(hmsArray[0]);
}
return time;
}
});
}