-
Notifications
You must be signed in to change notification settings - Fork 0
/
memento.js
executable file
·385 lines (381 loc) · 11.6 KB
/
memento.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
(function() {
'use strict';
// function factory
var memento;
memento = function() {
// return value of the factory function
var func,
data,
node;
func = function() {
if (typeof func.data === 'function') {
func.__update();
}
};
// getter/setter for bound data
func.all_data = function(obj) {
if (obj) {
data = func.__wrap(obj);
return func;
} else {
return data;
}
};
// getter/setter for media node
func.node = function(element) {
if (element) {
node = element;
return func;
} else {
return node;
}
};
// add an abstract getter function
func.__add_extractor = function(obj) {
obj.__extract = function(key) {
if ( (key === 'start') || (key === 'end') ) {
if (obj[key]) {
return func.seconds(obj[key]);
}
}
if (obj[key]) {
return obj[key];
} else {
return false;
}
}
return obj;
};
// extend all items in the data array with custom methods
func.__wrap = function(data) {
data = data.map(function(item) {
item = func.__add_extractor(item);
return item;
});
return data;
};
func.__update = function() {
// every time the node updates
node.ontimeupdate = function() {
var data,
timestamp,
actions,
action;
timestamp = func.timestamp();
data = func.data();
actions = func.timed_actions(timestamp);
for (var i = 0; i < actions.length; i++) {
action = actions[i];
if (typeof action === 'function') {
action(data, timestamp, node);
}
}
};
};
func.__actions = [];
func.__add_action = function(new_function, start, end) {
if (start) {new_function.start = start;}
if (end) {new_function.end = end;}
func.__actions.push(new_function);
};
func.all_actions = function() {
var actions;
actions = this.__actions;
return actions;
};
func.timed_actions = function(timestamp) {
var all_actions,
timed_actions,
match;
timestamp = timestamp || this.timestamp();
all_actions = this.all_actions();
timed_actions = all_actions.filter(function(item) {
var breakpoints,
both_undefined,
either_undefined,
one_undefined;
both_undefined = !item.start && !item.end;
either_undefined = !item.start || !item.end;
one_undefined = ( (!item.start && item.end) || (item.start && !item.end) )
if (both_undefined) {
return true;
}
if (one_undefined) {
return false;
}
if (!item.start || !item.end) {
return false;
} else {
breakpoints = {low: item.start, high: item.end};
match = func.test_breakpoints(breakpoints);
return match;
}
});
return timed_actions;
};
// add a new item to the bound data
func.add_item = function(new_data) {
if (typeof data.map !== 'function') {
data = [];
}
if (new_data) {
data = data.concat(new_data);
return data;
}
};
// remove a bound item by index
func.remove_item = function(int) {
if (typeof int !== 'number') {
return false;
}
data = data.filter(function(item, index) {
return index !== int;
})
return data;
};
// get exact timestamp from node
func.timestamp = function() {
var timestamp = node.currentTime;
return timestamp;
};
// resolve string times in format DD:HH:MM:SS to a number
// of seconds
func.seconds = function(time) {
var has_colon,
is_number,
time_elements,
days,
hours,
minutes,
seconds;
if (time.indexOf && time.indexOf(':') === -1) {
has_colon = true;
} else {
has_colon = false;
}
is_number = typeof time === 'number';
// if it's just a numerical value, return it
if (!has_colon && is_number) {
return time;
} else {
time_elements = time.split(':').reverse();
seconds = time_elements[0] || "0";
if (seconds.indexOf('.') !== -1) {
seconds = parseFloat(seconds);
} else {
seconds = parseInt(seconds);
}
minutes = parseInt(time_elements[1]) || 0;
hours = parseInt(time_elements[2]) || 0;
days = parseInt(time_elements[3]) || 0;
}
seconds = seconds + (minutes * 60) + (hours * 60 * 60) + (days * 24 * 60 * 60);
return seconds;
};
// test whether a timestamp is between a range
func.test_breakpoints = function(breakpoints, timestamp) {
var mode;
timestamp = timestamp || this.timestamp();
if (breakpoints.low) {
mode = 'single';
} else if (breakpoints.length && typeof breakpoints.map === 'function') {
mode = 'multiple';
}
// if you only pass in one breakpoints object
// test it and return a boolean
if (mode === 'single') {
return this.test_single_breakpoint(breakpoints, timestamp);
// if you pass in an array of breakpoints objects,
// test all and return a mapped array of booleans
} else if (mode === 'multiple') {
return this.test_multiple_breakpoints(breakpoints, timestamp);
}
};
// test a single range
func.test_single_breakpoint = function(breakpoints, timestamp) {
var between;
// resolve breakpoints to numbers if necessary
if (typeof timestamp !== 'number') {
timestamp = func.seconds(timestamp);
}
if (typeof breakpoints.low !== 'number') {
breakpoints.low = func.seconds(breakpoints.low);
}
if (typeof breakpoints.high !== 'number') {
breakpoints.high = func.seconds(breakpoints.high);
}
// check timestamp position
between = ( (breakpoints.low <= timestamp) && (timestamp <= breakpoints.high) );
return between;
};
// test multiple ranges and return a map;
// uses the single item test internally
func.test_multiple_breakpoints = function(breakpoints, timestamp) {
var betweens;
timestamp = timestamp || this.timestamp();
betweens = breakpoints.map(function(item) {
var between;
between = test_single_breakpoint(item, timestamp);
return between;
});
return betweens;
};
// get data
func.data = function(timestamp) {
var timestamp = timestamp || this.timestamp(),
nearest_breakpoints,
current_data;
current_data = data.filter(function(item) {
var between, breakpoints, start, end;
start = item.__extract('start');
end = item.__extract('end');
if (!start || !end) {
return false;
}
breakpoints = {low: start, high: end};
between = func.test_breakpoints(breakpoints, timestamp);
return between;
});
if (current_data.length > 1) {
current_data = current_data.sort(function(a, b) {
var a_start, b_start;
a_start = a.__extract('start');
b_start = b.__extract('start');
return a_start > b_start;
});
}
if (current_data.length === 0) {
return false;
} else {
return current_data;
}
}
// round timestamp down for use in less precise lookups
func.rounded_timestamp = function() {
var timestamp, rounded_timestamp;
timestamp = func.timestamp();
rounded_timestamp = Math.floor(timestamp);
return rounded_timestamp;
};
// get all timestamps registered in the data
// object with time_ prefixes
func.breakpoints = function() {
var breakpoints, start, end;
breakpoints = [];
// for each item
for (var i = 0; i < data.length; i++) {
// check start
start = data[i].__extract('start');
if (breakpoints.indexOf(start) === -1) {
breakpoints.push(start);
}
// check end
end = data[i].__extract('end');
if (breakpoints.indexOf(end) === -1) {
breakpoints.push(end);
}
}
// sort chronologically
breakpoints = breakpoints.sort(function(a, b) {
return a > b;
});
return breakpoints;
};
// get the breakpoints closest to a timestamp
func.nearest_breakpoints = function(timestamp) {
var breakpoints,
nearest_breakpoints,
current_breakpoint,
between,
next_breakpoint,
timestamp = timestamp || this.timestamp();
breakpoints = func.breakpoints();
for (var i = 0; i < breakpoints.length; i++) {
current_breakpoint = breakpoints[i];
next_breakpoint = breakpoints[i + 1];
// if the timestamp is between one breakpoint and the next
between = current_breakpoint < timestamp && timestamp < next_breakpoint;
if (between) {
// return both values as an object
nearest_breakpoints = {low: current_breakpoint, high: next_breakpoint};
return nearest_breakpoints;
}
}
};
// attach an arbitrary function under a key, and pass it
// the scoped data via arguments
func.extend = function(label, bound_function) {
if (typeof label !== 'string' || typeof bound_function !== 'function') {
return;
}
// if we're still running, bind the function with the current values
func[label] = function() {
var timestamp,
current_data,
in_range;
timestamp = this.timestamp();
current_data = this.data(timestamp);
bound_function(current_data, timestamp, node);
}
return func;
};
// run a function every time the player updates
func.tick = function(breakpoints, iterator) {
if (typeof iterator !== 'function') {
return;
}
if (breakpoints === true) {
this.__add_action(iterator);
}
if (breakpoints.low && breakpoints.high) {
this.__add_action(iterator, breakpoints.low, breakpoints.high);
}
};
// fire a function once when the trigger time is passed
func.trigger = function(trigger_time, trigger_function) {
var sent,
counter,
event,
event_label,
event_handler,
trigger_start;
sent = false;
// resolve trigger time to seconds in case it's a string
trigger_time = this.seconds(trigger_time);
event_label = 'trigger-' + trigger_time;
event = new Event(event_label);
event_handler = function(event) {
var data, timestamp;
data = func.data();
timestamp = func.timestamp();
trigger_function(data, timestamp, node);
};
this.__add_action(event_handler, trigger_time, null);
node.addEventListener(event_label, event_handler)
func.tick(true, function() {
var timestamp,
passed;
if (sent) {
return;
} else {
timestamp = func.timestamp();
passed = timestamp > trigger_time;
if (passed) {
node.dispatchEvent(event);
sent = true;
}
}
});
};
// return results of the factory
return func;
};
// attach factory to global scope
if (!window.memento) {
window.memento = memento;
} else if (!window.memento_factory) {
window.memento_factory = memento;
} else {
console.log('could not expose memento.js because global variables named "memento" and "memento_factory" already exist');
}
}).call(this);