From: rwldrn Date: Sun, 5 Dec 2010 21:07:39 +0000 (-0500) Subject: [#192] Create exec shorthand X-Git-Tag: v0.2~59^2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=359d1469c96a9bfd9a2ff58f131009a046a4516f;p=popcorn-js.git [#192] Create exec shorthand --- diff --git a/popcorn.js b/popcorn.js index 386ed5c..5543e02 100644 --- a/popcorn.js +++ b/popcorn.js @@ -140,10 +140,33 @@ }, - - - - + exec: function ( time, fn ) { + + !fn && ( fn = Popcorn.nop ); + + + var timer = 0, + self = this, + callback = function execCallback( event ) { + + if ( this.currentTime() >= time && !timer ) { + + fn.call(self, event); + + this.unlisten("execCallback"); + + timer++; + } + }; + + + + this.listen("timeupdate", callback); + + + + return this; + }, @@ -247,7 +270,7 @@ } // Register - this.data.events[type][ fn.toString() + Popcorn.guid() ] = fn; + this.data.events[type][ fn.name || ( fn.toString() + Popcorn.guid() ) ] = fn; // only attach one event of any type if ( !hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) { @@ -255,7 +278,11 @@ this.video.addEventListener( type, function( event ) { Popcorn.forEach( self.data.events[type], function ( obj, key ) { - obj.call(self, event); + + if ( typeof obj === "function" ) { + obj.call(self, event); + } + }); //fn.call( self, event ); @@ -264,7 +291,13 @@ } return this; }, - unlisten: function( type ) { + unlisten: function( type, fn ) { + + if ( this.data.events[type] && this.data.events[type][fn] ) { + this.data.events[type][fn] = null; + return this; + } + this.data.events[type] = null; return this; }, diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index fd888d6..8b3d31e 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -54,7 +54,9 @@ test("Object", function () { var popped = Popcorn("#video"), - methods = "load play pause currentTime mute volume roundTime"; + methods = "load play pause currentTime mute volume roundTime exec"; + + ok( "video" in popped, "instance has `video` property" ); ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" ); @@ -78,8 +80,11 @@ test("Object", function () { }); -module("Popcorn Object Instance"); -test("Methods", function () { +module("Popcorn Methods"); + + + +test("roundTime", function () { QUnit.reset(); @@ -88,9 +93,39 @@ test("Methods", function () { popped.play().pause().currentTime( 0.98 ); equals( 1, popped.roundTime(), ".roundTime() returns 1 when currentTime is 0.98s" ); + + +}); + + +test("exec", function () { + + QUnit.reset(); + + var popped = Popcorn("#video"), + expects = 1, + count = 0; + + expect(expects); + + function plus(){ + if ( ++count == expects ) start(); + } + + stop(); + popped.exec( 4, function () { + + + ok(true, "exec function"); + plus(); + + }).currentTime(3).play(); + + + });