popcorn-js/modules/timeline-sources/popcorn.timeline-sources.js

64 строки
1.8 KiB
JavaScript

(function( Popcorn ) {
document.addEventListener( "DOMContentLoaded", function() {
// Supports non-specific elements
var dataAttr = "data-timeline-sources",
medias = document.querySelectorAll( "[" + dataAttr + "]" );
Popcorn.forEach( medias, function( idx, key ) {
var media = medias[ key ],
hasDataSources = false,
dataSources, data, popcornMedia;
// Ensure that the DOM has an id
if ( !media.id ) {
media.id = Popcorn.guid( "__popcorn" );
}
// Ensure we're looking at a dom node
if ( media.nodeType && media.nodeType === 1 ) {
popcornMedia = Popcorn( "#" + media.id );
dataSources = ( media.getAttribute( dataAttr ) || "" ).split( "," );
if ( dataSources[ 0 ] ) {
Popcorn.forEach( dataSources, function( source ) {
// split the parser and data as parser!file
data = source.split( "!" );
// if no parser is defined for the file, assume "parse" + file extension
if ( data.length === 1 ) {
// parse a relative URL for the filename, split to get extension
data = source.match( /(.*)[\/\\]([^\/\\]+\.\w+)$/ )[ 2 ].split( "." );
data[ 0 ] = "parse" + data[ 1 ].toUpperCase();
data[ 1 ] = source;
}
// If the media has data sources and the correct parser is registered, continue to load
if ( dataSources[ 0 ] && popcornMedia[ data[ 0 ] ] ) {
// Set up the media and load in the datasources
popcornMedia[ data[ 0 ] ]( data[ 1 ] );
}
});
}
// Only play the media if it was specified to do so
if ( !!popcornMedia.autoplay() ) {
popcornMedia.play();
}
}
});
}, false );
})( Popcorn );