-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaTrigger-es5.js
108 lines (98 loc) · 3.68 KB
/
MediaTrigger-es5.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
/**
* MediaTrigger v.2.0.5
*/
(function (window, document, undefined) {
function MediaTrigger(params) {
this.params = params;
this.startASAP = false;
this.media = params.media;
this._getMediaDuration(params.triggers);
this.currentTrigger = 0;
this._bindedCheck = this._check.bind(this);
}
MediaTrigger.prototype._getMediaDuration = function() {
if (Number.isNaN(this.media.duration)) {
this.media.addEventListener('durationchange', function() {
this.mediaDuration = this.media.duration;
this.triggers = this._sortTriggers(this._handleTriggers());
if (this.startASAP) {
this.media.addEventListener('timeupdate', this._bindedCheck);
this.media.addEventListener('ended', this._bindedCheck);
}
}.bind(this));
} else {
this.mediaDuration = this.media.duration;
this.triggers = this._sortTriggers(this._handleTriggers());
}
}
/**
* Converts percent-based triggers to time-based triggers
* and store them in a uniform array of objects.
*/
MediaTrigger.prototype._handleTriggers = function() {
var handledTriggers = [];
this.params.triggers.forEach(function(trigger) {
var unit = trigger[0].slice(-1);
switch (unit) {
case 's':
handledTriggers.push({
triggerTime: parseFloat(trigger[0].slice(0, trigger[0].length-1)),
action: trigger[1]
});
break;
case '%':
var triggerPercent = parseFloat(trigger[0].slice(0, trigger[0].length-1)) / 100;
handledTriggers.push({
triggerTime: this.mediaDuration * triggerPercent,
action: trigger[1]
});
break;
default:
console.warn("[MediaTrigger] Trigger unit not recognized. Try 's' or '%'.");
}
}.bind(this));
return handledTriggers;
}
/**
* Sort triggers chronologically.
*/
MediaTrigger.prototype._sortTriggers = function (triggers) {
function compare( a, b ) {
if ( a.triggerTime < b.triggerTime ){
return -1;
}
if ( a.triggerTime > b.triggerTime ){
return 1;
}
return 0;
}
return triggers.sort(compare);
}
MediaTrigger.prototype._check = function () {
if (this.triggers[this.currentTrigger].triggerTime >= 0) {
if (this.media.currentTime >= this.triggers[this.currentTrigger].triggerTime) {
this._triggerAction();
}
}
}
MediaTrigger.prototype._triggerAction = function () {
this.triggers[this.currentTrigger].action();
this.currentTrigger++;
if (this.currentTrigger > this.triggers.length - 1) this.stop();
}
MediaTrigger.prototype.start = function () {
this.currentTrigger = 0;
if (this.mediaDuration != undefined) {
this.media.addEventListener('timeupdate', this._bindedCheck);
this.media.addEventListener('ended', this._bindedCheck);
}
else {
this.startASAP = true;
}
}
MediaTrigger.prototype.stop = function () {
this.media.removeEventListener('timeupdate', this._bindedCheck);
this.media.removeEventListener('ended', this._bindedCheck);
}
window.MediaTrigger = MediaTrigger;
})(window, document);