]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
Implements new plugin signature; updates demos; new unit tests
authorrwldrn <waldron.rick@gmail.com>
Sun, 5 Dec 2010 00:15:33 +0000 (19:15 -0500)
committerrwldrn <waldron.rick@gmail.com>
Sun, 5 Dec 2010 00:15:33 +0000 (19:15 -0500)
popcorn.js
test/demo.js
test/popcorn-api-draft-unit.js

index 095a903a8eca661ee0364cff7b4c164204317f6b..cd1c66917d40267fb1ac5690acdcf88e83b327bf 100644 (file)
   Popcorn.extend(Popcorn.p, (function () {
       
       // todo: play, pause, mute should toggle
-      var methods = "load play pause currentTime playbackRate mute volume", 
+      var methods = "load play pause currentTime playbackRate mute volume duration", 
           ret = {};
       
       //  Build methods, store in object that is returned and passed to extend
             return this;
           }
           
-          if ( arg ) {
+          
+          
+          if ( arg !== false && arg !== null && typeof arg !== "undefined" ) {
+            
             this.video[ name ] = arg;
             
             return this;
     //  Provides some sugar, but ultimately extends
     //  the definition into Popcorn.p 
     
-    var plugin = {};
+    var plugin = {}, setup;
+    
+    
+    
+    if ( typeof definition === "object" ) {
+      
+      setup = definition;
+      
+      
+      definition  = function ( options ) {
+        
+        var self = this;
+        
+        if ( !options ) {
+          return this;
+        } 
+        
+        if ( !( "start" in options ) ) {
+          options.start = 0;
+        }
+        
+        if ( !( "end" in options ) ) {
+          options.end = this.duration();
+        }
+        
+        this.video.addEventListener( "timeupdate", function( event ) {
+          
+          
+          if ( ~~self.currentTime() === options.start || 
+                  self.currentTime() === options.start ) {
+          
+            setup.start.call(self, event);
+          }
+
+          if ( self.currentTime() > options.start && 
+                self.currentTime() < options.end ) {
+            
+            setup.timeupdate.call(self, event);
+            
+          }
+
+          if ( ~~self.currentTime() === options.end || 
+                  self.currentTime() === options.end ) {
+                
+            setup.end.call(self, event);
+          }
+          
+          
+
+        });
+      
+        
+        return this;
+      };
+      
+      
+      console.log("setup", setup);
+      console.log("definition", definition);
+      
+    
+    }
+
     
     plugin[ name ] = definition;
     
index 7bb8266fd78b93045090189e33bb8b3bbaf8d891..240b72830dc7543cc06c0bdf3fa83528d8c3b892 100644 (file)
@@ -2,6 +2,52 @@
 
 (function (Popcorn) {
   
+  
+  
+  
+  
+  
+  Popcorn.plugin( "complicator", {
+    
+    
+    
+    start: function () {
+
+      var $qhead  = document.getElementById("qunit-header");
+    
+      $qhead.innerHTML = "complicator STARTED!";
+      
+    },
+    
+    
+    
+    end: function () {
+    
+      var $qhead  = document.getElementById("qunit-header");
+    
+      $qhead.innerHTML = "complicator ENDED!";
+    
+    }, 
+    
+    
+    
+    timeupdate: function () {
+    
+      var $qhead  = document.getElementById("qunit-header");
+    
+      $qhead.innerHTML = $qhead.innerHTML + " " + this.currentTime();
+    
+    }
+    
+    
+    
+  });
+  
+  
+  
+  
+  
+  
   Popcorn.plugin( "subtitler" , function ( options ) {
     
     var subtitles = [], 
     if ( !div ) {
       // if no existing frame, create it.
       div = document.createElement("div");
-      div.id  = "subtitle-container";
+      div.id = "subtitle-container";
+      
+      context.parentNode.appendChild(div);
       
       Popcorn.extend(div.style, {
         //border: "1px solid red",
-        width: context.clientWidth + "px", 
-        height: context.clientHeight + "px", 
+        width: context.clientWidth + "px",  //offsetWidth
+        height: context.clientHeight + "px", //offsetHeight
         top: context.offsetTop + "px", 
         left: context.offsetLeft + "px",         
         position: "absolute",
@@ -28,7 +76,7 @@
         textAlign: "center"
       });
       
-      context.parentNode.appendChild(div);
+      
     }
     
     if ( typeof options === "object" && "join" in options ) {
           div.innerHTML = div.innerHTML + subtitle.html;
         }
 
-        if ( this.currentTime() >= subtitle.end ) {
-          
-          if ( temp.length ) {
-            Popcorn.forEach(temp, function ( title ) {
+        if ( temp.length && this.currentTime() >= subtitle.end ) {
+
+          Popcorn.forEach(temp, function ( title ) {
+            if ( title && title.style ) {
               title.style.display = "none";
-            });
-          }
+            }
+            
+          });
+
         }
       }, this);
+      
     });
   });
 
@@ -69,6 +120,9 @@ $(function () {
 
   var p = Popcorn('#video');
   
+
+  
+  
   p.play();  
   
  
@@ -107,6 +161,14 @@ $(function () {
     }
     
   ]);
+  
+  
+  
+  
+  p.complicator({
+    start: 1, 
+    end: 10
+  });
 
   
 
index 98d67fb96ab1a4eede45b0516d4e2e606bd922d5..a41229eadeb4082290b6f78de5f0fb4b3c827396 100644 (file)
@@ -73,14 +73,18 @@ test("Popcorn Object", function () {
 
 test("Popcorn Plugin", function () {
   
+  QUnit.reset();
   
   // needs expectation
 
   var popped = Popcorn("#video"), 
       methods = "load play pause currentTime mute volume";
+      
   
+  stop();
   
-  Popcorn.plugin("subtitles", function ( ) {
+  
+  Popcorn.plugin("subtitles", function () {
     
     var self = this;
     
@@ -101,19 +105,72 @@ test("Popcorn Plugin", function () {
   });
   
   
+  //  Call plugin to test scope within 
+  popped.subtitles();
   
   
-  ok( "subtitles" in popped, "plugin is now available to instance" );
   
-  popped.subtitles();
+  ok( "subtitles" in popped, "subtitles plugin is now available to instance" );
+  ok( Popcorn.registry.length === 1, "One item in the registry");
+  
   
   
-  ok( Popcorn.registry.length === 1, "One item in the registry");
+  
+  
+  Popcorn.plugin("complicator", {
+    
+    start: function ( event ) {
+      
+      console.log(this );
+      equals( ~~this.currentTime(), 1, "~~this.currentTime() === 1");
+      
+      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, "instance has method: " + k );
+      });
+
+      ok( "video" in this, "instance has `video` property" );
+      ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" );
+
+      ok( "data" in this, "instance has `data` property" );
+      ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" );
+
+      ok( "tracks" in this.data, "instance has `tracks` property" );
+      ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "tracks property is an array" )      
+      
+    },
+    end: function () {
+    
+      start();
+    
+    }, 
+    timeupdate: function () {
+    }    
+  });
+  
+  
+  ok( "complicator" in popped, "complicator plugin is now available to instance" );
+  ok( Popcorn.registry.length === 2, "Two items in the registry");
+  
+  
+  
+  popped.currentTime(0);
+  
+  popped.complicator({
+    start: 1, 
+    end: 2
+  });  
+  
   
 });
 
 test("Popcorn Events Stored By Type", function () {
   
+  QUnit.reset();
+  
   expect(6)
   
   var p = Popcorn("#video"),