]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
can only target divs now. perf fixes. _teardown method + tests
authorChristopher De Cairos <chris.decairos@gmail.com>
Thu, 23 Jun 2011 15:47:39 +0000 (11:47 -0400)
committerChristopher De Cairos <chris.decairos@gmail.com>
Thu, 23 Jun 2011 15:47:39 +0000 (11:47 -0400)
plugins/processing/popcorn.processing.html
plugins/processing/popcorn.processing.js
plugins/processing/popcorn.processing.unit.html
plugins/processing/popcorn.processing.unit.js

index 36ffe9d825d240238a8d9371393eed4911080679..94bb360def78cd33a20bb0b0a3cf59c1a817ac11 100644 (file)
@@ -35,7 +35,7 @@
         start: 5,
         end: 15,
         sketch: "test2.pjs",
-        target: "processing-canvas",
+        target: "processing-div",
         noPause: false
       });
     }, false);
@@ -67,6 +67,4 @@
   </div>
   <div id="processing-div2" width="50%" height="50%">
   </div>
-  <canvas id="processing-canvas"></canvas>
-  </body>
 </html>
index c6f36350966be1f1f656204cf1439ae81ef45141..ae849e683c51331c12c97c4a773e30424e24be88 100644 (file)
 
     var init = function( context ) {
     
-      var parentTarget = document.getElementById( options.target ),
+      var initProcessing,
+        canvas;
+      
+      options.parentTarget = document.getElementById( options.target );
+      
+      if ( !options.parentTarget ) {
+        throw ( "target container doesn't exist" );
+      }
       
       initProcessing = function() {
         var addListeners = function() {
         }
       };
       
-      if ( parentTarget.tagName === "CANVAS" ) {
-        options.canvas = parentTarget;
-      } else if ( parentTarget.tagName === "DIV" ) {
-        options.canvas = document.createElement( "canvas" );
-        parentTarget.appendChild( options.canvas );
-      }    
-      options.canvas.id = Popcorn.guid( options.target + "-sketch-" );
-      options.canvas.setAttribute( "data-processing-sources", options.sketch );
-      options.canvas.style.display = "none";
+      canvas = document.createElement( "canvas" );
+      canvas.id = Popcorn.guid( options.target + "-sketch-" );
+      canvas[ "data-processing-sources" ] =  options.sketch;
+      canvas.style.display = "none";   
+      options.canvas = canvas;
+      
+      options.parentTarget.appendChild( options.canvas );
       
       Popcorn.xhr({
         url: options.sketch,
 
       end: function( event, options ) {
         toggle.call( this, false, options );
+      },
+      
+      _teardown: function( options ) {
+        options.parentTarget && options.parentTarget.removeChild( options.canvas );
       }
     };
   });
index a82a073f2844f20ab2f73e96123a81c694f71079..6dd7ad75e1b668b21480ce6b1127e60b25d33d85 100644 (file)
 
     </video>
   </div>
-  <div id="processing-div" width="50%" height="50%"></div>
+  <div id="processing-div-1" width="50%" height="50%"></div>
   <div id="processing-div-2" width="50%" height="50%"></div>
   <div id="processing-div-3" width="50%" height="50%"></div>
-  <div id="processing-canvas-div">
-  <canvas id="processing-canvas"></canvas>
-  </div>
+  <div id="processing-div-4" width="50%" height="50%"></div>
 </html>
index 42753aaf88ca25918d9f1c046ab6a39b0d881c66..e68c6be7d601badc03e10d146561fc7803fbeee9 100644 (file)
@@ -1,12 +1,13 @@
 test( "Processing plugin tests", function() {
   var popped = Popcorn( "#video" ),
-      expects = 26,
+      expects = 31,
       count = 0,
-      processingDiv = document.getElementById( "processing-div" ),
+      processingDiv = document.getElementById( "processing-div-1" ),
       processingDiv2 = document.getElementById( "processing-div-2" ),
       processingDiv3 = document.getElementById( "processing-div-3" ),
-      processingCanvas = document.getElementById( "processing-canvas" ),
-      canvases = [];
+      processingDiv4 = document.getElementById( "processing-div-4" ),
+      canvases = [],
+      trackids = [];
 
   expect( expects );
   
@@ -21,7 +22,7 @@ test( "Processing plugin tests", function() {
   ok( "processing" in popped, "processing is a method of the popped instance" );
   plus();
 
-  equals( processingDiv.innerHTML, "", "'processing-div' is empty" );
+  equals( processingDiv.innerHTML, "", "'processing-div-1' is empty" );
   plus();
   
   equals( processingDiv2.innerHTML, "", "'processing-div-2' is empty" );
@@ -30,41 +31,49 @@ test( "Processing plugin tests", function() {
   equals( processingDiv3.innerHTML, "", "'processing-div-3' is empty" );
   plus();
   
-  ok( processingCanvas, "'processing-canvas' is present in DOM" );
+  equals( processingDiv4.innerHTML, "", "'processing-div-4' is empty" );
   plus();
 
   popped.processing({
     start: 0,
     end: 1,
-    target: "processing-div",
+    target: "processing-div-1",
     sketch: "test.pjs"
-  })
-  .processing({
+  });
+  trackids.push( popped.getLastTrackEventId() );
+  
+  popped.processing({
     start: 1,
     end: 3,
     target: "processing-div-2",
     sketch: "test2.pjs"
-  })
-  .processing({
+  });
+  trackids.push( popped.getLastTrackEventId() );
+  
+  popped.processing({
     start: 3,
     end: 4,
     target: "processing-div-3",
     sketch: "test.pjs"
-  })
-  .processing({
+  });
+  trackids.push( popped.getLastTrackEventId() );
+
+  popped.processing({
     start: 3,
     end: 4,
-    target: "processing-canvas",
+    target: "processing-div-4",
     sketch: "test2.pjs"
-  })
-  .exec( 0.5, function() {
+  });
+  trackids.push( popped.getLastTrackEventId() );
+   
+  popped.exec( 0.5, function() {
     equals( processingDiv.children[ 0 ].style.display, "inline", "canvas '" + processingDiv.children[ 0 ].id + "' is displayed" );
     plus();
     equals( processingDiv2.children[ 0 ].style.display, "none", "canvas '" + processingDiv2.children[ 0 ].id + "' is hidden" );
     plus();
     equals( processingDiv3.children[ 0 ].style.display, "none", "canvas '" + processingDiv3.children[ 0 ].id + "' is hidden" );
     plus();
-    equals( processingCanvas.style.display, "none", "canvas '" + processingCanvas.id + "' is hidden" );
+    equals( processingDiv4.children[ 0 ].style.display, "none", "canvas '" + processingDiv4.children[ 0 ].id + "' is hidden" );
     plus();
     //TODO: David Humphrey has written code that allows for listening to events on Processing instances. when 
     // it lands In the next processing release we'll be able to detect if a processing instance is looping or not.
@@ -77,7 +86,7 @@ test( "Processing plugin tests", function() {
     plus();
     equals( processingDiv3.children[ 0 ].style.display, "none", "canvas '" + processingDiv3.children[ 0 ].id + "' is hidden" );
     plus();
-    equals( processingCanvas.style.display, "none", "canvas '" + processingCanvas.id + "' is hidden" );
+    equals( processingDiv4.children[ 0 ].style.display, "none", "canvas '" + processingDiv4.children[ 0 ].id + "' is hidden" );
     plus();
   }).exec( 3.5, function() {
     equals( processingDiv.children[ 0 ].style.display, "none", "canvas '" + processingDiv.children[ 0 ].id + "' is hidden" );
@@ -86,17 +95,27 @@ test( "Processing plugin tests", function() {
     plus();
     equals( processingDiv3.children[ 0 ].style.display, "inline", "canvas '" + processingDiv3.children[ 0 ].id + "' is displayed" );
     plus();
-    equals( processingCanvas.style.display, "inline", "canvas '" + processingCanvas.id + "' is displayed" );
+    equals( processingDiv4.children[ 0 ].style.display, "inline", "canvas '" + processingDiv4.children[ 0 ].id + "' is displayed" );
     plus();
+  }).exec( 4.5, function() { 
+    var idx = 5, div, id;
+    while ( --idx ) {
+      div = document.getElementById( "processing-div-" + ( idx ) );
+      id = trackids.pop();
+      popped.removeTrackEvent( id );
+      ok ( !div.children[0], "track event was removed" );
+      plus();
+    }
   });
   
-  //check that three canvases were created
+  ok( "processing" in popped, "processing is a method of the popped instance" );
+  plus();
+  
+  //check that four canvases were created
   canvases = document.querySelectorAll( "canvas" ); 
   equals( canvases.length, 4, "four canvases are present" );
   plus();
 
-  var called = 0;
-  
   //enable this when ticket #583 lands
   //Popcorn.forEach( canvases, function( ctx, idx ) {
   [].forEach.call( canvases, function( ctx ) {