]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#586] Implements Popcorn.events.hooks & canplayall hook;
authorrwldrn <waldron.rick@gmail.com>
Fri, 17 Jun 2011 19:35:04 +0000 (15:35 -0400)
committerrwldrn <waldron.rick@gmail.com>
Fri, 17 Jun 2011 19:36:11 +0000 (15:36 -0400)
popcorn.js
test/popcorn.unit.js

index efa17bf8d5bd634104f5e168c6b14cf2a4766b17..5c6b668c2470984f6a5c4a1f278a1c0272b95ec6 100644 (file)
         // Stores DOM event queues by type
         events: {},
 
+        // Stores Special event hooks data
+        hooks: {},
+
         // Store track event history data
         history: [],
 
       listen: function( type, fn ) {
 
         var self = this,
-            hasEvents = true;
+          hasEvents = true, 
+          eventHook = Popcorn.events.hooks[ type ], 
+          origType = type, 
+          tmp;
 
         if ( !this.data.events[ type ] ) {
           this.data.events[ type ] = {};
           hasEvents = false;
         }
 
-        //  Register
+        // Check and setup event hooks
+        if ( eventHook ) {
+
+          // Execute hook add method if defined
+          if ( eventHook.add ) {
+            eventHook.add.call( this );
+          }
+
+          // Reassign event type to our piggyback event type if defined
+          if ( eventHook.bind ) {
+            type = eventHook.bind;
+          }
+
+          // Reassign handler if defined
+          if ( eventHook.handler ) {
+            tmp = fn;
+
+            fn = function wrapper( event ) {
+              eventHook.handler.call( self, event, tmp );
+            };
+          }
+
+          // assume the piggy back event is registered
+          hasEvents = true;
+
+          // Setup event registry entry
+          if ( !this.data.events[ type ] ) {
+            this.data.events[ type ] = {};
+            // Toggle if the previous assumption was untrue
+            hasEvents = false;
+          }
+        }
+
+        //  Register event and handler
         this.data.events[ type ][ fn.name || ( fn.toString() + Popcorn.guid() ) ] = fn;
 
         // only attach one event of any type
               }
             });
 
-            //fn.call( self, event );
-
           }, false);
         }
         return this;
 
         return this;
       }
+    },
+    hooks: {
+      canplayall: {
+        bind: "canplaythrough",
+        add: function() {
+          this.data.hooks.canplayall = {
+            fired: false
+          };
+        },
+        // declare special handling instructions
+        handler: function canplayall( event, callback ) {
+
+          if ( !this.data.hooks.canplayall.fired ) {
+            // trigger original user callback once
+            callback.call( this, event );
+
+            this.data.hooks.canplayall.fired = true;
+          }
+        }
+      }
     }
   };
 
index 7c4a603bb4fd2c446ae3428db8271073a5029660..cdf1df603096851c9518018cbba07a0018d7065a 100644 (file)
@@ -433,8 +433,8 @@ test("Popcorn.[addTrackEvent | removeTrackEvent].ref()", function() {
   
   var popped = Popcorn("#video");
 
-       // Calling exec() will create tracks and added them to the
-       // trackreference internally
+  // Calling exec() will create tracks and added them to the
+  // trackreference internally
   popped.exec( 1, function() { /* ... */ });
   popped.exec( 3, function() { /* ... */ });
   popped.exec( 5, function() { /* ... */ });
@@ -593,6 +593,72 @@ test( "Popcorn.events", function() {
 
 });
 
+test("Popcorn.events.hooks", function() {
+
+  expect(1);
+  
+  ok( Popcorn.events.hooks, "Popcorn.events.hooks exists" );
+
+});
+
+test("Popcorn.events.hooks: canplayall", function() {
+  //qunit-fixture
+
+  var expects = 1,
+    count = 0, 
+    fired = 0;
+
+  expect(expects);
+
+  function plus(){ 
+    if ( ++count == expects ) {
+      start();
+    }
+  }
+
+  stop( 10000 );
+
+  var video = document.createElement("video"),
+    sources = {
+      mp4: document.createElement("source"), 
+      ogv: document.createElement("source"), 
+      webm: document.createElement("source")
+    },
+    url = "http://videos.mozilla.org/serv/webmademovies/popcornplug.";
+
+  video.id = "event-fixture";
+  video.controls = true;
+  video.preload = "auto";
+
+  Popcorn.forEach( sources, function( source, type ) {
+    source.src = url + type;
+    video.appendChild( source );
+  });
+
+  document.getElementById("qunit-fixture").appendChild( video );
+
+  var $pop = Popcorn("#event-fixture");
+
+  $pop.listen("canplayall", function( event ) {
+    equal( ++fired, 1, "canplayall is fired only once" );
+    plus();
+  });
+
+  $pop.listen("canplaythrough", function( event ) {
+    // this should trigger re-fires of the original event
+    this.currentTime(0);
+  });
+});
+
+/*
+<video height="180" width="300" id="video" controls> 
+<source src="http://videos.mozilla.org/serv/webmademovies/popcornplug.mp4"></source>
+<source src="http://videos.mozilla.org/serv/webmademovies/popcornplug.ogv"></source>
+<source src="http://videos.mozilla.org/serv/webmademovies/popcornplug.webm"></source>
+</video>
+
+*/
+
 module("Popcorn Position");
 test("position", function () {