]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#546] Implements track reference hash table, .ref static methods; Unit tests;
authorrwldrn <waldron.rick@gmail.com>
Thu, 2 Jun 2011 15:08:11 +0000 (11:08 -0400)
committerrwldrn <waldron.rick@gmail.com>
Thu, 2 Jun 2011 15:08:11 +0000 (11:08 -0400)
popcorn.js
test/popcorn.unit.js

index 434ac924fa95c4bcefaf9b0f85b13cdd7bad51fe..7e4ef9c9639df43425669b9fbc05c2d8287cbb25 100644 (file)
         // 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: {
   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 ) {
         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 ) {
         byEnd = [],
         history = [];
 
-
     Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context ) {
       // Preserve the original start/end trackEvents
       if ( !o._id ) {
 
     });
 
-
     //  Update
     if ( indexWasAt <= obj.data.trackEvents.startIndex ) {
       obj.data.trackEvents.startIndex--;
       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 ) {
     getTrackEvents: function() {
       return Popcorn.getTrackEvents.call( null, this );
     },
-    
+
     getTrackEvent: function( id ) {
       return Popcorn.getTrackEvent.call( null, this, id );
     },
       Popcorn.removePlugin.call( null, this, name );
       return this;
     }
-
   });
 
   //  Plugin manifests
index 0aa4023ff421c9507db9152a2c777441fd837056..5e324c889f7d0131fa87a8e8d2e8af73cf468c1e 100644 (file)
@@ -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");