Skip to content

Commit

Permalink
[#904] moved parser into modules, updated tests, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottDowne committed Jan 31, 2012
1 parent 096fe11 commit 46256bf
Show file tree
Hide file tree
Showing 30 changed files with 2,884 additions and 257 deletions.
1 change: 1 addition & 0 deletions demos/semantic_video/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta charset="UTF-8" />
<script src="http://code.jquery.com/jquery.js"></script>
<script src="../../popcorn.js"></script>
<script src="../../modules/parser/popcorn.parser.js"></script>

<script src="../../plugins/webpage/popcorn.webpage.js"></script>
<script src="../../plugins/googlemap/popcorn.googlemap.js"></script>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
103 changes: 103 additions & 0 deletions modules/parser/popcorn.parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
(function( Popcorn ) {

var

AP = Array.prototype,
OP = Object.prototype,

forEach = AP.forEach,
slice = AP.slice,
hasOwn = OP.hasOwnProperty,
toString = OP.toString;

// 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;
}

// fixes parameters for overloaded function call
if ( typeof type === "function" && !definition ) {
definition = type;
type = "";
}

if ( typeof definition !== "function" || typeof type !== "string" ) {
return;
}

// Provides some sugar, but ultimately extends
// the definition into Popcorn.p

var natives = Popcorn.events.all,
parseFn,
parser = {};

parseFn = function( filename, callback ) {

if ( !filename ) {
return this;
}

var that = this;

Popcorn.xhr({
url: filename,
dataType: type,
success: function( data ) {

var tracksObject = definition( data ),
tracksData,
tracksDataLen,
tracksDef,
idx = 0;

tracksData = tracksObject.data || [];
tracksDataLen = tracksData.length;
tracksDef = null;

// If no tracks to process, return immediately
if ( !tracksDataLen ) {
return;
}

// Create tracks out of parsed object
for ( ; idx < tracksDataLen; idx++ ) {

tracksDef = tracksData[ idx ];

for ( var key in tracksDef ) {

if ( hasOwn.call( tracksDef, key ) && !!that[ key ] ) {

that[ key ]( tracksDef[ key ] );
}
}
}
if ( callback ) {
callback();
}
}
});

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[ name ] = true;

return parser;
};
})( Popcorn );
46 changes: 46 additions & 0 deletions modules/parser/popcorn.parser.unit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<title>Popcorn Module: Timeline-Sources</title>
<link rel="stylesheet" href="../../test/qunit/qunit.css" type="text/css" media="screen">
<script src="../../test/qunit/qunit.js"></script>

<script src="../../popcorn.js"></script>
<script src="popcorn.parser.js"></script>
<script src="popcorn.parser.unit.js"></script>
</head>
<body>
<h1 id="qunit-header">Popcorn Module: player</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture"> </div>

<video id="video"
style="display:;width:300px"
controls
preload="auto">

<source id="mp4"
src="../../test/trailer.mp4"
type='video/mp4; codecs="avc1, mp4a"'>

<source id="ogv"
src="../../test/trailer.ogv"
type='video/ogg; codecs="theora, vorbis"'>

<source id="webm"
src="../../test/trailer.webm"
type='video/webm; codecs="vp8, vorbis"'>

<p>Your user agent does not support the HTML5 Video element.</p>

</video>

<audio id="audio" data-timeline-sources="data/parserData.json" controls>
<source src="../../test/italia.mp4" type='audio/mp4; codecs="mp4a.40.2"'>
<source src="../../test/italia.ogg" type='audio/ogg; codecs="vorbis"'>
</audio>
</body>
</html>
162 changes: 162 additions & 0 deletions modules/parser/popcorn.parser.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
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;
});

ok( typeof popperly.parseJSON === "function", "Popcorn.parser created a parseJSON function" );
plus();

ok( typeof popperly.parseJSON().parseJSON( "data/test.js" ).parseJSON === "function" , "parseJSON function is chainable" );
plus();
});

test( "Parsing Integrity", function() {

var expects = 6,
count = 0,
timeOut = 0,
poppercore = Popcorn( "#video" );

function plus() {
if ( ++count === expects ) {
start();
// clean up added events after tests
Popcorn.removePlugin( "parserTest" );
}
}

expect( expects );

stop( 10000 );

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() {
ok( true, "parserTest started" );
plus();
},
end: function() {
ok( true, "parserTest ended" );
plus();
}
});

poppercore.parseJSON2( "data/parserData.json", function() {

poppercore.parseJSON3( "data/parserData.json", function() {
poppercore.currentTime( 5 ).play();
});
});
});

test( "Parsing Handler - References unavailable plugin", function() {

var expects = 1,
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();

Popcorn.parser( "parseJson", function( data ){

return data.json;
});

poppercore.parseJson( "data/parseMissing.json" );

// interval used to wait for data to be parsed
interval = setInterval( function() {
poppercore.currentTime( 5 ).play().currentTime( 6 );

ok( true, "Ignored call to missing plugin " );
plus();
}, 2000 );
});

test( "Parser Support - audio", function() {

var expects = 3,
count = 0,
timeOut = 0,
interval,
audiocorn = Popcorn( "#audio" );

function plus() {
if ( ++count === expects ) {
start();

Popcorn.removePlugin( "testAudioParser" );
}
}

expect( expects );
stop( 5000 );

Popcorn.plugin( "testAudioParser", {

start: function() {
ok( true, "testAudioParser started: " + Math.round( this.currentTime() ) );
plus();
},
end: function() {
ok( true, "testAudioParser ended: " + Math.round( this.currentTime() ) );
plus();
}
});

Popcorn.parser( "parseAudio", function( data ){
ok( typeof data.json === "object", "data.json exists");
plus();
return data.json;
});

audiocorn.parseAudio( "data/parserAudio.json", function() {

audiocorn.currentTime( 4 ).play();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<script src="../../test/qunit/qunit.js"></script>

<script src="../../popcorn.js"></script>
<script src="../parser/popcorn.parser.js"></script>
<script src="popcorn.timeline-sources.js"></script>
<script src="popcorn.timeline-sources.unit.js"></script>
<script src="../../parsers/parserJSON/popcorn.parserJSON.js"></script>
Expand Down
49 changes: 49 additions & 0 deletions parsers/parserJSON/data/data - Copy.sm
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"title":"Crockford Lives!",
"remote":"http://dl.dropbox.com/u/3531958/crockford.ogv",
"data": [
{
"webpage": {
"start":"0.0897589878863602",
"end":"2.001204869833337",
"target":"iframe-container",
"src":"http://json.org"
}
},
{
"footnote": {
"start":"0.23530116023787565",
"end":"2.0193976413772767",
"target":"footnote-container",
"text":"I invented JSON"
}
},
{
"googlemap": {
"start":"0.09096385771969716",
"end":"8.617349362660605",
"target":"map-container",
"type":"ROADMAP",
"lat":37.7749295,
"lng":-122.4194155,
"location":"San Francisco, CA"
}
},
{
"webpage": {
"start":"2.073975956009095",
"end":"10.278915922325776",
"target":"iframe-container",
"src":"http://jslint.org"
}
},
{
"footnote": {
"start":2.145542172351516,
"end":10.145542172351513,
"target":"footnote-container",
"text":"I wrote JSLint"
}
}
]
}
Loading

0 comments on commit 46256bf

Please sign in to comment.