// ID string matching
rIdExp = /^(#([\w\-\_\.]+))$/,
- // ready fn cache
+ // Ready fn cache
readyStack = [],
readyBound = false,
readyFired = false,
- // Declare a pseudo-private constructor
+ // Declare constructor
// Returns an instance object.
Popcorn = function( entity ) {
// Return new Popcorn object
// If the video element has its own `id` use it, otherwise provide one
// Ensure that instances have unique ids and unique entries
// Uses `in` operator to avoid false positives on 0
- instance.id = !( instanceId in Popcorn.instanceIds ) && instanceId ||
+ instance.id = !( instanceId in Popcorn.instanceIds ) && instanceId ||
"__popcorn" + instanceLen;
// Create a reference entry for this instance
return;
}
-
-
+ // Add `entity` fn to ready stack
readyStack.push( entity );
// This process should happen once per page load
readyFired = true;
- // remove this listener
+ // Remove global DOM ready listener
document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
// Execute all ready function in the stack
readyStack = null;
};
+ // Register global DOM ready listener
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false);
}
return;
}
- // check if entity is a valid string id
+ // Check if entity is a valid string id
matches = rIdExp.exec( entity );
- // get video element by id or reference
+ // Get video element by id or object reference
this.video = matches && matches.length && matches[ 2 ] ?
document.getElementById( matches[ 2 ] ) :
entity;
+ // Register new instance
Popcorn.addInstance( this );
this.data = {
}
};
+ // Wrap true ready check
var isReady = function( that ) {
if ( that.video.readyState >= 2 ) {
- // adding padding to the front and end of the arrays
- // this is so we do not fall off either end
+ // Adding padding to the front and end of the arrays
+ // this is so we do not fall off either end
var duration = that.video.duration;
- // Check for no duration info (NaN)
+ // Check for no duration info (NaN)
var videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
Popcorn.addTrackEvent( that, {
tracksByEnd = tracks.byEnd,
tracksByStart = tracks.byStart;
- // Playbar advancing
+ // Playbar advancing
if ( previousTime < currentTime ) {
while ( tracksByEnd[ tracks.endIndex ] && tracksByEnd[ tracks.endIndex ].end <= currentTime ) {
- // if plugin does not exist on this instance, remove it
+ // If plugin does not exist on this instance, remove it
if ( !tracksByEnd[ tracks.endIndex ]._natives || !!that[ tracksByEnd[ tracks.endIndex ]._natives.type ] ) {
if ( tracksByEnd[ tracks.endIndex ]._running === true ) {
tracksByEnd[ tracks.endIndex ]._running = false;
}
while ( tracksByStart[ tracks.startIndex ] && tracksByStart[ tracks.startIndex ].start <= currentTime ) {
- // if plugin does not exist on this instance, remove it
+ // If plugin does not exist on this instance, remove it
if ( !tracksByStart[ tracks.startIndex ]._natives || !!that[ tracksByStart[ tracks.startIndex ]._natives.type ] ) {
if ( tracksByStart[ tracks.startIndex ].end > currentTime && tracksByStart[ tracks.startIndex ]._running === false ) {
tracksByStart[ tracks.startIndex ]._running = true;
}
}
}
- /*
- // This empty block causes errors with jslint
-
- 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);
}
};
- // This trick allows our api methods to be chained to
- // instance references.
+ // Extend constructor prototype to instance prototype
+ // Allows chaining methods to instances
Popcorn.p.init.prototype = Popcorn.p;
Popcorn.forEach = function( obj, fn, context ) {
},
guid: function( prefix ) {
Popcorn.guid.counter++;
- return ( prefix ? prefix : '' ) + ( +new Date() + Popcorn.guid.counter );
+ return ( prefix ? prefix : "" ) + ( +new Date() + Popcorn.guid.counter );
},
sizeOf: function ( obj ) {
var size = 0;
nop: function () {}
});
- // Memoization property
+ // Memoized GUID Counter
Popcorn.guid.counter = 1;
- // 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
+ // Factory to implement getters, setters and controllers
+ // as Popcorn instance methods. The IIFE will create and return
+ // an object with defined methods
Popcorn.extend(Popcorn.p, (function () {
- // todo: play, pause, mute should toggle
var methods = "load play pause currentTime playbackRate mute volume duration",
ret = {};
Popcorn.extend(Popcorn.p, {
- // getting properties
+ // Rounded currentTime
roundTime: function () {
return -~this.video.currentTime;
},
-
+
+ // Attach an event to a single point in time
exec: function ( time, fn ) {
- // creating a one second track event with an empty end
+ // Creating a one second track event with an empty end
Popcorn.addTrackEvent( this, {
start: time,
end: time + 1,
isNative: function( type ) {
- var checks = Popcorn.Events.Natives.split(/\s+/g);
+ var checks = Popcorn.Events.Natives.split( /\s+/g );
for ( var i = 0; i < checks.length; i++ ) {
if ( checks[i] === type ) {
return false;
}
- var natives = Popcorn.Events, proto;
+ var natives = Popcorn.Events,
+ proto;
for ( var p in natives ) {
- if ( p !== "Natives" && natives[p].indexOf(type) > -1 ) {
+ if ( p !== "Natives" && natives[ p ].indexOf( type ) > -1 ) {
proto = p;
}
}
return proto;
-
},
-
-
+ // Compile all native events to single array
all: Popcorn.Events.Natives.split(/\s+/g),
-
+ // Defines all Event handling static functions
fn: {
trigger: function ( type, data ) {
// setup checks for custom event system
- if ( this.data.events[type] && Popcorn.sizeOf(this.data.events[type]) ) {
+ if ( this.data.events[ type ] && Popcorn.sizeOf( this.data.events[ type ] ) ) {
- var eventInterface = Popcorn.events.getInterface(type);
+ var eventInterface = Popcorn.events.getInterface( type );
if ( eventInterface ) {
}
// Custom events
- Popcorn.forEach(this.data.events[type], function ( obj, key ) {
+ Popcorn.forEach(this.data.events[ type ], function ( obj, key ) {
obj.call( this, data );
}
// Register
- this.data.events[type][ fn.name || ( fn.toString() + Popcorn.guid() ) ] = fn;
+ 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 ) {
}
};
- // Extend listen and trigger to all Popcorn instances
+ // Extend Popcorn.events.fns (listen, unlisten, trigger) to all Popcorn instances
Popcorn.forEach( ["trigger", "listen", "unlisten"], function ( key ) {
- Popcorn.p[key] = Popcorn.events.fn[key];
+ Popcorn.p[ key ] = Popcorn.events.fn[ key ];
});
-
+ // Protected API methods
Popcorn.protect = {
natives: "load play pause currentTime playbackRate mute volume duration removePlugin roundTime trigger listen unlisten".toLowerCase().split(/\s+/)
};
-
+ // Internal Only
Popcorn.addTrackEvent = function( obj, track ) {
if ( track._natives ) {
- // supports user defined track event id
+ // Supports user defined track event id
track._id = !track.id ? Popcorn.guid( track._natives.type ) : track.id;
// Push track event ids into the history
track._natives.end = track._natives.end || Popcorn.nop;
}
- // Store this definition in an array sorted by times
+ // 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 ){
};
- // removePlugin( type ) removes all tracks of that from all instances of popcorn
- // removePlugin( obj, type ) removes all tracks of type from obj, where obj is a single instance of popcorn
+ // removePlugin( type ) removes all tracks of that from all instances of popcorn
+ // removePlugin( obj, type ) removes all tracks of type from obj, where obj is a single instance of popcorn
Popcorn.removePlugin = function( obj, name ) {
- // check if we are removing plugin from an instance or from all of Popcorn
+ // Check if we are removing plugin from an instance or from all of Popcorn
if ( !name ) {
- // all of Popcorn it is
-
- // fix the order
+ // Fix the order
name = obj;
obj = Popcorn.p;
- var registryLength = Popcorn.registry.length,
- registryIndex;
+ var registryLen = Popcorn.registry.length,
+ registryIdx;
// remove plugin reference from registry
- for ( registryIndex = 0; registryIndex < registryLength; registryIndex++ ) {
- if ( Popcorn.registry[ registryIndex ].type === name ) {
- Popcorn.registry.splice( registryIndex, 1 );
+ for ( registryIdx = 0; registryIdx < registryLen; registryIdx++ ) {
+ if ( Popcorn.registry[ registryIdx ].type === name ) {
+ Popcorn.registry.splice( registryIdx, 1 );
// delete the plugin
delete obj[ name ];
history = [];
- Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context) {
+ Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context ) {
// Preserve the original start/end trackEvents
if ( !o._id ) {
byStart.push( obj.data.trackEvents.byStart[i] );
var trackevents = [];
- Popcorn.forEach( obj.data.trackEvents.byStart, function(o, i, context) {
+ Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context ) {
if ( o._id ) {
trackevents.push(o);
}
return obj.data.history[ obj.data.history.length - 1 ];
};
- // Map TrackEvents functions to the {popcorn}.prototype
+ // Map and Extend TrackEvent functions to all Popcorn instances
Popcorn.extend( Popcorn.p, {
getTrackEvents: function() {
if ( type !== "type" ) {
- if ( reserved.indexOf(type) === -1 ) {
+ if ( reserved.indexOf( type ) === -1 ) {
this.listen( type, callback );
}
// Assign new named definition
plugin[ name ] = function( options ) {
- return pluginFn.call( this,
- isfn ? definition.call( this, options )
- : definition, options );
+ return pluginFn.call( this, isfn ? definition.call( this, options ) : definition,
+ options );
};
// Extend Popcorn.p with new named definition
Popcorn.extend( Popcorn.p, plugin );
// Push into the registry
- Popcorn.registry.push( Popcorn.extend( plugin, {
- type: name
- }) );
+ Popcorn.registry.push(
+ Popcorn.extend( plugin, {
+ type: name
+ })
+ );
return plugin;
};
};
Popcorn.xhr = function ( options ) {
-
+
if ( options.dataType &&
( options.dataType.toLowerCase() === "jsonp" ||
options.dataType.toLowerCase() === "script" ) ) {
if ( settings.type === "GET" && settings.data ) {
// append query string
- settings.url += ( rparams.test( settings.url ) ? "&" : "?") + settings.data;
+ settings.url += ( rparams.test( settings.url ) ? "&" : "?" ) + settings.data;
// Garbage collect and reset settings.data
settings.data = null;
var scripts = document.querySelectorAll('script[src="' + url + '"]');
- // If there are scripts with this url loaded, early return
+ // If there are scripts with this url loaded, early return
if ( scripts.length ) {
// Execute success callback and pass "exists" flag
}
}
- var head = document.getElementsByTagName("head")[0] || document.documentElement,
+ var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement,
script = document.createElement("script"),
paramStr = url.split("?")[1],
fired = false,
parts = params[ params.length - 1 ].split("=");
}
- callback = params.length ? ( parts[1] ? parts[1] : parts[0] ) : "jsonp";
+ callback = params.length ? ( parts[1] ? parts[1] : parts[0] ) : "jsonp";
if ( !paramStr && !isScript ) {
url += "?callback=" + callback;
}
if ( callback && !isScript ) {
-
- // If a callback name already exists...
+
+ // If a callback name already exists
if ( !!window[ callback ] ) {
-
+
// Create a new unique callback name
callback = Popcorn.guid( callback );
}
-
+
// Define the jsonp success callback globally
window[ callback ] = function ( data ) {
fired = true;
};
-
+
// Replace callback param and callback name
url = url.replace( parts.join("="), parts[0] + "=" + callback );
-
+
}
-
+
script.src = url;
-
+
script.onload = script.onreadystatechange = function() {
// Executing remote scripts
// Garbage collect the callback
delete window[ callback ];
-
+
// Garbage collect the script resource
head.removeChild( script );
}
- };
+ };
head.insertBefore( script, head.firstChild );
-
+
return;
-
+
};
-
+
Popcorn.getJSONP = Popcorn.xhr.getJSONP;
-
+
Popcorn.getScript = Popcorn.xhr.getScript = function( url, success ) {
return Popcorn.xhr.getJSONP( url, success, true );
data = source.split( "." );
data[ 0 ] = "parse" + data[ data.length - 1 ].toUpperCase();
data[ 1 ] = source;
-
+
}
// If the video has data sources and the correct parser is registered, continue to load