From a38a73e77fd92e669d99f76990bab8fa04d7df8b Mon Sep 17 00:00:00 2001 From: rwldrn Date: Fri, 17 Jun 2011 15:35:04 -0400 Subject: [PATCH] [#586] Implements Popcorn.events.hooks & canplayall hook; --- popcorn.js | 65 +++++++++++++++++++++++++++++++++++++--- test/popcorn.unit.js | 70 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 129 insertions(+), 6 deletions(-) diff --git a/popcorn.js b/popcorn.js index efa17bf..5c6b668 100644 --- a/popcorn.js +++ b/popcorn.js @@ -165,6 +165,9 @@ // Stores DOM event queues by type events: {}, + // Stores Special event hooks data + hooks: {}, + // Store track event history data history: [], @@ -592,14 +595,50 @@ listen: function( type, fn ) { var self = this, - hasEvents = true; + hasEvents = true, + eventHook = Popcorn.events.hooks[ type ], + origType = type, + tmp; if ( !this.data.events[ type ] ) { this.data.events[ type ] = {}; hasEvents = false; } - // Register + // Check and setup event hooks + if ( eventHook ) { + + // Execute hook add method if defined + if ( eventHook.add ) { + eventHook.add.call( this ); + } + + // Reassign event type to our piggyback event type if defined + if ( eventHook.bind ) { + type = eventHook.bind; + } + + // Reassign handler if defined + if ( eventHook.handler ) { + tmp = fn; + + fn = function wrapper( event ) { + eventHook.handler.call( self, event, tmp ); + }; + } + + // assume the piggy back event is registered + hasEvents = true; + + // Setup event registry entry + if ( !this.data.events[ type ] ) { + this.data.events[ type ] = {}; + // Toggle if the previous assumption was untrue + hasEvents = false; + } + } + + // Register event and handler this.data.events[ type ][ fn.name || ( fn.toString() + Popcorn.guid() ) ] = fn; // only attach one event of any type @@ -613,8 +652,6 @@ } }); - //fn.call( self, event ); - }, false); } return this; @@ -632,6 +669,26 @@ return this; } + }, + hooks: { + canplayall: { + bind: "canplaythrough", + add: function() { + this.data.hooks.canplayall = { + fired: false + }; + }, + // declare special handling instructions + handler: function canplayall( event, callback ) { + + if ( !this.data.hooks.canplayall.fired ) { + // trigger original user callback once + callback.call( this, event ); + + this.data.hooks.canplayall.fired = true; + } + } + } } }; diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 7c4a603..cdf1df6 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -433,8 +433,8 @@ test("Popcorn.[addTrackEvent | removeTrackEvent].ref()", function() { var popped = Popcorn("#video"); - // Calling exec() will create tracks and added them to the - // trackreference internally + // Calling exec() will create tracks and added them to the + // trackreference internally popped.exec( 1, function() { /* ... */ }); popped.exec( 3, function() { /* ... */ }); popped.exec( 5, function() { /* ... */ }); @@ -593,6 +593,72 @@ test( "Popcorn.events", function() { }); +test("Popcorn.events.hooks", function() { + + expect(1); + + ok( Popcorn.events.hooks, "Popcorn.events.hooks exists" ); + +}); + +test("Popcorn.events.hooks: canplayall", function() { + //qunit-fixture + + var expects = 1, + count = 0, + fired = 0; + + expect(expects); + + function plus(){ + if ( ++count == expects ) { + start(); + } + } + + stop( 10000 ); + + var video = document.createElement("video"), + sources = { + mp4: document.createElement("source"), + ogv: document.createElement("source"), + webm: document.createElement("source") + }, + url = "http://videos.mozilla.org/serv/webmademovies/popcornplug."; + + video.id = "event-fixture"; + video.controls = true; + video.preload = "auto"; + + Popcorn.forEach( sources, function( source, type ) { + source.src = url + type; + video.appendChild( source ); + }); + + document.getElementById("qunit-fixture").appendChild( video ); + + var $pop = Popcorn("#event-fixture"); + + $pop.listen("canplayall", function( event ) { + equal( ++fired, 1, "canplayall is fired only once" ); + plus(); + }); + + $pop.listen("canplaythrough", function( event ) { + // this should trigger re-fires of the original event + this.currentTime(0); + }); +}); + +/* + + +*/ + module("Popcorn Position"); test("position", function () { -- 2.43.0