]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#415] Adds Popcorn.prototype.position(); Adds unit tests
authorrwldrn <waldron.rick@gmail.com>
Fri, 1 Apr 2011 13:44:34 +0000 (09:44 -0400)
committerrwldrn <waldron.rick@gmail.com>
Fri, 1 Apr 2011 13:44:34 +0000 (09:44 -0400)
popcorn.js
test/popcorn.unit.js

index 8b064aafd63f849374e86052b5a82b156b96e42f..2220f0aa55be2cc32f1e50d618dae59480564dd5 100644 (file)
 
       return this;
     }
+    //  Popcorn Object Element Utils
+    position: function() {
+      var  media = this.video,  
+          bounds = media.getBoundingClientRect(),
+          doc = media.ownerDocument,
+          docElem = document.documentElement,
+          body = document.body,
+          clientTop, clientLeft, scrollTop, scrollLeft, top, left;
+
+      //  Determine correct clientTop/Left
+      clientTop  = docElem.clientTop  || body.clientTop  || 0;
+      clientLeft = docElem.clientLeft || body.clientLeft || 0;
+
+      //  Determine correct scrollTop/Left
+      scrollTop  = (global.pageYOffset && docElem.scrollTop  || body.scrollTop );
+      scrollLeft = (global.pageXOffset && docElem.scrollLeft || body.scrollLeft);
+
+      //  Temp top/left
+      top  = Math.ceil( bounds.top  + scrollTop  - clientTop );
+      left = Math.ceil( bounds.left + scrollLeft - clientLeft );
+
+      for ( var p in bounds ) {
+        bounds[ p ] = Math.round( bounds[ p ] );
+      }
+      
+      return Popcorn.extend({}, bounds, { top: top, left: left });
+    }
   });
 
   Popcorn.Events  = {
 
     script.onload = script.onreadystatechange = function() {
 
-                       if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
+      if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
 
-                               //      Handling remote script loading callbacks
-                               if ( isScript ) {
+        //  Handling remote script loading callbacks
+        if ( isScript ) {
 
-                                       //      getScript
-                                       success && success();
-                               }
+          //  getScript
+          success && success();
+        }
 
-             //  Executing for JSONP requests
-                               if ( isFired ) {
+        //  Executing for JSONP requests
+        if ( isFired ) {
 
-                     //  Garbage collect the callback
-                     delete window[ callback ];
+          //  Garbage collect the callback
+          delete window[ callback ];
 
-                     //  Garbage collect the script resource
-                     head.removeChild( script );
-                               }
-                       }
+          //  Garbage collect the script resource
+          head.removeChild( script );
+        }
+      }
     };
 
     script.src = url;
index 0f63263743651292588a665be32a77b67b44e9e3..4d4aa966d03b6be3b1a89c24f42baa93ede6683a 100755 (executable)
@@ -307,6 +307,92 @@ test("exec", function () {
 
 });
 
+module("Popcorn Position");
+test("position", function () {
+
+  var pops = [ Popcorn("#pos-1"),
+                Popcorn("#pos-2"),
+                Popcorn("#pos-3"),
+                Popcorn("#pos-4")
+            ],
+      expects = 22,
+      count = 0,
+      tests = [
+                {"top":0,"height":150,"width":300,"bottom":150,"left":1123,"right":1423},
+                {"top":100,"height":150,"width":300,"bottom":250,"left":1023,"right":1323},
+                {"height":150,"width":300,"left":1366,"right":1666},
+                {"top":0,"height":150,"width":300,"bottom":150,"left":1123,"right":1423}
+                ],
+      bounds,
+      against,
+      styles;
+
+  expect( expects );
+
+  function plus(){ 
+    if ( ++count == expects ) {
+      start();
+    }
+  }
+
+  stop( 10000 ); 
+
+  pops.forEach(function( pop, idx ) {
+
+    bounds = pop.position();
+    against = tests[ idx ];
+    styles = document.getElementById( pop.video.id ).style;
+
+    for ( var p in bounds ) {
+      if ( p in against ) {
+        equal( bounds[ p ], against[ p ], "video#" + pop.video.id + ": " + p );
+        plus();
+      }
+    }
+  });
+});
+
+test("position called from plugin", function () {
+
+  var $pop = Popcorn("#video"),
+      expects = 3,
+      count = 0;
+
+  expect( expects );
+
+  function plus(){ 
+    if ( ++count == expects ) {
+      start();
+      Popcorn.removePlugin("positionPlugin");
+      delete Popcorn.manifest.positionPlugin;
+    }
+  }
+
+  stop( 10000 );
+
+  Popcorn.plugin( "positionPlugin" , function(){
+    return {
+      _setup: function( options ) {
+        ok( "position" in this, "this.position() avaliable in _setup");
+        plus();
+      },
+      start: function(event, options){
+
+        ok( "position" in this, "this.position() avaliable in start");
+        plus();
+      },
+      end: function(event, options){
+        ok( "position" in this, "this.position() avaliable in end");
+        plus();
+      }
+    };
+  });
+
+  $pop.positionPlugin({
+    start: 0,
+    end: 1
+  }).currentTime(0).play();
+});
 
 module("Popcorn Events");
 
@@ -1920,11 +2006,11 @@ test("Parser Support", function () {
     }
   });
 
-       Popcorn.parser( "parseAudio", function( data ){
-         ok( typeof data.json === "object", "data.json exists");
-         plus();
-         return data.json;
-       });
+  Popcorn.parser( "parseAudio", function( data ){
+    ok( typeof data.json === "object", "data.json exists");
+    plus();
+    return data.json;
+  });
 
   audiocorn.parseAudio("data/parserAudio.json", function() {