From 8e100faf9c4b960034ec88975ba531e9436d6f23 Mon Sep 17 00:00:00 2001 From: rwldrn Date: Thu, 2 Jun 2011 11:08:11 -0400 Subject: [PATCH] [#546] Implements track reference hash table, .ref static methods; Unit tests; --- popcorn.js | 76 +++++++++++++++++++++++++++++--------------- test/popcorn.unit.js | 61 ++++++++++++++++++++++++++--------- 2 files changed, 97 insertions(+), 40 deletions(-) diff --git a/popcorn.js b/popcorn.js index 434ac92..7e4ef9c 100644 --- a/popcorn.js +++ b/popcorn.js @@ -161,8 +161,8 @@ // Store track event history data history: [], - // Store track event object references by trackId - trackRefs: {}, + // Store track event object references by trackId + trackRefs: {}, // Playback track event queues trackEvents: { @@ -609,12 +609,13 @@ Popcorn.forEach( [ "trigger", "listen", "unlisten" ], function( key ) { Popcorn.p[ key ] = Popcorn.events.fn[ key ]; }); + // Protected API methods Popcorn.protect = { natives: "load play pause currentTime playbackRate mute volume duration removePlugin roundTime trigger listen unlisten".toLowerCase().split( /\s+/ ) }; - // Internal Only + // Internal Only - Adds track events to the instance object Popcorn.addTrackEvent = function( obj, track ) { if ( track._natives ) { @@ -651,6 +652,18 @@ break; } } + + // Store references to user added trackevents in ref table + if ( track._id ) { + Popcorn.addTrackEvent.ref( obj, track ); + } + }; + + // Internal Only - Adds track event references to the instance object's trackRefs hash table + Popcorn.addTrackEvent.ref = function( obj, track ) { + obj.data.trackRefs[ track._id ] = track; + + return obj; }; Popcorn.removeTrackEvent = function( obj, trackId ) { @@ -661,7 +674,6 @@ byEnd = [], history = []; - Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context ) { // Preserve the original start/end trackEvents if ( !o._id ) { @@ -687,7 +699,6 @@ }); - // Update if ( indexWasAt <= obj.data.trackEvents.startIndex ) { obj.data.trackEvents.startIndex--; @@ -697,46 +708,62 @@ obj.data.trackEvents.endIndex--; } - obj.data.trackEvents.byStart = byStart; obj.data.trackEvents.byEnd = byEnd; - for ( var i = 0; i < historyLen; i++ ) { if ( obj.data.history[ i ] !== trackId ) { history.push( obj.data.history[ i ] ); } } + // Update ordered history array obj.data.history = history; + // Update track event references + Popcorn.removeTrackEvent.ref( obj, trackId ); }; - Popcorn.getTrackEvents = function( obj ) { + // Internal Only - Removes track event references from instance object's trackRefs hash table + Popcorn.removeTrackEvent.ref = function( obj, trackId ) { + delete obj.data.trackRefs[ trackId ]; - var trackevents = []; + return obj; + }; - Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context ) { - if ( o._id ) { - trackevents.push( o ); + // Return an array of track events bound to this instance object + Popcorn.getTrackEvents = function( obj ) { + + var trackevents = [], + refs = obj.data.trackEvents.byStart, + length = refs.length, + idx = 0, + ref; + + for ( ; idx < length; idx++ ) { + ref = refs[ idx ]; + // Return only user attributed track event references + if ( ref._id ) { + trackevents.push( ref ); } - }); + } return trackevents; }; - Popcorn.getTrackEvent = function( obj, trackId ) { + // Internal Only - Returns an instance object's trackRefs hash table + Popcorn.getTrackEvents.ref = function( obj ) { + return obj.data.trackRefs; + }; - var byStart = obj.data.trackEvents.byStart, - len = byStart.length, o; + // Return a single track event bound to this instance object + Popcorn.getTrackEvent = function( obj, trackId ) { + return obj.data.trackRefs[ trackId ] || null; + }; - while ( len-- ) { - o = byStart[len]; - if ( o._id === trackId ) { - return o; - } - } - return undefined; + // Internal Only - Returns an instance object's track reference by track id + Popcorn.getTrackEvent.ref = function( obj, trackId ) { + return obj.data.trackRefs[ trackId ]; }; Popcorn.getLastTrackEventId = function( obj ) { @@ -749,7 +776,7 @@ getTrackEvents: function() { return Popcorn.getTrackEvents.call( null, this ); }, - + getTrackEvent: function( id ) { return Popcorn.getTrackEvent.call( null, this, id ); }, @@ -767,7 +794,6 @@ Popcorn.removePlugin.call( null, this, name ); return this; } - }); // Plugin manifests diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 0aa4023..5e324c8 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -1,4 +1,4 @@ -module("Popcorn"); +module("Popcorn API"); test("API", function () { var expects = 4, @@ -49,24 +49,28 @@ test("API", function () { }); -test("Utility", function () { +test("Popcorn.* Static Methods", function () { - expect(10); - // TODO: comprehensive tests for these utilities + var statics = [ "forEach", "extend", "error", "guid", "sizeOf", "nop", + "addTrackEvent", "removeTrackEvent", "getTrackEvents", "getTrackEvent", "position", "disable", "enable" ], + substatics = [ "addTrackEvent", "removeTrackEvent", "getTrackEvents", "getTrackEvent"]; + + expect(statics.length + substatics.length); + + statics.forEach(function(val, idx) { + + equals( typeof Popcorn[val], "function" , "Popcorn."+val+"() is a provided static function"); + + }); - equals( typeof Popcorn.forEach, "function" , "Popcorn.forEach is a provided static function"); - equals( typeof Popcorn.extend, "function" , "Popcorn.extend is a provided static function"); - equals( typeof Popcorn.error, "function" , "Popcorn.error is a provided static function"); - equals( typeof Popcorn.guid, "function" , "Popcorn.guid is a provided static function"); - equals( typeof Popcorn.sizeOf, "function" , "Popcorn.sizeOf is a provided static function"); - equals( typeof Popcorn.nop, "function" , "Popcorn.nop is a provided static function"); - equals( typeof Popcorn.addTrackEvent, "function" , "Popcorn.addTrackEvent is a provided static function"); - equals( typeof Popcorn.position, "function" , "Popcorn.position is a provided static function"); - equals( typeof Popcorn.disable, "function" , "Popcorn.disable is a provided static function"); - equals( typeof Popcorn.enable, "function" , "Popcorn.enable is a provided static function"); + substatics.forEach(function(val, idx) { + + equals( typeof Popcorn[val].ref, "function" , "Popcorn."+val+".ref() is a private use static function"); + + }); }); -test("Standard Time Strings" , function () { +test("Popcorn.util.toSeconds" , function () { var framerate = 24, storedStartTime, storedEndTime, @@ -402,6 +406,33 @@ test( "Object", function () { }); }); +module("Popcorn Static"); + +test("Popcorn.[addTrackEvent | removeTrackEvent].ref()", function() { + + expect(2); + + var popped = Popcorn("#video"); + + // Calling exec() will create tracks and added them to the + // trackreference internally + popped.exec( 1, function() { /* ... */ }); + popped.exec( 3, function() { /* ... */ }); + popped.exec( 5, function() { /* ... */ }); + + equal( Popcorn.sizeOf( popped.data.trackRefs ), 3, "There are 3 trackRefs in popped.data.trackRefs" ); + + Popcorn.forEach( popped.data.trackRefs, function( ref, key ) { + popped.removeTrackEvent( key ); + }); + + equal( Popcorn.sizeOf( popped.data.trackRefs ), 0, "There are 0 trackRefs in popped.data.trackRefs" ); + + Popcorn.removeInstance( popped ); +}); + + + module("Popcorn Methods"); -- 2.43.0