]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#176] Add DOMContentLoaded check to popcorn core
authorrwldrn <waldron.rick@gmail.com>
Mon, 20 Dec 2010 15:44:54 +0000 (10:44 -0500)
committerrwldrn <waldron.rick@gmail.com>
Mon, 20 Dec 2010 15:54:29 +0000 (10:54 -0500)
popcorn.js
test/ready.html [new file with mode: 0644]

index 73b84a24eb6bbf2dd3b86c6009ed7e31b1a29c54..0bb56ebf35a6ae707bf943708a70347f15b88fc7 100644 (file)
 
   //  ID string matching
   rIdExp  = /^(#([\w\-\_\.]+))$/, 
+  
+  // ready fn cache
+  readyStack = [], 
+  readyBound = false, 
+  
 
   //  Declare a pseudo-private constructor
-  //  This constructor returns the instance object.    
+  //  Returns an instance object.    
   Popcorn = function( entity ) {
     //  Return new Popcorn object
     return new Popcorn.p.init( entity );
 
       var elem, matches;
       
+      //  Supports Popcorn(function () { /../ }) 
+      //  Originally proposed by Daniel Brooks
       
       if ( typeof entity === "function" ) {
         
-        document.addEventListener( "DOMContentLoaded", entity, false);
+        readyStack.push( entity );
+        
+        //  This process should happen once per page load
+        if ( !readyBound ) {
+
+          //  set readyBound flag
+          readyBound = true;
+          
+          var DOMContentLoaded  = function () {
+            
+            //  remove this listener
+            document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
+            
+            //  Execute all ready function in the stack
+            for ( var i = 0; i < readyStack.length; i++ ) {
+            
+              readyStack[i].call( document, Popcorn );
+            
+            }
+            //  GC readyStack
+            readyStack = null;  
+          };
+          
+          document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false);
+        }
         
         return;  
       }
+ 
       
       matches = rIdExp.exec( entity );
       
@@ -41,6 +73,7 @@
         elem = document.getElementById(matches[2]);
       }
       
+      
       this.video = elem ? elem : null;
       
       this.data = {
       var methods = "load play pause currentTime playbackRate mute volume duration", 
           ret = {};
       
+      
       //  Build methods, store in object that is returned and passed to extend
       Popcorn.forEach( methods.split(/\s+/g), function( name ) {
         
   
   
   
-
-  global.Popcorn = Popcorn;
+  //  Exposes Popcorn and a shorthand to global context
+  global.Popcorn = global.$P = Popcorn;
   
 })(window, document);
diff --git a/test/ready.html b/test/ready.html
new file mode 100644 (file)
index 0000000..3b835a6
--- /dev/null
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn API</title>
+  <link rel="stylesheet" href="qunit/qunit.css" type="text/css" media="screen">
+  <script src="../popcorn.js"></script>
+  <script>
+  
+  Popcorn(function (e) {
+
+    console.log("Popcorn(function() { /../ }) 1");
+    
+    var p = Popcorn("#video");
+    
+    
+    p.exec( 4, function () {
+
+      
+
+    }).play();
+    
+  });
+
+  Popcorn(function () {
+  
+    console.log("Popcorn(function() { /../ }) 2");
+  
+  });  
+  </script>
+  
+</head>
+<body>
+  <video id='video'style="display:" 
+    controls 
+    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 user agent does not support the HTML5 Video element.</p> 
+
+  </video>   
+</body>
+</html>