-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpopcorn.js
552 lines (403 loc) · 15.5 KB
/
popcorn.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
(function(global) {
// Cache refs to speed up calls to native utils
var
forEach = Array.prototype.forEach,
hasOwn = Object.prototype.hasOwnProperty,
slice = Array.prototype.slice,
// intentionally left undefined
undef,
// ID string matching
rIdExp = /^(#([\w\-\_\.]+))$/,
// Declare a pseudo-private constructor
// This constructor returns the instance object.
Popcorn = function( entity ) {
// Return new Popcorn object
return new Popcorn.p.init( entity );
};
// Declare a shortcut (Popcorn.p) to and a definition of
// the new prototype for our Popcorn constructor
Popcorn.p = Popcorn.prototype = {
init: function( entity ) {
var elem, matches;
matches = rIdExp.exec( entity );
if ( matches.length && matches[2] ) {
elem = document.getElementById(matches[2]);
}
this.video = elem ? elem : null;
this.data = {
events: {},
trackEvents: {
byStart: [{start: -1, end: -1}],
byEnd: [{start: -1, end: -1}],
startIndex: 0,
endIndex: 0,
previousUpdateTime: 0
}
};
var isReady = function( that ) {
if ( that.video.readyState >= 3 ) {
// adding padding to the front and end of the arrays
// this is so we do not fall off either end
var videoDurationPlus = that.video.duration + 1;
Popcorn.addTrackEvent( that, {
start: videoDurationPlus,
end: videoDurationPlus
});
that.video.addEventListener( "timeupdate", function( event ) {
var currentTime = this.currentTime,
previousTime = that.data.trackEvents.previousUpdateTime,
tracks = that.data.trackEvents,
tracksByEnd = tracks.byEnd,
tracksByStart = tracks.byStart;
// Playbar advancing
if ( previousTime < currentTime ) {
while ( tracksByEnd[tracks.endIndex] && tracksByEnd[tracks.endIndex].end <= currentTime ) {
if ( tracksByEnd[tracks.endIndex].running === true ) {
tracksByEnd[tracks.endIndex].running = false;
tracksByEnd[tracks.endIndex].natives.end.call( that, event, tracksByEnd[tracks.endIndex] );
}
tracks.endIndex++;
}
while ( tracksByStart[tracks.startIndex] && tracksByStart[tracks.startIndex].start <= currentTime ) {
if ( tracksByStart[tracks.startIndex].end > currentTime && tracksByStart[tracks.startIndex].running === false ) {
tracksByStart[tracks.startIndex].running = true;
tracksByStart[tracks.startIndex].natives.start.call( that, event, tracksByStart[tracks.startIndex] );
}
tracks.startIndex++;
}
// Playbar receding
} else if ( previousTime > currentTime ) {
while ( tracksByStart[tracks.startIndex] && tracksByStart[tracks.startIndex].start > currentTime ) {
if ( tracksByStart[tracks.startIndex].running === true ) {
tracksByStart[tracks.startIndex].running = false;
tracksByStart[tracks.startIndex].natives.end.call( that, event, tracksByStart[tracks.startIndex] );
}
tracks.startIndex--;
}
while ( tracksByEnd[tracks.endIndex] && tracksByEnd[tracks.endIndex].end > currentTime ) {
if ( tracksByEnd[tracks.endIndex].start <= currentTime && tracksByEnd[tracks.endIndex].running === false ) {
tracksByEnd[tracks.endIndex].running = true;
tracksByEnd[tracks.endIndex].natives.start.call( that, event, tracksByEnd[tracks.endIndex] );
}
tracks.endIndex--;
}
} else {
// When user seeks, currentTime can be equal to previousTime on the
// timeUpdate event. We are not doing anything with this right now, but we
// may need this at a later point and should be aware that this behavior
// happens in both Chrome and Firefox.
}
tracks.previousUpdateTime = currentTime;
}, false);
} else {
setTimeout( function() {
isReady( that );
}, 1);
}
};
isReady( this );
return this;
}
};
// This trick allows our api methods to be chained to
// instance references.
Popcorn.p.init.prototype = Popcorn.p;
Popcorn.forEach = function( obj, fn, context ) {
if ( !obj || !fn ) {
return {};
}
context = context || this;
// Use native whenever possible
if ( forEach && obj.forEach === forEach ) {
return obj.forEach(fn, context);
}
for ( var key in obj ) {
if ( hasOwn.call(obj, key) ) {
fn.call(context, obj[key], key, obj);
}
}
return obj;
};
Popcorn.extend = function( obj ) {
var dest = obj, src = slice.call(arguments, 1);
Popcorn.forEach( src, function( copy ) {
for ( var prop in copy ) {
dest[prop] = copy[prop];
}
});
return dest;
};
Popcorn.addTrackEvent = function( obj, track ) {
// Store this definition in an array sorted by times
obj.data.trackEvents.byStart.push( track );
obj.data.trackEvents.byEnd.push( track );
obj.data.trackEvents.byStart.sort( function( a, b ){
return ( a.start - b.start );
});
obj.data.trackEvents.byEnd.sort( function( a, b ){
return ( a.end - b.end );
});
};
// A Few reusable utils, memoized onto Popcorn
Popcorn.extend( Popcorn, {
error: function( msg ) {
throw msg;
},
guid: function() {
return +new Date() + Math.floor(Math.random()*11);
},
sizeOf: function ( obj ) {
var size = 0;
for ( var prop in obj ) {
size++;
}
return size;
},
nop: function () {}
});
// Simple Factory pattern to implement getters, setters and controllers
// as methods of the returned Popcorn instance. The immediately invoked function
// creates and returns an object of methods
Popcorn.extend(Popcorn.p, (function () {
// todo: play, pause, mute should toggle
var methods = "load play pause currentTime playbackRate mute volume duration",
ret = {};
// Build methods, store in object that is returned and passed to extend
Popcorn.forEach( methods.split(/\s+/g), function( name ) {
ret[ name ] = function( arg ) {
if ( typeof this.video[name] === "function" ) {
this.video[ name ]();
return this;
}
if ( arg !== false && arg !== null && typeof arg !== "undefined" ) {
this.video[ name ] = arg;
return this;
}
return this.video[ name ];
};
});
return ret;
})()
);
Popcorn.extend(Popcorn.p, {
// getting properties
roundTime: function () {
return -~this.video.currentTime;
},
exec: function ( time, fn ) {
!fn && ( fn = Popcorn.nop );
var timer = 0,
self = this,
callback = function execCallback( event ) {
if ( this.currentTime() >= time && !timer ) {
fn.call(self, event);
this.unlisten("execCallback");
timer++;
}
};
this.listen("timeupdate", callback);
return this;
},
removePlugin: function( name ) {
var byStart = this.data.trackEvents.byStart,
byEnd = this.data.trackEvents.byEnd;
delete Popcorn.p[ name ];
// remove plugin reference from registry
for ( var i = 0, rl = Popcorn.registry.length; i < rl; i++ ) {
if ( Popcorn.registry[i].type === name ) {
Popcorn.registry.splice(i, 1);
break; // plugin found, stop checking
}
}
// remove all trackEvents
for ( var i = 0, sl = byStart.length; i < sl; i++ ) {
if ( byStart[i] && byStart[i].natives && byStart[i].natives.type === name ) {
byStart.splice( i, 1 );
i--; sl--; // update for loop if something removed, but keep checking
if ( this.data.trackEvents.startIndex <= i ) {
this.data.trackEvents.startIndex--; // write test for this
}
}
}
for ( var i = 0, el = byEnd.length; i < el; i++ ) {
if ( byEnd[i] && byEnd[i].natives && byEnd[i].natives.type === name ) {
byEnd.splice( i, 1 );
i--; el--; // update for loop if something removed, but keep checking
if ( this.data.trackEvents.endIndex <= i ) {
this.data.trackEvents.endIndex--; // write test for this
}
}
}
return this;
}
});
Popcorn.Events = {
UIEvents: "blur focus focusin focusout load resize scroll unload ",
MouseEvents: "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave click dblclick",
Events: "loadstart progress suspend emptied stalled play pause " +
"loadedmetadata loadeddata waiting playing canplay canplaythrough " +
"seeking seeked timeupdate ended ratechange durationchange volumechange"
};
Popcorn.Events.Natives = Popcorn.Events.UIEvents + " " +
Popcorn.Events.MouseEvents + " " +
Popcorn.Events.Events,
Popcorn.events = {
isNative: function( type ) {
var checks = Popcorn.Events.Natives.split(/\s+/g);
for ( var i = 0; i < checks.length; i++ ) {
if ( checks[i] === type ) {
return true;
}
}
return false;
},
getInterface: function( type ) {
if ( !Popcorn.events.isNative( type ) ) {
return false;
}
var natives = Popcorn.Events, proto;
for ( var p in natives ) {
if ( p !== "Natives" && natives[p].indexOf(type) > -1 ) {
proto = p;
}
}
return proto;
},
all: Popcorn.Events.Natives.split(/\s+/g),
fn: {
trigger: function ( type, data ) {
// setup checks for custom event system
if ( this.data.events[type] && Popcorn.sizeOf(this.data.events[type]) ) {
var interface = Popcorn.events.getInterface(type);
if ( interface ) {
var evt = document.createEvent( interface );
evt.initEvent(type, true, true, window, 1);
this.video.dispatchEvent(evt);
return this;
}
// Custom events
Popcorn.forEach(this.data.events[type], function ( obj, key ) {
obj.call( this, evt, data );
}, this);
}
return this;
},
listen: function ( type, fn ) {
var self = this, hasEvents = true, ns = '';
if ( !this.data.events[type] ) {
this.data.events[type] = {};
hasEvents = false;
}
// Register
this.data.events[type][ fn.name || ( fn.toString() + Popcorn.guid() ) ] = fn;
// only attach one event of any type
if ( !hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) {
this.video.addEventListener( type, function( event ) {
Popcorn.forEach( self.data.events[type], function ( obj, key ) {
if ( typeof obj === "function" ) {
obj.call(self, event);
}
});
//fn.call( self, event );
}, false);
}
return this;
},
unlisten: function( type, fn ) {
if ( this.data.events[type] && this.data.events[type][fn] ) {
this.data.events[type][fn] = null;
return this;
}
this.data.events[type] = null;
return this;
},
special: {
// handles timeline controllers
play: function () {
// renders all of the interally stored track commands
}
}
}
};
// Extend listen and trigger to all Popcorn instances
Popcorn.forEach( ["trigger", "listen", "unlisten"], function ( key ) {
Popcorn.p[key] = Popcorn.events.fn[key];
});
Popcorn.protect = {
natives: "load play pause currentTime playbackRate mute volume duration removePlugin roundTime trigger listen unlisten".toLowerCase().split(/\s+/)
};
// Plugins are registered
Popcorn.registry = [];
// An interface for extending Popcorn
// with plugin functionality
Popcorn.plugin = function( name, definition ) {
if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
Popcorn.error("'" + name + "' is a protected function name");
return;
}
// Provides some sugar, but ultimately extends
// the definition into Popcorn.p
var natives = Popcorn.events.all,
reserved = [ "start", "end"],
plugin = {type: name},
pluginFn,
setup;
if ( typeof definition === "object" ) {
setup = definition;
/*if ( !( "timeupdate" in setup ) ) {
setup.timeupdate = Popcorn.nop;
}*/
console.log(options);
pluginFn = function ( options ) {
if ( !options ) {
return this;
}
// storing the plugin natives
options.natives = setup;
options.natives.type = name;
options.running = false;
// Checks for expected properties
if ( !( "start" in options ) ) {
options.start = 0;
}
if ( !( "end" in options ) ) {
options.end = this.duration();
}
// If a _setup was declared, then call it before
// the events commence
if ( "_setup" in setup && typeof setup._setup === "function" ) {
setup._setup.call(self, options);
}
Popcorn.addTrackEvent( this, options );
// Future support for plugin event definitions
// for all of the native events
Popcorn.forEach( setup, function ( callback, type ) {
if ( reserved.indexOf(type) === -1 ) {
this.listen( type, callback );
}
}, this);
return this;
};
}
// If a function is passed...
if ( typeof definition === "function" ) {
// Execute and capture returned object
setup = definition.call(this);
// Ensure an object was returned
// it has properties and isnt an array
if ( typeof setup === "object" &&
!( "length" in setup ) ) {
Popcorn.plugin( name, setup );
}
return;
}
// Assign new named definition
plugin[ name ] = pluginFn;
// Extend Popcorn.p with new named definition
Popcorn.extend( Popcorn.p, plugin );
// Push into the registry
Popcorn.registry.push(plugin);
return plugin;
};
global.Popcorn = Popcorn;
})(window);