зеркало из https://github.com/mozilla/popcorn-js.git
[#916] internal use of canplaytype and other nits
This commit is contained in:
Родитель
60c03fc2c7
Коммит
d5adf461ff
|
@ -1,7 +1,7 @@
|
|||
(function( Popcorn ) {
|
||||
|
||||
// ID string matching
|
||||
rIdExp = /^(#([\w\-\_\.]+))$/;
|
||||
var rIdExp = /^(#([\w\-\_\.]+))$/;
|
||||
|
||||
Popcorn.player = function( name, player ) {
|
||||
|
||||
|
@ -195,6 +195,10 @@
|
|||
basePlayer.addEventListener( key, val, false );
|
||||
});
|
||||
|
||||
// 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 ) {
|
||||
|
||||
if ( player._setup ) {
|
||||
|
||||
player._setup.call( basePlayer, options );
|
||||
|
@ -205,6 +209,10 @@
|
|||
basePlayer.dispatchEvent( "load" );
|
||||
basePlayer.dispatchEvent( "loadeddata" );
|
||||
}
|
||||
} else {
|
||||
|
||||
basePlayer.dispatchEvent( "error" );
|
||||
}
|
||||
|
||||
// when a custom player is loaded, load basePlayer state into custom player
|
||||
basePlayer.addEventListener( "load", function() {
|
||||
|
@ -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,19 +272,21 @@
|
|||
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 ] ) {
|
||||
|
||||
// attempting to create a video in a container
|
||||
if ( node.nodeType !== "VIDEO" ) {
|
||||
|
||||
target = document.createElement( "video" );
|
||||
|
@ -285,13 +295,9 @@
|
|||
node = target;
|
||||
}
|
||||
|
||||
options && options.onerror && node.addEventListener( "error", options.onerror, false );
|
||||
options && options.events && options.events.error && node.addEventListener( "error", options.events.error, false );
|
||||
node.src = src;
|
||||
|
||||
return Popcorn( node, options );
|
||||
}
|
||||
|
||||
// Popcorn.smart( player, src, /* options */ )\
|
||||
return Popcorn[ playerType ]( target, src, options );
|
||||
};
|
||||
})( Popcorn );
|
||||
|
|
|
@ -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,11 +229,13 @@ 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" );
|
||||
ok( true, "invalid player failed and called error callback" );
|
||||
plus();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
equals( thisIsNotSparta.media.nodeName, "VIDEO", "no player was found for this URL, default to video element" );
|
||||
|
@ -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();
|
||||
});
|
||||
|
|
|
@ -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,
|
||||
|
|
Загрузка…
Ссылка в новой задаче