From: Anna Sobiepanek Date: Tue, 15 Feb 2011 21:55:52 +0000 (-0500) Subject: Added Scotts #259 work X-Git-Tag: v0.4~51 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=160025ba66a4b995e6849169e237ec36e574de87;p=popcorn-js.git Added Scotts #259 work --- diff --git a/popcorn.js b/popcorn.js index 6ae2e38..9f63c0c 100644 --- a/popcorn.js +++ b/popcorn.js @@ -91,8 +91,14 @@ history: [], events: {}, trackEvents: { - byStart: [{start: -1, end: -1}], - byEnd: [{start: -1, end: -1}], + byStart: [{ + start: -1, + end: -1 + }], + byEnd: [{ + start: -1, + end: -1 + }], startIndex: 0, endIndex: 0, previousUpdateTime: 0 @@ -101,7 +107,7 @@ var isReady = function( that ) { - if ( that.video.readyState >= 3 ) { + 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 @@ -122,39 +128,67 @@ // Playbar advancing if ( previousTime < currentTime ) { - while ( tracksByEnd[tracks.endIndex] && tracksByEnd[tracks.endIndex].end <= currentTime ) { - if ( tracksByEnd[tracks.endIndex]._running === true ) { - tracksByEnd[tracks.endIndex]._running = false; - tracksByEnd[tracks.endIndex]._natives.end.call( that, event, tracksByEnd[tracks.endIndex] ); + 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; } - tracks.endIndex++; } - while ( tracksByStart[tracks.startIndex] && 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.call( that, event, tracksByStart[tracks.startIndex] ); + 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 ) { + 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; } - tracks.startIndex++; } // Playbar receding } else if ( previousTime > currentTime ) { - while ( tracksByStart[tracks.startIndex] && tracksByStart[tracks.startIndex].start > currentTime ) { - if ( tracksByStart[tracks.startIndex]._running === true ) { - tracksByStart[tracks.startIndex]._running = false; - tracksByStart[tracks.startIndex]._natives.end.call( that, event, tracksByStart[tracks.startIndex] ); + 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; } - tracks.startIndex--; } - while ( tracksByEnd[tracks.endIndex] && 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.call( that, event, tracksByEnd[tracks.endIndex] ); + 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 ) { + 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.endIndex--; } } /* @@ -315,41 +349,6 @@ this.listen("timeupdate", callback); - return this; - }, - removePlugin: function( name ) { - - var byStart = this.data.trackEvents.byStart, - byEnd = this.data.trackEvents.byEnd, - idx, rl, sl; - - delete Popcorn.p[ name ]; - - // remove plugin reference from registry - for ( idx = 0, rl = Popcorn.registry.length; idx < rl; idx++ ) { - if ( Popcorn.registry[ idx ].type === name ) { - Popcorn.registry.splice( idx, 1 ); - break; // plugin found, stop checking - } - } - - // 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.splice( idx, 1 ); - byEnd.splice( idx, 1 ); - - // update for loop if something removed, but keep checking - idx--; sl--; - if ( this.data.trackEvents.startIndex <= idx ) { - this.data.trackEvents.startIndex--; - this.data.trackEvents.endIndex--; - } - } - } return this; } }); @@ -514,8 +513,61 @@ }; + // 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 ) { + + // all of Popcorn it is + + // fix the order + name = obj; + obj = Popcorn.p; + + var registryLength = Popcorn.registry.length, + registryIndex; + + // remove plugin reference from registry + for ( registryIndex = 0; registryIndex < registryLength; registryIndex++ ) { + if ( Popcorn.registry[ registryIndex ].type === name ) { + Popcorn.registry.splice( registryIndex, 1 ); + + // 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.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.removeTrackEvent = function( obj, trackId ) { - var historyLen = obj.data.history.length, indexWasAt = 0, byStart = [], @@ -523,7 +575,7 @@ 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 ) { @@ -604,6 +656,11 @@ removeTrackEvent: function( id ) { Popcorn.removeTrackEvent.call( null, this, id ); return this; + }, + + removePlugin: function( name ) { + Popcorn.removePlugin.call( null, this, name ); + return this; } }); @@ -1016,4 +1073,4 @@ }); }, false ); -})(window, window.document); \ No newline at end of file +})(window, window.document); diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 03fe924..c1660c1 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -432,7 +432,7 @@ test("Manifest", function () { if ( ++count === expects ) { start(); // clean up added events after tests - p.removePlugin("footnote"); + Popcorn.removePlugin("footnote"); } } @@ -516,12 +516,12 @@ test("Update Timer", function () { if ( ++count === expects ) { start(); // clean up added events after tests - p2.removePlugin("forwards"); - p2.removePlugin("backwards"); + Popcorn.removePlugin("forwards"); + Popcorn.removePlugin("backwards"); } } - stop( 10000 ); + stop(); Popcorn.plugin("forwards", function () { return { @@ -577,13 +577,16 @@ test("Plugin Factory", function () { var popped = Popcorn("#video"), methods = "load play pause currentTime mute volume roundTime exec removePlugin", - expects = 24, + expects = 26, count = 0; //expect(expects); function plus() { if ( ++count == expects ) { + Popcorn.removePlugin("breaker"); + Popcorn.removePlugin("executor"); + Popcorn.removePlugin("complicator"); start(); } } @@ -736,39 +739,105 @@ test("Plugin Factory", function () { popped.currentTime(0).play(); }); -test("removePlugin", function () { - - expect(10); - - var p = Popcorn("#video"), - rlen = Popcorn.registry.length; - - equals( rlen, 3, "Popcorn.registry.length is 3"); +test("Remove Plugin", function () { + var p = Popcorn("#video"), + p2 = Popcorn("#video"), + rlen = Popcorn.registry.length, + count = 0, + expects = 19, + interval; + function plus() { + if ( ++count === expects ) { + start(); + } + } + + expect( expects ); + stop( 10000 ); + p.currentTime(0).pause(); + + equals( rlen, 0, "Popcorn.registry.length is empty"); + plus(); + equals( p.data.trackEvents.byStart.length, 2, "p.data.trackEvents.byStart is initialized and has 2 entries"); + plus(); equals( p.data.trackEvents.byEnd.length, 2, "p.data.trackEvents.byEnd is initialized and has 2 entries"); + plus(); + + Popcorn.plugin("removeme", { + + start: function () { + + }, + end: function () { + + } + }); + + p.removeme({ + start: 2, + end: 3 + }); + + p2.removeme({ + start: 2, + end: 3 + }); - p.breaker({ - start: 2, - end: 30 - }); - + equals( Popcorn.registry.length, 1, "Popcorn.registry.length is 1"); + plus(); equals( p.data.trackEvents.byStart.length, 3, "p.data.trackEvents.byStart is updated and has 3 entries"); + plus(); equals( p.data.trackEvents.byEnd.length, 3, "p.data.trackEvents.byEnd is updated and has 3 entries"); + plus(); + p.removePlugin("removeme"); - p.removePlugin("breaker"); - - - ok( !("breaker" in p), "breaker plugin is no longer available to instance" ); - ok( !("breaker" in Popcorn.prototype), "breaker plugin is no longer available to Popcorn.prototype" ); - - - equals( Popcorn.registry.length, 2, "Popcorn.registry.length is 2"); - + // p.removeme still exists on the prototype, even though we said to remove it + // the tracks of that type though, are removed. + // think of it as removing all tracks of plugin type attached to an instance + ok( typeof p.removeme === "function", "removeme plugin is still defined to p instance" ); + plus(); + ok( ( "removeme" in Popcorn.prototype ), "removeme plugin is still available to Popcorn.prototype" ); + plus(); + equals( Popcorn.registry.length, 1, "Popcorn.registry.length has not changed"); + plus(); + + ok( (typeof p2.removeme === "function"), "removeme plugin is defined to p2 instance" ); + plus(); + + equals( p2.data.trackEvents.byStart.length, 3, "p2.data.trackEvents.byStart is updated and has 3 entries"); + plus(); + equals( p2.data.trackEvents.byEnd.length, 3, "p2.data.trackEvents.byEnd is updated and has 3 entries"); + plus(); + equals( p.data.trackEvents.byStart.length, 2, "p.data.trackEvents.byStart is updated and has 2 entries"); + plus(); equals( p.data.trackEvents.byEnd.length, 2, "p.data.trackEvents.byEnd is updated and has 2 entries"); + plus(); + Popcorn.removePlugin("removeme"); + + ok( !("removeme" in p2), "removeme plugin is no longer available to p2 instance" ); + plus(); + ok( !("removeme" in Popcorn.prototype), "removeme plugin is no longer available to Popcorn.prototype" ); + plus(); + equals( Popcorn.registry.length, 0, "Popcorn.registry.length is empty again"); + plus(); + + + interval = setInterval( function() { + if( p2.currentTime() > 3 ) { + + equals( p2.data.trackEvents.byStart.length, 2, "p2.data.trackEvents.byStart is updated and has 2 entries"); + plus(); + equals( p2.data.trackEvents.byEnd.length, 2, "p2.data.trackEvents.byEnd is updated and has 2 entries"); + plus(); + clearInterval( interval ); + } + }, 1); + p2.currentTime( 2 ).play(); }); @@ -776,7 +845,6 @@ test("removePlugin", function () { test("Protected Names", function () { - //QUnit.reset(); expect(8); @@ -840,7 +908,6 @@ test("Functions", function () { equals( historyRef.length, 2, "2 TrackEvents in history index"); - equals( popped.data.trackEvents.byStart.length, 4, "4 TrackEvents in popped.data.trackEvents.byStart "); equals( popped.data.trackEvents.byEnd.length, 4, "4 TrackEvents in popped.data.trackEvents.byEnd "); @@ -1323,7 +1390,7 @@ test("Parsing Integrity", function () { start(); // clean up added events after tests clearInterval( interval ); - poppercore.removePlugin( "parserTest" ); + Popcorn.removePlugin( "parserTest" ); } } @@ -1351,7 +1418,7 @@ test("Parsing Integrity", function () { // interval used to wait for data to be parsed interval = setInterval( function() { - poppercore.currentTime(5).play().currentTime(6); + poppercore.currentTime(5).play(); }, 2000); });