this.video = elem ? elem : null;
this.data = {
+ history: [],
events: {},
trackEvents: {
byStart: [{start: -1, end: -1}],
return dest;
};
- Popcorn.addTrackEvent = function( obj, track ) {
- // Store this definition in an array sorted by times
- obj.data.trackEvents.byStart.push( track );
- obj.data.trackEvents.byEnd.push( track );
- obj.data.trackEvents.byStart.sort( function( a, b ){
- return ( a.start - b.start );
- });
- obj.data.trackEvents.byEnd.sort( function( a, b ){
- return ( a.end - b.end );
- });
- };
// A Few reusable utils, memoized onto Popcorn
Popcorn.extend( Popcorn, {
natives: "load play pause currentTime playbackRate mute volume duration removePlugin roundTime trigger listen unlisten".toLowerCase().split(/\s+/)
};
+
+ Popcorn.addTrackEvent = function( obj, track ) {
+
+ if ( "natives" in track ) {
+ track["_id"] = track.natives.type + Popcorn.guid();
+
+ // Push track event ids into the history
+ obj.data.history.push( track["_id"] );
+ }
+
+ // Store this definition in an array sorted by times
+ obj.data.trackEvents.byStart.push( track );
+ obj.data.trackEvents.byEnd.push( track );
+ obj.data.trackEvents.byStart.sort( function( a, b ){
+ return ( a.start - b.start );
+ });
+ obj.data.trackEvents.byEnd.sort( function( a, b ){
+ return ( a.end - b.end );
+ });
+
+ };
+
+ Popcorn.removeTrackEvent = function( obj, trackId ) {
+
+ var trackLen = obj.data.trackEvents.byStart.length,
+ historyLen = obj.data.history.length,
+ byStart = [],
+ byEnd = [],
+ history = [];
+
+
+ Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context) {
+
+ // Preserve the original start/end trackEvents
+ if ( !o._id ) {
+ byStart.push( obj.data.trackEvents.byStart[i] );
+ byEnd.push( obj.data.trackEvents.byEnd[i] );
+ }
+
+ // Filter for the trackevent to remove
+ if ( o._id && o._id !== trackId ) {
+ byStart.push( obj.data.trackEvents.byStart[i] );
+ byEnd.push( obj.data.trackEvents.byEnd[i] );
+ }
+ });
+
+ 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] );
+ }
+ }
+
+ obj.data.history = history;
+
+ };
+
+ Popcorn.getTrackEvents = function( obj ) {
+
+ var trackevents = [];
+
+ Popcorn.forEach( obj.data.trackEvents.byStart, function(o, i, context) {
+ if ( o._id ) {
+ trackevents.push(o);
+ }
+ });
+
+ return trackevents;
+ };
+
+
+ Popcorn.getLastTrackEventId = function( obj ) {
+ return obj.data.history[ obj.data.history.length - 1 ];
+ };
+
+ // Map TrackEvents functions to the {popcorn}.prototype
+ Popcorn.extend( Popcorn.p, {
+
+ getTrackEvents: function() {
+ return Popcorn.getTrackEvents.call( null, this );
+ },
+
+ getLastTrackEventId: function() {
+ return Popcorn.getLastTrackEventId.call( null, this );
+ },
+
+ removeTrackEvent: function( id ) {
+ Popcorn.removeTrackEvent.call( null, this, id );
+ return this;
+ }
+
+ });
+
+
// Plugins are registered
Popcorn.registry = [];
// An interface for extending Popcorn
};
});
});
-Popcorn.xhr.httpData
+
+module("Popcorn TrackEvents");
+test("Functions", function () {
+
+ // TODO: break this into sep. units per function
+
+ var popped = Popcorn("#video"), ffTrackId, rwTrackId, rw2TrackId, historyRef, trackEvents;
+
+
+ Popcorn.plugin("ff", function () {
+ return {
+ start: function () {},
+ end: function () {}
+ };
+ });
+
+ popped.ff({
+ start: 3,
+ end: 4
+ });
+
+
+ ffTrackId = popped.getLastTrackEventId();
+
+
+
+ Popcorn.plugin("rw", function () {
+ return {
+ start: function () {},
+ end: function () {}
+ };
+ });
+
+ popped.rw({
+ start: 1,
+ end: 2
+ });
+
+
+ rwTrackId = popped.getLastTrackEventId();
+
+ historyRef = popped.data.history;
+
+
+ equals( historyRef.length, 2, "2 TrackEvents in history index");
+
+ equals( popped.data.trackEvents.byStart.length, 4, "4 TrackEvents in popped.data.trackEvents.byStart ");
+ equals( popped.data.trackEvents.byEnd.length, 4, "4 TrackEvents in popped.data.trackEvents.byEnd ");
+
+
+ trackEvents = popped.getTrackEvents();
+
+ equals( trackEvents.length, 2, "2 user created trackEvents returned by popped.getTrackEvents()" )
+
+
+ ok( ffTrackId !== rwTrackId, "Track Events have different ids" );
+
+ popped.removeTrackEvent( rwTrackId );
+
+ equals( popped.data.history.length, 1, "1 TrackEvent in history index - after popped.removeTrackEvent( rwTrackId ); ");
+ equals( popped.data.trackEvents.byStart.length, 3, "3 TrackEvents in popped.data.trackEvents.byStart ");
+ equals( popped.data.trackEvents.byEnd.length, 3, "3 TrackEvents in popped.data.trackEvents.byEnd ");
+
+ trackEvents = popped.getTrackEvents();
+
+ equals( trackEvents.length, 1, "1 user created trackEvents returned by popped.getTrackEvents()" )
+
+ popped.rw({
+ start: 1,
+ end: 2
+ });
+
+
+ rw2TrackId = popped.getLastTrackEventId();
+
+ equals( popped.data.history.length, 2, "2 TrackEvents in history index - after new track added ");
+
+
+ ok( rw2TrackId !== rwTrackId, "rw2TrackId !== rwTrackId" );
+
+ equals( popped.data.trackEvents.byStart.length, 4, "4 TrackEvents in popped.data.trackEvents.byStart - after new track added");
+ equals( popped.data.trackEvents.byEnd.length, 4, "4 TrackEvents in popped.data.trackEvents.byEnd - after new track added");
+
+
+ trackEvents = popped.getTrackEvents();
+
+ equals( trackEvents.length, 2, "2 user created trackEvents returned by popped.getTrackEvents()" )
+
+
+
+
+
+});
+
module("Popcorn XHR");
test("Basic", function () {