-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoScript.js
316 lines (286 loc) · 7.68 KB
/
videoScript.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
/*!
* Copyright 2015 SelmanMade
*
* Changes in 0.16
* - Added support for poster attribute on videos
* - Fix to support autoplay = false
*
* Changes in 0.15.1
* - Added small array filter shim (from Mozilla) for older browsers
* - Added addEvent function for older browsers
*
* Changes in 0.15
* - Added support for 'toUrl' for buttons
* - Added support for 'target' for buttons when using 'toUrl'
* - Added support for 'delay' for buttons in ms
*
* Changes in 0.14
* - Minor fixes for wider browser support
* - Native controls fix for Firefox
* - Added WebM priority
*
* Changes in 0.13
* - Added alert when video is not found
* - Script will not break when video's fail to load
* - Buttons will not be created for video's that failed to load
*
* Changes in 0.12
* - Added a transparent white background color to the buttons for older IE versions
*
* Freely distributable under the MIT license.
*/
if (!Array.prototype.filter) {
Array.prototype.filter = function(fun/*, thisArg*/) {
'use strict';
if (this === void 0 || this === null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}
var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i];
if (fun.call(thisArg, val, i, t)) {
res.push(val);
}
}
}
return res;
};
}
window.videoScript = (function () {
var videos = [];
function addEvent(element, type, listener, useCapture) {
if (element.attachEvent) {
return element.attachEvent('on' + type, listener);
}
else {
return element.addEventListener(type, listener, useCapture);
}
}
function getVideo(id) {
var result = videos.filter(function (video, index) {
return video.id === id;
});
if (result.length > 0) {
return result[0];
}
return null;
}
function removeVideos(excepts) {
var containerElem = document.getElementById(videoScript.containerId);
for (var i = containerElem.children.length - 1; i >= 0; i--) {
var child = containerElem.children[i];
if (excepts.indexOf(child.id) == -1) {
containerElem.removeChild(child);
}
};
}
function getVideoWrapper(id) {
var video = getVideo(id);
if (video == null) {
alert('Video with id "' + id + '" not found');
return null;
}
var wrapperElem = document.getElementById(id);
if (wrapperElem) {
return wrapperElem;
}
wrapperElem = document.createElement('div');
wrapperElem.style.position = 'relative';
wrapperElem.id = id;
wrapperElem.buttons = [];
videoElem = document.createElement('video');
videoElem.controls = video.controls === true;
videoElem.setAttribute('controls', video.controls === true ? 'true' : 'false');
videoElem.loop = video.loop === true;
if (video.poster) {
videoElem.poster = video.poster;
}
videoElem.preload = 'auto';
videoElem.width = 800;
videoElem.height = 450;
videoElem.style.width = videoScript.width;
if (video.nextVideo) {
var nextVideo = getVideo(video.nextVideo);
if (nextVideo != null) {
addEvent(videoElem, 'ended', function () {
loadVideo(nextVideo.id);
});
}
}
if (video.buttons) {
for (var i = 0; i < video.buttons.length; i++) {
var button = video.buttons[i];
var buttonElem;
if (button.toVideo) {
if (getVideo(button.toVideo) == null) {
continue;
}
buttonElem = document.createElement('button');
buttonElem.type = 'button';
addEvent(buttonElem, 'click', function (button) {
return function () {
loadVideo(button.toVideo);
};
}(button));
}
else if (button.toUrl) {
buttonElem = document.createElement('a');
buttonElem.href = button.toUrl;
buttonElem.target = button.target || '_blank';
}
else {
continue;
}
buttonElem.style.zIndex = 99;
buttonElem.style.cursor = 'pointer';
buttonElem.style.position = 'absolute';
buttonElem.style.background = 'none';
buttonElem.style.backgroundColor = videoScript.buttonBackgroundColor;
buttonElem.style.border = 'none';
buttonElem.style.top = button.position.top + 'px';
buttonElem.style.left = button.position.left + 'px';
buttonElem.style.width = button.size.width + 'px';
buttonElem.style.height = button.size.height + 'px';
buttonElem.delay = 0;
wrapperElem.buttons.push(buttonElem);
if (button.delay) {
buttonElem.delay = Number(button.delay);
}
};
}
for (var i = 0; i < video.sources.length; i++) {
var sourceElem = document.createElement('source');
sourceElem.src = video.sources[i].src;
sourceElem.type = video.sources[i].type;
if (sourceElem.type == 'video/webm') {
videoElem.insertBefore(sourceElem, videoElem.firstChild);
}
else {
videoElem.appendChild(sourceElem);
}
};
wrapperElem.appendChild(videoElem);
return wrapperElem;
}
function preloadVideos(ids) {
for (var i = 0; i < ids.length; i++) {
var wrapperElem = document.getElementById(ids[i]);
if (!wrapperElem) {
wrapperElem = getVideoWrapper(ids[i]);
if (wrapperElem != null) {
var containerElem = document.getElementById(videoScript.containerId);
containerElem.appendChild(wrapperElem);
}
}
if (wrapperElem != null) {
var videoElem = wrapperElem.getElementsByTagName('video')[0];
if (!videoElem.paused || videoElem.currentTime != 0) {
tryStopVideo(videoElem);
}
wrapperElem.style.display = 'none';
}
};
}
function loadVideo(id) {
var video = getVideo(id);
var ids = [];
ids.push(id);
if (video.nextVideo) {
ids.push(video.nextVideo);
}
if (video.buttons) {
for (var i = 0; i < video.buttons.length; i++) {
if (video.buttons[i].toVideo) {
ids.push(video.buttons[i].toVideo);
}
};
}
preloadVideos(ids);
removeVideos(ids);
var wrapperElem = document.getElementById(id);
for (var i = 0; i < wrapperElem.buttons.length; i++) {
setTimeout(function (wrapperElem, buttonElem) {
return function () {
wrapperElem.appendChild(buttonElem);
};
}(wrapperElem, wrapperElem.buttons[i]), wrapperElem.buttons[i].delay);
};
wrapperElem.style.display = "block";
var videoElem = wrapperElem.getElementsByTagName('video')[0];
if (video.autoplay === true) {
tryPlayVideo(videoElem);
}
}
function tryStopVideo(videoElem) {
if (videoElem.readyState == 4) {
videoElem.pause();
videoElem.currentTime = 0;
}
else {
setTimeout(function () {
tryStopVideo(videoElem);
}, 1000);
}
}
function tryPlayVideo(videoElem) {
if (videoElem.ended == true) {
videoElem.currentTime = 0;
}
if (videoElem.readyState == 4) {
videoElem.currentTime = 0;
videoElem.play();
}
else {
setTimeout(function () {
tryPlayVideo(videoElem);
}, 1000);
}
}
function loadVideos(url) {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 ) {
if(xmlhttp.status == 200) {
videos = JSON.parse(xmlhttp.responseText).videos;
loadVideo(videos[0].id);
}
else if(xmlhttp.status == 400) {
alert('There was an error 400')
}
else {
alert('Could not load videos')
}
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
var videoScript = {
loadUrl: function(url) {
loadVideos(url);
},
loadData: function(jsonData) {
videos = jsonData.videos;
loadVideo(videos[0].id);
},
containerId: 'videoContainer',
buttonBackgroundColor: 'rgba(255,255,255,0)',
width: '100%'
};
return videoScript;
})();