From: rwldrn Date: Sun, 5 Dec 2010 20:04:29 +0000 (-0500) Subject: [#188] Support binding to custom events in plugins X-Git-Tag: v0.2~59^2~3 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=b27cd33ac9a3b722a3ddbb99d2dd6b76dae0791d;p=popcorn-js.git [#188] Support binding to custom events in plugins --- diff --git a/popcorn.js b/popcorn.js index 41e839e..a96403c 100644 --- a/popcorn.js +++ b/popcorn.js @@ -95,11 +95,9 @@ nop: function () {} }); - // Simple Factory pattern to implement native - // getters/setters and controllers - // as methods of the returned Popcorn instance - // The immediately invoked function creates - // and returns an object of methods + // Simple Factory pattern to implement getters, setters and controllers + // as methods of the returned Popcorn instance. The immediately invoked function + // creates and returns an object of methods Popcorn.extend(Popcorn.p, (function () { // todo: play, pause, mute should toggle @@ -118,7 +116,6 @@ } - if ( arg !== false && arg !== null && typeof arg !== "undefined" ) { this.video[ name ] = arg; @@ -155,40 +152,83 @@ } }); - - var nativeEvents = "loadstart progress suspend emptied stalled play pause " + - "loadedmetadata loadeddata waiting playing canplay canplaythrough " + - "seeking seeked timeupdate ended ratechange durationchange volumechange"; + + Popcorn.Events = { + UIEvents: "blur focus focusin focusout load resize scroll unload ", + MouseEvents: "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave click dblclick", + Events: "loadstart progress suspend emptied stalled play pause " + + "loadedmetadata loadeddata waiting playing canplay canplaythrough " + + "seeking seeked timeupdate ended ratechange durationchange volumechange" + }; + + Popcorn.Events.Natives = Popcorn.Events.UIEvents + " " + + Popcorn.Events.MouseEvents + " " + + Popcorn.Events.Events, Popcorn.events = { - //todo, fix types to be custom, natives will be native only - types: nativeEvents.split(/\s+/g), - natives: nativeEvents.split(/\s+/g), - custom: [ "frame" ], - all: [ "frame" ].concat( nativeEvents.split(/\s+/g) ), + + + isNative: function( type ) { + + var checks = Popcorn.Events.Natives.split(/\s+/g); + + for ( var i = 0; i < checks.length; i++ ) { + if ( checks[i] === type ) { + return true; + } + } + + return false; + }, + getInterface: function( type ) { + + if ( !Popcorn.events.isNative( type ) ) { + return false; + } + + var natives = Popcorn.Events, proto; + + for ( var p in natives ) { + if ( p !== "Natives" && natives[p].indexOf(type) > -1 ) { + proto = p; + } + } + + return proto; + + }, + + + all: Popcorn.Events.Natives.split(/\s+/g), + fn: { trigger: function ( type, data ) { // setup checks for custom event system if ( this.data.events[type] && Popcorn.sizeOf(this.data.events[type]) ) { - var evt = document.createEvent("Events"); - evt.initEvent(type, true, true, window, 1); + var interface = Popcorn.events.getInterface(type); - this.video.dispatchEvent(evt); - - /* + if ( interface ) { + + var evt = document.createEvent( interface ); + evt.initEvent(type, true, true, window, 1); - TODO: implement some form of custom event system + this.video.dispatchEvent(evt); + + return this; + } + + // Custom events Popcorn.forEach(this.data.events[type], function ( obj, key ) { - //obj.call(_target, _data); + obj.call( this, evt, data ); - // update to dispatch an event - - }); - */ + }, this); + } + + return this; }, listen: function ( type, fn ) { @@ -203,7 +243,7 @@ this.data.events[type][ fn.toString() + Popcorn.guid() ] = fn; // only attach one event of any type - if (!hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) { + if ( !hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) { this.video.addEventListener( type, function( event ) { @@ -244,7 +284,7 @@ // Provides some sugar, but ultimately extends // the definition into Popcorn.p - var natives = nativeEvents.split(/\s+/g), + var natives = Popcorn.events.all, reserved = [ "start", "end", "timeupdate" ], plugin = {}, setup; @@ -313,7 +353,6 @@ }); - // Future support for plugin event definitions // for all of the native events Popcorn.forEach( setup, function ( callback, type ) { diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index a81665b..a3c2f95 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -57,9 +57,6 @@ test("Object", function () { methods = "load play pause currentTime mute volume"; - - console.log(popped); - popped.play(); methods.split(/\s+/g).forEach(function (k,v) { @@ -195,11 +192,10 @@ test("Real", function () { completed = []; - var expects = 11, + var expects = 10, count = 0; - //expect(expects); - // not in full use + function plus(){ if ( ++count == expects ) start(); } @@ -208,8 +204,9 @@ test("Real", function () { Setup.events.forEach(function ( name ) { + p.listen( name, function (event) { - + if ( completed.indexOf(name) === -1 ) { ok(true, name + " fired"); plus(); @@ -235,6 +232,61 @@ test("Real", function () { +}); + +test("Custom", function () { + + var expects = 1, + count = 0; + + expect(expects); + + function plus(){ if ( ++count == expects ) start(); } + + stop(); + + var p = Popcorn("#video"); + + + p.listen("eventz0rz", function ( event ) { + + ok( true, "Custom event fired" ); + plus(); + + }); + + p.trigger("eventz0rz"); + + + +}); + + +test("UI/Mouse", function () { + + var expects = 1, + count = 0; + + expect(expects); + + function plus(){ if ( ++count == expects ) start(); } + + stop(); + + var p = Popcorn("#video"); + + + p.listen("click", function ( event ) { + + ok( true, "click event fired" ); + plus(); + + }); + + p.trigger("click"); + + + }); module("Popcorn Plugin") @@ -436,6 +488,7 @@ test("Events Extended", function () { }); + /* module("Popcorn Video Object") test("wait()", function () {