From 98d244d0f77ab612bb8cbeb7526b8bd441fa3cee Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Wed, 8 Dec 2010 17:20:01 -0500 Subject: [PATCH] [#209] Drop support for callback only plugins; replace with an executed callback result --- popcorn.js | 38 +++++++++++++++------- test/popcorn.unit.js | 77 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 88 insertions(+), 27 deletions(-) diff --git a/popcorn.js b/popcorn.js index ff96db893..bb2937df4 100644 --- a/popcorn.js +++ b/popcorn.js @@ -337,11 +337,10 @@ var natives = Popcorn.events.all, reserved = [ "start", "end", "timeupdate" ], - plugin = {}, + plugin = {}, + pluginFn, setup; - - if ( typeof definition === "object" ) { setup = definition; @@ -350,7 +349,7 @@ setup.timeupdate = Popcorn.nop; } - definition = function ( options ) { + pluginFn = function ( options ) { var self = this, fired = { @@ -418,19 +417,34 @@ return this; }; } - - plugin[ name ] = definition; + // If a function is passed... + if ( typeof definition === "function" ) { + + // Execute and capture returned object + setup = definition.call(this); + + // Ensure an object was returned + // it has properties and isnt an array + if ( typeof setup === "object" && + !( "length" in setup ) ) { + + Popcorn.plugin( name, setup ); + } + return; + } + + // Assign new named definition + plugin[ name ] = pluginFn; + // Extend Popcorn.p with new named definition Popcorn.extend( Popcorn.p, plugin ); - Popcorn.registry.push({ - name: name, - plugin: plugin - }); + // Push into the registry + Popcorn.registry.push(plugin); + - // within the context of a plugin - // any of the events can be listened to + return plugin; }; diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 76d52b2a5..f6b757fba 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -361,7 +361,7 @@ test("UI/Mouse", function () { }); module("Popcorn Plugin") -test("Plugin API", function () { +test("Plugin Factory", function () { QUnit.reset(); @@ -369,7 +369,7 @@ test("Plugin API", function () { var popped = Popcorn("#video"), methods = "load play pause currentTime mute volume roundTime exec", - expects = 21, + expects = 34, count = 0; expect(expects); @@ -381,7 +381,53 @@ test("Plugin API", function () { } stop(); + + Popcorn.plugin("executor", function () { + + return { + + start: function () { + var self = this; + + // These ensure that a popcorn instance is the value of `this` inside a plugin definition + + methods.split(/\s+/g).forEach(function (k,v) { + ok( k in self, "executor instance has method: " + k ); + + plus(); + }); + + ok( "video" in this, "executor instance has `video` property" ); + plus(); + ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" ); + plus(); + + ok( "data" in this, "executor instance has `data` property" ); + plus(); + ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" ); + plus(); + + ok( "tracks" in this.data, "executor instance has `tracks` property" ); + plus(); + ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "executor tracks property is an array" ) + plus(); + }, + end: function () { + + } + }; + }); + ok( "executor" in popped, "executor plugin is now available to instance" ); + plus(); + ok( Popcorn.registry.length === 1, "One item in the registry"); + plus(); + + popped.executor({ + start: 1, + end: 20 + }); + Popcorn.plugin("complicator", { @@ -396,24 +442,24 @@ test("Plugin API", function () { // These ensure that a popcorn instance is the value of `this` inside a plugin definition methods.split(/\s+/g).forEach(function (k,v) { - ok( k in self, "instance has method: " + k ); + ok( k in self, "complicator instance has method: " + k ); plus(); }); - ok( "video" in this, "instance has `video` property" ); + ok( "video" in this, "complicator instance has `video` property" ); plus(); ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" ); plus(); - ok( "data" in this, "instance has `data` property" ); + ok( "data" in this, "complicator instance has `data` property" ); plus(); - ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" ); + ok( Object.prototype.toString.call(popped.data) === "[object Object]", "complicator data property is an object" ); plus(); - ok( "tracks" in this.data, "instance has `tracks` property" ); + ok( "tracks" in this.data, " complicatorinstance has `tracks` property" ); plus(); - ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "tracks property is an array" ) + ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "complicator tracks property is an array" ) plus(); }, @@ -429,13 +475,9 @@ test("Plugin API", function () { ok( "complicator" in popped, "complicator plugin is now available to instance" ); plus(); - ok( Popcorn.registry.length === 1, "Two items in the registry"); + ok( Popcorn.registry.length === 2, "Two items in the registry"); plus(); - - - - popped.complicator({ start: 1, end: 20 @@ -457,14 +499,14 @@ test("Plugin API", function () { breaker.start++; - ok(true, "plugin:breaker started"); + ok(true, "breaker started"); plus(); }, end: function () { breaker.end++; - ok(true, "plugin:ended started"); + ok(true, "breaker started"); plus(); @@ -485,6 +527,11 @@ test("Plugin API", function () { start: 1, end: 2 }); + + + + + }); test("Protected Names", function () {