]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#447] Added getBoundingClientrect(), getStyle()
authorstevenaw <sweerdenburg@gmail.com>
Thu, 31 Mar 2011 17:47:33 +0000 (13:47 -0400)
committerstevenaw <sweerdenburg@gmail.com>
Thu, 7 Apr 2011 16:10:03 +0000 (12:10 -0400)
players/baseplayer/popcorn.baseplayer.html
players/baseplayer/popcorn.baseplayer.js
players/baseplayer/popcorn.baseplayer.unit.html
players/baseplayer/popcorn.baseplayer.unit.js

index 8c3b7940f46a1e4f626a07bf7b4bb9f6705c97cc..73ecaeb64724fbc178c24e82b1fc55969553ec05 100644 (file)
@@ -3,10 +3,6 @@
     <head>
         <title>Popcorn Base Player Example</title>
         <style>
-            iframe{
-                display:block;
-                border:0;
-            }
             body > div {
                 width: 100%;
                 float: left;
             div div {
                 margin-bottom:20px;
             }
-
-            .api_output {
-                float:right;
-                padding-left: 20px;
+            
+            #player_1 {
+              position: absolute;
+              left: 250px;
+              top: 20px;
+              width: 300px;
+              height: 150px;
+              background: #FFAAAA;
             }
         </style>
         
             document.addEventListener( "DOMContentLoaded", function() {
               var ticks = 0,
                   player = Popcorn.baseplayer(),
-                  popcorn = Popcorn( player )
-                  .listen( "timeupdate", function() {
-                    document.getElementById( "playerTicks" ).innerHTML = ++ticks;
-                    document.getElementById( "playerMs" ).innerHTML = popcorn.currentTime();
-                  });
+                  dimensions,
+                  popcorn;
                   
               document.getElementById( "btnPlay" ).addEventListener( "click", function() { popcorn.play(); }, false );
               document.getElementById( "btnPause" ).addEventListener( "click", function() { popcorn.pause(); }, false );
               
-              popcorn.exec( 2, function() {
-                document.getElementById( "playerOutput" ).innerHTML = "2 seconds reached";
-              });
+              player.resource = document.getElementById('player_1');
+              
+              player.resource.innerHTML += '<br />Width and Height: '+player.getStyle( 'width' ) + ' by ' + player.getStyle( 'height' )
+                                            + '<br /> Positioned at: '+player.getStyle( 'left' ) + ',' + player.getStyle( 'top' );
+              
+              dimensions = player.getBoundingClientRect();
+              document.getElementById( "playerTop" ).innerHTML = dimensions.top;
+              document.getElementById( "playerLeft" ).innerHTML = dimensions.left;
+              document.getElementById( "playerBottom" ).innerHTML = dimensions.bottom;
+              document.getElementById( "playerRight" ).innerHTML = dimensions.right;
+              document.getElementById( "playerWidth" ).innerHTML = dimensions.width;
+              document.getElementById( "playerHeight" ).innerHTML = dimensions.height;
               
+              popcorn = Popcorn( player )
+                .listen( "timeupdate", function() {
+                  document.getElementById( "playerTicks" ).innerHTML = ++ticks;
+                  document.getElementById( "playerMs" ).innerHTML = popcorn.currentTime().toFixed(5);
+                })
+                .exec( 2, function() {
+                  document.getElementById( "playerOutput" ).innerHTML = "2 seconds reached";
+                });
+              
+              // The ready state and resource selection would be handled by the player extending this base player
               player.readyState = 2;
               popcorn.play();
             }, false);
             Seconds: <span id="playerMs" ></span><br />
             Event Output: <span id="playerOutput" ></span><br />
             <button class="simple" id="btnPlay">Play</button>
-            <button class="simple" id="btnPause">Pause</button>
+            <button class="simple" id="btnPause">Pause</button><br r/>
+            Top: <span id="playerTop" ></span><br />
+            Left: <span id="playerLeft" ></span><br />
+            Bottom: <span id="playerBottom" ></span><br />
+            Right: <span id="playerRight" ></span><br />
+            Width: <span id="playerWidth" ></span><br />
+            Height: <span id="playerHeight" ></span><br />
           </div>
           
+          <span id="player_1">This is a container for the player, auto-sized for testing purposes. It displays CSS values.</span>
+          
           <br />
           <br />
           This demo showcases basic functionality of a player. It is a shell of an HTMLMediaElement with basic timing routines.<br />
index df082e362c3a2b78784661acff2e3eaf4a4f5e75..3720a7b0795311e612d49e7cec5d1d1fabf77b73 100644 (file)
-Popcorn.baseplayer = function() {
-  return new Popcorn.baseplayer.init();
-}
-
-Popcorn.baseplayer.init = function() {
-  this.readyState = 0;
-  this.currentTime = 0;
-  this.duration = 0;
-  this.paused = 1;
-  this.ended = 0;
-  this.volume = 1;
-  this.muted = 0;
-  this.playbackRate = 1;
-
-  // These are considered to be "on" by being defined. Initialize to undefined
-  this.autoplay;
-  this.loop;
-  
-  // List of events
-  this.events = {};
-}
+(function( global, doc ) {
+  Popcorn.baseplayer = function() {
+    return new Popcorn.baseplayer.init();
+  }
 
-Popcorn.baseplayer.init.prototype = {
-  load: function() {},
-  
-  play: function() {
-    this.paused = 0;
-    this.timeupdate();
-  },
-  
-  pause: function() {
+  Popcorn.baseplayer.init = function() {
+    this.readyState = 0;
+    this.currentTime = 0;
+    this.duration = 0;
     this.paused = 1;
-  },
-  
-  timeupdate: function() {
-    // The player was paused since the last time update
-    if ( this.paused ) {
-      return;
-    }
+    this.ended = 0;
+    this.volume = 1;
+    this.muted = 0;
+    this.playbackRate = 1;
 
-    // So we can refer to the instance when setTimeout is run
-    var self = this;
-    this.currentTime += 0.015;
+    // These are considered to be "on" by being defined. Initialize to undefined
+    this.autoplay;
+    this.loop;
     
-    this.dispatchEvent( "timeupdate" );
-    setTimeout( function() {
-      self.timeupdate.call( self );
-    }, 15 );
-  },
-  
-  // Add an event listener to the object
-  addEventListener: function( evtName, fn ) {
-    if ( !this.events[evtName] ) {
-      this.events[evtName] = [];
-    }
+    // List of events
+    this.events = {};
     
-    this.events[evtName].push( fn );
-    return fn;
-  },
-  
-  // Can take event object or simple string
-  dispatchEvent: function( oEvent ) {
-    var evt,
-        self = this,
-        eventInterface,
-        eventName = oEvent.type;
+    // The underlying player resource. May be <canvas>, <iframe>, <object>, array, etc
+    this.resource;
+  }
+
+  Popcorn.baseplayer.init.prototype = {
+    load: function() {},
+    
+    play: function() {
+      this.paused = 0;
+      this.timeupdate();
+    },
+    
+    pause: function() {
+      this.paused = 1;
+    },
+    
+    timeupdate: function() {
+      // The player was paused since the last time update
+      if ( this.paused ) {
+        return;
+      }
+
+      // So we can refer to the instance when setTimeout is run
+      var self = this;
+      this.currentTime += 0.015;
+      
+      this.dispatchEvent( "timeupdate" );
+      setTimeout( function() {
+        self.timeupdate.call( self );
+      }, 15 );
+    },
+    
+    // By default, assumes this.resource is a DOM Element
+    // Changing the type of this.resource requires this method to be overridden
+    getBoundingClientRect: function() {
+      var b = this.resource.getBoundingClientRect();
+      
+      return {
+        bottom: b.bottom,
+        left: b.left,
+        right: b.right,
+        top: b.top,
         
-    // A string was passed, create event object
-    if ( !eventName ) {
-      eventName = oEvent;
-      eventInterface  = Popcorn.events.getInterface( eventName );
+        //  These not guaranteed to be in there
+        width: b.width || ( b.right - b.left ),
+        height: b.height || ( b.bottom - b.top )
+      };
+    },
+    
+    // By default, assumes this.resource is a DOM Element
+    // Changing the type of this.resource requires this method to be overridden
+    // Returns the computed value for CSS style 'prop' as computed by the browser
+    getStyle: function( prop ) {
+      var elem = this.resource;
       
-      if ( eventInterface ) {
-        evt = document.createEvent( eventInterface );
-        evt.initEvent( eventName, true, true, window, 1 );
+      if ( elem.currentStyle ) {
+        // IE syntax
+        return elem.currentStyle[prop];
+      } else if ( global.getComputedStyle ) {
+        // Firefox, Chrome et. al
+        return doc.defaultView.getComputedStyle( elem, null ).getPropertyValue( prop );
+      } else {
+        // Fallback, just in case
+        return elem.style[prop];
       }
-    }
+    },
     
-    Popcorn.forEach( this.events[eventName], function( val ) {
-      val.call( self, evt, self );
-    });
-  }
-};
\ No newline at end of file
+    // Add an event listener to the object
+    addEventListener: function( evtName, fn ) {
+      if ( !this.events[evtName] ) {
+        this.events[evtName] = [];
+      }
+      
+      this.events[evtName].push( fn );
+      return fn;
+    },
+    
+    // Can take event object or simple string
+    dispatchEvent: function( oEvent ) {
+      var evt,
+          self = this,
+          eventInterface,
+          eventName = oEvent.type;
+          
+      // A string was passed, create event object
+      if ( !eventName ) {
+        eventName = oEvent;
+        eventInterface  = Popcorn.events.getInterface( eventName );
+        
+        if ( eventInterface ) {
+          evt = document.createEvent( eventInterface );
+          evt.initEvent( eventName, true, true, window, 1 );
+        }
+      }
+      
+      Popcorn.forEach( this.events[eventName], function( val ) {
+        val.call( self, evt, self );
+      });
+    }
+  };
+})( window, document );
\ No newline at end of file
index d9ee76233b7d636cc72382833b3fc5e9c6aaf8b3..676ab77adc72c400adb02e20a80826c8d01b7b35 100644 (file)
   <script src="popcorn.baseplayer.unit.js"></script>
   <script src="popcorn.baseplayer.js"></script>
   
+  <style>
+    body > div {
+        width: 100%;
+        float: left;
+        overflow: hidden;
+    }
+    div div {
+        margin-bottom:20px;
+    }
+    
+    #player_1 {
+      position: absolute;
+      left: 220px;
+      top: 260px;
+      width: 300px;
+      height: 250px;
+      background: #FFAAAA;
+    }
+  </style>
 </head>
 <body>
   <h1 id="qunit-header">Popcorn Base Player Plugin</h1>
@@ -23,6 +42,6 @@
   <ol id="qunit-tests"></ol>
   <div id="qunit-fixture">  </div>
   
-  <div id="player_1" width="500" height="281"></div>
+  <span id="player_1">This is a container for the player, auto-sized for testing purposes.</span>
 </body>
 </html>
index 68165c4e220ad16644717545075de5588be0a43e..a461aa9450bc0a6253c4b1e0bbf6fb976327b08c 100644 (file)
@@ -1,10 +1,11 @@
-module("Popcorn Base Player");
-test("API", function () {
+module( "Popcorn Base Player" );
+test( "API", function () {
 
-  var expects = 17,
+  var expects = 0,
       count = 0,
       player = Popcorn.baseplayer(),
       members = {
+        // HTMLMediaElement members
         'readyState' : 'number',
         'currentTime' : 'number',
         'duration' : 'number',
@@ -15,13 +16,20 @@ test("API", function () {
         'playbackRate' : 'number',
         'autoplay' : 'undefined',
         'loop' : 'undefined',
-        'events' : 'object',
         'load' : 'function',
         'play' : 'function',
         'pause' : 'function',
         'timeupdate' : 'function',
+        
+        // DOMElement members
         'addEventListener' : 'function',
-        'dispatchEvent' : 'function'
+        'dispatchEvent' : 'function',
+        'getBoundingClientRect' : 'function',
+        
+        // Helper functions and members
+        'events' : 'object',
+        'resource' : 'undefined',
+        'getStyle' : 'function'
       };
       
   function plus() {
@@ -30,7 +38,11 @@ test("API", function () {
     }
   }
   
-  expect(expects);
+  Popcorn.forEach( members, function () {
+    expects++;
+  });
+  
+  expect( expects );
   stop( 10000 );
   
   Popcorn.forEach( members, function ( type, prop ) {
@@ -39,11 +51,17 @@ test("API", function () {
   });
 });
 
-test("Default Functionality", function () {
-  var expects = 5,
+test( "Default Functionality", function () {
+  var expects = 7,
       count = 0,
       player = Popcorn.baseplayer(),
-      popcorn = Popcorn( player );
+      dimensions,
+      expectedVals = {
+        left: 220,
+        top: 260,
+        width: 300,
+        height: 250
+      };
       
   function plus() {
     if ( ++count === expects ) {
@@ -51,9 +69,29 @@ test("Default Functionality", function () {
     }
   }
   
-  expect(expects);
+  player.resource = document.getElementById('player_1');
+  dimensions = player.getBoundingClientRect();
+  
+  Popcorn.forEach( expectedVals, function() {
+    expects+= 2;
+  });
+  
+  expect( expects );
   stop( 1000 );
-      
+  
+  Popcorn.forEach( expectedVals, function( val, prop ) {
+    equals( player.getStyle( prop ), val+'px', "Style '" + prop + "' correctly got" );
+    plus();
+    
+    equals( dimensions[prop], val, "Bounding Client " + prop + " works" );
+    plus();
+  });
+  
+  equals( dimensions.right, expectedVals.left + expectedVals.width, "Bounding Client right works" );
+  plus();
+  equals( dimensions.bottom, expectedVals.top + expectedVals.height, "Bounding Client bottom works" );
+  plus();
+  
   player.addEventListener( 'timeupdate', function( evt, caller ) {
     ok( true, "Time update called" );
     plus();
@@ -76,7 +114,7 @@ test("Default Functionality", function () {
   player.play();
 });
 
-test("Extension and Method Overriding", function () {
+test( "Extension and Method Overriding", function () {
   var expects = 4,
       count = 0,
       player = Popcorn.baseplayer(),