]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#209] Drop support for callback only plugins; replace with an executed callback...
authorRick Waldron <waldron.rick@gmail.com>
Wed, 8 Dec 2010 22:20:01 +0000 (17:20 -0500)
committerRick Waldron <waldron.rick@gmail.com>
Wed, 8 Dec 2010 22:20:01 +0000 (17:20 -0500)
popcorn.js
test/popcorn.unit.js

index ff96db8938c6406923ad77b3998864ccd1d73560..bb2937df4864ca2208fd4d23850554e31bf58d9f 100644 (file)
     
     var natives = Popcorn.events.all, 
         reserved = [ "start", "end", "timeupdate" ], 
-        plugin = {},
+        plugin = {}, 
+        pluginFn, 
         setup;
     
-    
-    
     if ( typeof definition === "object" ) {
       
       setup = definition;
         setup.timeupdate = Popcorn.nop;
       }         
 
-      definition  = function ( options ) {
+      pluginFn  = function ( options ) {
         
         var self = this, 
             fired = {
         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;
   };
   
 
index 76d52b2a5c9e116d5c5bf8596d322fb24c8b9bf5..f6b757fba29a790f34e0486a1e090c7c7f4f9562 100644 (file)
@@ -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 () {