forked from theonion/videojs-endcard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
videojs.endcard.js
109 lines (92 loc) · 2.79 KB
/
videojs.endcard.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
(function(vjs) {
"use strict";
var
extend = function(obj) {
var arg, i, k;
for (i = 1; i < arguments.length; i++) {
arg = arguments[i];
for (k in arg) {
if (arg.hasOwnProperty(k)) {
obj[k] = arg[k];
}
}
}
return obj;
},
defaults = {
count: 10,
counter: "counter",
countdown: "countdown",
countdown_text: "Next video in:",
endcard: "player-endcard",
related: "related-content",
next: "next-video",
getRelatedContent: function(callback){callback();},
getNextVid: function(callback){callback();}
},
endcard = function(options) {
var player = this;
var el = this.el();
var settings = extend({}, defaults, options || {});
// set background
var card = document.createElement('div');
card.id = settings.endcard;
card.style.display = 'none';
el.appendChild(card);
settings.getRelatedContent(function(content) {
if (content instanceof Array) {
var related_content_div = document.createElement('div');
related_content_div.id = settings.related;
for (var i = 0; i < content.length; i++) {
related_content_div.appendChild(content[i]);
}
card.appendChild(related_content_div);
}
else {
throw new TypeError("options.getRelatedContent must return an array");
}
});
settings.getNextVid(function(next) {
if (typeof next !== "undefined") {
var next_div = document.createElement('div');
var counter = document.createElement('span');
var countdown = document.createElement('div');
counter.id = settings.counter;
countdown.id = settings.countdown;
next_div.id = settings.next;
countdown.innerHTML = settings.countdown_text;
countdown.appendChild(counter);
next_div.appendChild(countdown);
next_div.appendChild(next);
card.appendChild(next_div);
}
});
var counter_started = 0;
player.on('ended', function() {
card.style.display = 'block';
var next = document.getElementById(settings.next);
if (next !== null) {
var href = next.getElementsByTagName("a")[0].href;
var count = settings.count;
counter.innerHTML = count;
var interval = setInterval(function(){
count--;
if (count <= 0) {
clearInterval(interval);
window.location = href;
return;
}
counter.innerHTML = count;
}, 1000);
}
if (counter_started === 0) {
counter_started++;
player.on('playing', function() {
card.style.display = 'none';
clearInterval(interval);
});
}
});
};
(vjs.registerPlugin || vjs.plugin)('endcard', endcard);
})(window.videojs);