From d5adf461ffb8b74e27f3a32c0a835ea9513b1bbc Mon Sep 17 00:00:00 2001 From: ScottDowne Date: Fri, 2 Mar 2012 17:57:36 -0500 Subject: [PATCH] [#916] internal use of canplaytype and other nits --- modules/player/popcorn.player.js | 70 +++++++++++++++------------ modules/player/popcorn.player.unit.js | 45 +++++++++++++++-- players/vimeo/popcorn.vimeo.js | 18 ------- 3 files changed, 78 insertions(+), 55 deletions(-) diff --git a/modules/player/popcorn.player.js b/modules/player/popcorn.player.js index b017c212..c9d27d08 100644 --- a/modules/player/popcorn.player.js +++ b/modules/player/popcorn.player.js @@ -1,7 +1,7 @@ (function( Popcorn ) { // ID string matching - rIdExp = /^(#([\w\-\_\.]+))$/; + var rIdExp = /^(#([\w\-\_\.]+))$/; Popcorn.player = function( name, player ) { @@ -195,15 +195,23 @@ basePlayer.addEventListener( key, val, false ); }); - if ( player._setup ) { + // true and undefined returns on canPlayType means we should attempt to use it, + // false means we cannot play this type + if ( player._canPlayType( container.nodeName, src ) !== false ) { - player._setup.call( basePlayer, options ); + if ( player._setup ) { + + player._setup.call( basePlayer, options ); + } else { + + // there is no setup, which means there is nothing to load + basePlayer.readyState = 4; + basePlayer.dispatchEvent( "load" ); + basePlayer.dispatchEvent( "loadeddata" ); + } } else { - // there is no setup, which means there is nothing to load - basePlayer.readyState = 4; - basePlayer.dispatchEvent( "load" ); - basePlayer.dispatchEvent( "loadeddata" ); + basePlayer.dispatchEvent( "error" ); } // when a custom player is loaded, load basePlayer state into custom player @@ -228,7 +236,7 @@ return popcorn; }; - playerFn.canPlayType = player._canPlayType || Popcorn.nop; + playerFn.canPlayType = player._canPlayType = player._canPlayType || Popcorn.nop; Popcorn[ name ] = Popcorn.player.registry[ name ] = Popcorn[ name ] || playerFn; }; @@ -264,34 +272,32 @@ return Popcorn( node, options ); } - // for now we loop through and use the last valid player we find. - // not yet sure what to do when two players both find it valid. - Popcorn.forEach( Popcorn.player.registry, function( val, key ) { + // for now we loop through and use the first valid player we find. + for ( var key in Popcorn.player.registry ) { - if ( val.canPlayType( node.nodeName, src ) === true ) { + if ( Popcorn.player.registry.hasOwnProperty( key ) ) { - playerType = key; + if ( Popcorn.player.registry[ key ].canPlayType( node.nodeName, src ) ) { + + // Popcorn.smart( player, src, /* options */ ) + return Popcorn[ key ]( target, src, options ); + } } - }); - - // Popcorn.smart( div, src, /* options */ ) - if ( !Popcorn[ playerType ] ) { - - if ( node.nodeType !== "VIDEO" ) { - - target = document.createElement( "video" ); - - node.appendChild( target ); - node = target; - } - - options && options.onerror && node.addEventListener( "error", options.onerror, false ); - node.src = src; - - return Popcorn( node, options ); } - // Popcorn.smart( player, src, /* options */ )\ - return Popcorn[ playerType ]( target, src, options ); + // Popcorn.smart( div, src, /* options */ ) + // attempting to create a video in a container + if ( node.nodeType !== "VIDEO" ) { + + target = document.createElement( "video" ); + + node.appendChild( target ); + node = target; + } + + options && options.events && options.events.error && node.addEventListener( "error", options.events.error, false ); + node.src = src; + + return Popcorn( node, options ); }; })( Popcorn ); diff --git a/modules/player/popcorn.player.unit.js b/modules/player/popcorn.player.unit.js index 581a967a..1b44f70c 100644 --- a/modules/player/popcorn.player.unit.js +++ b/modules/player/popcorn.player.unit.js @@ -193,7 +193,7 @@ test( "Base player functionality", function() { test( "Popcorn.smart player selector", function() { - var expects = 8, + var expects = 10, count = 0; function plus() { @@ -208,7 +208,7 @@ test( "Popcorn.smart player selector", function() { Popcorn.player( "spartaPlayer", { _canPlayType: function( nodeName, url ) { - return /this is sparta/.test( url ) && nodeName !== "unsupported element"; + return url === "this is sparta" && nodeName !== "unsupported element"; } }); @@ -229,10 +229,12 @@ test( "Popcorn.smart player selector", function() { plus(); var thisIsNotSparta = Popcorn.smart( "#video", "this is not sparta", { - onerror: function( e ) { + events: { + error: function( e ) { - ok( true, "invalid player failed and called onerror callback" ); - plus(); + ok( true, "invalid player failed and called error callback" ); + plus(); + } } }); @@ -242,4 +244,37 @@ test( "Popcorn.smart player selector", function() { // no existing canPlayType function returns undefined ok( Popcorn.neverEverLand.canPlayType( "guessing time!", "is this sparta?" ) === undefined, "non exist canPlayType returns undefined" ); plus(); + + var loaded = false, + error = false; + + Popcorn.player( "somePlayer", { + _canPlayType: function( nodeName, url ) { + + return url === "canPlayType"; + } + }); + + Popcorn.somePlayer( "#video", "canPlayType", { + events: { + load: function( e ) { + + loaded = true; + } + } + }).destroy(); + + Popcorn.somePlayer( "#video", "cantPlayType", { + events: { + error: function( e ) { + + error = true; + } + } + }).destroy(); + + equals( loaded, true, "canPlayType passed on a valid type" ); + plus(); + equals( error, true, "canPlayType failed on an invalid type" ); + plus(); }); diff --git a/players/vimeo/popcorn.vimeo.js b/players/vimeo/popcorn.vimeo.js index 8aab95de..b66eabae 100644 --- a/players/vimeo/popcorn.vimeo.js +++ b/players/vimeo/popcorn.vimeo.js @@ -216,24 +216,6 @@ media.dispatchEvent( "loadeddata" ); }; - function extractId( videoUrl ) { - - if ( !videoUrl ) { - return; - } - - var rPlayerUri = /^http:\/\/player\.vimeo\.com\/video\/[\d]+/i, - rWebUrl = /vimeo\.com\/[\d]+/; - - var matches = videoUrl.match( rPlayerUri ) ? videoUrl.match( rPlayerUri )[ 0 ].substr( 30 ) : ""; - return matches ? matches : videoUrl.match( rWebUrl ) ? videoUrl.match( rWebUrl )[ 0 ].substr( 10 ) : ""; - } - - if ( !( src = extractId( src ) ) ) { - - throw "Invalid Video Id"; - } - flashvars = { clip_id: src, js_api: 1,