From: ScottDowne Date: Tue, 21 Dec 2010 21:26:18 +0000 (-0500) Subject: [#221] initial parser plugin structure and tests X-Git-Tag: v0.2~18^2~6 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=d0df2db00d61fb49b937fb421ce01ba74d7ef0b9;p=popcorn-js.git [#221] initial parser plugin structure and tests --- diff --git a/popcorn.js b/popcorn.js index 54434cd..b3d6a1c 100644 --- a/popcorn.js +++ b/popcorn.js @@ -724,6 +724,82 @@ return plugin; }; + // 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; + } + + if ( typeof definition !== "function" ) { + return; + } + + // Provides some sugar, but ultimately extends + // the definition into Popcorn.p + + var natives = Popcorn.events.all, + parseFn, + parser = {}; + + parseFn = function ( filename ) { + + if ( !filename ) { + return this; + } + + var that = this; + + Popcorn.xhr({ + url: filename, + success: function( data ) { + + var tracksObject = definition( data ); + + // creating tracks out of parsed object + for ( var key in tracksObject ) { + if ( tracksObject.hasOwnProperty(key) ) { + + // an array of tracks of all one type + if ( tracksObject[key].constructor === Array ) { + + for (var i = 0, tol = tracksObject[key].length; i < tol; i++) { + if ( typeof that[key] === "function") { + that[key]( tracksObject[key][i] ); + } + } + + // one single track + } else if ( typeof tracksObject[key] === "object" ) { + if ( typeof that[key] === "function") { + that[key]( tracksObject[key] ); + } + } + } + } + } + }); + + 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[type] = name; + + return parser; + }; + var setup = { url: '', diff --git a/test/data/parserData.json b/test/data/parserData.json new file mode 100644 index 0000000..4974e8d --- /dev/null +++ b/test/data/parserData.json @@ -0,0 +1,7 @@ +{ + "parserTest": + { + "start": 5, + "end": 6 + } +} diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index a468e7f..22f26af 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -1063,6 +1063,85 @@ test("XML Response", function () { }); +module("Popcorn Parser"); + +test("Parsing Functions", function () { + + var expects = 3, + count = 0, + popperly = Popcorn("#video"); + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + expect(expects); + + stop( 10000 ); + + ok(typeof Popcorn.parser === "function", "Popcorn.parser is a function"); + plus(); + + Popcorn.parser( "parseJSON" , "json", function( data ){ + return data.json; + }); + + ok(typeof popperly.parseJSON === "function", "Popcorn.parser created a parserJSON function"); + plus(); + + ok(typeof popperly.parseJSON().parseJSON("data/test.js").parseJSON === "function" , "parseJSON function is chainable"); + plus(); + +}); + +test("Parsing Integrity", function () { + + var expects = 2, + count = 0, + timeOut = 0, + interval, + poppercore = Popcorn( "#video" ); + + function plus() { + if ( ++count === expects ) { + start(); + // clean up added events after tests + clearInterval( interval ); + poppercore.removePlugin( "parserTest" ); + } + } + + expect(expects); + + stop( 10000 ); + + Popcorn.parser( "parseJSON2" , "json", function( data ){ + return data.json; + }); + + Popcorn.plugin("parserTest", { + + start: function () { + ok(true, "parserTest started"); + plus(); + }, + end: function () { + ok(true, "parserTest ended"); + plus(); + } + }); + + poppercore.parseJSON2("data/parserData.json"); + + // interval used to wait for data to be parsed + interval = setInterval( function() { + poppercore.currentTime(5).play().currentTime(6); + }, 2000); + +}); + module("Popcorn Test Runner End");