From a14b5bdc7e8e6c85d9ec541a62a5213b46ab3d21 Mon Sep 17 00:00:00 2001 From: euzuro Date: Fri, 6 Jul 2007 18:30:09 +0000 Subject: [PATCH] patch for #809 - add the addEventType() function to the OpenLayers.Events objects so that subclasses can safely add new event types without modifying their parents' constant values git-svn-id: http://svn.openlayers.org/trunk/openlayers@3622 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Events.js | 20 ++++++++++++++++---- tests/test_Events.html | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/OpenLayers/Events.js b/lib/OpenLayers/Events.js index 9ac1de2e0c..a3a321ab91 100644 --- a/lib/OpenLayers/Events.js +++ b/lib/OpenLayers/Events.js @@ -410,7 +410,7 @@ OpenLayers.Events.prototype = { // custom application event. if (this.eventTypes != null) { for (var i = 0; i < this.eventTypes.length; i++) { - this.listeners[ this.eventTypes[i] ] = new Array(); + this.addEventType(this.eventTypes[i]); } } @@ -437,6 +437,20 @@ OpenLayers.Events.prototype = { this.eventHandler = null; }, + /** + * APIMethod: addEventType + * Add a new event type to this events object. + * If the event type has already been added, do nothing. + * + * Parameters: + * eventName - {String} + */ + addEventType: function(eventName) { + if (!this.listeners[eventName]) { + this.listeners[eventName] = new Array(); + } + }, + /** * Method: attachToElement * @@ -449,9 +463,7 @@ OpenLayers.Events.prototype = { // every browser event has a corresponding application event // (whether it's listened for or not). - if (this.listeners[eventType] == null) { - this.listeners[eventType] = new Array(); - } + this.addEventType(eventType); // use Prototype to register the event cross-browser OpenLayers.Event.observe(element, eventType, this.eventHandler); diff --git a/tests/test_Events.html b/tests/test_Events.html index 1f84922626..b15d21578c 100644 --- a/tests/test_Events.html +++ b/tests/test_Events.html @@ -304,6 +304,33 @@ } + function test_06_Events_addEventType(t) { + + t.plan( 6 ); + + var mapDiv = OpenLayers.Util.getElement('map'); + var obj = {result: 0}; + var eventTypes = ["doThingA", "doThingB"]; + + events = new OpenLayers.Events(obj, mapDiv, eventTypes); + + + t.eq( events.listeners["doThingA"].length, 0, "event type passed as passed as param to OpenLayers.Events constructor correctly set up" ); + t.eq( events.listeners["doThingB"].length, 0, "event type passed as passed as param to OpenLayers.Events constructor correctly set up" ); + + var newEventType = "onFoo"; + t.ok( events.listeners[newEventType] == null, "event type not yet registered has null entry in listeners array"); + + events.addEventType(newEventType); + t.eq( events.listeners[newEventType].length, 0, "event type passed to addEventType correctly set up" ); + + var func = function () {}; + events.register( "doThingA", obj, func ); + t.eq( events.listeners["doThingA"].length, 1, "listener correctly registered" ); + + events.addEventType("doThingsA"); + t.eq( events.listeners["doThingA"].length, 1, "event type passed to addEventType correctly does nothing if clashes with already defined event type" ); + } // -->