From: rwldrn Date: Sat, 4 Dec 2010 20:54:38 +0000 (-0500) Subject: Renames popcorn-api-draft.js X-Git-Tag: v0.2~72 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=499dace533aae418ea8e122d0e93978a2ebbdeac;p=popcorn-js.git Renames popcorn-api-draft.js --- diff --git a/popcorn.js b/popcorn.js new file mode 100644 index 0000000..41eab06 --- /dev/null +++ b/popcorn.js @@ -0,0 +1,268 @@ +// Immediately invoked function expression is used to +// create a pseudo-private scope for our api definition +// a reference to the `window` object is passed into the +// closure and referenced as `global`. This is both +// beneficial for compressors and provides a fast +// reference to the window context +(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 ) { + // Return new instance of + // Popcorn.prototype.instance constructor + 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: {}, + tracks: [] + }; + + 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; + }; + + // A Few reusable utils, memoized onto Popcorn + Popcorn.extend( Popcorn, { + guid: function() { + return +new Date() + Math.floor(Math.random()*11); + }, + sizeOf: function ( obj ) { + var size = 0; + + for ( var prop in obj ) { + size++; + } + + return size; + } + }); + + // Simple Factory pattern to implement native + // 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", + 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 ) { + this.video[ name ] = arg; + + return this; + } + + return this.video[ name ]; + }; + }); + + return ret; + + })() + ); + + Popcorn.extend(Popcorn.p, { + + setCommand: function() { + + }, + getCommand: function() { + + }, + toTrack: function( setup ) { + /* + { + in: ts, + out: ts, + command: f() + } + */ + // stores the command in a track + } + + }); + + var nativeEvents = "loadstart progress suspend emptied stalled play pause " + + "loadedmetadata loadeddata waiting playing canplay canplaythrough " + + "seeking seeked timeupdate ended ratechange durationchange volumechange"; + + Popcorn.events = { + //todo, fix types to be custom, natives will be native only + types: nativeEvents.split(/\s+/g), + natives: nativeEvents.split(/\s+/g), + custom: [ "frame" ], + all: [ "frame" ].concat( nativeEvents.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 evt = document.createEvent("Events"); + evt.initEvent(type, true, true, window, 1); + + this.video.dispatchEvent(evt); + + /* + + TODO: implement some form of custom event system + Popcorn.forEach(this.data.events[type], function ( obj, key ) { + + //obj.call(_target, _data); + + // update to dispatch an event + + }); + */ + } + }, + listen: function ( type, fn ) { + + var self = this, hasEvents = true; + + if ( !this.data.events[type] ) { + this.data.events[type] = {}; + hasEvents = false; + } + + // Register + this.data.events[type][ 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 ) { + obj.call(self, event); + }); + + //fn.call( self, event ); + + }, false); + } + return this; + }, + unlisten: function( type ) { + 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]; + }); + + // Plugins are registered + Popcorn.registry = []; + // An interface for extending Popcorn + // with plugin functionality + Popcorn.plugin = function( name, definition ) { + + // Provides some sugar, but ultimately extends + // the definition into Popcorn.p + + var plugin = {}; + + plugin[ name ] = definition; + + Popcorn.extend( Popcorn.p, plugin ); + + Popcorn.registry.push({ + name: name, + plugin: plugin + }); + + + // within the context of a plugin + // any of the events can be listened to + }; + + + global.Popcorn = Popcorn; + +})(window); diff --git a/test/demo.html b/test/demo.html index 156d7f6..ce2b7dd 100644 --- a/test/demo.html +++ b/test/demo.html @@ -4,7 +4,7 @@ Popcorn API Draft DEMO - + diff --git a/test/index.html b/test/index.html index fb041a2..77f12bc 100644 --- a/test/index.html +++ b/test/index.html @@ -11,7 +11,7 @@ --> - + diff --git a/test/popcorn-api-draft-unit.js b/test/popcorn-api-draft-unit.js index ba34b63..6cc7709 100644 --- a/test/popcorn-api-draft-unit.js +++ b/test/popcorn-api-draft-unit.js @@ -1,5 +1,3 @@ - - test("Popcorn API", function () { var expects = 3, @@ -7,9 +5,6 @@ test("Popcorn API", function () { expect(expects); - - - function plus(){ if ( ++count == expects ) start(); } stop(); @@ -44,7 +39,6 @@ test("Popcorn Utility", function () { equals( typeof Popcorn.forEach, "function" , "Popcorn.forEach is a provided utility function"); equals( typeof Popcorn.extend, "function" , "Popcorn.extend is a provided utility function"); - });