]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
initial updater logic
authorScottDowne <sdowne@learn.senecac.on.ca>
Tue, 7 Dec 2010 16:15:30 +0000 (11:15 -0500)
committerScottDowne <sdowne@learn.senecac.on.ca>
Tue, 7 Dec 2010 16:15:30 +0000 (11:15 -0500)
plugins/image/popcorn.image.html [new file with mode: 0644]
plugins/image/popcorn.image.js [new file with mode: 0644]
popcorn.js
test/popcorn.unit.js

diff --git a/plugins/image/popcorn.image.html b/plugins/image/popcorn.image.html
new file mode 100644 (file)
index 0000000..d52755a
--- /dev/null
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn API Draft DEMO</title>
+  <script src="../../popcorn.js"></script>
+  <script src="popcorn.image.js"></script>
+  
+  <script>
+    document.addEventListener('DOMContentLoaded', function(){
+      Popcorn('#video')
+        .image({
+          start   : 1,
+          end     : 8,
+          target  : 'image-container',
+          src     : 'http://nutrihealth.in/wp-content/uploads/2010/04/popcorn-250x300.jpg',
+          alt     : 'Bucket of Popcorn',
+          title   : 'Popcorn.js!',
+          click   : function(){
+            alert('Re-directing you to Popcorn.js web site.');
+            window.location = 'http://mozilla.github.com/popcorn-js/';
+          }
+        });
+    }, false);
+  </script>
+  
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn Image Demo</h1>
+  
+  <video id='video'
+    style="float:left"
+    controls preload='none' 
+    poster="http://media.w3.org/2010/05/sintel/poster.png"> 
+  
+    <source id='mp4'
+      src="http://media.w3.org/2010/05/sintel/trailer.mp4"
+      type='video/mp4; codecs="avc1, mp4a"'> 
+  
+    <source id='ogv'
+      src="http://media.w3.org/2010/05/sintel/trailer.ogv"
+      type='video/ogg; codecs="theora, vorbis"'> 
+  
+    <p>Your browser does not support the HTML5 Video element.</p>
+  
+  </video> 
+  
+  <div id="image-container" style="border:1px solid black"></div>
+  
+</body>
+</html>
diff --git a/plugins/image/popcorn.image.js b/plugins/image/popcorn.image.js
new file mode 100644 (file)
index 0000000..632dbc3
--- /dev/null
@@ -0,0 +1,41 @@
+// EXAMPLE PLUGIN: IMAGE
+
+(function (Popcorn) {
+  
+  Popcorn.plugin( "image" , (function(){
+      
+    var container, img, src, title, alt, clickListener, loadListener
+    
+    return {
+
+      start: function(event, options){
+      
+        container = document.getElementById(options.target);
+        img = new Image();
+
+        loadListener = function(){
+          this.alt = options.alt;
+          this.title = options.title;
+
+          clickListener = function(){ options.click.call( img ); };
+          this.addEventListener('click', function(){ clickListener(); }, false);
+
+          container.appendChild( img );
+        };
+        
+        img.addEventListener('load', function(){ loadListener.call( img ) }, false); 
+        
+        img.src = options.src;
+      },
+      
+      end: function(event, options){
+        img.removeEventListener( 'load', loadListener, false );
+        img.removeEventListener( 'click', clickListener, false );
+        container.removeChild( img );
+      }
+      
+    };
+    
+  })());
+
+})( Popcorn );
index b6a4587c3645347eb974644bd87839052c630496..36e2b66b7803206a2c3fc437228dfdf28c74a3d3 100644 (file)
@@ -22,7 +22,7 @@
 
     init: function( entity ) {
 
-      var elem, matches;
+      var elem, matches, that = this;
 
       matches = rIdExp.exec( entity );
       
       
       this.data = {
         events: {},
-        tracks: []
+        tracks: {
+          byStart: [],
+          byEnd:   [],
+          startIndex: 0,
+          endIndex: 0,
+          previousUpdateTime: 0
+        }
       };
+
+      // adding padding to the front and end of the arrays
+      // this is so we do not fall off either end
+      this.data.tracks.byStart.push( {start: -1, end: -1} );
+      this.data.tracks.byEnd.push( {start: -1, end: -1} );
+      this.data.tracks.byStart.push( {start: 9999, end: 9999} );
+      this.data.tracks.byEnd.push( {start: 9999, end: 9999} );
+      
+      this.video.addEventListener( "timeupdate", function( event ) {
+
+        var currentTime    = this.currentTime,
+            previousTime   = that.data.tracks.previousUpdateTime,
+            tracksByEnd    = that.data.tracks.byEnd,
+            tracksByStart  = that.data.tracks.byStart;
+
+        // Playbar advancing
+        if (previousTime < currentTime) {
+
+          while (tracksByEnd[that.data.tracks.endIndex].end <= currentTime) {
+            if (tracksByEnd[that.data.tracks.endIndex].running === true) {
+              tracksByEnd[that.data.tracks.endIndex].running = false;
+              tracksByEnd[that.data.tracks.endIndex].natives.end(event, tracksByEnd[that.data.tracks.endIndex]);
+            }
+            that.data.tracks.endIndex++;
+          }
+          
+          while (tracksByStart[that.data.tracks.startIndex].start <= currentTime) {
+            if (tracksByStart[that.data.tracks.startIndex].end > currentTime && tracksByStart[that.data.tracks.startIndex].running === false) {
+              tracksByStart[that.data.tracks.startIndex].running = true;
+              tracksByStart[that.data.tracks.startIndex].natives.start(event, tracksByStart[that.data.tracks.startIndex]);
+            }
+            that.data.tracks.startIndex++;
+          }
+
+        // Playbar receding
+        } else if (previousTime > currentTime) {
+
+          while (tracksByStart[that.data.tracks.startIndex].start > currentTime) {
+            if (tracksByStart[that.data.tracks.startIndex].running === true) {
+              tracksByStart[that.data.tracks.startIndex].running = false;
+              tracksByStart[that.data.tracks.startIndex].natives.end(event, tracksByStart[that.data.tracks.startIndex]);
+            }
+            that.data.tracks.startIndex--;
+          }
+          
+          while (tracksByEnd[that.data.tracks.endIndex].end > currentTime) {
+            if (tracksByEnd[that.data.tracks.endIndex].start <= currentTime && tracksByEnd[that.data.tracks.endIndex].running === false) {
+              tracksByEnd[that.data.tracks.endIndex].running = true;
+              tracksByEnd[that.data.tracks.endIndex].natives.start(event, tracksByEnd[that.data.tracks.endIndex]);
+            }
+            that.data.tracks.endIndex--;
+          }
+        } else {
+          // When user seeks, currentTime can be equal to previousTime on the
+          // timeUpdate event. We are not doing anything with this right now, but we
+          // may need this at a later point and should be aware that this behavior
+          // happens in both Chrome and Firefox.
+        }
+
+        that.data.tracks.previousUpdateTime = currentTime;
+      }, false);
       
       return this;
     }
     //  the definition into Popcorn.p 
     
     var natives = Popcorn.events.all, 
-        reserved = [ "start", "end", "timeupdate" ], 
+        reserved = [ "start", "end"], 
         plugin = {},
         setup;
     
       
       setup = definition;
       
-      if ( !( "timeupdate" in setup ) ) {
+      /*if ( !( "timeupdate" in setup ) ) {
         setup.timeupdate = Popcorn.nop;
-      }         
+      }*/        
 
       definition  = function ( options ) {
-        
-        var self = this, 
-            fired = {
-              start: 0, 
-              end: 0
-            };
-        
+
         if ( !options ) {
           return this;
         } 
         
+        // storing the plugin natives
+        options.natives = setup;
+        options.natives.type = name;
+        options.running = false;
+
         //  Checks for expected properties
         if ( !( "start" in options ) ) {
           options.start = 0;
           setup._setup.call(self, options);
         }
         
-        //  Plugin timeline handler 
-        this.listen( "timeupdate", function( event ) {
-          
-          
-          if ( ~~self.currentTime() === options.start || 
-                  self.currentTime() === options.start ) {
-            
-            !fired.start && setup.start.call(self, event, options);
-            fired.start++;
-          }
-
-          if ( self.currentTime() > options.start && 
-                self.currentTime() < options.end ) {
-            
-            setup.timeupdate.call(self, event, options);
-          }
-
-          if ( ~~self.currentTime() === options.end || 
-                  self.currentTime() === options.end ) {
-                
-            !fired.end && setup.end.call(self, event, options);
-            fired.end++;
-          }
-          
+        // Store this definition in an array sorted by times
+        this.data.tracks.byStart.push( options );
+        this.data.tracks.byEnd.push( options );
+        this.data.tracks.byStart.sort( function( a, b ){
+          return ( a.start - b.start );
+        });
+        this.data.tracks.byEnd.sort( function( a, b ){
+          return ( a.end - b.end );
         });
         
         //  Future support for plugin event definitions 
index 8b3d31e6bcfcebe9deddc10026c81a55b84ce626..0d2ddf68252edcd133856634475d058cc868ba2d 100644 (file)
@@ -65,7 +65,7 @@ test("Object", function () {
   ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" );
 
   ok( "tracks" in popped.data, "instance has `tracks` property" );
-  ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "tracks property is an array" )
+  ok( Object.prototype.toString.call(popped.data.tracks) === "[object Object]", "tracks property is an object" )
 
   
   popped.play();
@@ -373,7 +373,7 @@ test("Plugin API", function () {
     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" )
+    ok( Object.prototype.toString.call(popped.data.tracks) === "[object Object]", "tracks property is an object" )
   });
   
   
@@ -411,7 +411,7 @@ test("Plugin API", function () {
       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" )      
+      ok( Object.prototype.toString.call(popped.data.tracks) === "[object Object]", "tracks property is an object" )      
       
     },
     end: function () {