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: '',
});
+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");