},
listen: function ( type, fn ) {
- var self = this;
+ var self = this, hasEvents = true;
if ( !this.data.events[type] ) {
this.data.events[type] = {};
+ hasEvents = false;
}
// Register
this.data.events[type][ fn.toString() + Popcorn.guid() ] = fn;
- if ( Popcorn.events.all.indexOf( type ) > -1 ) {
-
+ // only attach one event of any type
+ if (!hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) {
+
this.video.addEventListener( type, function( event ) {
Popcorn.forEach( self.data.events[type], function ( obj, key ) {
obj.call(self, event);
- });
+ });
+
+ //fn.call( self, event );
- }, false);
-
+ }, false);
}
return this;
},
+ unlisten: function( type ) {
+ this.data.events[type] = null;
+ return this;
+ },
special: {
// handles timeline controllers
play: function () {
};
// Extend listen and trigger to all Popcorn instances
- Popcorn.forEach( ["trigger", "listen"], function ( key ) {
+ Popcorn.forEach( ["trigger", "listen", "unlisten"], function ( key ) {
Popcorn.p[key] = Popcorn.events.fn[key];
});
});
+test("Popcorn Events Stored By Type", function () {
+
+ expect(6)
+
+ var p = Popcorn("#video"),
+ count = 0,
+ fired = 0
+ ;
+ function plus(){
+ if ( ++count == 4 ) {
+
+ if ( fired === 4 ) {
+ ok( true, fired + " callbacks fired from 1 handler" );
+ }
+ else {
+ ok( false, fired + " callbacks fired - CHECK HANDLERS" );
+ }
-test("Popcorn Events Simulated", function () {
+ p.unlisten("play");
+
+
+ ok( !p.data.events["play"], "play handlers removed" );
+
+
+ start();
+ }
+ }
+
+ stop();
+
+
+ p.listen("play", function () {
+ fired++;
+
+ ok(true, "Play fired");
+ plus();
+ });
+
+ p.listen("play", function () {
+ fired++;
+
+ ok(true, "Play fired");
+ plus();
+ });
+
+ p.listen("play", function () {
+ fired++;
+
+ ok(true, "Play fired");
+ plus();
+ });
+ p.listen("play", function () {
+ fired++;
+
+ ok(true, "Play fired");
+ plus();
+ });
+
+ p.trigger("play");
+
+
+
+});
+
+
+test("Popcorn Events Simulated", function () {
var p = Popcorn("#video"),
completed = [],
eventtest = "loadstart progress suspend emptied stalled play pause " +
- "loadedmetadata loadeddata waiting playing canplay canplaythrough " +
- "seeking seeked timeupdate ended ratechange durationchange volumechange",
+ "loadedmetadata loadeddata waiting playing canplay canplaythrough " +
+ "seeking seeked timeupdate ended ratechange durationchange volumechange",
events = eventtest.split(/\s+/g);
-
+
var expects = events.length,
count = 0;