forked from toolness/mas-bebe-itvs-sprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instapoppin.js
185 lines (169 loc) · 5.47 KB
/
instapoppin.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
/*
* instapoppin.js version 0.3
*
* Copyright 2011, Mozilla Foundation
* Licensed under the MIT license
*/
var Instapoppin = (function() {
var activateOnLoad = true;
function ParseError(message) { this.message = message; }
function getTimestampInSeconds(ts) {
if (ts.length == 0)
return 0;
var timeParts = ts.split(':');
realTimeParts = [];
timeParts.forEach(function(number) {
if (number.length == 0)
number = '00';
var floatNumber = parseFloat(number);
if (isNaN(floatNumber))
throw new ParseError('unable to parse time: ' + ts);
realTimeParts.push(floatNumber);
});
switch (realTimeParts.length) {
case 1:
// It's a pure seconds-based offset.
return realTimeParts[0];
case 2:
// It's in MM:SS format.
return realTimeParts[0] * 60 + realTimeParts[1];
case 3:
// It's in HH:MM:SS format.
return ((realTimeParts[0] * 60) + realTimeParts[1]) * 60 +
realTimeParts[2];
default:
throw new ParseError('too many colons in time: ' + ts);
}
}
var self = {
parseDurations: function parseDurations(str) {
var durations = [];
if (typeof(str) == 'string') {
var segments = str.split(',');
segments.forEach(function(segment) {
segment = segment.trim();
segment = segment.split('-');
if (segment.length > 2) {
throw new ParseError("multiple dashes in segment:" + str);
} else if (segment.length == 1) {
throw new ParseError("missing dash in segment:" + str);
}
var duration = {
start: getTimestampInSeconds(segment[0]),
end: getTimestampInSeconds(segment[1]) || Infinity
};
durations.push(duration);
});
return durations;
}
throw new ParseError("unable to parse time durations: " + str);
},
ParseError: ParseError,
preventDefault: function preventDefault() {
activateOnLoad = false;
},
getParticipatingElements: function() {
var elements = document.querySelectorAll('[data-active-during]');
var array = [];
for (var i = 0; i < elements.length; i++)
array.push(elements[i]);
return array;
},
getActiveDurations: function(element) {
var activeDuring = element.getAttribute('data-active-during');
return Instapoppin.parseDurations(activeDuring);
}
};
// Taken from http://hacks.mozilla.org/2010/01/classlist-in-firefox-3-6/
var addClass = function (elm, className) {
if (document.documentElement.classList) {
addClass = function (elm, className) {
elm.classList.add(className);
}
} else {
addClass = function (elm, className) {
if (!elm) {
return false;
}
if (!containsClass(elm, className)) {
elm.className += (elm.className ? " " : "") + className;
}
}
}
addClass(elm, className);
}
var removeClass = function (elm, className) {
if (document.documentElement.classList) {
removeClass = function (elm, className) {
elm.classList.remove(className);
}
} else {
removeClass = function (elm, className) {
if (!elm || !elm.className) {
return false;
}
var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)", "g");
elm.className = elm.className.replace(regexp, "$2");
}
}
removeClass(elm, className);
}
function warn(txt) {
if (window.console && window.console.warn)
window.console.warn(txt);
}
function info(txt) {
if (window.console && window.console.info)
window.console.info(txt);
}
window.addEventListener("DOMContentLoaded", function() {
if (!activateOnLoad)
return;
var primaries = document.querySelectorAll('.primary-sync-source');
if (primaries.length == 0) {
var mediaElements = document.querySelectorAll('video, audio');
if (mediaElements.length == 0) {
warn("No media elements found on the page, aborting.");
return;
} else {
info("No media elements found with class 'primary-sync-source', " +
"using the first <" + mediaElements[0].nodeName + "> element " +
"on the page.");
primaries = mediaElements;
}
} else if (primaries.length > 1) {
warn("More than one media element with class 'primary-sync-source' " +
"found, aborting.");
return;
}
var pop = Popcorn(primaries[0]);
Instapoppin.getParticipatingElements().forEach(function(elem) {
var durations = Instapoppin.getActiveDurations(elem);
durations.forEach(function(duration) {
pop.simplecode({
start: duration.start,
end: duration.end,
onStart: function() {
addClass(elem, 'active');
},
onEnd: function() {
removeClass(elem, 'active');
}
});
});
});
}, false);
// This is just like Popcorn's code plugin, but even simpler
// because we don't need an onFrame callback.
Popcorn.plugin("simplecode", function(options) {
return {
start: function(event, options) {
options.onStart();
},
end: function(event, options) {
options.onEnd();
}
};
});
return self;
})();