зеркало из https://github.com/mozilla/popcorn-js.git
103 строки
2.3 KiB
JavaScript
103 строки
2.3 KiB
JavaScript
(function( Popcorn ) {
|
|
|
|
var
|
|
|
|
AP = Array.prototype,
|
|
OP = Object.prototype,
|
|
|
|
forEach = AP.forEach,
|
|
slice = AP.slice,
|
|
hasOwn = OP.hasOwnProperty,
|
|
toString = OP.toString;
|
|
|
|
// stores parsers keyed on filetype
|
|
Popcorn.parsers = {};
|
|
|
|
// An interface for extending Popcorn
|
|
// with parser functionality
|
|
Popcorn.parser = function( name, type, definition ) {
|
|
|
|
if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
|
|
Popcorn.error( "'" + name + "' is a protected function name" );
|
|
return;
|
|
}
|
|
|
|
// fixes parameters for overloaded function call
|
|
if ( typeof type === "function" && !definition ) {
|
|
definition = type;
|
|
type = "";
|
|
}
|
|
|
|
if ( typeof definition !== "function" || typeof type !== "string" ) {
|
|
return;
|
|
}
|
|
|
|
// Provides some sugar, but ultimately extends
|
|
// the definition into Popcorn.p
|
|
|
|
var natives = Popcorn.events.all,
|
|
parseFn,
|
|
parser = {};
|
|
|
|
parseFn = function( filename, callback ) {
|
|
|
|
if ( !filename ) {
|
|
return this;
|
|
}
|
|
|
|
var that = this;
|
|
|
|
Popcorn.xhr({
|
|
url: filename,
|
|
dataType: type,
|
|
success: function( data ) {
|
|
|
|
var tracksObject = definition( data ),
|
|
tracksData,
|
|
tracksDataLen,
|
|
tracksDef,
|
|
idx = 0;
|
|
|
|
tracksData = tracksObject.data || [];
|
|
tracksDataLen = tracksData.length;
|
|
tracksDef = null;
|
|
|
|
// If no tracks to process, return immediately
|
|
if ( !tracksDataLen ) {
|
|
return;
|
|
}
|
|
|
|
// Create tracks out of parsed object
|
|
for ( ; idx < tracksDataLen; idx++ ) {
|
|
|
|
tracksDef = tracksData[ idx ];
|
|
|
|
for ( var key in tracksDef ) {
|
|
|
|
if ( hasOwn.call( tracksDef, key ) && !!that[ key ] ) {
|
|
|
|
that[ key ]( tracksDef[ key ] );
|
|
}
|
|
}
|
|
}
|
|
if ( callback ) {
|
|
callback();
|
|
}
|
|
}
|
|
});
|
|
|
|
return this;
|
|
};
|
|
|
|
// Assign new named definition
|
|
parser[ name ] = parseFn;
|
|
|
|
// Extend Popcorn.p with new named definition
|
|
Popcorn.extend( Popcorn.p, parser );
|
|
|
|
// keys the function name by filetype extension
|
|
//Popcorn.parsers[ name ] = true;
|
|
|
|
return parser;
|
|
};
|
|
})( Popcorn ); |