From: ScottDowne Date: Fri, 18 Feb 2011 21:01:11 +0000 (-0500) Subject: [#297] parser plugin datatype playing nice with xhr and data-timeline-sources, plus... X-Git-Tag: v0.4~32^2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=8026a9f12811e8fad56fed2f673df3d5d026db2a;p=popcorn-js.git [#297] parser plugin datatype playing nice with xhr and data-timeline-sources, plus parser callback for testing --- diff --git a/parsers/parserJSON/popcorn.parserJSON.js b/parsers/parserJSON/popcorn.parserJSON.js index 9d398e2..4297f37 100644 --- a/parsers/parserJSON/popcorn.parserJSON.js +++ b/parsers/parserJSON/popcorn.parserJSON.js @@ -10,7 +10,7 @@ data: [] }, manifestData = {}, - dataObj = data.json.data; + dataObj = data; /* @@ -31,7 +31,7 @@ */ - Popcorn.forEach( dataObj, function ( obj, key ) { + Popcorn.forEach( dataObj.data, function ( obj, key ) { retObj.data.push( obj ); }); diff --git a/parsers/parserSBV/popcorn.parseSBV.js b/parsers/parserSBV/popcorn.parseSBV.js index 364fba8..145bda0 100644 --- a/parsers/parserSBV/popcorn.parseSBV.js +++ b/parsers/parserSBV/popcorn.parseSBV.js @@ -16,7 +16,7 @@ 0:00:02.400,0:00:07.200 Senator, we're making our final approach into Coruscant. */ - Popcorn.parser( "parseSBV", "SBV", function( data ) { + Popcorn.parser( "parseSBV", function( data ) { // declare needed variables var retObj = { diff --git a/parsers/parserSRT/popcorn.parserSRT.js b/parsers/parserSRT/popcorn.parserSRT.js index 2ab5f83..0ff2826 100644 --- a/parsers/parserSRT/popcorn.parserSRT.js +++ b/parsers/parserSRT/popcorn.parserSRT.js @@ -27,7 +27,7 @@ SSA tags with {\i1} would open and close italicize {\i0}, but are stripped Multiple {\pos(142,120)\b1}SSA tags are stripped */ - Popcorn.parser( "parseSRT", "SRT", function( data ) { + Popcorn.parser( "parseSRT", function( data ) { // declare needed variables var retObj = { diff --git a/parsers/parserSSA/popcorn.parseSSA.js b/parsers/parserSSA/popcorn.parseSSA.js index 1597358..51574c1 100644 --- a/parsers/parserSSA/popcorn.parseSSA.js +++ b/parsers/parserSSA/popcorn.parseSSA.js @@ -28,7 +28,7 @@ */ // Register for SSA extensions - Popcorn.parser( "parseSSA", "SSA", function( data ) { + Popcorn.parser( "parseSSA", function( data ) { // declare needed variables var retObj = { diff --git a/parsers/parserTTML/popcorn.parseTTML.js b/parsers/parserTTML/popcorn.parseTTML.js index 42e88fe..e4ec9f8 100644 --- a/parsers/parserTTML/popcorn.parseTTML.js +++ b/parsers/parserTTML/popcorn.parseTTML.js @@ -26,7 +26,7 @@ */ - Popcorn.parser( "parseTTML", "TTML", function( data ) { + Popcorn.parser( "parseTTML", function( data ) { // declare needed variables var returnData = { diff --git a/parsers/parserTTXT/popcorn.parseTTXT.js b/parsers/parserTTXT/popcorn.parseTTXT.js index d3c7341..592f9b7 100644 --- a/parsers/parserTTXT/popcorn.parseTTXT.js +++ b/parsers/parserTTXT/popcorn.parseTTXT.js @@ -14,7 +14,7 @@ * Example: */ - Popcorn.parser( "parseTTXT", "TTXT", function( data ) { + Popcorn.parser( "parseTTXT", function( data ) { // declare needed variables var returnData = { diff --git a/parsers/parserVTT/popcorn.parserVTT.js b/parsers/parserVTT/popcorn.parserVTT.js index 6aee024..1bcdbc7 100644 --- a/parsers/parserVTT/popcorn.parserVTT.js +++ b/parsers/parserVTT/popcorn.parserVTT.js @@ -17,7 +17,7 @@ 00:00:15.542 --> 00:00:18.542 A:start D:vertical L:98% It's a trap! */ - Popcorn.parser( "parseVTT", "VTT", function( data ) { + Popcorn.parser( "parseVTT", function( data ) { // declare needed variables var retObj = { diff --git a/parsers/parserXML/popcorn.parseXML.js b/parsers/parserXML/popcorn.parseXML.js index 8363bb1..d33895a 100644 --- a/parsers/parserXML/popcorn.parseXML.js +++ b/parsers/parserXML/popcorn.parseXML.js @@ -99,7 +99,7 @@ }; // this is where things actually start - var x = data.xml.documentElement.childNodes; + var x = data.documentElement.childNodes; for ( var i = 0, xl = x.length; i < xl; i++ ) { diff --git a/popcorn.js b/popcorn.js index 9f63c0c..d80b53a 100644 --- a/popcorn.js +++ b/popcorn.js @@ -797,8 +797,14 @@ 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" ) { + if ( typeof definition !== "function" || typeof type !== "string" ) { return; } @@ -809,7 +815,7 @@ parseFn, parser = {}; - parseFn = function ( filename ) { + parseFn = function ( filename, callback ) { if ( !filename ) { return this; @@ -819,6 +825,7 @@ Popcorn.xhr({ url: filename, + dataType: type, success: function( data ) { var tracksObject = definition( data ), @@ -849,6 +856,9 @@ } } } + if ( callback ) { + callback(); + } } }); @@ -862,7 +872,7 @@ Popcorn.extend( Popcorn.p, parser ); // keys the function name by filetype extension - Popcorn.parsers[ type ] = name; + //Popcorn.parsers[ name ] = true; return parser; }; @@ -1016,7 +1026,7 @@ var video = videos[ key ], hasDataSources = false, - dataSources, dataTemp, dataType, parserFn, popcornVideo; + dataSources, data, popcornVideo; // Ensure that the DOM has an id if ( !video.id ) { @@ -1032,37 +1042,32 @@ dataSources = ( video.getAttribute( "data-timeline-sources" ) || "" ).split(","); - if ( dataSources.length ) { + if ( dataSources[ 0 ] ) { Popcorn.forEach( dataSources, function ( source ) { - dataTemp = source.split( ":" ); - - dataType = dataTemp[0]; - - if ( dataTemp.length === 1 ) { + // split the parser and data as parser:file + data = source.split( ":" ); - dataTemp = source.split( "." ); + // if no parser is defined for the file, assume "parse" + file extension + if ( data.length === 1 ) { - dataType = dataTemp[ dataTemp.length - 1 ]; + data = source.split( "." ); + data[ 0 ] = "parse" + data[ data.length - 1 ].toUpperCase(); + data[ 1 ] = source; } - dataType = dataType.toUpperCase(); - - parserFn = "parse" + dataType; - // If the video has data sources and the correct parser is registered, continue to load - if ( dataSources && Popcorn.parsers[ dataType ] ) { + if ( dataSources[ 0 ] && popcornVideo[ data[ 0 ] ] ) { // Set up the video and load in the datasources - popcornVideo[ parserFn ]( source ); - + popcornVideo[ data[ 0 ] ]( data[ 1 ] ); } }); - } + } // Only play the video if it was specified to do so if ( !!popcornVideo.autoplay ) { diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index c1660c1..0ca2c0c 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -1366,7 +1366,7 @@ test("Parsing Functions", function () { plus(); Popcorn.parser( "parseJSON" , "json", function( data ){ - return data.json; + return data; }); ok(typeof popperly.parseJSON === "function", "Popcorn.parser created a parseJSON function"); @@ -1379,29 +1379,35 @@ test("Parsing Functions", function () { test("Parsing Integrity", function () { - var expects = 2, + var expects = 6, count = 0, timeOut = 0, - interval, poppercore = Popcorn( "#video" ); function plus() { if ( ++count === expects ) { start(); // clean up added events after tests - clearInterval( interval ); Popcorn.removePlugin( "parserTest" ); } } - + expect(expects); - + stop( 10000 ); - Popcorn.parser( "parseJSON2" , "json", function( data ){ + Popcorn.parser( "parseJSON2", function( data ){ + ok( typeof data.json === "object", "data.json exists"); + plus(); return data.json; }); + Popcorn.parser( "parseJSON3" , "json", function( data ){ + ok( typeof data === "object", "data exists"); + plus(); + return data; + }); + Popcorn.plugin("parserTest", { start: function () { @@ -1414,12 +1420,13 @@ test("Parsing Integrity", function () { } }); - poppercore.parseJSON2("data/parserData.json"); + poppercore.parseJSON2("data/parserData.json", function() { + + poppercore.parseJSON3("data/parserData.json", function() { + poppercore.currentTime(5).play(); + }); - // interval used to wait for data to be parsed - interval = setInterval( function() { - poppercore.currentTime(5).play(); - }, 2000); + }); }); @@ -1445,7 +1452,7 @@ test("Parsing Handler - References unavailable plugin", function () { stop(); - Popcorn.parser( "parseJson" , "json", function( data ){ + Popcorn.parser( "parseJson", function( data ){ return data.json; });