From: ScottDowne Date: Tue, 7 Dec 2010 16:15:30 +0000 (-0500) Subject: initial updater logic X-Git-Tag: v0.2~52^2~16^2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=55df7f67143d1737d7f6b953c9b1e42e036a8340;p=popcorn-js.git initial updater logic --- diff --git a/plugins/image/popcorn.image.html b/plugins/image/popcorn.image.html new file mode 100644 index 0000000..d52755a --- /dev/null +++ b/plugins/image/popcorn.image.html @@ -0,0 +1,50 @@ + + + + Popcorn API Draft DEMO + + + + + + + +

Popcorn Image Demo

+ + + +
+ + + diff --git a/plugins/image/popcorn.image.js b/plugins/image/popcorn.image.js new file mode 100644 index 0000000..632dbc3 --- /dev/null +++ b/plugins/image/popcorn.image.js @@ -0,0 +1,41 @@ +// EXAMPLE PLUGIN: IMAGE + +(function (Popcorn) { + + Popcorn.plugin( "image" , (function(){ + + var container, img, src, title, alt, clickListener, loadListener + + return { + + start: function(event, options){ + + container = document.getElementById(options.target); + img = new Image(); + + loadListener = function(){ + this.alt = options.alt; + this.title = options.title; + + clickListener = function(){ options.click.call( img ); }; + this.addEventListener('click', function(){ clickListener(); }, false); + + container.appendChild( img ); + }; + + img.addEventListener('load', function(){ loadListener.call( img ) }, false); + + img.src = options.src; + }, + + end: function(event, options){ + img.removeEventListener( 'load', loadListener, false ); + img.removeEventListener( 'click', clickListener, false ); + container.removeChild( img ); + } + + }; + + })()); + +})( Popcorn ); diff --git a/popcorn.js b/popcorn.js index b6a4587..36e2b66 100644 --- a/popcorn.js +++ b/popcorn.js @@ -22,7 +22,7 @@ init: function( entity ) { - var elem, matches; + var elem, matches, that = this; matches = rIdExp.exec( entity ); @@ -34,8 +34,75 @@ this.data = { events: {}, - tracks: [] + tracks: { + byStart: [], + byEnd: [], + startIndex: 0, + endIndex: 0, + previousUpdateTime: 0 + } }; + + // adding padding to the front and end of the arrays + // this is so we do not fall off either end + this.data.tracks.byStart.push( {start: -1, end: -1} ); + this.data.tracks.byEnd.push( {start: -1, end: -1} ); + this.data.tracks.byStart.push( {start: 9999, end: 9999} ); + this.data.tracks.byEnd.push( {start: 9999, end: 9999} ); + + this.video.addEventListener( "timeupdate", function( event ) { + + var currentTime = this.currentTime, + previousTime = that.data.tracks.previousUpdateTime, + tracksByEnd = that.data.tracks.byEnd, + tracksByStart = that.data.tracks.byStart; + + // Playbar advancing + if (previousTime < currentTime) { + + while (tracksByEnd[that.data.tracks.endIndex].end <= currentTime) { + if (tracksByEnd[that.data.tracks.endIndex].running === true) { + tracksByEnd[that.data.tracks.endIndex].running = false; + tracksByEnd[that.data.tracks.endIndex].natives.end(event, tracksByEnd[that.data.tracks.endIndex]); + } + that.data.tracks.endIndex++; + } + + while (tracksByStart[that.data.tracks.startIndex].start <= currentTime) { + if (tracksByStart[that.data.tracks.startIndex].end > currentTime && tracksByStart[that.data.tracks.startIndex].running === false) { + tracksByStart[that.data.tracks.startIndex].running = true; + tracksByStart[that.data.tracks.startIndex].natives.start(event, tracksByStart[that.data.tracks.startIndex]); + } + that.data.tracks.startIndex++; + } + + // Playbar receding + } else if (previousTime > currentTime) { + + while (tracksByStart[that.data.tracks.startIndex].start > currentTime) { + if (tracksByStart[that.data.tracks.startIndex].running === true) { + tracksByStart[that.data.tracks.startIndex].running = false; + tracksByStart[that.data.tracks.startIndex].natives.end(event, tracksByStart[that.data.tracks.startIndex]); + } + that.data.tracks.startIndex--; + } + + while (tracksByEnd[that.data.tracks.endIndex].end > currentTime) { + if (tracksByEnd[that.data.tracks.endIndex].start <= currentTime && tracksByEnd[that.data.tracks.endIndex].running === false) { + tracksByEnd[that.data.tracks.endIndex].running = true; + tracksByEnd[that.data.tracks.endIndex].natives.start(event, tracksByEnd[that.data.tracks.endIndex]); + } + that.data.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. + } + + that.data.tracks.previousUpdateTime = currentTime; + }, false); return this; } @@ -324,7 +391,7 @@ // the definition into Popcorn.p var natives = Popcorn.events.all, - reserved = [ "start", "end", "timeupdate" ], + reserved = [ "start", "end"], plugin = {}, setup; @@ -334,22 +401,21 @@ setup = definition; - if ( !( "timeupdate" in setup ) ) { + /*if ( !( "timeupdate" in setup ) ) { setup.timeupdate = Popcorn.nop; - } + }*/ definition = function ( options ) { - - var self = this, - fired = { - start: 0, - end: 0 - }; - + 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; @@ -366,30 +432,14 @@ setup._setup.call(self, options); } - // Plugin timeline handler - this.listen( "timeupdate", function( event ) { - - - if ( ~~self.currentTime() === options.start || - self.currentTime() === options.start ) { - - !fired.start && setup.start.call(self, event, options); - fired.start++; - } - - if ( self.currentTime() > options.start && - self.currentTime() < options.end ) { - - setup.timeupdate.call(self, event, options); - } - - if ( ~~self.currentTime() === options.end || - self.currentTime() === options.end ) { - - !fired.end && setup.end.call(self, event, options); - fired.end++; - } - + // Store this definition in an array sorted by times + this.data.tracks.byStart.push( options ); + this.data.tracks.byEnd.push( options ); + this.data.tracks.byStart.sort( function( a, b ){ + return ( a.start - b.start ); + }); + this.data.tracks.byEnd.sort( function( a, b ){ + return ( a.end - b.end ); }); // Future support for plugin event definitions diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 8b3d31e..0d2ddf6 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -65,7 +65,7 @@ test("Object", function () { ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" ); ok( "tracks" in popped.data, "instance has `tracks` property" ); - ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "tracks property is an array" ) + ok( Object.prototype.toString.call(popped.data.tracks) === "[object Object]", "tracks property is an object" ) popped.play(); @@ -373,7 +373,7 @@ test("Plugin API", function () { ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" ); ok( "tracks" in this.data, "instance has `tracks` property" ); - ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "tracks property is an array" ) + ok( Object.prototype.toString.call(popped.data.tracks) === "[object Object]", "tracks property is an object" ) }); @@ -411,7 +411,7 @@ test("Plugin API", function () { ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" ); ok( "tracks" in this.data, "instance has `tracks` property" ); - ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "tracks property is an array" ) + ok( Object.prototype.toString.call(popped.data.tracks) === "[object Object]", "tracks property is an object" ) }, end: function () {