From 249fc2e650602b320fc1eab3cd1fc4f6f51a0dcc Mon Sep 17 00:00:00 2001 From: David Seifried Date: Wed, 22 Jun 2011 17:05:02 -0400 Subject: [PATCH] [#565] Added teardown for subtitle plugin, also made the subtitles each have their own child div --- plugins/subtitle/popcorn.subtitle.js | 103 +++++++++++++--------- plugins/subtitle/popcorn.subtitle.unit.js | 32 ++++--- 2 files changed, 85 insertions(+), 50 deletions(-) diff --git a/plugins/subtitle/popcorn.subtitle.js b/plugins/subtitle/popcorn.subtitle.js index 7fce866..6f57533 100755 --- a/plugins/subtitle/popcorn.subtitle.js +++ b/plugins/subtitle/popcorn.subtitle.js @@ -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() { @@ -24,21 +25,21 @@ 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(); @@ -100,10 +101,13 @@ 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; + } + } } ); }; @@ -111,21 +115,37 @@ 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' ) && @@ -137,11 +157,14 @@ } 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() {}; @@ -149,7 +172,7 @@ if ( !scriptLoaded ) { return; } - clearInterval(readyCheck); + clearInterval( readyCheck ); if ( options.languagesrc ) { options.showSubtitle = translate; @@ -162,7 +185,6 @@ if ( !this.languageSources[ options.languagesrc ] ) { this.languageSources[ options.languagesrc ] = {}; - } if ( !this.languageSources[ options.languagesrc ][ options.target ] ) { @@ -171,7 +193,12 @@ 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 ); @@ -182,15 +209,7 @@ 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 ); @@ -207,8 +226,8 @@ * 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 ); }, /** @@ -217,9 +236,13 @@ * 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 ); } } ); diff --git a/plugins/subtitle/popcorn.subtitle.unit.js b/plugins/subtitle/popcorn.subtitle.unit.js index 2206bc2..fe3e965 100755 --- a/plugins/subtitle/popcorn.subtitle.unit.js +++ b/plugins/subtitle/popcorn.subtitle.unit.js @@ -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(); + }); }); -- 2.43.0