/external/layouts/bookreport/js/popcorn-complete.js
JavaScript | 8646 lines | 5169 code | 1405 blank | 2072 comment | 967 complexity | 33e6a61aeaeac78654bf04862934d33e MD5 | raw file
Possible License(s): Apache-2.0, MIT, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- /*
- * popcorn.js version defaults
- * http://popcornjs.org
- *
- * Copyright 2011, Mozilla Foundation
- * Licensed under the MIT license
- */
- (function(global, document) {
- // Cache refs to speed up calls to native utils
- var
- AP = Array.prototype,
- OP = Object.prototype,
- forEach = AP.forEach,
- slice = AP.slice,
- hasOwn = OP.hasOwnProperty,
- toString = OP.toString,
- // ID string matching
- rIdExp = /^(#([\w\-\_\.]+))$/,
- // Ready fn cache
- readyStack = [],
- readyBound = false,
- readyFired = false,
- // Non-public internal data object
- internal = {
- events: {
- hash: {},
- apis: {}
- }
- },
- // Declare constructor
- // Returns an instance object.
- Popcorn = function( entity, options ) {
- // Return new Popcorn object
- return new Popcorn.p.init( entity, options || null );
- };
- // Instance caching
- Popcorn.instances = [];
- Popcorn.instanceIds = {};
- Popcorn.removeInstance = function( instance ) {
- // If called prior to any instances being created
- // Return early to avoid splicing on nothing
- if ( !Popcorn.instances.length ) {
- return;
- }
- // Remove instance from Popcorn.instances
- Popcorn.instances.splice( Popcorn.instanceIds[ instance.id ], 1 );
- // Delete the instance id key
- delete Popcorn.instanceIds[ instance.id ];
- // Return current modified instances
- return Popcorn.instances;
- };
- // Addes a Popcorn instance to the Popcorn instance array
- Popcorn.addInstance = function( instance ) {
- var instanceLen = Popcorn.instances.length,
- instanceId = instance.media.id && instance.media.id;
- // If the media 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 ||
- "__popcorn" + instanceLen;
- // Create a reference entry for this instance
- Popcorn.instanceIds[ instance.id ] = instanceLen;
- // Add this instance to the cache
- Popcorn.instances.push( instance );
- // Return the current modified instances
- return Popcorn.instances;
- };
- // Request Popcorn object instance by id
- Popcorn.getInstanceById = function( id ) {
- return Popcorn.instances[ Popcorn.instanceIds[ id ] ];
- };
- // Remove Popcorn object instance by id
- Popcorn.removeInstanceById = function( id ) {
- return Popcorn.removeInstance( Popcorn.instances[ Popcorn.instanceIds[ id ] ] );
- };
- // Declare a shortcut (Popcorn.p) to and a definition of
- // the new prototype for our Popcorn constructor
- Popcorn.p = Popcorn.prototype = {
- init: function( entity, options ) {
- var matches;
- // Supports Popcorn(function () { /../ })
- // Originally proposed by Daniel Brooks
- if ( typeof entity === "function" ) {
- // If document ready has already fired
- if ( document.readyState === "interactive" || document.readyState === "complete" ) {
- entity( document, Popcorn );
- return;
- }
- // Add `entity` fn to ready stack
- readyStack.push( entity );
- // This process should happen once per page load
- if ( !readyBound ) {
- // set readyBound flag
- readyBound = true;
- var DOMContentLoaded = function() {
- readyFired = true;
- // Remove global DOM ready listener
- document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
- // Execute all ready function in the stack
- for ( var i = 0, readyStackLength = readyStack.length; i < readyStackLength; i++ ) {
- readyStack[ i ].call( document, Popcorn );
- }
- // GC readyStack
- readyStack = null;
- };
- // Register global DOM ready listener
- document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
- }
- return;
- }
- // Check if entity is a valid string id
- matches = rIdExp.exec( entity );
- // Get media element by id or object reference
- this.media = matches && matches.length && matches[ 2 ] ?
- document.getElementById( matches[ 2 ] ) :
- entity;
- // Create an audio or video element property reference
- this[ ( this.media.nodeName && this.media.nodeName.toLowerCase() ) || "video" ] = this.media;
- // Register new instance
- Popcorn.addInstance( this );
- this.options = options || {};
- this.data = {
- // Allows disabling a plugin per instance
- disabled: [],
- // Stores DOM event queues by type
- events: {},
- // Stores Special event hooks data
- hooks: {},
- // Store track event history data
- history: [],
- // Store track event object references by trackId
- trackRefs: {},
- // Playback track event queues
- trackEvents: {
- byStart: [{
- start: -1,
- end: -1
- }],
- byEnd: [{
- start: -1,
- end: -1
- }],
- startIndex: 0,
- endIndex: 0,
- previousUpdateTime: -1
- }
- };
- // Wrap true ready check
- var isReady = function( that ) {
- if ( that.media.readyState >= 2 ) {
- // Adding padding to the front and end of the arrays
- // this is so we do not fall off either end
- var duration = that.media.duration,
- // Check for no duration info (NaN)
- videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
- Popcorn.addTrackEvent( that, {
- start: videoDurationPlus,
- end: videoDurationPlus
- });
- that.media.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 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;
- tracksByEnd[ tracks.endIndex ]._natives.end.call( that, event, tracksByEnd[ tracks.endIndex ] );
- }
- tracks.endIndex++;
- } else {
- // remove track event
- Popcorn.removeTrackEvent( that, tracksByEnd[ tracks.endIndex ]._id );
- return;
- }
- }
- while ( tracksByStart[ tracks.startIndex ] && tracksByStart[ tracks.startIndex ].start <= currentTime ) {
- // 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 &&
- that.data.disabled.indexOf( tracksByStart[ tracks.startIndex ]._natives.type ) === -1 ) {
- tracksByStart[ tracks.startIndex ]._running = true;
- tracksByStart[ tracks.startIndex ]._natives.start.call( that, event, tracksByStart[ tracks.startIndex ] );
- }
- tracks.startIndex++;
- } else {
- // remove track event
- Popcorn.removeTrackEvent( that, tracksByStart[ tracks.startIndex ]._id );
- return;
- }
- }
- // Playbar receding
- } else if ( previousTime > currentTime ) {
- while ( tracksByStart[ tracks.startIndex ] && tracksByStart[ tracks.startIndex ].start > currentTime ) {
- // 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 ]._running === true ) {
- tracksByStart[ tracks.startIndex ]._running = false;
- tracksByStart[ tracks.startIndex ]._natives.end.call( that, event, tracksByStart[ tracks.startIndex ] );
- }
- tracks.startIndex--;
- } else {
- // remove track event
- Popcorn.removeTrackEvent( that, tracksByStart[ tracks.startIndex ]._id );
- return;
- }
- }
- while ( tracksByEnd[ tracks.endIndex ] && tracksByEnd[ tracks.endIndex ].end > currentTime ) {
- // 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 ].start <= currentTime &&
- tracksByEnd[ tracks.endIndex ]._running === false &&
- that.data.disabled.indexOf( tracksByEnd[ tracks.endIndex ]._natives.type ) === -1 ) {
- tracksByEnd[ tracks.endIndex ]._running = true;
- tracksByEnd[ tracks.endIndex ]._natives.start.call( that, event, tracksByEnd[tracks.endIndex] );
- }
- tracks.endIndex--;
- } else {
- // remove track event
- Popcorn.removeTrackEvent( that, tracksByEnd[ tracks.endIndex ]._id );
- return;
- }
- }
- }
- tracks.previousUpdateTime = currentTime;
- }, false );
- } else {
- global.setTimeout(function() {
- isReady( that );
- }, 1 );
- }
- };
- isReady( this );
- return this;
- }
- };
- // Extend constructor prototype to instance prototype
- // Allows chaining methods to instances
- Popcorn.p.init.prototype = Popcorn.p;
- Popcorn.forEach = function( obj, fn, context ) {
- if ( !obj || !fn ) {
- return {};
- }
- context = context || this;
- var key, len;
- // Use native whenever possible
- if ( forEach && obj.forEach === forEach ) {
- return obj.forEach( fn, context );
- }
- if ( toString.call( obj ) === "[object NodeList]" ) {
- for ( key = 0, len = obj.length; key < len; key++ ) {
- fn.call( context, obj[ key ], key, obj );
- }
- return obj;
- }
- for ( 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;
- };
- // A Few reusable utils, memoized onto Popcorn
- Popcorn.extend( Popcorn, {
- error: function( msg ) {
- throw new Error( msg );
- },
- guid: function( prefix ) {
- Popcorn.guid.counter++;
- return ( prefix ? prefix : "" ) + ( +new Date() + Popcorn.guid.counter );
- },
- sizeOf: function( obj ) {
- var size = 0;
- for ( var prop in obj ) {
- size++;
- }
- return size;
- },
- isArray: Array.isArray || function( array ) {
- return toString.call( array ) === "[object Array]";
- },
- nop: function() {},
- position: function( elem ) {
- var clientRect = elem.getBoundingClientRect(),
- bounds = {},
- doc = elem.ownerDocument,
- docElem = document.documentElement,
- body = document.body,
- clientTop, clientLeft, scrollTop, scrollLeft, top, left;
- // Determine correct clientTop/Left
- clientTop = docElem.clientTop || body.clientTop || 0;
- clientLeft = docElem.clientLeft || body.clientLeft || 0;
- // Determine correct scrollTop/Left
- scrollTop = ( global.pageYOffset && docElem.scrollTop || body.scrollTop );
- scrollLeft = ( global.pageXOffset && docElem.scrollLeft || body.scrollLeft );
- // Temp top/left
- top = Math.ceil( clientRect.top + scrollTop - clientTop );
- left = Math.ceil( clientRect.left + scrollLeft - clientLeft );
- for ( var p in clientRect ) {
- bounds[ p ] = Math.round( clientRect[ p ] );
- }
- return Popcorn.extend({}, bounds, { top: top, left: left });
- },
- disable: function( instance, plugin ) {
- var disabled = instance.data.disabled;
- if ( disabled.indexOf( plugin ) === -1 ) {
- disabled.push( plugin );
- }
- return instance;
- },
- enable: function( instance, plugin ) {
- var disabled = instance.data.disabled,
- index = disabled.indexOf( plugin );
- if ( index > -1 ) {
- disabled.splice( index, 1 );
- }
- return instance;
- }
- });
- // Memoized GUID Counter
- Popcorn.guid.counter = 1;
- // 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() {
- 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.media[ name ] === "function" ) {
- this.media[ name ]();
- return this;
- }
- if ( arg !== false && arg !== null && typeof arg !== "undefined" ) {
- this.media[ name ] = arg;
- return this;
- }
- return this.media[ name ];
- };
- });
- return ret;
- })()
- );
- Popcorn.forEach( "enable disable".split(" "), function( method ) {
- Popcorn.p[ method ] = function( plugin ) {
- return Popcorn[ method ]( this, plugin );
- };
- });
- Popcorn.extend(Popcorn.p, {
- // Rounded currentTime
- roundTime: function() {
- return -~this.media.currentTime;
- },
- // Attach an event to a single point in time
- exec: function( time, fn ) {
- // Creating a one second track event with an empty end
- Popcorn.addTrackEvent( this, {
- start: time,
- end: time + 1,
- _running: false,
- _natives: {
- start: fn || Popcorn.nop,
- end: Popcorn.nop,
- type: "exec"
- }
- });
- return this;
- },
- // Get the client bounding box of an instance element
- position: function() {
- return Popcorn.position( this.media );
- },
- // Toggle a plugin's playback behaviour (on or off) per instance
- toggle: function( plugin ) {
- return Popcorn[ this.data.disabled.indexOf( plugin ) > -1 ? "enable" : "disable" ]( this, plugin );
- },
- // Set default values for plugin options objects per instance
- defaults: function( plugin, defaults ) {
- // If an array of default configurations is provided,
- // iterate and apply each to this instance
- if ( Popcorn.isArray( plugin ) ) {
- Popcorn.forEach( plugin, function( obj ) {
- for ( var name in obj ) {
- this.defaults( name, obj[ name ] );
- }
- }, this );
- return this;
- }
- if ( !this.options.defaults ) {
- this.options.defaults = {};
- }
- if ( !this.options.defaults[ plugin ] ) {
- this.options.defaults[ plugin ] = {};
- }
- Popcorn.extend( this.options.defaults[ plugin ], defaults );
- 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;
- internal.events.apiTypes = [ "UIEvents", "MouseEvents", "Events" ];
- // Privately compile events table at load time
- (function( events, data ) {
- var apis = internal.events.apiTypes,
- eventsList = events.Natives.split( /\s+/g ),
- idx = 0, len = eventsList.length, prop;
- for( ; idx < len; idx++ ) {
- data.hash[ eventsList[idx] ] = true;
- }
- apis.forEach(function( val, idx ) {
- data.apis[ val ] = {};
- var apiEvents = events[ val ].split( /\s+/g ),
- len = apiEvents.length,
- k = 0;
- for ( ; k < len; k++ ) {
- data.apis[ val ][ apiEvents[ k ] ] = true;
- }
- });
- })( Popcorn.Events, internal.events );
- Popcorn.events = {
- isNative: function( type ) {
- return !!internal.events.hash[ type ];
- },
- getInterface: function( type ) {
- if ( !Popcorn.events.isNative( type ) ) {
- return false;
- }
- var eventApi = internal.events,
- apis = eventApi.apiTypes,
- apihash = eventApi.apis,
- idx = 0, len = apis.length, api, tmp;
- for ( ; idx < len; idx++ ) {
- tmp = apis[ idx ];
- if ( apihash[ tmp ][ type ] ) {
- api = tmp;
- break;
- }
- }
- return api;
- },
- // 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 ) {
- var eventInterface, evt;
- // setup checks for custom event system
- if ( this.data.events[ type ] && Popcorn.sizeOf( this.data.events[ type ] ) ) {
- eventInterface = Popcorn.events.getInterface( type );
- if ( eventInterface ) {
- evt = document.createEvent( eventInterface );
- evt.initEvent( type, true, true, global, 1 );
- this.media.dispatchEvent( evt );
- return this;
- }
- // Custom events
- Popcorn.forEach( this.data.events[ type ], function( obj, key ) {
- obj.call( this, data );
- }, this );
- }
- return this;
- },
- listen: function( type, fn ) {
- var self = this,
- hasEvents = true,
- eventHook = Popcorn.events.hooks[ type ],
- origType = type,
- tmp;
- if ( !this.data.events[ type ] ) {
- this.data.events[ type ] = {};
- hasEvents = false;
- }
- // Check and setup event hooks
- if ( eventHook ) {
- // Execute hook add method if defined
- if ( eventHook.add ) {
- eventHook.add.call( this, {}, fn );
- }
- // Reassign event type to our piggyback event type if defined
- if ( eventHook.bind ) {
- type = eventHook.bind;
- }
- // Reassign handler if defined
- if ( eventHook.handler ) {
- tmp = fn;
- fn = function wrapper( event ) {
- eventHook.handler.call( self, event, tmp );
- };
- }
- // assume the piggy back event is registered
- hasEvents = true;
- // Setup event registry entry
- if ( !this.data.events[ type ] ) {
- this.data.events[ type ] = {};
- // Toggle if the previous assumption was untrue
- hasEvents = false;
- }
- }
- // Register event and handler
- 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.media.addEventListener( type, function( event ) {
- Popcorn.forEach( self.data.events[ type ], function( obj, key ) {
- if ( typeof obj === "function" ) {
- obj.call( self, event );
- }
- });
- }, false);
- }
- return this;
- },
- unlisten: function( type, fn ) {
- if ( this.data.events[ type ] && this.data.events[ type ][ fn ] ) {
- delete this.data.events[ type ][ fn ];
- return this;
- }
- this.data.events[ type ] = null;
- return this;
- }
- },
- hooks: {
- canplayall: {
- bind: "canplaythrough",
- add: function( event, callback ) {
- var state = false;
- if ( this.media.readyState ) {
- callback.call( this, event );
- state = true;
- }
- this.data.hooks.canplayall = {
- fired: state
- };
- },
- // declare special handling instructions
- handler: function canplayall( event, callback ) {
- if ( !this.data.hooks.canplayall.fired ) {
- // trigger original user callback once
- callback.call( this, event );
- this.data.hooks.canplayall.fired = true;
- }
- }
- }
- }
- };
- // 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 ];
- });
- // Protected API methods
- Popcorn.protect = {
- natives: "load play pause currentTime playbackRate mute volume duration removePlugin roundTime trigger listen unlisten exec".toLowerCase().split( /\s+/ )
- };
- // Internal Only - Adds track events to the instance object
- Popcorn.addTrackEvent = function( obj, track ) {
- // Determine if this track has default options set for it
- // If so, apply them to the track object
- if ( track && track._natives && track._natives.type &&
- ( obj.options.defaults && obj.options.defaults[ track._natives.type ] ) ) {
- track = Popcorn.extend( {}, obj.options.defaults[ track._natives.type ], track );
- }
- if ( track._natives ) {
- // Supports user defined track event id
- track._id = !track.id ? Popcorn.guid( track._natives.type ) : track.id;
- // Push track event ids into the history
- obj.data.history.push( track._id );
- }
- track.start = Popcorn.util.toSeconds( track.start, obj.options.framerate );
- track.end = Popcorn.util.toSeconds( track.end, obj.options.framerate );
- // Store this definition in an array sorted by times
- var byStart = obj.data.trackEvents.byStart,
- byEnd = obj.data.trackEvents.byEnd,
- idx;
- for ( idx = byStart.length - 1; idx >= 0; idx-- ) {
- if ( track.start >= byStart[ idx ].start ) {
- byStart.splice( idx + 1, 0, track );
- break;
- }
- }
- for ( idx = byEnd.length - 1; idx >= 0; idx-- ) {
- if ( track.end > byEnd[ idx ].end ) {
- byEnd.splice( idx + 1, 0, track );
- break;
- }
- }
- // Store references to user added trackevents in ref table
- if ( track._id ) {
- Popcorn.addTrackEvent.ref( obj, track );
- }
- };
- // Internal Only - Adds track event references to the instance object's trackRefs hash table
- Popcorn.addTrackEvent.ref = function( obj, track ) {
- obj.data.trackRefs[ track._id ] = track;
- return obj;
- };
- Popcorn.removeTrackEvent = function( obj, trackId ) {
- var historyLen = obj.data.history.length,
- indexWasAt = 0,
- byStart = [],
- byEnd = [],
- history = [];
- 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] );
- byEnd.push( obj.data.trackEvents.byEnd[i] );
- }
- // Filter for user track events (vs system track events)
- if ( o._id ) {
- // Filter for the trackevent to remove
- if ( o._id !== trackId ) {
- byStart.push( obj.data.trackEvents.byStart[i] );
- byEnd.push( obj.data.trackEvents.byEnd[i] );
- }
- // Capture the position of the track being removed.
- if ( o._id === trackId ) {
- indexWasAt = i;
- o._natives._teardown && o._natives._teardown.call( obj, o );
- }
- }
- });
- // Update
- if ( indexWasAt <= obj.data.trackEvents.startIndex ) {
- obj.data.trackEvents.startIndex--;
- }
- if ( indexWasAt <= obj.data.trackEvents.endIndex ) {
- obj.data.trackEvents.endIndex--;
- }
- obj.data.trackEvents.byStart = byStart;
- obj.data.trackEvents.byEnd = byEnd;
- for ( var i = 0; i < historyLen; i++ ) {
- if ( obj.data.history[ i ] !== trackId ) {
- history.push( obj.data.history[ i ] );
- }
- }
- // Update ordered history array
- obj.data.history = history;
- // Update track event references
- Popcorn.removeTrackEvent.ref( obj, trackId );
- };
- // Internal Only - Removes track event references from instance object's trackRefs hash table
- Popcorn.removeTrackEvent.ref = function( obj, trackId ) {
- delete obj.data.trackRefs[ trackId ];
- return obj;
- };
- // Return an array of track events bound to this instance object
- Popcorn.getTrackEvents = function( obj ) {
- var trackevents = [],
- refs = obj.data.trackEvents.byStart,
- length = refs.length,
- idx = 0,
- ref;
- for ( ; idx < length; idx++ ) {
- ref = refs[ idx ];
- // Return only user attributed track event references
- if ( ref._id ) {
- trackevents.push( ref );
- }
- }
- return trackevents;
- };
- // Internal Only - Returns an instance object's trackRefs hash table
- Popcorn.getTrackEvents.ref = function( obj ) {
- return obj.data.trackRefs;
- };
- // Return a single track event bound to this instance object
- Popcorn.getTrackEvent = function( obj, trackId ) {
- return obj.data.trackRefs[ trackId ];
- };
- // Internal Only - Returns an instance object's track reference by track id
- Popcorn.getTrackEvent.ref = function( obj, trackId ) {
- return obj.data.trackRefs[ trackId ];
- };
- Popcorn.getLastTrackEventId = function( obj ) {
- return obj.data.history[ obj.data.history.length - 1 ];
- };
- // Map and Extend TrackEvent functions to all Popcorn instances
- Popcorn.extend( Popcorn.p, {
- getTrackEvents: function() {
- return Popcorn.getTrackEvents.call( null, this );
- },
- getTrackEvent: function( id ) {
- return Popcorn.getTrackEvent.call( null, this, id );
- },
- getLastTrackEventId: function() {
- return Popcorn.getLastTrackEventId.call( null, this );
- },
- removeTrackEvent: function( id ) {
- Popcorn.removeTrackEvent.call( null, this, id );
- return this;
- },
- removePlugin: function( name ) {
- Popcorn.removePlugin.call( null, this, name );
- return this;
- }
- });
- // Plugin manifests
- Popcorn.manifest = {};
- // Plugins are registered
- Popcorn.registry = [];
- Popcorn.registryByName = {};
- // An interface for extending Popcorn
- // with plugin functionality
- Popcorn.plugin = function( name, definition, manifest ) {
- 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 reserved = [ "start", "end" ],
- plugin = {},
- setup,
- isfn = typeof definition === "function",
- methods = [ "_setup", "_teardown", "start", "end" ];
- // combines calls of two function calls into one
- var combineFn = function( first, second ) {
- first = first || Popcorn.nop;
- second = second || Popcorn.nop;
- return function() {
- first.apply( this, arguments );
- second.apply( this, arguments );
- };
- };
- // If `manifest` arg is undefined, check for manifest within the `definition` object
- // If no `definition.manifest`, an empty object is a sufficient fallback
- if ( !manifest ) {
- manifest = definition.manifest || {};
- }
- // apply safe, and empty default functions
- methods.forEach(function( method ) {
- definition[ method ] = definition[ method ] || Popcorn.nop;
- });
- var pluginFn = function( setup, options ) {
- if ( !options ) {
- return this;
- }
- // Storing the plugin natives
- var natives = options._natives = {},
- compose = "",
- defaults, originalOpts, manifestOpts, mergedSetupOpts;
- Popcorn.extend( natives, setup );
- options._natives.type = name;
- options._running = false;
- // Check for previously set default options
- defaults = this.options.defaults && this.options.defaults[ options._natives && options._natives.type ];
- // default to an empty string if no effect exists
- // split string into an array of effects
- options.compose = options.compose && options.compose.split( " " ) || [];
- options.effect = options.effect && options.effect.split( " " ) || [];
- // join the two arrays together
- options.compose = options.compose.concat( options.effect );
- options.compose.forEach(function( composeOption ) {
- // if the requested compose is garbage, throw it away
- compose = Popcorn.compositions[ composeOption ] || {};
- // extends previous functions with compose function
- methods.forEach(function( method ) {
- natives[ method ] = combineFn( natives[ method ], compose[ method ] );
- });
- });
- // Ensure a manifest object, an empty object is a sufficient fallback
- options._natives.manifest = manifest;
- // Checks for expected properties
- if ( !( "start" in options ) ) {
- options.start = 0;
- }
- if ( !( "end" in options ) ) {
- options.end = this.duration() || Number.MAX_VALUE;
- }
- // Merge with defaults if they exist, make sure per call is prioritized
- mergedSetupOpts = defaults ? Popcorn.extend( {}, defaults, options ) :
- options;
- // Resolves 239, 241, 242
- if ( !mergedSetupOpts.target ) {
- // Sometimes the manifest may be missing entirely
- // or it has an options object that doesn't have a `target` property
- manifestOpts = "options" in manifest && manifest.options;
- mergedSetupOpts.target = manifestOpts && "target" in manifestOpts && manifestOpts.target;
- }
- // Trigger _setup method if exists
- options._natives._setup && options._natives._setup.call( this, mergedSetupOpts );
- // Create new track event for this instance
- Popcorn.addTrackEvent( this, Popcorn.extend( mergedSetupOpts, options ) );
- // Future support for plugin event definitions
- // for all of the native events
- Popcorn.forEach( setup, function( callback, type ) {
- if ( type !== "type" ) {
- if ( reserved.indexOf( type ) === -1 ) {
- this.listen( type, callback );
- }
- }
- }, this );
- return this;
- };
- // Augment the manifest object
- if ( manifest || ( "manifest" in definition ) ) {
- Popcorn.manifest[ name ] = manifest || definition.manifest;
- }
- // Assign new named definition
- plugin[ name ] = function( 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
- var entry = {
- fn: plugin[ name ],
- definition: definition,
- base: definition,
- parents: [],
- name: name
- };
- Popcorn.registry.push(
- Popcorn.extend( plugin, entry, {
- type: name
- })
- );
- Popcorn.registryByName[ name ] = entry;
- return plugin;
- };
- Popcorn.plugin.debug = false;
- // 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
- if ( !name ) {
- // Fix the order
- name = obj;
- obj = Popcorn.p;
- if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
- Popcorn.error( "'" + name + "' is a protected function name" );
- return;
- }
- var registryLen = Popcorn.registry.length,
- registryIdx;
- // remove plugin reference from registry
- for ( registryIdx = 0; registryIdx < registryLen; registryIdx++ ) {
- if ( Popcorn.registry[ registryIdx ].name === name ) {
- Popcorn.registry.splice( registryIdx, 1 );
- delete Popcorn.registryByName[ name ];
- // delete the plugin
- delete obj[ name ];
- // plugin found and removed, stop checking, we are done
- return;
- }
- }
- }
- var byStart = obj.data.trackEvents.byStart,
- byEnd = obj.data.trackEvents.byEnd,
- idx, sl;
- // remove all trackEvents
- for ( idx = 0, sl = byStart.length; idx < sl; idx++ ) {
- if ( ( byStart[ idx ] && byStart[ idx ]._natives && byStart[ idx ]._natives.type === name ) &&
- ( byEnd[ idx ] && byEnd[ idx ]._natives && byEnd[ idx ]._natives.type === name ) ) {
- byStart[ idx ]._natives._teardown && byStart[ idx ]._natives._teardown.call( obj, byStart[ idx ] );
- byStart.splice( idx, 1 );
- byEnd.splice( idx, 1 );
- // update for loop if something removed, but keep checking
- idx--; sl--;
- if ( obj.data.trackEvents.startIndex <= idx ) {
- obj.data.trackEvents.startIndex--;
- obj.data.trackEvents.endIndex--;
- }
- }
- }
- };
- Popcorn.compositions = {};
- // Plugin inheritance
- Popcorn.compose = function( name, definition, manifest ) {
- // If `manifest` arg is undefined, check for manifest within the `definition` object
- // If no `definition.manifest`, an empty object is a sufficient fallback
- Popcorn.manifest[ name ] = manifest = manifest || definition.manifest || {};
- // register the effect by name
- Popcorn.compositions[ name ] = definition;
- };
- Popcorn.plugin.effect = Popcorn.effect = Popcorn.compose;
- // stores parsers keyed on filetype
- Popcorn.parsers = {};
- // An interface for extending Popcorn
- // with parser functionality
- Popcorn.parser = function( name, type, definition ) {
- if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
- Popcorn.error( "'" + name + "' is a protected function name" );
- return;
- }
- // fixes parameters for overloaded function call
- if ( typeof type === "function" && !definition ) {
- definition = type;
- type = "";
- }
- if ( typeof definition !== "function" || typeof type !== "string" ) {
- return;
- }
- // Provides some sugar, but ultimately extends
- // the definition into Popcorn.p
- var natives = Popcorn.events.all,
- parseFn,
- parser = {};
- parseFn = function( filename, callback ) {
- if ( !filename ) {
- return this;
- }
- var that = this;
- Popcorn.xhr({
- url: filename,
- dataType: type,
- success: function( data ) {
- var tracksObject = definition( data ),
- tracksData,
- tracksDataLen,
- tracksDef,
- idx = 0;
- tracksData = tracksObject.data || [];
- tracksDataLen = tracksData.length;
- tracksDef = null;
- // If no tracks to process, return immediately
- if ( !tracksDataLen ) {
- return;
- }
- // Create tracks out of parsed object
- for ( ; idx < tracksDataLen; idx++ ) {
- tracksDef = tracksData[ idx ];
- for ( var key in tracksDef ) {
- if ( hasOwn.call( tracksDef, key ) && !!that[ key ] ) {
- that[ key ]( tracksDef[ key ] );
- }
- }
- }
- if ( callback ) {
- callback();
- }
- }
- });
- return this;
- };
- // Assign new named definition
- parser[ name ] = parseFn;
- // Extend Popcorn.p with new named definition
- Popcorn.extend( Popcorn.p, parser );
- // keys the function name by filetype extension
- //Popcorn.parsers[ name ] = true;
- return parser;
- };
- // Cache references to reused RegExps
- var rparams = /\?/,
- // XHR Setup object
- setup = {
- url: "",
- data: "",
- dataType: "",
- success: Popcorn.nop,
- type: "GET",
- async: true,
- xhr: function() {
- return new global.XMLHttpRequest();
- }
- };
- Popcorn.xhr = function( options ) {
- options.dataType = options.dataType && options.dataType.toLowerCase() || null;
- if ( options.dataType &&
- ( options.dataType === "jsonp" || options.dataType === "script" ) ) {
- Popcorn.xhr.getJSONP(
- options.url,
- options.success,
- options.dataType === "script"
- );
- return;
- }
- var settings = Popcorn.extend( {}, setup, options );
- // Create new XMLHttpRequest object
- settings.ajax = settings.xhr();
- if ( settings.ajax ) {
- if ( settings.type === "GET" && settings.data ) {
- // append query string
- settings.url += ( rparams.test( settings.url ) ? "&" : "?" ) + settings.data;
- // Garbage collect and reset settings.data
- settings.data = null;
- }
- settings.ajax.open( settings.type, settings.url, settings.async );
- settings.ajax.send( settings.data || null );
- return Popcorn.xhr.httpData( settings );
- }
- };
- Popcorn.xhr.httpData = function( settings ) {
- var data, json = null;
- settings.ajax.onreadystatechange = function() {
- if ( settings.ajax.readyState === 4 ) {
- try {
- json = JSON.parse( settings.ajax.responseText );
- } catch( e ) {
- //suppress
- }
- data = {
- xml: settings.ajax.responseXML,
- text: settings.ajax.responseText,
- json: json
- };
- // If a dataType was specified, return that type of data
- if ( settings.dataType ) {
- data = data[ settings.dataType ];
- }
- settings.success.call( settings.ajax, data );
- }
- };
- return data;
- };
- Popcorn.xhr.getJSONP = function( url, success, isScript ) {
- // If this is a script request, ensure that we do not call something that has already been loaded
- if ( isScript ) {
- var scripts = document.querySelectorAll( "script[src=\"" + url + "\"]" );
- // If there are scripts with this url loaded, early return
- if ( scripts.length ) {
- // Execute success callback and pass "exists" flag
- success && success( true );
- return;
- }
- }
- var head = document.head || document.getElementsByTagName( "head" )[ 0 ] || document.documentElement,
- script = document.createElement( "script" ),
- paramStr = url.split( "?" )[ 1 ],
- isFired = false,
- params = [],
- callback, parts, callparam;
- if ( paramStr && !isScript ) {
- params = paramStr.split( "&" );
- }
- if ( params.length ) {
- parts = params[ params.length - 1 ].split( "=" );
- }
- 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 ( !!window[ callback ] ) {
- // Create a new unique callback name
- callback = Popcorn.guid( callback );
- }
- // Define the JSONP success callback globally
- window[ callback ] = function( data ) {
- success && success( data );
- isFired = true;
- };
- // Replace callback param and callback name
- url = url.replace( parts.join( "=" ), parts[ 0 ] + "=" + callback );
- }
- script.onload = script.onreadystatechange = function() {
- if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
- // Handling remote script loading callbacks
- if ( isScript ) {
- // getScript
- success && success();
- }
- // Executing for JSONP requests
- if ( isFired ) {
- // Garbage collect the callback
- delete window[ callback ];
- // Garbage collect the script resource
- head.removeChild( script );
- }
- }
- };
- script.src = url;
- 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 );
- };
- Popcorn.util = {
- // Simple function to parse a timestamp into seconds
- // Acceptable formats are:
- // HH:MM:SS.MMM
- // HH:MM:SS;FF
- // Hours and minutes are optional. They default to 0
- toSeconds: function( timeStr, framerate ) {
- //Hours and minutes are optional
- //Seconds must be specified
- //Seconds can be followed by milliseconds OR by the frame information
- var validTimeFormat = /^([0-9]+:){0,2}[0-9]+([.;][0-9]+)?$/,
- errorMessage = "Invalid time format";
- if ( typeof timeStr === "number" ) {
- return timeStr;
- } else if ( typeof timeStr === "string" ) {
- if ( ! validTimeFormat.test( timeStr ) ) {
- Popcorn.error( errorMessage );
- }
- } else {
- Popcorn.error( errorMessage );
- }
- var t = timeStr.split( ":" ),
- lastIndex = t.length - 1,
- lastElement = t[ lastIndex ];
- //Fix last element:
- if ( lastElement.indexOf( ";" ) > -1 ) {
- var frameInfo = lastElement.split( ";" ),
- frameTime = 0;
- if ( framerate && ( typeof framerate === "number" ) ) {
- frameTime = parseFloat( frameInfo[ 1 ], 10 ) / framerate;
- }
- t[ lastIndex ] =
- parseInt( frameInfo[ 0 ], 10 ) + frameTime;
- }
- if ( t.length === 1 ) {
- return parseFloat( t[ 0 ], 10 );
- } else if ( t.length === 2 ) {
- return ( parseInt( t[ 0 ], 10 ) * 60 ) + parseFloat( t[ 1 ], 10 );
- } else if ( t.length === 3 ) {
- return ( parseInt( t[ 0 ], 10 ) * 3600 ) +
- ( parseInt( t[ 1 ], 10 ) * 60 ) +
- parseFloat( t[ 2 ], 10 );
- }
- }
- };
- // Exposes Popcorn to global context
- global.Popcorn = Popcorn;
- document.addEventListener( "DOMContentLoaded", function() {
- // Supports non-specific elements
- var dataAttr = "data-timeline-sources",
- medias = document.querySelectorAll( "[" + dataAttr + "]" );
- Popcorn.forEach( medias, function( idx, key ) {
- var media = medias[ key ],
- hasDataSources = false,
- dataSources, data, popcornMedia;
- // Ensure that the DOM has an id
- if ( !media.id ) {
- media.id = Popcorn.guid( "__popcorn" );
- }
- // Ensure we're looking at a dom node
- if ( media.nodeType && media.nodeType === 1 ) {
- popcornMedia = Popcorn( "#" + media.id );
- dataSources = ( media.getAttribute( dataAttr ) || "" ).split( "," );
- if ( dataSources[ 0 ] ) {
- Popcorn.forEach( dataSources, function( source ) {
- // split the parser and data as parser!file
- data = source.split( "!" );
- // if no parser is defined for the file, assume "parse" + file extension
- if ( data.length === 1 ) {
- data = source.split( "." );
- data[ 0 ] = "parse" + data[ data.length - 1 ].toUpperCase();
- data[ 1 ] = source;
- }
- // If the media has data sources and the correct parser is registered, continue to load
- if ( dataSources[ 0 ] && popcornMedia[ data[ 0 ] ] ) {
- // Set up the media and load in the datasources
- popcornMedia[ data[ 0 ] ]( data[ 1 ] );
- }
- });
- }
- // Only play the media if it was specified to do so
- if ( !!popcornMedia.autoplay ) {
- popcornMedia.play();
- }
- }
- });
- }, false );
- })(window, window.document);
- // PLUGIN: Mustache
- (function (Popcorn) {
- /**
- * Mustache Popcorn Plug-in
- *
- * Adds the ability to render JSON using templates via the Mustache templating library.
- *
- * @param {Object} options
- *
- * Required parameters: start, end, template, data, and target.
- * Optional parameter: static.
- *
- * start: the time in seconds when the mustache template should be rendered
- * in the target div.
- *
- * end: the time in seconds when the rendered mustache template should be
- * removed from the target div.
- *
- * target: a String -- the target div's id.
- *
- * template: the mustache template for the plugin to use when rendering. This can be
- * a String containing the template, or a Function that returns the template's
- * String.
- *
- * data: the data to be rendered using the mustache template. This can be a JSON String,
- * a JavaScript Object literal, or a Function returning a String or Literal.
- *
- * dynamic: an optional argument indicating that the template and json data are dynamic
- * and need to be loaded dynamically on every use. Defaults to True.
- *
- * Example:
- var p = Popcorn('#video')
- // Example using template and JSON strings.
- .mustache({
- start: 5, // seconds
- end: 15, // seconds
- target: 'mustache',
- template: '<h1>{{header}}</h1>' +
- '{{#bug}}' +
- '{{/bug}}' +
- '' +
- '{{#items}}' +
- ' {{#first}}' +
- ' <li><strong>{{name}}</strong></li>' +
- ' {{/first}}' +
- ' {{#link}}' +
- ' <li><a href="{{url}}">{{name}}</a></li>' +
- ' {{/link}}' +
- '{{/items}}' +
- '' +
- '{{#empty}}' +
- ' <p>The list is empty.</p>' +
- '{{/empty}}' ,
- data: '{' +
- ' "header": "Colors", ' +
- ' "items": [ ' +
- ' {"name": "red", "first": true, "url": "#Red"}, ' +
- ' {"name": "green", "link": true, "url": "#Green"}, ' +
- ' {"name": "blue", "link": true, "url": "#Blue"} ' +
- ' ],' +
- ' 'empty': false' +
- '}',
- dynamic: false // The json is not going to change, load it early.
- } )
- // Example showing Functions instead of Strings.
- .mustache({
- start: 20, // seconds
- end: 25, // seconds
- target: 'mustache',
- template: function(instance, options) {
- var template = // load your template file here...
- return template;
- },
- data: function(instance, options) {
- var json = // load your json here...
- return json;
- }
- } );
- *
- */
- Popcorn.plugin( 'mustache' , function( options ) {
- var getData, data, getTemplate, template;
- Popcorn.getScript('https://github.com/janl/mustache.js/raw/master/mustache.js');
- var shouldReload = !!options.dynamic,
- typeOfTemplate = typeof options.template,
- typeOfData = typeof options.data,
- target = document.getElementById( options.target );
- if ( !target && Popcorn.plugin.debug ) {
- throw new Error( "target container doesn't exist" );
- }
- options.container = target || document.createElement( "div" );
- if ( typeOfTemplate === 'function' ) {
- if ( !shouldReload ) {
- template = options.template( options );
- } else {
- getTemplate = options.template;
- }
- } else if ( typeOfTemplate === 'string' ) {
- template = options.template;
- } else if ( Popcorn.plugin.debug ) {
- throw new Error( 'Mustache Plugin Error: options.template must be a String or a Function.' );
- } else {
- …
Large files files are truncated, but you can click here to view the full file