diff --git a/modules/player/popcorn.player.js b/modules/player/popcorn.player.js index bac46ba6..8d69e03a 100644 --- a/modules/player/popcorn.player.js +++ b/modules/player/popcorn.player.js @@ -1,5 +1,18 @@ (function( Popcorn ) { + // combines calls of two function calls into one + var combineFn = function( first, second ) { + + first = first || Popcorn.nop; + second = second || Popcorn.nop; + + return function() { + + first.apply( this, arguments ); + second.apply( this, arguments ); + }; + }; + Popcorn.player = function( name, player ) { // ID string matching @@ -240,6 +253,11 @@ basePlayer.paused = true; basePlayer.ended = 0; + options && options.events && Popcorn.forEach( options.events, function( val, key ) { + + basePlayer.addEventListener( key, val, false ); + }); + if ( player._setup ) { player._setup.call( basePlayer, options ); @@ -270,6 +288,14 @@ popcorn = new Popcorn.p.init( basePlayer, options ); + if ( player._teardown ) { + + popcorn.destroy = combineFn( popcorn.destroy, function() { + + player._teardown.call( basePlayer, options ); + }); + } + return popcorn; }; @@ -282,4 +308,4 @@ object.__defineSetter__( description, options.set || Popcorn.nop ); }; -})( Popcorn ); \ No newline at end of file +})( Popcorn ); diff --git a/modules/player/popcorn.player.unit.js b/modules/player/popcorn.player.unit.js index 5457efb4..a613f466 100644 --- a/modules/player/popcorn.player.unit.js +++ b/modules/player/popcorn.player.unit.js @@ -187,3 +187,25 @@ test( "Base player functionality", function() { p2.currentTime( 3 ).play(); }); + +test( "player gets a proper _teardown", function() { + + QUnit.reset(); + + var teardownCalled = false; + + expect( 1 ); + stop( 10000 ); + + Popcorn.player( "teardownTester", { + _teardown: function() { + teardownCalled = true; + } + }); + + var pop = Popcorn.teardownTester( "#video" ); + pop.destroy(); + + equal( teardownCalled, true, "teardown function was called." ); + start(); +}); diff --git a/players/youtube/popcorn.youtube.html b/players/youtube/popcorn.youtube.html index 1830e6f5..f8f19aaf 100644 --- a/players/youtube/popcorn.youtube.html +++ b/players/youtube/popcorn.youtube.html @@ -23,7 +23,7 @@ document.addEventListener( 'DOMContentLoaded', function() { var paused = true, popcorn; - + popcorn = Popcorn.youtube( '#video', 'http://www.youtube.com/watch?v=nfGV32RNkhw&autoplay=0' ); popcorn = popcorn diff --git a/players/youtube/popcorn.youtube.js b/players/youtube/popcorn.youtube.js index 959b6a3f..d49b7fd2 100755 --- a/players/youtube/popcorn.youtube.js +++ b/players/youtube/popcorn.youtube.js @@ -27,6 +27,8 @@ Popcorn.player( "youtube", { media.paused = undefined; container.id = media.id + Popcorn.guid(); + options._container = container; + media.appendChild( container ); var youtubeInit = function() { @@ -112,6 +114,8 @@ Popcorn.player( "youtube", { media.dispatchEvent( "loadeddata" ); return; + } else if ( state === 0 ) { + media.dispatchEvent( "ended" ); } }; @@ -128,7 +132,7 @@ Popcorn.player( "youtube", { var timeupdate = function() { - if ( !media.paused ) { + if ( !media.paused && youtubeObject.getCurrentTime ) { currentTime = youtubeObject.getCurrentTime(); media.dispatchEvent( "timeupdate" ); @@ -138,19 +142,22 @@ Popcorn.player( "youtube", { var volumeupdate = function() { - if ( lastMuted !== youtubeObject.isMuted() ) { + if ( youtubeObject.isMuted ) { - lastMuted = youtubeObject.isMuted(); - media.dispatchEvent( "volumechange" ); + if ( lastMuted !== youtubeObject.isMuted() ) { + + lastMuted = youtubeObject.isMuted(); + media.dispatchEvent( "volumechange" ); + } + + if ( lastVolume !== youtubeObject.getVolume() ) { + + lastVolume = youtubeObject.getVolume(); + media.dispatchEvent( "volumechange" ); + } + + setTimeout( volumeupdate, 250 ); } - - if ( lastVolume !== youtubeObject.getVolume() ) { - - lastVolume = youtubeObject.getVolume(); - media.dispatchEvent( "volumechange" ); - } - - setTimeout( volumeupdate, 250 ); }; media.play = function() { @@ -275,5 +282,9 @@ Popcorn.player( "youtube", { youtubeInit(); } + }, + _teardown: function( options ) { + + this.removeChild( document.getElementById( options._container.id ) ); } }); diff --git a/players/youtube/popcorn.youtube.unit.html b/players/youtube/popcorn.youtube.unit.html index 52194dd4..8f53f4ee 100644 --- a/players/youtube/popcorn.youtube.unit.html +++ b/players/youtube/popcorn.youtube.unit.html @@ -42,5 +42,6 @@
+