From: rwldrn Date: Wed, 11 May 2011 15:38:23 +0000 (-0400) Subject: [#414] Adds support for disable/enable plugin commands; Includes unit tests X-Git-Tag: v0.6~2^2~11^2~1 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=88a906d5680ca904ba5cca12c531ca86ea4dbd16;p=popcorn-js.git [#414] Adds support for disable/enable plugin commands; Includes unit tests --- diff --git a/popcorn.js b/popcorn.js index 0e4debd..f80c100 100644 --- a/popcorn.js +++ b/popcorn.js @@ -1,11 +1,16 @@ (function(global, document) { // Cache refs to speed up calls to native utils + var - forEach = Array.prototype.forEach, - hasOwn = Object.prototype.hasOwnProperty, - slice = Array.prototype.slice, - toString = Object.prototype.toString, + + AP = Array.prototype, + OP = Object.prototype, + + forEach = AP.forEach, + slice = AP.slice, + hasOwn = OP.hasOwnProperty, + toString = OP.toString, // ID string matching rIdExp = /^(#([\w\-\_\.]+))$/, @@ -143,10 +148,20 @@ // Register new instance Popcorn.addInstance( this ); - this.options = options || { }; + this.options = options || {}; + this.data = { - history: [], + + // Allows disabling a plugin per instance + disabled: [], + + // Stores DOM event queues by type events: {}, + + // Store track event history data + history: [], + + // Playback track event queues trackEvents: { byStart: [{ start: -1, @@ -207,7 +222,10 @@ 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 ) { + if ( tracksByStart[ tracks.startIndex ].end > currentTime && + tracksByStart[ tracks.startIndex ]._running === false && + that.data.disabled.indexOf( tracksByStart[ tracks.endIndex ]._natives.type ) === -1 ) { + tracksByStart[ tracks.startIndex ]._running = true; tracksByStart[ tracks.startIndex ]._natives.start.call( that, event, tracksByStart[ tracks.startIndex ] ); } @@ -240,7 +258,10 @@ 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 ) { + if ( tracksByEnd[ tracks.endIndex ].start <= currentTime && + tracksByEnd[ tracks.endIndex ]._running === false && + that.data.disabled.indexOf( tracksByStart[ tracks.endIndex ]._natives.type ) === -1 ) { + tracksByEnd[ tracks.endIndex ]._running = true; tracksByEnd[ tracks.endIndex ]._natives.start.call( that, event, tracksByEnd[tracks.endIndex] ); } @@ -356,6 +377,26 @@ } 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; } }); @@ -428,6 +469,12 @@ } }); + Popcorn.forEach( "enable disable".split(" "), function( method ) { + Popcorn.p[ method ] = function( plugin ) { + return Popcorn[ method ]( this, plugin ); + }; + }); + Popcorn.Events = { UIEvents: "blur focus focusin focusout load resize scroll unload ", MouseEvents: "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave click dblclick", diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index bd33bdd..84d13ec 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -51,17 +51,19 @@ test("API", function () { test("Utility", function () { - expect(8); + expect(10); // TODO: comprehensive tests for these utilities - equals( typeof Popcorn.forEach, "function" , "Popcorn.forEach is a provided utility function"); - equals( typeof Popcorn.extend, "function" , "Popcorn.extend is a provided utility function"); - equals( typeof Popcorn.error, "function" , "Popcorn.error is a provided utility function"); - equals( typeof Popcorn.guid, "function" , "Popcorn.guid is a provided utility function"); - equals( typeof Popcorn.sizeOf, "function" , "Popcorn.sizeOf is a provided utility function"); - equals( typeof Popcorn.nop, "function" , "Popcorn.nop is a provided utility function"); - equals( typeof Popcorn.addTrackEvent, "function" , "Popcorn.addTrackEvent is a provided utility function"); - equals( typeof Popcorn.position, "function" , "Popcorn.position is a provided utility function"); + equals( typeof Popcorn.forEach, "function" , "Popcorn.forEach is a provided static function"); + equals( typeof Popcorn.extend, "function" , "Popcorn.extend is a provided static function"); + equals( typeof Popcorn.error, "function" , "Popcorn.error is a provided static function"); + equals( typeof Popcorn.guid, "function" , "Popcorn.guid is a provided static function"); + equals( typeof Popcorn.sizeOf, "function" , "Popcorn.sizeOf is a provided static function"); + equals( typeof Popcorn.nop, "function" , "Popcorn.nop is a provided static function"); + equals( typeof Popcorn.addTrackEvent, "function" , "Popcorn.addTrackEvent is a provided static function"); + equals( typeof Popcorn.position, "function" , "Popcorn.position is a provided static function"); + equals( typeof Popcorn.disable, "function" , "Popcorn.disable is a provided static function"); + equals( typeof Popcorn.enable, "function" , "Popcorn.enable is a provided static function"); }); test("Standard Time Strings" , function () { @@ -203,16 +205,16 @@ test("Instances", function() { Popcorn("#video"); - ok( typeof Popcorn.addInstance === "function" , "Popcorn.addInstance is a provided utility function"); + ok( typeof Popcorn.addInstance === "function" , "Popcorn.addInstance is a provided static function"); plus(); - ok( typeof Popcorn.removeInstance === "function" , "Popcorn.removeInstance is a provided utility function"); + ok( typeof Popcorn.removeInstance === "function" , "Popcorn.removeInstance is a provided static function"); plus(); - ok( typeof Popcorn.getInstanceById === "function" , "Popcorn.getInstanceById is a provided utility function"); + ok( typeof Popcorn.getInstanceById === "function" , "Popcorn.getInstanceById is a provided static function"); plus(); - ok( typeof Popcorn.removeInstanceById === "function" , "Popcorn.removeInstanceById is a provided utility function"); + ok( typeof Popcorn.removeInstanceById === "function" , "Popcorn.removeInstanceById is a provided static function"); plus(); ok( typeof Popcorn.instanceIds === "object" , "Popcorn.instanceIds is a provided cache object"); @@ -1694,19 +1696,59 @@ test("Index Integrity", function () { }); +test("Popcorn.disable/enable", function() { + + + var $pop = Popcorn.getInstanceById( "video" ), + count = 0, + expects = 4; + + expect( expects ); + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + stop( 10000 ); + // rw/ff + // Test static function call + Popcorn.disable( $pop, "rw" ); + ok( $pop.data.disabled.indexOf("rw") > -1, "Plugin: rw is disabled" ); + plus(); + // Test per-instance function call + $pop.disable( "ff" ); + + ok( $pop.data.disabled.indexOf("ff") > -1, "Plugin: ff is disabled" ); + plus(); + // Test static function call + Popcorn.enable( $pop, "rw" ); + + ok( $pop.data.disabled.indexOf("rw") === -1, "Plugin: rw is enabled" ); + plus(); + + // Test per-instance function call + $pop.enable( "ff" ); + + ok( $pop.data.disabled.indexOf("ff") === -1, "Plugin: ff is enabled" ); + plus(); + + //console.log( Popcorn.instances[ Popcorn.instanceIds.video ], ); +}); module("Popcorn XHR"); test("Basic", function () { expect(2); - equals( typeof Popcorn.xhr, "function" , "Popcorn.xhr is a provided utility function"); - equals( typeof Popcorn.xhr.httpData, "function" , "Popcorn.xhr.httpData is a provided utility function"); + equals( typeof Popcorn.xhr, "function" , "Popcorn.xhr is a provided static function"); + equals( typeof Popcorn.xhr.httpData, "function" , "Popcorn.xhr.httpData is a provided static function"); });