From 6c6a012c1f116fd96e1617b77eebcdc1e95b7f06 Mon Sep 17 00:00:00 2001 From: rwldrn Date: Wed, 11 May 2011 14:04:33 -0400 Subject: [PATCH] [#414] Implements .toggle shorthand; Updates to tests; Changes per review --- popcorn.js | 15 +++++---- test/popcorn.unit.js | 77 ++++++++++++++++++++++++++++++-------------- 2 files changed, 62 insertions(+), 30 deletions(-) diff --git a/popcorn.js b/popcorn.js index f80c100..586936b 100644 --- a/popcorn.js +++ b/popcorn.js @@ -440,6 +440,12 @@ })() ); + Popcorn.forEach( "enable disable".split(" "), function( method ) { + Popcorn.p[ method ] = function( plugin ) { + return Popcorn[ method ]( this, plugin ); + }; + }); + Popcorn.extend(Popcorn.p, { // Rounded currentTime @@ -466,15 +472,12 @@ }, position: function() { return Popcorn.position( this.media ); + }, + toggle: function( plugin ) { + return Popcorn[ this.data.disabled.indexOf( plugin ) > -1 ? "enable" : "disable" ]( this, plugin ); } }); - 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 84d13ec..a45f06f 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -1696,50 +1696,79 @@ test("Index Integrity", function () { }); -test("Popcorn.disable/enable", function() { +test("Popcorn.disable/enable/toggle", function() { - var $pop = Popcorn.getInstanceById( "video" ), + var $pop = Popcorn( "#video" ), count = 0, - expects = 4; + expects = 5; expect( expects ); function plus() { if ( ++count === expects ) { start(); + + Popcorn.removeInstance( $pop ); } } - stop( 10000 ); + Popcorn.plugin("toggler", function () { + return { + start: function () { + var div = document.createElement("div"); + div.id = "toggler-test"; + div.innerHTML = "foo"; - // rw/ff + document.body.appendChild(div); + }, + end: function () { + document.getElementById("toggler-test").parentNode.removeChild(document.getElementById("toggler-test")); + } + }; + }); - // Test static function call - Popcorn.disable( $pop, "rw" ); + $pop.exec( 40, function() { - ok( $pop.data.disabled.indexOf("rw") > -1, "Plugin: rw is disabled" ); - plus(); + console.log( "WOO!" ); + // make sure toggler never happened + // look for: "toggler-test" - // Test per-instance function call - $pop.disable( "ff" ); - - ok( $pop.data.disabled.indexOf("ff") > -1, "Plugin: ff is disabled" ); - plus(); + ok( !document.getElementById("toggler-test"), "No toggler container, disabled toggler plugin correctly never ran" ); + plus(); - // Test static function call - Popcorn.enable( $pop, "rw" ); + // Test per-instance toggle on + $pop.toggle( "toggler" ); + ok( $pop.data.disabled.indexOf("toggler") === -1, "toggle() plugin: toggler is re-enabled" ); + plus(); + }); + + $pop.toggler({ + start: 40, + end: 50 + }); + + // rw/ff + + // Test per-instance function call + $pop.disable( "toggler" ); + + ok( $pop.data.disabled.indexOf("toggler") > -1, "disable() plugin: toggler is disabled" ); + plus(); - ok( $pop.data.disabled.indexOf("rw") === -1, "Plugin: rw is enabled" ); - plus(); + // Test per-instance function call + $pop.enable( "toggler" ); + + ok( $pop.data.disabled.indexOf("toggler") === -1, "enable() plugin: toggler is enabled" ); + plus(); - // Test per-instance function call - $pop.enable( "ff" ); - - ok( $pop.data.disabled.indexOf("ff") === -1, "Plugin: ff is enabled" ); - plus(); + // Test per-instance toggle off + $pop.toggle( "toggler" ); + + ok( $pop.data.disabled.indexOf("toggler") > -1, "toggle() plugin: toggler is disabled" ); + plus(); - //console.log( Popcorn.instances[ Popcorn.instanceIds.video ], ); + stop( 10000 ); }); module("Popcorn XHR"); -- 2.43.0