From: stevenaw Date: Wed, 16 Mar 2011 06:48:38 +0000 (-0400) Subject: [#280] No Core changes works in FF3.6. Chrome doesn't display (but DOM is updated... X-Git-Tag: v0.4~6^2~9 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=76a0961a003addcca6134c94b50fa0d41197d2b6;p=popcorn-js.git [#280] No Core changes works in FF3.6. Chrome doesn't display (but DOM is updated), FF4Beta just doesn't --- diff --git a/players/vimeo/popcorn.vimeo.html b/players/vimeo/popcorn.vimeo.html index 20b5e51..60d0c01 100644 --- a/players/vimeo/popcorn.vimeo.html +++ b/players/vimeo/popcorn.vimeo.html @@ -52,7 +52,7 @@ // Loop document.getElementById( "btnLoop" ).addEventListener( "click", function() { loop = loop === "loop" ? "" : "loop"; - player.loop( loop ); + player.video.setLoop( loop ); }, false); // Volume @@ -94,7 +94,7 @@ //On document ready document.addEventListener( "DOMContentLoaded", function() { - var player = Popcorn.vimeo( "player_1", "http://vimeo.com/11127501" ) + var player = Popcorn( Popcorn.vimeo( "player_1", "http://vimeo.com/11127501" ) ) .footnote({ start: 5, // seconds end: 40, // seconds @@ -132,6 +132,8 @@ +
+
diff --git a/players/vimeo/popcorn.vimeo.js b/players/vimeo/popcorn.vimeo.js index 8d18741..e345001 100644 --- a/players/vimeo/popcorn.vimeo.js +++ b/players/vimeo/popcorn.vimeo.js @@ -166,6 +166,9 @@ }; }; + var timeupdateInterval = 33, + timeCheckInterval = 0.75; + Popcorn.vimeo = function( mediaId, list ) { return new Popcorn.vimeo.init( mediaId, list ); }; @@ -205,7 +208,7 @@ var matches = url.match( rWebUrl ); return matches ? matches[0].substr(10) : ""; }; - + // If container id is not supplied, assumed to be same as player id var ctor = function ( containerId, videoUrl ) { if ( !containerId ) { @@ -215,13 +218,22 @@ var vidId, that = this; - this.swfObj = document.getElementById( containerId ), - this.addEventFn, - this.evtHolder, - this.popped, - this.autoplay, - this.readyState = 0, - this.objVars; + this.swfObj = document.getElementById( containerId ); + this.addEventFn; + this.evtHolder; + this.autoplay; + this.paused = true; + this.duration = Number.MAX_VALUE; + this.ended = 0; + this.currentTime = 0; + this.volume = 1; + this.loop = 0; + this.initialTime = 0; + this.played = 0; + this.readyState = 0; + + this.previousCurrentTime = this.currentTime; + this.previousVolume = this.volume; if ( !this.swfObj ) { throw "Invalid id, could not find it!"; @@ -230,19 +242,6 @@ this.evtHolder = new LikeADOM( this ); this.autoplay = this.swfObj.autoplay; - // Tracking variables for the video player - // Better to store them here than on the DOM - this.objVars = { - paused: true, - duration: Number.NaN, - ended: 0, - currentTime: Number.NaN, - volume: 1, - loop: 0, - initialTime: 0, - played: 0, - }; - // Try and get a video id from a vimeo site url // Try either from ctor param or from iframe itself if( videoUrl ) { @@ -308,7 +307,7 @@ var loadingFn; that.swfObj.get( "api_getDuration", function( duration ) { - that.objVars.duration = duration; + that.duration = parseFloat( duration ); that.evtHolder.dispatchEvent( "durationchange" ); }); @@ -316,27 +315,27 @@ // Done by Getting the Current Time while the video plays that.swfObj.addEvent( "onProgress", function() { that.swfObj.get( "api_getCurrentTime", function( time ) { - that.objVars.currentTime = parseFloat( time ); + that.currentTime = parseFloat( time ); that.evtHolder.dispatchEvent( "timeupdate" ); }); }); // Add pause listener to keep track of playing state that.addEventListener( "pause", function() { - that.objVars.paused = true; + that.paused = true; }); // Add play listener to keep track of playing state that.addEventListener( "playing", function() { - that.objVars.paused = false; - that.objVars.ended = 0; + that.paused = false; + that.ended = 0; }); // Add ended listener to keep track of playing state that.addEventListener( "ended", function() { - if ( that.objVars.loop !== "loop" ) { - that.objVars.paused = true; - that.objVars.ended = 1; + if ( that.loop !== "loop" ) { + that.paused = true; + that.ended = 1; } }); @@ -347,10 +346,6 @@ that.evtHolder.removeEventListener( "progress", loadingFn ); }); }); - - // 'this' is a video wrapper, so put into Popcorn and return the result - this.popped = Popcorn( this ); - return this.popped; } return ctor; })(); @@ -360,20 +355,20 @@ // Sequence object prototype Popcorn.extend( Popcorn.vimeo.prototype, { // Do everything as functions instead of get/set - loop: function( val ) { + setLoop: function( val ) { if ( !val ) { - return this.objVars.loop; + return; } - this.objVars.loop = val; + this.loop = val; var isLoop = val === "loop" ? 1 : 0; // HTML convention says to loop if value is 'loop' this.swfObj.api('api_setLoop', isLoop ); }, // Set the volume as a value between 0 and 1 - volume: function( val ) { + setVolume: function( val ) { if ( !val && val !== 0 ) { - return this.objVars.volume; + return; } // Normalize in case outside range of expected values @@ -386,18 +381,18 @@ } // HTML video expects to be 0.0 -> 1.0, Vimeo expects 0-100 - this.objVars.volume = val; + this.volume = this.previousVolume = val; this.swfObj.api( "api_setVolume", val*100 ); this.evtHolder.dispatchEvent( "volumechange" ); }, // Seeks the video - currentTime: function ( time ) { + setCurrentTime: function ( time ) { if ( !time && time !== 0 ) { - return this.objVars.currentTime; + return; } - this.objVars.currentTime = time; - this.objVars.ended = time < this.objVars.duration; + this.currentTime = this.previousCurrentTime = time; + this.ended = time >= this.duration; this.swfObj.api( "api_seekTo", time ); // Fire events for seeking and time change @@ -406,8 +401,9 @@ }, // Play the video play: function() { - if ( !this.objVars.played ) { - this.objVars.played = 1; + if ( !this.played ) { + this.played = 1; + this.startTimeUpdater(); this.evtHolder.dispatchEvent( "loadstart" ); } @@ -418,19 +414,25 @@ pause: function() { this.swfObj.api( "api_pause" ); }, - duration: function() { - return this.objVars.duration; - }, mute: function() { if ( !this.muted() ) { - this.objVars.oldVol = this.objVars.volume; - this.volume( 0 ); + this.oldVol = this.volume; + + if ( this.paused ) { + this.setVolume( 0 ); + } else { + this.volume = 0; + } } else { - this.volume( this.objVars.oldVol ); + if ( this.paused ) { + this.setVolume( this.oldVol ); + } else { + this.volume = this.oldVol; + } } }, muted: function() { - return this.objVars.volume === 0; + return this.volume === 0; }, // Force loading by playing the player. Pause afterwards load: function() { @@ -485,6 +487,32 @@ }, dispatchEvent: function( evtName ) { return this.evtHolder.dispatchEvent( evtName ); - } + }, + startTimeUpdater: function() { + var self = this, + seeked = 0; + + if ( Math.abs( this.currentTime - this.previousCurrentTime ) > timeCheckInterval ) { + // Has programatically set the currentTime + this.setCurrentTime( this.currentTime ); + seeked = 1; + } else { + this.previousCurrentTime = this.currentTime; + } + + if ( this.volume !== this.previousVolume ) { + this.setVolume( this.volume ); + } + + if ( !self.paused || seeked ) { + this.dispatchEvent( 'timeupdate' ); + } + + if( !self.ended ) { + setTimeout( function() { + self.startTimeUpdater.call(self); + }, timeupdateInterval); + } + }, }); })( Popcorn ); \ No newline at end of file diff --git a/players/vimeo/popcorn.vimeo.unit.js b/players/vimeo/popcorn.vimeo.unit.js index 036fd3c..f5c706e 100644 --- a/players/vimeo/popcorn.vimeo.unit.js +++ b/players/vimeo/popcorn.vimeo.unit.js @@ -80,7 +80,7 @@ test("Popcorn Vimeo Plugin", function () { if ( timesCalled === 1 ) { ok( true, "Pause event works" ); plus(); - ok( popped.video.objVars.paused, "Pause attribute set" ); + ok( popped.video.paused, "Pause attribute set" ); plus(); popped.play(); } @@ -99,7 +99,7 @@ test("Popcorn Vimeo Plugin", function () { ok( true, "Video autoplay attribute works" ); plus(); - ok( !popped.video.objVars.paused, "Player paused attribute not set" ); + ok( !popped.video.paused, "Player paused attribute not set" ); plus(); popped.exec( 5, function() { @@ -125,7 +125,7 @@ test("Popcorn Vimeo Plugin", function () { ok( "vimeo" in Popcorn, "Vimeo is a method of Popcorn" ); plus(); - equals( popped.video.objVars.initialTime, 0, "Player initial time is 0" ); + equals( popped.video.initialTime, 0, "Player initial time is 0" ); plus(); Popcorn.plugin( "timingTest", { diff --git a/popcorn.js b/popcorn.js index 88fc775..24ba6c7 100644 --- a/popcorn.js +++ b/popcorn.js @@ -22,13 +22,66 @@ return new Popcorn.p.init( entity ); }; + // Instance caching + Popcorn.instances = []; + Popcorn.instanceIds = {}; + + Popcorn.removeInstance = function( instance ) { + // If called prior to any instances being created + // Return early to avoid splicing on nothing + if ( !Popcorn.instances.length ) { + return; + } + + // Remove instance from Popcorn.instances + Popcorn.instances.splice( Popcorn.instanceIds[ instance.id ], 1 ); + + // Delete the instance id key + delete Popcorn.instanceIds[ instance.id ]; + + // Return current modified instances + return Popcorn.instances; + }; + + // Addes a Popcorn instance to the Popcorn instance array + Popcorn.addInstance = function( instance ) { + + var instanceLen = Popcorn.instances.length, + instanceId = instance.video.id && instance.video.id; + + // If the video element has its own `id` use it, otherwise provide one + // Ensure that instances have unique ids and unique entries + // Uses `in` operator to avoid false positives on 0 + instance.id = !( instanceId in Popcorn.instanceIds ) && instanceId || + "__popcorn" + instanceLen; + + // Create a reference entry for this instance + Popcorn.instanceIds[ instance.id ] = instanceLen; + + // Add this instance to the cache + Popcorn.instances.push( instance ); + + // Return the current modified instances + return Popcorn.instances; + }; + + // Request Popcorn object instance by id + Popcorn.getInstanceById = function( id ) { + return Popcorn.instances[ Popcorn.instanceIds[ id ] ]; + }; + + // Remove Popcorn object instance by id + Popcorn.removeInstanceById = function( id ) { + return Popcorn.removeInstance( Popcorn.instances[ Popcorn.instanceIds[ id ] ] ); + }; + // Declare a shortcut (Popcorn.p) to and a definition of // the new prototype for our Popcorn constructor Popcorn.p = Popcorn.prototype = { init: function( entity ) { - var elem, matches; + var matches; // Supports Popcorn(function () { /../ }) // Originally proposed by Daniel Brooks @@ -72,23 +125,18 @@ document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false); } - - return; } + // check if entity is a valid string id + matches = rIdExp.exec( entity ); - if ( (typeof entity) == "string" ) { - matches = rIdExp.exec( entity ); + // get video element by id or reference + this.video = matches && matches.length && matches[ 2 ] ? + document.getElementById( matches[ 2 ] ) : + entity; - if ( matches.length && matches[2] ) { - elem = document.getElementById(matches[2]); - } - - this.video = elem ? elem : null; - } else if ( entity instanceof Object ) { - this.video = entity; - } + Popcorn.addInstance( this ); this.data = { history: [], @@ -115,11 +163,6 @@ // this is so we do not fall off either end var duration = that.video.duration; - - if( typeof that.video.duration === "function") { - duration = that.video.duration(); - } - // Check for no duration info (NaN) var videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1; @@ -136,11 +179,6 @@ tracksByEnd = tracks.byEnd, tracksByStart = tracks.byStart; - - if (typeof this.currentTime === "function") { - currentTime = this.currentTime(); - } - // Playbar advancing if ( previousTime < currentTime ) { @@ -227,9 +265,7 @@ } }; - if ( this.video ) { - isReady( this ); - } + isReady( this ); return this; } @@ -303,7 +339,6 @@ // todo: play, pause, mute should toggle var methods = "load play pause currentTime playbackRate mute volume duration", - noArgMethods = /load|play|pause|mute/, ret = {}; @@ -312,21 +347,18 @@ ret[ name ] = function( arg ) { - var isFunc = typeof this.video[name] === "function"; - - if ( noArgMethods.test( name ) || ( arg !== false && arg !== null && typeof arg !== "undefined" ) ) { - - if ( isFunc ) { - this.video[ name ]( arg ); - } else { - this.video[ name ] = arg; - } + if ( typeof this.video[name] === "function" ) { + this.video[ name ](); return this; } - if ( isFunc ) { - return this.video[ name ](); + + if ( arg !== false && arg !== null && typeof arg !== "undefined" ) { + + this.video[ name ] = arg; + + return this; } return this.video[ name ]; @@ -354,6 +386,7 @@ _running: false, _natives: { start: fn || Popcorn.nop, + end: Popcorn.nop, type: "exec" } }); @@ -503,6 +536,9 @@ // Push track event ids into the history obj.data.history.push( track._id ); + + track._natives.start = track._natives.start || Popcorn.nop; + track._natives.end = track._natives.end || Popcorn.nop; } // Store this definition in an array sorted by times @@ -1121,4 +1157,3 @@ }, false ); })(window, window.document); - diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 8f6f373..32294c6 100755 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -67,6 +67,63 @@ test("Utility", function () { }); +test("Instances", function() { + var expects = 11, + count = 0, + instance; + + expect(expects); + + function plus(){ + if ( ++count == expects ) { + start(); + } + } + + stop(); + + ok( typeof Popcorn.addInstance === "function" , "Popcorn.addInstance is a provided utility function"); + plus(); + + ok( typeof Popcorn.removeInstance === "function" , "Popcorn.removeInstance is a provided utility function"); + plus(); + + ok( typeof Popcorn.getInstanceById === "function" , "Popcorn.getInstanceById is a provided utility function"); + plus(); + + ok( typeof Popcorn.removeInstanceById === "function" , "Popcorn.removeInstanceById is a provided utility function"); + plus(); + + ok( typeof Popcorn.instanceIds === "object" , "Popcorn.instanceIds is a provided cache object"); + plus(); + + ok( "length" in Popcorn.instances && "join" in Popcorn.instances, "Popcorn.instances is a provided cache array"); + plus(); + + instance = Popcorn.getInstanceById("video"); + + ok( instance.video, "Stored instance as a `video` property" ); + plus(); + + ok( instance.data, "Stored instance as a `data` property" ); + plus(); + + ok( instance instanceof Popcorn, "Instance instanceof Popcorn" ); + plus(); + + equal( Popcorn.instances.length, 1, "There are the correct number of Popcorn instances" ); + plus(); + + // Create another instance + Popcorn("#video"); + + // Get a reference to remove + var remove = Popcorn.instances[1]; + + // Remove and check the length of the currently cached instances + equal( Popcorn.removeInstanceById( remove.id ).length, 1, "Removing an instance by id: 1 instance remains" ); + plus(); +}); test("guid", function () { @@ -120,34 +177,73 @@ test("Protected", function () { -test("Object", function () { +test( "Object", function () { + var popped = Popcorn( "#video" ), + popObj = Popcorn( document.getElementById( "video" ) ), + methods = "load play pause currentTime mute volume roundTime exec removePlugin", + count = 0, + expects = 30; - var popped = Popcorn("#video"), - methods = "load play pause currentTime mute volume roundTime exec removePlugin"; - - + expect( expects ); - ok( "video" in popped, "instance has `video` property" ); - ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" ); + function plus() { - ok( "data" in popped, "instance has `data` property" ); - ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" ); + if ( ++count === expects ) { + + start(); + } + } - ok( "trackEvents" in popped.data, "instance has `trackEvents` property" ); - ok( Object.prototype.toString.call(popped.data.trackEvents) === "[object Object]", "trackEvents property is an object" ) + stop( 10000 ); + + // testing element passed by id + ok( "video" in popped, "instance by id has `video` property" ); + plus(); + equal( Object.prototype.toString.call( popped.video ), "[object HTMLVideoElement]", "instance by id video property is a HTMLVideoElement" ); + plus(); + + ok( "data" in popped, "instance by id has `data` property" ); + plus(); + equal( Object.prototype.toString.call( popped.data ), "[object Object]", "instance by id data property is an object" ); + plus(); + + ok( "trackEvents" in popped.data, "instance by id has `trackEvents` property" ); + plus(); + equal( Object.prototype.toString.call( popped.data.trackEvents ), "[object Object]", "instance by id trackEvents property is an object" ); + plus(); - popped.play(); + methods.split( /\s+/g ).forEach(function ( k,v ) { - methods.split(/\s+/g).forEach(function (k,v) { + ok( k in popped, "instance by id has method: " + k ); + plus(); + }); - ok( k in popped, "instance has method: " + k ); - + // testing element passed by reference + ok( "video" in popObj, "instance by reference has `video` property" ); + plus(); + equal( Object.prototype.toString.call( popObj.video ), "[object HTMLVideoElement]", "instance by reference video property is a HTMLVideoElement" ); + plus(); + + ok( "data" in popObj, "instance by reference has `data` property" ); + plus(); + equal( Object.prototype.toString.call( popObj.data ), "[object Object]", "instance by reference data property is an object" ); + plus(); + + ok( "trackEvents" in popObj.data, "instance by reference has `trackEvents` property" ); + plus(); + equal( Object.prototype.toString.call( popObj.data.trackEvents ), "[object Object]", "instance by reference trackEvents property is an object" ); + plus(); + + popObj.play(); + + methods.split( /\s+/g ).forEach(function ( k,v ) { + + ok( k in popObj, "instance by reference has method: " + k ); + plus(); }); - - }); module("Popcorn Methods"); @@ -279,82 +375,6 @@ test("Stored By Type", function () { }); -test("Passed an object", function () { - - QUnit.reset(); - - var evtMethods = "addEventListener dispatchEvent".split(/\s+/), - methods = "load play pause currentTime playbackRate mute volume duration".split(/\s+/), - attributes = "readyState autoplay".split(/\s+/), - obj = {}, - p, - playEvent, - count = 0, - wants; - - function plus(){ - - if ( ++count === wants ) { - start(); - } - } - - Popcorn.forEach( methods, function( method ) { - obj[method] = (function() { - var calledWithArgs = 0, - calledWithoutArgs = 0; - - return function( arg ) { - if ( arg && !calledWithArgs ) { - calledWithArgs = 1; - ok(true, method+" called with argument"); - plus(); - } else if ( !arg && !calledWithoutArgs ) { - calledWithoutArgs = 1; - ok(true, method+" called without argument"); - plus(); - } - } - })(); - }); - - Popcorn.forEach( evtMethods, function( method ) { - obj[method] = (function() { - var hasCalled = 0; - - return function() { - if ( hasCalled ) { - return; - } - - hasCalled = 1; - ok(true, method+" called"); - plus(); - }; - })() - }); - - Popcorn.forEach( attributes, function( attr ) { - obj[attr] = 3; - }); - - p = Popcorn( obj ); - wants = methods.length*2 + evtMethods.length; - - expect( wants ); - stop( 10000 ); - - p.listen( "play", Popcorn.nop ); - - Popcorn.forEach( methods, function( method ) { - p[method](); - p[method]( 1 ); - }); - - p.trigger( "play" ); -}); - - test("Simulated", function () { QUnit.reset(); @@ -653,28 +673,22 @@ test("Update Timer", function () { test("Plugin Factory", function () { QUnit.reset(); - - // needs expectation var popped = Popcorn("#video"), methods = "load play pause currentTime mute volume roundTime exec removePlugin", - expects = 48, + expects = 34, count = 0; - - //expect(expects); - + function plus() { if ( ++count == expects ) { - Popcorn.removePlugin("breaker"); Popcorn.removePlugin("executor"); Popcorn.removePlugin("complicator"); - Popcorn.removePlugin("closure"); - start(); + start(); } } + expect( expects ); stop( 10000 ); - expect(expects); Popcorn.plugin("executor", function () { @@ -715,7 +729,7 @@ test("Plugin Factory", function () { ok( "executor" in popped, "executor plugin is now available to instance" ); plus(); - ok( Popcorn.registry.length === 1, "One item in the registry"); + equals( Popcorn.registry.length, 1, "One item in the registry"); plus(); @@ -724,8 +738,7 @@ test("Plugin Factory", function () { start: 1, end: 2 }); - - + Popcorn.plugin("complicator", { start: function ( event ) { @@ -763,18 +776,38 @@ test("Plugin Factory", function () { timeupdate: function () { } }); - - + ok( "complicator" in popped, "complicator plugin is now available to instance" ); plus(); - ok( Popcorn.registry.length === 2, "Two items in the registry"); + equals( Popcorn.registry.length, 2, "Two items in the registry"); plus(); popped.complicator({ start: 3, end: 4 - }); + }); + + popped.currentTime(0).play(); + +}); + +test("Plugin Breaker", function () { + + QUnit.reset(); + + var popped = Popcorn("#video"), + expects = 6, + count = 0; + + function plus() { + if ( ++count == expects ) { + Popcorn.removePlugin("breaker"); + start(); + } + } + expect( expects ); + stop( 10000 ); var breaker = { @@ -811,7 +844,7 @@ test("Plugin Factory", function () { ok( "breaker" in popped, "breaker plugin is now available to instance" ); plus(); - ok( Popcorn.registry.length === 3, "Three items in the registry"); + equals( Popcorn.registry.length, 1, "Three items in the registry"); plus(); popped.breaker({ @@ -819,6 +852,61 @@ test("Plugin Factory", function () { end: 2 }); + popped.currentTime(0).play(); + +}); + +test("Plugin Empty", function () { + + QUnit.reset(); + + var popped = Popcorn("#video"), + expects = 2, + testObj = {}, + count = 0; + + function plus() { + if ( ++count == expects ) { + Popcorn.removePlugin("empty"); + start(); + } + } + + expect( expects ); + stop( 10000 ); + + Popcorn.plugin("empty", testObj); + + popped.empty({}); + + ok( testObj.start, "default start function is generated" ); + plus(); + ok( testObj.end, "default end function is generated" ); + plus(); + + popped.currentTime(0).play(); + +}); + +test("Plugin Closure", function () { + + QUnit.reset(); + + var popped = Popcorn("#video"), + methods = "load play pause currentTime mute volume roundTime exec removePlugin", + expects = 8, + count = 0; + + function plus() { + if ( ++count == expects ) { + Popcorn.removePlugin("closure"); + start(); + } + } + + expect( expects ); + stop( 10000 ); + Popcorn.plugin("closure", function() { var startCount = 0; @@ -857,7 +945,7 @@ test("Plugin Factory", function () { nick: "second closure track" }); - popped.currentTime(0).play(); + popped.currentTime(5).play(); }); @@ -1728,9 +1816,6 @@ test("Parsing Handler - References unavailable plugin", function () { }); - - - module("Popcorn Test Runner End"); test("Last Check", function () {