From: Christopher De Cairos Date: Fri, 16 Sep 2011 20:28:26 +0000 (-0400) Subject: moved data-timeline automagical parsing into a new dir: modules, fixed core unit... X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=0d5540a53438aa9480160d415cf144165970fc6c;p=popcorn-js.git moved data-timeline automagical parsing into a new dir: modules, fixed core unit test issues associate with the move. added unit tests for automagical parsing. fixed data-timeline parsing to accept relative paths --- diff --git a/modules/timeline-sources/popcorn.timeline-sources.js b/modules/timeline-sources/popcorn.timeline-sources.js new file mode 100644 index 0000000..3970350 --- /dev/null +++ b/modules/timeline-sources/popcorn.timeline-sources.js @@ -0,0 +1,64 @@ +(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 ); \ No newline at end of file diff --git a/modules/timeline-sources/popcorn.timeline-sources.unit.html b/modules/timeline-sources/popcorn.timeline-sources.unit.html new file mode 100644 index 0000000..122aee3 --- /dev/null +++ b/modules/timeline-sources/popcorn.timeline-sources.unit.html @@ -0,0 +1,43 @@ + + + + Popcorn Module: Timeline-Sources + + + + + + + + + +

Popcorn Module: timeline-sources

+

+
+

+
    +
    + +   + + diff --git a/modules/timeline-sources/popcorn.timeline-sources.unit.js b/modules/timeline-sources/popcorn.timeline-sources.unit.js new file mode 100644 index 0000000..e48f174 --- /dev/null +++ b/modules/timeline-sources/popcorn.timeline-sources.unit.js @@ -0,0 +1,79 @@ +(function( Popcorn ) { + + var setupCalled = 0, + startCalled = 0, + endCalled = 0; + + Popcorn.plugin( "footnote", function( options ) { + return { + _setup: function() { + setupCalled++; + }, + start: function() { + startCalled++; + }, + end: function() { + endCalled++; + } + }; + }); + + Popcorn.plugin( "googlemap", function( options ) { + return { + _setup: function() { + setupCalled++; + }, + start: function() { + startCalled++; + }, + end: function() { + endCalled++; + } + }; + }); + + Popcorn.plugin( "webpage", function( options ) { + return { + _setup: function() { + setupCalled++; + }, + start: function() { + startCalled++; + }, + end: function() { + endCalled++; + } + }; + }); + + test( "Timeline-sources Module", function() { + expect( 3 ); + + var count = 0, + p = Popcorn.getInstanceById( "video" ); + + function plus() { + if ( ++count === 3 ) { + start(); + } + } + + stop(); + + p.exec( 3, function() { + equals( startCalled, 5, "start was called 5 times from the parsed data" ); + plus(); + p.currentTime( 9 ); + }); + + p.exec( 10.5, function() { + equals( endCalled, 5, "end was called 5 times from the parsed data" ); + plus() + }); + + equals( setupCalled, 5, "setup was called 5 times from the parsed data" ); + plus(); + + }); + +})( Popcorn ); \ No newline at end of file diff --git a/popcorn.js b/popcorn.js index e2c7320..4376580 100644 --- a/popcorn.js +++ b/popcorn.js @@ -1777,65 +1777,4 @@ // Exposes Popcorn to global context global.Popcorn = 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 ) { - - data = source.split( "." ); - data[ 0 ] = "parse" + data[ data.length - 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 ); - })(window, window.document); - diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 6dfff8f..37f0340 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -268,7 +268,7 @@ test("Instances", function() { ok( instance instanceof Popcorn, "Instance instanceof Popcorn" ); plus(); - equal( Popcorn.instances.length, 2, "There are the correct number of Popcorn instances" ); + equal( Popcorn.instances.length, 1, "There are the correct number of Popcorn instances" ); plus(); // Create another instance @@ -278,7 +278,7 @@ test("Instances", function() { var remove = Popcorn.instances[1]; // Remove and check the length of the currently cached instances - equal( Popcorn.removeInstanceById( remove.id ).length, 2, "Removing an instance by id: 1 instance remains" ); + equal( Popcorn.removeInstanceById( remove.id ).length, 1, "Removing an instance by id: 1 instance remains" ); plus(); });