]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#565] Added teardown for subtitle plugin, also made the subtitles each have their...
authorDavid Seifried <david.c.seifried@gmail.com>
Wed, 22 Jun 2011 21:05:02 +0000 (17:05 -0400)
committerDavid Seifried <david.c.seifried@gmail.com>
Wed, 22 Jun 2011 21:05:02 +0000 (17:05 -0400)
plugins/subtitle/popcorn.subtitle.js
plugins/subtitle/popcorn.subtitle.unit.js

index 7fce8665525af8b9c18c3568000eb49c6ba9741a..6f5753381c4b3933ce5c35f210cc3838c34c48c9 100755 (executable)
@@ -1,13 +1,14 @@
 // PLUGIN: Subtitle
 
-(function (Popcorn) {
+(function ( Popcorn ) {
 
   var scriptLoaded = false,
-      callBack     = function( data ) {
+      i = 0,
+      callBack = function( data ) {
 
         if ( typeof google !== 'undefined' && google.load ) {
 
-          google.load("language", "1", {callback: function() {scriptLoaded = true;}});
+          google.load( "language", "1", { callback: function() { scriptLoaded = true; } } );
         } else {
 
           setTimeout( function() {
         var updatePosition = function() {
 
           // the video element must have height and width defined
-          context.container.style.fontSize   = "18px";
-          context.container.style.width      = context.media.offsetWidth + "px";
-          context.container.style.top        = context.position().top  + context.media.offsetHeight - context.container.offsetHeight - 40 + "px";
-          context.container.style.left       = context.position().left + "px";
+          context.container.style.fontSize = "18px";
+          context.container.style.width = context.media.offsetWidth + "px";
+          context.container.style.top = context.position().top  + context.media.offsetHeight - context.container.offsetHeight - 40 + "px";
+          context.container.style.left = context.position().left + "px";
 
           setTimeout( updatePosition, 10 );
         };
 
-        context.container = document.createElement('div');
-        context.container.id               = "subtitlediv";
-        context.container.style.position   = "absolute";
-        context.container.style.color      = "white";
+        context.container = document.createElement( 'div' );
+        context.container.id = "subtitlediv";
+        context.container.style.position = "absolute";
+        context.container.style.color = "white";
         context.container.style.textShadow = "black 2px 2px 6px";
         context.container.style.fontWeight = "bold";
-        context.container.style.textAlign  = "center";
+        context.container.style.textAlign = "center";
 
         updatePosition();
 
   var translate = function( options, text ) {
 
     options.selectedLanguage = options.languageSrc.options[ options.languageSrc.selectedIndex ].value;
-
     google.language.translate( text, '', options.selectedLanguage, function( result ) {
-
-      options.container.innerHTML = result.translation;
+      
+      for( var k = 0; k < options.container.children.length; k++ ) {
+        if ( options.container.children[ k ].style.display === "inline" ) {   
+          options.container.children[ k ].innerHTML = result.translation;    
+        }  
+      }
 
     } );
   };
   Popcorn.plugin( "subtitle" , {
     
       manifest: {
-        about:{
+        about: {
           name: "Popcorn Subtitle Plugin",
           version: "0.1",
-          author:  "Scott Downe",
+          author: "Scott Downe",
           website: "http://scottdowne.wordpress.com/"
         },
-        options:{
-          start    : {elem:'input', type:'text', label:'In'},
-          end      : {elem:'input', type:'text', label:'Out'},
-          target  :  'subtitle-container',
-          text     : {elem:'input', type:'text', label:'Text'}
+        options: {
+          start: {
+            elem: 'input', 
+            type: 'text', 
+            label: 'In'
+          },
+          end: {
+            elem: 'input', 
+            type: 'text', 
+            label: 'Out'
+          },
+          target: 'subtitle-container',
+          text: {
+            elem: 'input', 
+            type: 'text', 
+            label: 'Text'
+          }
         }
       },
 
       _setup: function( options ) {
+        var newdiv = document.createElement( "div" );
+        newdiv.id = "subtitle-" + i;
+        newdiv.style.display = "none";
+        i++;
 
         // Creates a div for all subtitles to use
         ( !this.container && !options.target || options.target === 'subtitle-container' ) && 
         } else { // use shared default container
           options.container = this.container;
         }
+        
+        document.getElementById( options.container.id ).appendChild( newdiv );
+        options.innerContainer = newdiv;
 
         var accessibility = document.getElementById( options.accessibilitysrc );
 
         options.showSubtitle = function() {
-          options.container.innerHTML = options.text;
+          options.innerContainer.innerHTML = options.text;
         };
         options.toggleSubtitles = function() {};
         
           if ( !scriptLoaded ) {
             return;
           }
-          clearInterval(readyCheck);
+          clearInterval( readyCheck );
 
           if ( options.languagesrc ) {
             options.showSubtitle = translate;
 
             if ( !this.languageSources[ options.languagesrc ] ) {
               this.languageSources[ options.languagesrc ] = {};
-            
             }
 
             if ( !this.languageSources[ options.languagesrc ][ options.target ] ) {
               options.languageSrc.addEventListener( "change", function() {
 
                 options.toggleSubtitles();
-                options.showSubtitle( options, options.container.innerHTML );
+
+                for( var k = 0; k < options.container.children.length; k++ ) {
+                  if ( options.container.children[ k ].style.display === "inline" ) {   
+                    options.showSubtitle( options, options.container.children[ k ].innerHTML );   
+                  }  
+                }
 
               }, false );
 
             options.accessibility = accessibility;
 
             options.toggleSubtitles = function() {
-              options.selectedLanguage = options.languageSrc.options[ options.languageSrc.selectedIndex ].value;
-              if ( options.accessibility.checked || options.selectedLanguage !== ( options.language || "") ) {
-                options.display = "inline";
-                options.container.style.display = options.display;
-              } else if ( options.selectedLanguage === ( options.language || "") ) {
-                options.display = "none";
-                options.container.style.display = options.display;
-              }
-
+              options.selectedLanguage = options.languageSrc.options[ options.languageSrc.selectedIndex ].value;              
             };
 
             options.accessibility.addEventListener( "change", options.toggleSubtitles, false );
        * of the video  reaches the start time provided by the 
        * options variable
        */
-      start: function(event, options){
-        options.container.style.display = options.display;
+      start: function( event, options ){
+        options.innerContainer.style.display = "inline";
         options.showSubtitle( options, options.text );
       },
       /**
        * of the video  reaches the end time provided by the 
        * options variable
        */
-      end: function(event, options){
-        options.container.style.display = options.display;
-        options.container.innerHTML = "";
+      end: function( event, options ) {
+        options.innerContainer.style.display = "none";
+        options.innerContainer.innerHTML = "";
+      },
+
+      _teardown: function ( options ) {
+        options.container.removeChild( options.innerContainer );
       }
    
   } );
index 2206bc21654f3e56a9e402b0217957cf45b83ffe..fe3e965fb6c6a686ab4651cd81a8222956b48cdc 100755 (executable)
@@ -1,7 +1,7 @@
 test("Popcorn Subtitle Plugin", function () {
  
-  var popped = Popcorn("#video"),
-      expects = 9,
+  var popped = Popcorn( "#video" ),
+      expects = 11,
       count = 0,
       subTop = 9001,
       subLeft = 9001,
@@ -10,14 +10,14 @@ test("Popcorn Subtitle Plugin", function () {
   expect(expects);
  
   function plus() {
-    if ( ++count===expects) {
+    if ( ++count === expects ) {
       start();
     }
   }
  
   stop();
    
-  ok ('subtitle' in popped, "subtitle is a method of the popped instance");
+  ok ( 'subtitle' in popped, "subtitle is a method of the popped instance" );
   plus();
  
   popped.subtitle({
@@ -47,12 +47,12 @@ test("Popcorn Subtitle Plugin", function () {
     .volume(0)
     .play();
  
-  subtitlediv = document.getElementById('subtitlediv');
- 
+  subtitlediv = document.getElementById( 'subtitlediv' );
+
   popped.exec( 0.5, function() {
    
     popped.media.pause();
-    equals( subtitlediv.innerHTML, "this is the first subtitle of 2011", "subtitle displaying correct information" );
+    equals( subtitlediv.children[ 0 ].innerHTML, "this is the first subtitle of 2011", "subtitle displaying correct information" );
     plus();
    
  
@@ -89,7 +89,7 @@ test("Popcorn Subtitle Plugin", function () {
     ok( Popcorn.position( subtitlediv ).top > popped.position().top, "subtitle top position moved" );
     plus();
  
-    equals (subtitlediv.innerHTML, "this is the second subtitle of 2011", "subtitle displaying correct information" );
+    equals (subtitlediv.children[ 1 ].innerHTML, "this is the second subtitle of 2011", "subtitle displaying correct information" );
     plus();
     popped.media.play();
    
@@ -98,7 +98,7 @@ test("Popcorn Subtitle Plugin", function () {
   popped.exec( 2.5, function() {
    
     popped.media.pause();
-    equals (subtitlediv.innerHTML, "", "subtitle is clear" );
+    equals (subtitlediv.children[ 1 ].innerHTML, "", "subtitle is clear" );
     plus();
     popped.media.play();
  
@@ -107,10 +107,22 @@ test("Popcorn Subtitle Plugin", function () {
   popped.exec( 3.5, function() {
  
     popped.media.play();
-    equals (subtitlediv.innerHTML, "this is the third subtitle of 2011", "subtitle displaying correct information" );
+    equals (subtitlediv.children[ 2 ].innerHTML, "this is the third subtitle of 2011", "subtitle displaying correct information" );
     plus();
     popped.media.play();
    
   });
+
+  popped.exec( 5, function() {
+    ok ( document.getElementById( 'subtitle-0 ').style.display === "none" && 
+        document.getElementById( 'subtitle-1' ).style.display === "none" &&
+        document.getElementById( 'subtitle-2' ).style.display === "none", "All subtitles are no longer visible" );
+    plus();
+
+    popped.pause().removeTrackEvent( popped.data.trackEvents.byStart[ 6 ]._id );
+
+    ok( !document.getElementById( 'subtitle-2' ), "removed subtitle div was properly destroyed"  );
+    plus();
+  });
 });