]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#192] Create exec shorthand
authorrwldrn <waldron.rick@gmail.com>
Sun, 5 Dec 2010 21:07:39 +0000 (16:07 -0500)
committerrwldrn <waldron.rick@gmail.com>
Sun, 5 Dec 2010 21:07:39 +0000 (16:07 -0500)
popcorn.js
test/popcorn.unit.js

index 386ed5c4c9a3838661d44774511c234c06473e90..5543e0200e1d833ac36cd1607a2c15d57772a064 100644 (file)
     },
     
     
-    
-    
-    
-
+    exec: function ( time, fn ) {
+      
+      !fn && ( fn = Popcorn.nop );
+      
+      
+      var timer = 0, 
+          self  = this, 
+          callback = function execCallback( event ) {
+            
+            if ( this.currentTime() >= time && !timer ) {
+              
+              fn.call(self, event);
+              
+              this.unlisten("execCallback");
+              
+              timer++;
+            }
+          };
+      
+      
+      
+      this.listen("timeupdate", callback);
+      
+      
+      
+      return this;
+    },
 
 
 
         }
         
         //  Register 
-        this.data.events[type][ fn.toString() + Popcorn.guid() ] = fn;
+        this.data.events[type][ fn.name || ( fn.toString() + Popcorn.guid() ) ] = fn;
         
         // only attach one event of any type          
         if ( !hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) {
           this.video.addEventListener( type, function( event ) {
             
             Popcorn.forEach( self.data.events[type], function ( obj, key ) {
-              obj.call(self, event);
+
+              if ( typeof obj === "function" ) {
+                obj.call(self, event);
+              }
+
             });
             
             //fn.call( self, event );
         }
         return this;
       }, 
-      unlisten: function( type ) {
+      unlisten: function( type, fn ) {
+        
+        if ( this.data.events[type] && this.data.events[type][fn] ) {
+          this.data.events[type][fn]  = null;
+          return this;
+        }
+      
         this.data.events[type] = null;
         return this;        
       },      
index fd888d6ecaa7bdeb4c63a88ef250731daef0ee7b..8b3d31e6bcfcebe9deddc10026c81a55b84ce626 100644 (file)
@@ -54,7 +54,9 @@ test("Object", function () {
 
   
   var popped = Popcorn("#video"), 
-      methods = "load play pause currentTime mute volume roundTime";
+      methods = "load play pause currentTime mute volume roundTime exec";
+  
+  
   
   ok( "video" in popped, "instance has `video` property" );
   ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" );
@@ -78,8 +80,11 @@ test("Object", function () {
   
 });
 
-module("Popcorn Object Instance");
-test("Methods", function () {
+module("Popcorn Methods");
+
+
+
+test("roundTime", function () {
   
   QUnit.reset();
   
@@ -88,9 +93,39 @@ test("Methods", function () {
   popped.play().pause().currentTime( 0.98 );
   
   equals( 1, popped.roundTime(), ".roundTime() returns 1 when currentTime is 0.98s" );
+
+
+});
+
+
+test("exec", function () {
+  
+  QUnit.reset();
+  
+  var popped = Popcorn("#video"),
+      expects = 1, 
+      count = 0;
+
+  expect(expects);
+  
+  function plus(){ 
+    if ( ++count == expects ) start(); 
+  }
+  
+  stop(); 
   
 
 
+  popped.exec( 4, function () {
+    
+    
+    ok(true, "exec function");
+    plus();
+    
+  }).currentTime(3).play();
+  
+  
+
 });