// Stores DOM event queues by type
events: {},
+ // Stores Special event hooks data
+ hooks: {},
+
// Store track event history data
history: [],
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
}
});
- //fn.call( self, event );
-
}, false);
}
return this;
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;
+ }
+ }
+ }
}
};
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() { /* ... */ });
});
+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);
+ });
+});
+
+/*
+<video height="180" width="300" id="video" controls>
+<source src="http://videos.mozilla.org/serv/webmademovies/popcornplug.mp4"></source>
+<source src="http://videos.mozilla.org/serv/webmademovies/popcornplug.ogv"></source>
+<source src="http://videos.mozilla.org/serv/webmademovies/popcornplug.webm"></source>
+</video>
+
+*/
+
module("Popcorn Position");
test("position", function () {