]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#188] Support binding to custom events in plugins
authorrwldrn <waldron.rick@gmail.com>
Sun, 5 Dec 2010 20:04:29 +0000 (15:04 -0500)
committerrwldrn <waldron.rick@gmail.com>
Sun, 5 Dec 2010 20:04:29 +0000 (15:04 -0500)
popcorn.js
test/popcorn.unit.js

index 41e839e2eed9f2daeef534d73ae89bfd6d1b9560..a96403c5826a055420f893c606866705222bf4c0 100644 (file)
     nop: function () {}
   });    
   
-  //  Simple Factory pattern to implement native 
-  //  getters/setters and controllers 
-  //  as methods of the returned Popcorn instance
-  //  The immediately invoked function creates 
-  //  and returns an object of methods
+  //  Simple Factory pattern to implement getters, setters and controllers 
+  //  as methods of the returned Popcorn instance. The immediately invoked function 
+  //  creates and returns an object of methods
   Popcorn.extend(Popcorn.p, (function () {
       
       // todo: play, pause, mute should toggle
           }
           
           
-          
           if ( arg !== false && arg !== null && typeof arg !== "undefined" ) {
             
             this.video[ name ] = arg;
     }
   
   });
-  
-  var nativeEvents = "loadstart progress suspend emptied stalled play pause " + 
-                     "loadedmetadata loadeddata waiting playing canplay canplaythrough " + 
-                     "seeking seeked timeupdate ended ratechange durationchange volumechange";
+
+  Popcorn.Events  = {
+    UIEvents: "blur focus focusin focusout load resize scroll unload  ", 
+    MouseEvents: "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave click dblclick", 
+    Events: "loadstart progress suspend emptied stalled play pause " + 
+           "loadedmetadata loadeddata waiting playing canplay canplaythrough " + 
+           "seeking seeked timeupdate ended ratechange durationchange volumechange"
+  };
+    
+  Popcorn.Events.Natives = Popcorn.Events.UIEvents + " " + 
+                            Popcorn.Events.MouseEvents + " " +
+                              Popcorn.Events.Events,
   
   Popcorn.events  = {
-    //todo, fix types to be custom, natives will be native only
-    types: nativeEvents.split(/\s+/g),
-    natives: nativeEvents.split(/\s+/g),
-    custom: [ "frame" ], 
-    all: [ "frame" ].concat( nativeEvents.split(/\s+/g) ), 
+  
+
+    isNative: function( type ) {
+      
+      var checks = Popcorn.Events.Natives.split(/\s+/g);
+      
+      for ( var i = 0; i < checks.length; i++ ) {
+        if ( checks[i] === type ) {
+          return true;
+        }
+      }
+      
+      return false;
+    },  
+    getInterface: function( type ) {
+      
+      if ( !Popcorn.events.isNative( type ) ) {
+        return false;
+      }
+      
+      var natives = Popcorn.Events, proto;
+      
+      for ( var p in natives ) {
+        if ( p !== "Natives" && natives[p].indexOf(type) > -1 ) {
+          proto = p;
+        }
+      }
+      
+      return proto;
+    
+    }, 
+    
+    
+    all: Popcorn.Events.Natives.split(/\s+/g), 
+    
     fn: {
       trigger: function ( type, data ) {
         
         //  setup checks for custom event system
         if ( this.data.events[type] && Popcorn.sizeOf(this.data.events[type]) ) {
           
-          var evt = document.createEvent("Events");
-              evt.initEvent(type, true, true, window, 1);
+          var interface  = Popcorn.events.getInterface(type);
           
-          this.video.dispatchEvent(evt);
-        
-          /*
+          if ( interface ) {
+          
+            var evt = document.createEvent( interface );
+                evt.initEvent(type, true, true, window, 1);          
           
-          TODO: implement some form of custom event system
+            this.video.dispatchEvent(evt);
+            
+            return this;
+          }        
+
+          //  Custom events          
           Popcorn.forEach(this.data.events[type], function ( obj, key ) {
 
-            //obj.call(_target, _data);
+            obj.call( this, evt, data );
             
-            // update to dispatch an event
-
-          });
-          */
+          }, this);
+          
         }
+        
+        return this;
       }, 
       listen: function ( type, fn ) {
         
         this.data.events[type][ fn.toString() + Popcorn.guid() ] = fn;
         
         // only attach one event of any type          
-        if (!hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) {
+        if ( !hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) {
 
           this.video.addEventListener( type, function( event ) {
             
     //  Provides some sugar, but ultimately extends
     //  the definition into Popcorn.p 
     
-    var natives = nativeEvents.split(/\s+/g), 
+    var natives = Popcorn.events.all, 
         reserved = [ "start", "end", "timeupdate" ], 
         plugin = {},
         setup;
           
         });
         
-
         //  Future support for plugin event definitions 
         //  for all of the native events
         Popcorn.forEach( setup, function ( callback, type ) {
index a81665b82a54c51b6da328f3b9ce8dce10301bc6..a3c2f95265f90c15bb6eb41dbf8ac44018b23a9d 100644 (file)
@@ -57,9 +57,6 @@ test("Object", function () {
       methods = "load play pause currentTime mute volume";
   
   
-  
-  console.log(popped);
-  
   popped.play();
 
   methods.split(/\s+/g).forEach(function (k,v) {
@@ -195,11 +192,10 @@ test("Real", function () {
       completed = [];                              
   
   
-  var expects = 11, 
+  var expects = 10, 
       count = 0;
 
-  //expect(expects);
-  // not in full use
+
   function plus(){ 
     if ( ++count == expects ) start(); 
   }
@@ -208,8 +204,9 @@ test("Real", function () {
   
   
   Setup.events.forEach(function ( name ) {
+    
     p.listen( name, function (event) {
-      
+    
       if ( completed.indexOf(name) === -1 ) {
         ok(true, name + " fired");
         plus();
@@ -235,6 +232,61 @@ test("Real", function () {
 
   
   
+});
+
+test("Custom", function () {
+
+  var expects = 1, 
+      count = 0;
+  
+  expect(expects);
+  
+  function plus(){ if ( ++count == expects ) start(); }
+
+  stop();
+  
+  var p = Popcorn("#video");
+  
+  
+  p.listen("eventz0rz", function ( event ) {
+  
+    ok( true, "Custom event fired" );
+    plus();
+
+  });
+  
+  p.trigger("eventz0rz");
+
+  
+  
+});
+
+
+test("UI/Mouse", function () {
+
+  var expects = 1, 
+      count = 0;
+  
+  expect(expects);
+  
+  function plus(){ if ( ++count == expects ) start(); }
+
+  stop();
+  
+  var p = Popcorn("#video");
+  
+  
+  p.listen("click", function ( event ) {
+  
+    ok( true, "click event fired" );
+    plus();
+
+  });
+  
+  p.trigger("click");
+
+  
+  
 });
 
 module("Popcorn Plugin")
@@ -436,6 +488,7 @@ test("Events Extended", function () {
   
 });
 
+
 /*
 module("Popcorn Video Object")
 test("wait()", function () {