Skip to content

Commit

Permalink
Merge remote-tracking branch 'rwldrn/t586' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jbuck committed Jun 22, 2011
2 parents 9a20736 + ada7fc8 commit c6eadc9
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 4 deletions.
65 changes: 61 additions & 4 deletions popcorn.js
Expand Up @@ -165,6 +165,9 @@
// Stores DOM event queues by type
events: {},

// Stores Special event hooks data
hooks: {},

// Store track event history data
history: [],

Expand Down Expand Up @@ -601,14 +604,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
Expand All @@ -622,8 +661,6 @@
}
});

//fn.call( self, event );

}, false);
}
return this;
Expand All @@ -641,6 +678,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;
}
}
}
}
};

Expand Down
66 changes: 66 additions & 0 deletions test/popcorn.unit.js
Expand Up @@ -623,6 +623,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);
});
});

/*
<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 () {

Expand Down

0 comments on commit c6eadc9

Please sign in to comment.