-
Notifications
You must be signed in to change notification settings - Fork 54
/
super-video-background.js
319 lines (264 loc) · 9.37 KB
/
super-video-background.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
import { generateActionButton } from './buttons.js';
import { isArray, stringToType, isMobile, parseResolutionString, proportionalParentCoverResize, percentage, fixed } from 'book-of-spells';
export class SuperVideoBackground {
constructor(elem, params, id, uid, type, factoryInstance) {
if (!id) return;
this.is_mobile = isMobile();
this.type = type;
this.id = id;
this.factoryInstance = factoryInstance;
this.element = elem;
this.playerElement = null;
this.uid = uid;
this.element.setAttribute('data-vbg-uid', uid);
this.buttons = {};
this.isIntersecting = false;
this.paused = false; // user requested pause. used for blocking intersection softPlay
this.muted = false;
this.currentState = 'notstarted';
this.initialPlay = false;
this.initialVolume = false;
this.volume = 1;
this.params = {};
const DEFAULTS = {
'pause': false, //deprecated
'play-button': false,
'mute-button': false,
'autoplay': true,
'muted': true,
'loop': true,
'mobile': true,
'load-background': false,
'resolution': '16:9',
'inline-styles': true,
'fit-box': false,
'offset': 100, // since showinfo is deprecated and ignored after September 25, 2018. we add +100 to hide it in the overflow
'start-at': 0,
'end-at': 0,
'poster': null,
'always-play': false,
'volume': 1,
'no-cookie': true,
'force-on-low-battery': false,
'lazyloading': false,
'title': 'Video background'
};
this.params = this.parseProperties(params, DEFAULTS, this.element, ['data-ytbg-', 'data-vbg-']);
//pause deprecated
if (this.params.pause) {
this.params['play-button'] = this.params.pause;
}
this.params.resolution_mod = parseResolutionString(this.params.resolution);
this.muted = this.params.muted;
this.volume = this.params.volume;
this.currentTime = this.params['start-at'] || 0;
this.duration = this.params['end-at'] || 0;
this.percentComplete = 0;
if (this.params['start-at']) this.percentComplete = this.timeToPercentage(this.params['start-at']);
this.buildWrapperHTML();
if (this.is_mobile && !this.params.mobile) return;
if (this.params['play-button']) {
generateActionButton(this, {
name: 'playing',
className: 'play-toggle',
innerHtml: '<i class="fa"></i>',
initialState: !this.paused,
stateClassName: 'paused',
condition_parameter: 'paused',
stateChildClassNames: ['fa-pause-circle', 'fa-play-circle'],
actions: ['play', 'pause']
});
}
if (this.params['mute-button']) {
generateActionButton(this, {
name: 'muted',
className: 'mute-toggle',
innerHtml: '<i class="fa"></i>',
initialState: this.muted,
stateClassName: 'muted',
condition_parameter: 'muted',
stateChildClassNames: ['fa-volume-up', 'fa-volume-mute'],
actions: ['unmute', 'mute']
});
}
}
timeToPercentage(time) {
if (time <= this.params['start-at']) return 0;
if (time >= this.duration) return 100;
if (time <= 0) return 0;
time -= this.params['start-at']; // normalize
const duration = this.duration - this.params['start-at']; // normalize
return percentage(time, duration);
}
percentageToTime(percentage) {
if (!this.duration) return this.params['start-at'] || 0;
if (percentage > 100) return this.duration;
if (percentage <= 0) return this.params['start-at'] || 0;
const duration = this.duration - this.params['start-at']; // normalize
let time = percentage * duration / 100;
time = fixed(time, 3)
if (time > duration) time = duration;
if (this.params['start-at']) time += this.params['start-at']; // normalize
return time;
}
resize(element) {
if (!this.params['fit-box']) proportionalParentCoverResize(element || this.playerElement, this.params.resolution_mod, this.params.offset);
this.dispatchEvent('video-background-resize');
}
stylePlayerElement(element) {
if (!element) return;
if (this.params['inline-styles']) {
element.style.top = '50%';
element.style.left = '50%';
element.style.transform = 'translateX(-50%) translateY(-50%)';
element.style.position = 'absolute';
element.style.opacity = 0;
}
if (this.params['fit-box']) {
element.style.width = '100%';
element.style.height = '100%';
}
}
buildWrapperHTML() {
const parent = this.element.parentNode;
// wrap
this.element.classList.add('youtube-background', 'video-background');
//set css rules
const wrapper_styles = {
"height" : "100%",
"width" : "100%",
"z-index": "0",
"position": "absolute",
"overflow": "hidden",
"top": 0, // added by @insad
"left": 0,
"bottom": 0,
"right": 0
};
if (!this.params['mute-button']) {
wrapper_styles["pointer-events"] = "none" // avoid right mouse click popup menu
}
if (this.params['load-background'] || this.params['poster']) {
this.loadBackground(this.id);
if (this.params['poster']) wrapper_styles['background-image'] = `url(${ this.params['poster'] })`;
wrapper_styles['background-size'] = 'cover';
wrapper_styles['background-repeat'] = 'no-repeat';
wrapper_styles['background-position'] = 'center';
}
if (this.params['inline-styles']) {
for (let property in wrapper_styles) {
this.element.style[property] = wrapper_styles[property];
}
if (!['absolute', 'fixed', 'relative', 'sticky'].indexOf(parent.style.position)) {
parent.style.position = 'relative';
}
}
// set play/mute controls wrap
if (this.params['play-button'] || this.params['mute-button']) {
const controls = document.createElement('div');
controls.className = 'video-background-controls';
controls.style.position = 'absolute';
controls.style.top = '10px';
controls.style.right = '10px';
controls.style['z-index'] = 2;
this.controls_element = controls;
parent.appendChild(controls);
}
return this.element;
}
loadBackground(id) {
if (!this.params['load-background']) return;
if (!id) return;
if (this.type === 'youtube') this.element.style['background-image'] = `url(https://img.youtube.com/vi/${id}/hqdefault.jpg)`;
if (this.type === 'vimeo') this.element.style['background-image'] = `url(https://vumbnail.com/${id}.jpg)`;
}
destroy() {
this.playerElement.remove();
this.element.classList.remove('youtube-background', 'video-background');
this.element.removeAttribute('data-vbg-uid');
this.element.style = '';
if (this.params['play-button'] || this.params['mute-button']) {
this.controls_element.remove();
}
if (this.timeUpdateTimer) clearInterval(this.timeUpdateTimer);
this.dispatchEvent('video-background-destroyed');
}
setDuration(duration) {
if (this.duration === duration) return;
if (this.params['end-at']) {
if (duration > this.params['end-at']) {
this.duration = this.params['end-at'];
return;
}
if (duration < this.params['end-at']) {
this.duration = duration;
return;
}
} else {
this.duration = duration;
return;
}
if (duration <= 0) this.duration = this.params['end-at'];
}
setStartAt(startAt) {
this.params['start-at'] = startAt;
}
setEndAt(endAt) {
this.params['end-at'] = endAt;
if (this.duration > endAt) this.duration = endAt;
if (this.currentTime > endAt) this.onVideoEnded();
}
dispatchEvent(name) {
this.element.dispatchEvent(new CustomEvent(name, { bubbles: true, detail: this }));
}
shouldPlay() {
if (this.currentState === 'ended' && !this.params.loop) return false;
if (this.params['always-play'] && this.currentState !== 'playing') return true;
if (this.isIntersecting && this.params.autoplay && this.currentState !== 'playing') return true;
return false;
}
mobileLowBatteryAutoplayHack() {
if (!this.params['force-on-low-battery']) return;
if (!this.is_mobile && this.params.mobile) return;
const forceAutoplay = function() {
if (!this.initialPlay && this.params.autoplay && this.params.muted) {
this.softPlay();
if (!this.isIntersecting && !this.params['always-play']) {
this.softPause();
}
}
}
document.addEventListener('touchstart', forceAutoplay.bind(this), { once: true });
}
parseProperties(params, defaults, element, attr_prefix) {
let res_params = {};
if (!params) {
res_params = defaults;
} else {
for (let k in defaults) {
//load in defaults if the param hasn't been set
res_params[k] = !params.hasOwnProperty(k) ? defaults[k] : params[k];
}
}
if (!element) return res_params;
// load params from data attributes
for (let k in res_params) {
let data;
if (isArray(attr_prefix)) {
for (let i = 0; i < attr_prefix.length; i++) {
const temp_data = element.getAttribute(attr_prefix[i]+k);
if (temp_data) {
data = temp_data;
break;
}
}
} else {
data = element.getAttribute(attr_prefix+k);
}
if (data !== undefined && data !== null) {
res_params[k] = stringToType(data);
}
}
return res_params;
}
}