]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
adds wrapper div to timeline, fixes teardown, fixes up/down, added tests, support...
authorChristopher De Cairos <chris.decairos@gmail.com>
Tue, 23 Aug 2011 20:15:29 +0000 (16:15 -0400)
committerChristopher De Cairos <chris.decairos@gmail.com>
Tue, 23 Aug 2011 20:15:29 +0000 (16:15 -0400)
plugins/timeline/popcorn.timeline.html
plugins/timeline/popcorn.timeline.js
plugins/timeline/popcorn.timeline.unit.html
plugins/timeline/popcorn.timeline.unit.js

index e6dc422fe4793d5f6909c0d7d2ab7e0f73a3527a..3d8a48bcd8f62850f9a4cced902632e4f0d4a158 100644 (file)
@@ -16,7 +16,7 @@
         title: "This is a title",
         text: "this is some interesting text that goes inside",
         innerHTML: "Click here for <a href='http://www.google.ca'>Google</a>" ,
-        direction: "down"
+        direction: "up"
       } )
       .timeline({
         start: 3, // seconds
@@ -24,7 +24,7 @@
         title: "double as interesting",
         text: "this is some interesting text that goes inside",
         innerHTML: "Maybe a button? <button onClick=\"window.location.href='http://www.google.com'\">Click Me</button>",
-        direction: "down"
+        direction: "up"
       } )
      .timeline({
         start: 7, // seconds
         title: "3x as interesting",
         text: "this is some interesting text that goes inside",
         innerHTML: "",
-        direction: "down"
+        direction: "up"
       } )
-     .timeline({
-        start: 9, // seconds
-        target: "timeline",
-        title: "4x as interesting",
+      .timeline({
+        start: 1, // seconds
+        target: "timeline2",
+        title: "This is a title",
         text: "this is some interesting text that goes inside",
-        innerHTML: "perhaps tables <table border='1'>" +
-                      "<tr>" +
-                        "<td>row 1, cell 1</td>" +
-                        "<td>row 1, cell 2</td>" +
-                      "</tr>" +
-                      "<tr>" +
-                        "<td>row 2, cell 1</td>" +
-                       " <td>row 2, cell 2</td>" +
-                      "</tr>" +
-                    "</table>",
+        innerHTML: "Click here for <a href='http://www.google.ca'>Google</a>" ,
         direction: "down"
       } )
       .timeline({
-        start: 14, // seconds
-        target: "timeline",
-        title: "5x as interesting",
+        start: 3, // seconds
+        target: "timeline2",
+        title: "double as interesting",
         text: "this is some interesting text that goes inside",
-        innerHTML: "Click here for <a href='http://www.apple.com'>Apple</a>",
+        innerHTML: "Maybe a button? <button onClick=\"window.location.href='http://www.google.com'\">Click Me</button>",
         direction: "down"
       } )
-      .timeline({
-        start: 18, // seconds
-        target: "timeline",
-        title: "6x as interesting",
+     .timeline({
+        start: 7, // seconds
+        end: 700,
+        target: "timeline2",
+        title: "3x as interesting",
         text: "this is some interesting text that goes inside",
         innerHTML: "",
         direction: "down"
-      } );
+      } )
     }, false);
   </script>
 </head>
@@ -93,7 +85,8 @@
     </video>
   </div>
   <div style="width:1000px">
-    <div id="timeline"></div>
+    <div id="timeline" style="float:left;width: 250px; height: 250px;"></div>
+    <div id="timeline2" style="float:left;width: 250px; height: 250px;"></div>
   </div>
 </body>
 </html>
index ddc1fc4f97222a1e79caff295856df2bee768d91..e7d56da59789eec98b0dc174198ba79e4c736d61 100644 (file)
@@ -7,7 +7,7 @@
      * Options parameter will need a start, target, title, and text
      * -Start is the time that you want this plug-in to execute
      * -End is the time that you want this plug-in to stop executing, tho for this plugin an end time may not be needed ( optional )
-     * -Target is the id of the DOM element that you want the map to appear in. This element must be in the DOM
+     * -Target is the id of the DOM element that you want the timeline to appear in. This element must be in the DOM
      * -Title is the title of the current timeline box
      * -Text is text is simply related text that will be displayed
      * -innerHTML gives the user the option to add things such as links, buttons and so on
   Popcorn.plugin( "timeline" , function( options ) {
 
     var target = document.getElementById( options.target ),
-        newdiv = document.createElement( "div" );
-
-    target.style.width = "400px";
-    target.style.height = "200px";
-    target.style.overflow = "auto";
+        contentDiv = document.createElement( "div" ),
+        container,
+        goingUp;
+
+    if ( target && !target.firstChild ) {
+      target.appendChild ( container = document.createElement( "div" ) );
+      container.style.width = "inherit";
+      container.style.height = "inherit";
+      container.style.overflow = "auto";
+      container.id = "timeline-container";
+    } else {
+      container = target.firstChild;
+    }
 
-    newdiv.style.display = "none";
-    newdiv.id = "timelineDiv" + i;
+    contentDiv.style.display = "none";
+    contentDiv.id = "timelineDiv" + i;
 
     //  Default to up if options.direction is non-existant or not up or down
     options.direction = options.direction || "up";
-    if ( options.direction.toLowerCase() !== "up" || options.direction.toLowerCase() !== "down" ) {
-      options.direction = "up";
+    if ( options.direction.toLowerCase() === "up" ) {
+    
+      goingUp = true;
     }
+    else if ( options.direction.toLowerCase() === "down" ) {
 
-    if ( target ) {
-
-      target.appendChild( newdiv );
+      goingUp = false;
+    }
+    else {
+    
+      goingUp = true;
+    }
 
+    if ( target && container ) {
       // if this isnt the first div added to the target div
-      if( options.direction.length === 2 ){
+      if( goingUp ){
         // insert the current div before the previous div inserted
-        target.insertBefore( newdiv, document.getElementById( "timelineDiv" + ( i - 1 ) ) );
+        container.insertBefore( contentDiv, container.firstChild );
+      }
+      else {
+        
+        container.appendChild( contentDiv );
       }
 
     }
     //  Default to empty if not used
     //options.innerHTML = options.innerHTML || "";    
 
-    newdiv.innerHTML = "<p><span id='big'>" + options.title + "</span><br />" +
+    contentDiv.innerHTML = "<p><span id='big'>" + options.title + "</span><br />" +
     "<span id='mid'>" + options.text + "</span><br />" + options.innerHTML;
     
     return {
 
       start: function( event, options ) {
-        newdiv.style.display = "block";
+        contentDiv.style.display = "block";
         
         if( options.direction === "down" ) {
-          target.scrollTop = target.scrollHeight;
+          container.scrollTop = container.scrollHeight;
         }
       },
  
       end: function( event, options ) {
-        newdiv.style.display = "none";
+        contentDiv.style.display = "none";
       },
 
       _teardown: function( options ) {
-        while ( target.firstChild ) {
-          target.removeChild( target.firstChild );
-        } 
+
+        ( container && contentDiv ) && container.removeChild( contentDiv ) && !container.firstChild && target.removeChild( container );
       }
     };
   },
index 8adf467406d7a2ca3fd1750b7145212dc6202911..488bf66b4cfe931bb8c67fb547f8d64ec0a1f876 100644 (file)
@@ -25,7 +25,8 @@
   <video id='video' 
     controls preload='none' 
     width= '250px'
-    poster="../../test/poster.png"> 
+    poster="../../test/poster.png"
+    style="float: left;"> 
 
     <source id='mp4'
       src="../../test/trailer.mp4"
@@ -38,6 +39,7 @@
     <p>Your user agent does not support the HTML5 Video element.</p> 
 
   </video> 
-  <div id="timeline" style="float:left;width:400px;height:200px;overflow:auto"></div>  
+  <div id="timeline-up" style="float:left;width:400px;height:200px;overflow:auto"></div>
+  <div id="timeline-down" style="float:left;width:400px;height:200px;overflow:auto"></div>
 </body>
 </html>
index cd680d52328f09fc3fbb250da65a4b4fe20ee8ba..f337149754aadb696a7f68765e68a2727fe1dee0 100644 (file)
@@ -1,9 +1,11 @@
 test("Popcorn Timeline Plugin", function () {
   
   var popped = Popcorn( "#video" ),
-      expects = 8,
-      setupId, 
-      count = 0;
+      expects = 17,
+      upIds = downIds = [], 
+      count = 0,
+      timelineUp = document.getElementById( "timeline-up" ),
+      timelineDown = document.getElementById( "timeline-down" );
   
   expect( expects );
   
@@ -15,48 +17,108 @@ test("Popcorn Timeline Plugin", function () {
   
   stop();
 
-    ok ( 'timeline' in popped, "timeline is a method of the popped instance" );
+    ok ( "timeline" in popped, "timeline is a method of the popped instance" );
     plus();
-    ok ( document.getElementById( 'timeline' ).innerHTML === "", "initially, there is nothing inside the feed" );
+    equals ( document.getElementById( "timeline-up" ).firstChild, undefined, "initially, there is nothing inside the 'timeline-up' feed" );
+    plus();
+    equals ( document.getElementById( "timeline-down" ).firstChild, undefined, "initially, there is nothing inside the 'timeline-down' feed" );
     plus();
   
     popped.timeline({
       start: 1, // seconds
-      target: "timeline",
+      target: "timeline-up",
       title: "This is a title",
       text: "this is some interesting text that goes inside",
-      innerHTML: "Click here for <a href='http://www.google.ca'>Google</a>" 
-    } )
-    .timeline({
+      innerHTML: "Click here for <a href='http://www.google.ca'>Google</a>"
+    } );
+    upIds.push( popped.getLastTrackEventId() );
+    
+    popped.timeline({
       start: 2, // seconds
-      target: "timeline",
+      target: "timeline-up",
       title: "double as interesting",
       text: "this is some interesting text that goes inside",
       innerHTML: "Maybe a button? <button onClick=\"window.location.href='http://www.google.com'\">Click Me</button>" 
-    } )
-    .volume(0);
-
-  setupId = popped.getLastTrackEventId();
+    } );
+    upIds.push( popped.getLastTrackEventId() );
+    
+    popped.timeline({
+      start: 1, // seconds
+      target: "timeline-down",
+      title: "This is a title",
+      text: "this is some interesting text that goes inside",
+      innerHTML: "Maybe a button? <button onClick=\"window.location.href='http://www.google.com'\">Click Me</button>",
+      direction: "down"
+    } );
+    downIds.push( popped.getLastTrackEventId() );
+    
+    popped.timeline({
+      start: 2, // seconds
+      target: "timeline-down",
+      title: "double as interesting",
+      text: "this is some interesting text that goes inside",
+      innerHTML: "Maybe a button? <button onClick=\"window.location.href='http://www.google.com'\">Click Me</button>",
+      direction: "down"
+    } );
+    downIds.push( popped.getLastTrackEventId() );
+    
+    popped.volume(0);
   
-  popped.exec( 2, function() {
-    ok ( document.getElementById( 'timelineDiv1' ), "First timeline is on the page" );
+  popped.exec( 1.5, function() {
+    //up
+    ok ( document.getElementById( "timelineDiv1" ), "First timeline is on the page" );
     plus();
-    equals ( document.getElementById('timelineDiv1' ).parentNode.id, "timeline", "First timeline is inside the 'timeline' div" );
+    equals ( document.getElementById("timelineDiv1" ).parentNode.parentNode.id, "timeline-up", "First timeline is inside the 'timeline-up' div" );
     plus();
+    
+    //down
+    ok ( document.getElementById( "timelineDiv3" ), "First timeline is on the page" );
+    plus();
+    equals ( document.getElementById("timelineDiv3" ).parentNode.parentNode.id, "timeline-down", "First Timeline is inside the 'timeline-down' div" );
+    plus();
+    
   });
-  popped.exec( 3, function() {
-    ok ( document.getElementById( 'timelineDiv2' ), "Second timeline is on the page" );
+  popped.exec( 2.5, function() {
+   //up
+    var timelinediv2 = document.getElementById( "timelineDiv2" ),
+        timelinediv4 = document.getElementById( "timelineDiv4" );
+    ok ( timelinediv2, "Second timeline is on the page" );
+    plus();
+    equals ( timelinediv2 && timelinediv2.parentNode.parentNode.id, "timeline-up", "Second timelineDiv2 is inside the 'timeline-up' div" );
+    plus();
+    equals ( timelineUp.firstChild.firstChild.id,  "timelineDiv2", "'timelineDiv2' is the first child in 'timeline-up'" );
+    plus();
+    
+    //down
+    
+    ok ( timelinediv4, "Second down timeline is on the page" );
+    plus();
+    equals ( timelinediv4.parentNode.parentNode.id, "timeline-down", "timelineDiv4 is inside the 'timeline-down' div" );
     plus();
-    equals ( document.getElementById( 'timelineDiv2' ).parentNode.id, "timeline", "Second timelineDiv2 is inside the 'timeline' div" );
+    equals ( timelineDown.firstChild.firstChild.id, "timelineDiv3", "'timelineDiv3' is the first child in 'timeline-down'" );
     plus();
   });
   popped.exec( 4, function() {
-    ok (document.getElementById( 'timelineDiv1' ).style.display !== "none" && 
-        document.getElementById( 'timelineDiv2' ).style.display !== "none", "Both timelines are not visible" );
+    ok (document.getElementById( "timelineDiv1" ).style.display !== "none" && 
+        document.getElementById( "timelineDiv2" ).style.display !== "none", "Both timelines in 'timeline-up are not visible" );
          plus();
-
-    popped.pause().removeTrackEvent( setupId );
-    ok( !document.getElementById( 'timeline' ).children[ 0 ], "removed timeline was properly destroyed"  );
+         
+         ok (document.getElementById( "timelineDiv3" ).style.display !== "none" && 
+        document.getElementById( "timelineDiv4" ).style.display !== "none", "Both timelines in 'timeline-down' are not visible" );
+         plus();
+    
+    var uid, did;
+    while ( uid = upIds.pop() ) {
+      popped.removeTrackEvent( uid );
+    }
+    while ( did = downIds.pop() ) {
+      popped.removeTrackEvent( did );
+    }
+    
+    popped.pause();
+    equals( document.getElementById( "timeline-up" ).firstChild, null, "removed timeline was properly destroyed"  );
+    plus();
+    equals( document.getElementById( "timeline-down" ).firstChild, null, "removed timeline was properly destroyed"  );
     plus();
   });