popcorn-js/popcorn.js

521 строка
14 KiB
JavaScript
Исходник Обычный вид История

2010-12-04 23:54:38 +03:00
(function(global) {
// Cache refs to speed up calls to native utils
var
forEach = Array.prototype.forEach,
hasOwn = Object.prototype.hasOwnProperty,
slice = Array.prototype.slice,
// ID string matching
rIdExp = /^(#([\w\-\_\.]+))$/,
// Declare a pseudo-private constructor
// This constructor returns the instance object.
Popcorn = function( entity ) {
2010-12-06 01:37:33 +03:00
// Return new Popcorn object
2010-12-04 23:54:38 +03:00
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 ) {
2010-12-10 02:18:12 +03:00
var elem, matches;
2010-12-04 23:54:38 +03:00
matches = rIdExp.exec( entity );
if ( matches.length && matches[2] ) {
elem = document.getElementById(matches[2]);
}
this.video = elem ? elem : null;
this.data = {
events: {},
2010-12-07 19:15:30 +03:00
tracks: {
2010-12-10 02:18:12 +03:00
byStart: [{start: -1, end: -1}],
byEnd: [{start: -1, end: -1}],
2010-12-07 19:15:30 +03:00
startIndex: 0,
2010-12-10 02:18:12 +03:00
endIndex: 0,
2010-12-07 19:15:30 +03:00
previousUpdateTime: 0
}
2010-12-04 23:54:38 +03:00
};
2010-12-10 02:18:12 +03:00
2010-12-10 19:53:47 +03:00
var isReady = function(that) {
if (that.video.readyState >= 3) {
2010-12-10 02:18:12 +03:00
// adding padding to the front and end of the arrays
// this is so we do not fall off either end
2010-12-10 19:53:47 +03:00
2010-12-10 02:18:12 +03:00
var videoDurationPlus = that.video.duration + 1;
2010-12-10 19:53:47 +03:00
Popcorn.addTrack(that, {start: videoDurationPlus, end: videoDurationPlus});
2010-12-07 19:15:30 +03:00
2010-12-10 02:18:12 +03:00
that.video.addEventListener( "timeupdate", function( event ) {
var currentTime = this.currentTime,
previousTime = that.data.tracks.previousUpdateTime
tracks = that.data.tracks,
tracksByEnd = tracks.byEnd,
tracksByStart = tracks.byStart;
// Playbar advancing
if (previousTime < currentTime) {
while (tracksByEnd[tracks.endIndex].end <= currentTime) {
if (tracksByEnd[tracks.endIndex].running === true) {
tracksByEnd[tracks.endIndex].running = false;
tracksByEnd[tracks.endIndex].natives.end(event, tracksByEnd[tracks.endIndex]);
}
tracks.endIndex++;
}
while (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(event, tracksByStart[tracks.startIndex]);
}
tracks.startIndex++;
}
// Playbar receding
} else if (previousTime > currentTime) {
while (tracksByStart[tracks.startIndex].start > currentTime) {
if (tracksByStart[tracks.startIndex].running === true) {
tracksByStart[tracks.startIndex].running = false;
tracksByStart[tracks.startIndex].natives.end(event, tracksByStart[tracks.startIndex]);
}
tracks.startIndex--;
}
while (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(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.
2010-12-07 19:15:30 +03:00
}
2010-12-10 02:18:12 +03:00
tracks.previousUpdateTime = currentTime;
}, false);
2010-12-07 19:15:30 +03:00
} else {
2010-12-10 22:09:00 +03:00
setTimeout(function() { isReady(that); }, 1);
2010-12-07 19:15:30 +03:00
}
2010-12-10 02:18:12 +03:00
};
2010-12-10 19:53:47 +03:00
isReady(this);
2010-12-10 02:18:12 +03:00
2010-12-04 23:54:38 +03:00
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;
};
2010-12-10 19:53:47 +03:00
Popcorn.addTrack = function( obj, track ) {
console.log(obj);
// Store this definition in an array sorted by times
obj.data.tracks.byStart.push( track );
obj.data.tracks.byEnd.push( track );
obj.data.tracks.byStart.sort( function( a, b ){
return ( a.start - b.start );
});
obj.data.tracks.byEnd.sort( function( a, b ){
return ( a.end - b.end );
});
};
2010-12-04 23:54:38 +03:00
// A Few reusable utils, memoized onto Popcorn
Popcorn.extend( Popcorn, {
2010-12-08 23:12:33 +03:00
error: function( msg ) {
throw msg;
},
2010-12-04 23:54:38 +03:00
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 () {}
2010-12-04 23:54:38 +03:00
});
// 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
2010-12-04 23:54:38 +03:00
Popcorn.extend(Popcorn.p, (function () {
// todo: play, pause, mute should toggle
var methods = "load play pause currentTime playbackRate mute volume duration",
2010-12-04 23:54:38 +03:00
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" ) {
2010-12-04 23:54:38 +03:00
this.video[ name ] = arg;
return this;
}
return this.video[ name ];
};
});
return ret;
})()
);
Popcorn.extend(Popcorn.p, {
// getting properties
roundTime: function () {
return -~this.video.currentTime;
},
2010-12-04 23:54:38 +03:00
2010-12-06 00:07:39 +03:00
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;
},
2010-12-04 23:54:38 +03:00
toTrack: function( setup ) {
/*
{
in: ts,
out: ts,
command: f()
}
*/
// stores the command in a track
}
});
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,
2010-12-04 23:54:38 +03:00
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),
2010-12-04 23:54:38 +03:00
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);
2010-12-04 23:54:38 +03:00
if ( interface ) {
var evt = document.createEvent( interface );
evt.initEvent(type, true, true, window, 1);
2010-12-04 23:54:38 +03:00
this.video.dispatchEvent(evt);
return this;
}
// Custom events
2010-12-04 23:54:38 +03:00
Popcorn.forEach(this.data.events[type], function ( obj, key ) {
obj.call( this, evt, data );
2010-12-04 23:54:38 +03:00
}, this);
2010-12-04 23:54:38 +03:00
}
return this;
2010-12-04 23:54:38 +03:00
},
listen: function ( type, fn ) {
var self = this, hasEvents = true, ns = '';
2010-12-04 23:54:38 +03:00
if ( !this.data.events[type] ) {
this.data.events[type] = {};
hasEvents = false;
}
// Register
2010-12-06 00:07:39 +03:00
this.data.events[type][ fn.name || ( fn.toString() + Popcorn.guid() ) ] = fn;
2010-12-04 23:54:38 +03:00
// only attach one event of any type
if ( !hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) {
2010-12-04 23:54:38 +03:00
this.video.addEventListener( type, function( event ) {
Popcorn.forEach( self.data.events[type], function ( obj, key ) {
2010-12-06 00:07:39 +03:00
if ( typeof obj === "function" ) {
obj.call(self, event);
}
2010-12-04 23:54:38 +03:00
});
//fn.call( self, event );
}, false);
}
return this;
},
2010-12-06 00:07:39 +03:00
unlisten: function( type, fn ) {
if ( this.data.events[type] && this.data.events[type][fn] ) {
this.data.events[type][fn] = null;
return this;
}
2010-12-04 23:54:38 +03:00
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".toLowerCase().split(/\s+/)
};
2010-12-04 23:54:38 +03:00
// 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;
}
2010-12-04 23:54:38 +03:00
// Provides some sugar, but ultimately extends
// the definition into Popcorn.p
var natives = Popcorn.events.all,
2010-12-09 21:49:12 +03:00
2010-12-07 19:15:30 +03:00
reserved = [ "start", "end"],
2010-12-05 19:32:47 +03:00
plugin = {},
pluginFn,
2010-12-05 19:32:47 +03:00
setup;
if ( typeof definition === "object" ) {
setup = definition;
2010-12-07 19:15:30 +03:00
/*if ( !( "timeupdate" in setup ) ) {
setup.timeupdate = Popcorn.nop;
2010-12-07 19:15:30 +03:00
}*/
pluginFn = function ( options ) {
if ( !options ) {
return this;
}
2010-12-07 19:15:30 +03:00
// storing the plugin natives
options.natives = setup;
options.natives.type = name;
options.running = false;
2010-12-05 19:32:47 +03:00
// Checks for expected properties
if ( !( "start" in options ) ) {
options.start = 0;
}
if ( !( "end" in options ) ) {
options.end = this.duration();
}
2010-12-05 19:32:47 +03:00
// If a _setup was declared, then call it before
// the events commence
2010-12-05 04:01:40 +03:00
if ( "_setup" in setup && typeof setup._setup === "function" ) {
setup._setup.call(self, options);
}
2010-12-10 19:53:47 +03:00
Popcorn.addTrack( this, options );
2010-12-05 19:32:47 +03:00
// 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;
};
}
2010-12-04 23:54:38 +03:00
// 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;
2010-12-04 23:54:38 +03:00
// Extend Popcorn.p with new named definition
2010-12-04 23:54:38 +03:00
Popcorn.extend( Popcorn.p, plugin );
// Push into the registry
Popcorn.registry.push(plugin);
2010-12-04 23:54:38 +03:00
return plugin;
2010-12-04 23:54:38 +03:00
};
global.Popcorn = Popcorn;
})(window);