From f27833f1dbd60625310d355e0f42bbcae3546648 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 8 Feb 2008 23:31:27 +0000 Subject: [PATCH] Trigger feature related events with feature information and layer related events with layer information. Also adding events.on and events.un convenience methods. r=crschmidt (closes #1343) git-svn-id: http://svn.openlayers.org/trunk/openlayers@6149 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Control/Attribution.js | 22 ++++++---- lib/OpenLayers/Control/LayerSwitcher.js | 22 ++++++---- lib/OpenLayers/Control/ModifyFeature.js | 32 ++++++++------ lib/OpenLayers/Control/MouseDefaults.js | 30 ++++++++------ lib/OpenLayers/Control/MouseToolbar.js | 9 ++-- lib/OpenLayers/Control/OverviewMap.js | 8 ++-- lib/OpenLayers/Control/PanZoomBar.js | 43 ++++++++++++------- lib/OpenLayers/Control/Permalink.js | 9 ++-- lib/OpenLayers/Control/SelectFeature.js | 2 + lib/OpenLayers/Events.js | 55 ++++++++++++++++++++----- lib/OpenLayers/Layer.js | 35 +++++++++++++--- lib/OpenLayers/Layer/Grid.js | 7 +++- lib/OpenLayers/Layer/Vector.js | 50 ++++++++++++++++++++++ lib/OpenLayers/Layer/WFS.js | 7 +++- lib/OpenLayers/Map.js | 41 ++++++++++++++---- lib/OpenLayers/Popup.js | 16 ++++--- tests/Control/test_ModifyFeature.html | 27 ++++++++---- tests/Control/test_SelectFeature.html | 5 ++- tests/Layer/test_Grid.html | 3 +- tests/test_Events.html | 21 ++++++---- 20 files changed, 326 insertions(+), 118 deletions(-) diff --git a/lib/OpenLayers/Control/Attribution.js b/lib/OpenLayers/Control/Attribution.js index d9951b5d00..63d88279aa 100644 --- a/lib/OpenLayers/Control/Attribution.js +++ b/lib/OpenLayers/Control/Attribution.js @@ -35,10 +35,13 @@ OpenLayers.Control.Attribution = * Destroy control. */ destroy: function() { - this.map.events.unregister("removelayer", this, this.updateAttribution); - this.map.events.unregister("addlayer", this, this.updateAttribution); - this.map.events.unregister("changelayer", this, this.updateAttribution); - this.map.events.unregister("changebaselayer", this, this.updateAttribution); + this.map.events.un({ + "removelayer": this.updateAttribution, + "addlayer": this.updateAttribution, + "changelayer": this.updateAttribution, + "changebaselayer": this.updateAttribution, + scope: this + }); OpenLayers.Control.prototype.destroy.apply(this, arguments); }, @@ -53,10 +56,13 @@ OpenLayers.Control.Attribution = draw: function() { OpenLayers.Control.prototype.draw.apply(this, arguments); - this.map.events.register('changebaselayer', this, this.updateAttribution); - this.map.events.register('changelayer', this, this.updateAttribution); - this.map.events.register('addlayer', this, this.updateAttribution); - this.map.events.register('removelayer', this, this.updateAttribution); + this.map.events.on({ + 'changebaselayer': this.updateAttribution, + 'changelayer': this.updateAttribution, + 'addlayer': this.updateAttribution, + 'removelayer': this.updateAttribution, + scope: this + }); this.updateAttribution(); return this.div; diff --git a/lib/OpenLayers/Control/LayerSwitcher.js b/lib/OpenLayers/Control/LayerSwitcher.js index 5f8291a871..d90e30ec7c 100644 --- a/lib/OpenLayers/Control/LayerSwitcher.js +++ b/lib/OpenLayers/Control/LayerSwitcher.js @@ -113,10 +113,13 @@ OpenLayers.Control.LayerSwitcher = this.clearLayersArray("base"); this.clearLayersArray("data"); - this.map.events.unregister("addlayer", this, this.redraw); - this.map.events.unregister("changelayer", this, this.redraw); - this.map.events.unregister("removelayer", this, this.redraw); - this.map.events.unregister("changebaselayer", this, this.redraw); + this.map.events.un({ + "addlayer": this.redraw, + "changelayer": this.redraw, + "removelayer": this.redraw, + "changebaselayer": this.redraw, + scope: this + }); OpenLayers.Control.prototype.destroy.apply(this, arguments); }, @@ -130,10 +133,13 @@ OpenLayers.Control.LayerSwitcher = setMap: function(map) { OpenLayers.Control.prototype.setMap.apply(this, arguments); - this.map.events.register("addlayer", this, this.redraw); - this.map.events.register("changelayer", this, this.redraw); - this.map.events.register("removelayer", this, this.redraw); - this.map.events.register("changebaselayer", this, this.redraw); + this.map.events.on({ + "addlayer": this.redraw, + "changelayer": this.redraw, + "removelayer": this.redraw, + "changebaselayer": this.redraw, + scope: this + }); }, /** diff --git a/lib/OpenLayers/Control/ModifyFeature.js b/lib/OpenLayers/Control/ModifyFeature.js index 14bfffa180..9ee2dcb1fc 100644 --- a/lib/OpenLayers/Control/ModifyFeature.js +++ b/lib/OpenLayers/Control/ModifyFeature.js @@ -182,17 +182,16 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { var selectOptions = { geometryTypes: this.geometryTypes, clickout: this.clickout, - toggle: this.toggle, - onSelect: function(feature) { - control.selectFeature.apply(control, [feature]); - }, - onUnselect: function(feature) { - control.unselectFeature.apply(control, [feature]); - } + toggle: this.toggle }; this.selectControl = new OpenLayers.Control.SelectFeature( layer, selectOptions ); + this.layer.events.on({ + "featureselected": this.selectFeature, + "featureunselected": this.unselectFeature, + scope: this + }); // configure the drag control var dragOptions = { @@ -226,6 +225,11 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { * Take care of things that are not handled in superclass. */ destroy: function() { + this.layer.events.un({ + "featureselected": this.selectFeature, + "featureunselected": this.unselectFeature, + scope: this + }); this.layer = null; this.selectControl.destroy(); this.dragControl.destroy(); @@ -276,10 +280,11 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { * Called when the select feature control selects a feature. * * Parameters: - * feature - {} The selected feature. + * object - {Object} Object with a feature property referencing the + * selected feature. */ - selectFeature: function(feature) { - this.feature = feature; + selectFeature: function(object) { + this.feature = object.feature; this.resetVertices(); this.dragControl.activate(); this.onModificationStart(this.feature); @@ -290,9 +295,10 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { * Called when the select feature control unselects a feature. * * Parameters: - * feature - {} The unselected feature. + * object - {Object} Object with a feature property referencing the + * unselected feature. */ - unselectFeature: function(feature) { + unselectFeature: function(object) { this.layer.removeFeatures(this.vertices); this.vertices = []; this.layer.destroyFeatures(this.virtualVertices); @@ -307,7 +313,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { } this.feature = null; this.dragControl.deactivate(); - this.onModificationEnd(feature); + this.onModificationEnd(object.feature); }, /** diff --git a/lib/OpenLayers/Control/MouseDefaults.js b/lib/OpenLayers/Control/MouseDefaults.js index 78c075d213..d587b845c5 100644 --- a/lib/OpenLayers/Control/MouseDefaults.js +++ b/lib/OpenLayers/Control/MouseDefaults.js @@ -47,12 +47,15 @@ OpenLayers.Control.MouseDefaults = OpenLayers.Class(OpenLayers.Control, { } this.handler = null; - this.map.events.unregister( "click", this, this.defaultClick ); - this.map.events.unregister( "dblclick", this, this.defaultDblClick ); - this.map.events.unregister( "mousedown", this, this.defaultMouseDown ); - this.map.events.unregister( "mouseup", this, this.defaultMouseUp ); - this.map.events.unregister( "mousemove", this, this.defaultMouseMove ); - this.map.events.unregister( "mouseout", this, this.defaultMouseOut ); + this.map.events.un({ + "click": this.defaultClick, + "dblclick": this.defaultDblClick, + "mousedown": this.defaultMouseDown, + "mouseup": this.defaultMouseUp, + "mousemove": this.defaultMouseMove, + "mouseout": this.defaultMouseOut, + scope: this + }); //unregister mousewheel events specifically on the window and document OpenLayers.Event.stopObserving(window, "DOMMouseScroll", @@ -70,12 +73,15 @@ OpenLayers.Control.MouseDefaults = OpenLayers.Class(OpenLayers.Control, { * Method: draw */ draw: function() { - this.map.events.register( "click", this, this.defaultClick ); - this.map.events.register( "dblclick", this, this.defaultDblClick ); - this.map.events.register( "mousedown", this, this.defaultMouseDown ); - this.map.events.register( "mouseup", this, this.defaultMouseUp ); - this.map.events.register( "mousemove", this, this.defaultMouseMove ); - this.map.events.register( "mouseout", this, this.defaultMouseOut ); + this.map.events.on({ + "click": this.defaultClick, + "dblclick": this.defaultDblClick, + "mousedown": this.defaultMouseDown, + "mouseup": this.defaultMouseUp, + "mousemove": this.defaultMouseMove, + "mouseout": this.defaultMouseOut, + scope: this + }); this.registerWheelEvents(); diff --git a/lib/OpenLayers/Control/MouseToolbar.js b/lib/OpenLayers/Control/MouseToolbar.js index 0bed1e9928..c74639f3ef 100644 --- a/lib/OpenLayers/Control/MouseToolbar.js +++ b/lib/OpenLayers/Control/MouseToolbar.js @@ -105,9 +105,12 @@ OpenLayers.Control.MouseToolbar = OpenLayers.Class( btn.activeImgLocation = activeImgLocation; btn.events = new OpenLayers.Events(this, btn, null, true); - btn.events.register("mousedown", this, this.buttonDown); - btn.events.register("mouseup", this, this.buttonUp); - btn.events.register("dblclick", this, OpenLayers.Event.stop); + btn.events.on({ + "mousedown": this.buttonDown, + "mouseup": this.buttonUp, + "dblclick": OpenLayers.Event.stop, + scope: this + }); btn.action = id; btn.title = title; btn.alt = title; diff --git a/lib/OpenLayers/Control/OverviewMap.js b/lib/OpenLayers/Control/OverviewMap.js index a5cce99886..f63e1016c1 100644 --- a/lib/OpenLayers/Control/OverviewMap.js +++ b/lib/OpenLayers/Control/OverviewMap.js @@ -154,9 +154,11 @@ OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, { this.minimizeDiv = null; } - this.map.events.unregister('moveend', this, this.update); - this.map.events.unregister("changebaselayer", this, - this.baseLayerDraw); + this.map.events.un({ + "moveend": this.update, + "changebaselayer": this.baseLayerDraw, + scope: this + }); OpenLayers.Control.prototype.destroy.apply(this, arguments); }, diff --git a/lib/OpenLayers/Control/PanZoomBar.js b/lib/OpenLayers/Control/PanZoomBar.js index ed3e6ca1b2..adb7c65cc5 100644 --- a/lib/OpenLayers/Control/PanZoomBar.js +++ b/lib/OpenLayers/Control/PanZoomBar.js @@ -79,8 +79,11 @@ OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, { this.divEvents.destroy(); this.divEvents = null; - this.map.events.unregister("zoomend", this, this.moveZoomBar); - this.map.events.unregister("changebaselayer", this, this.redraw); + this.map.events.un({ + "zoomend": this.moveZoomBar, + "changebaselayer": this.redraw, + scope: this + }); OpenLayers.Control.PanZoom.prototype.destroy.apply(this, arguments); }, @@ -164,11 +167,13 @@ OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, { this.slider = slider; this.sliderEvents = new OpenLayers.Events(this, slider, null, true); - this.sliderEvents.register("mousedown", this, this.zoomBarDown); - this.sliderEvents.register("mousemove", this, this.zoomBarDrag); - this.sliderEvents.register("mouseup", this, this.zoomBarUp); - this.sliderEvents.register("dblclick", this, this.doubleClick); - this.sliderEvents.register("click", this, this.doubleClick); + this.sliderEvents.on({ + "mousedown": this.zoomBarDown, + "mousemove": this.zoomBarDrag, + "mouseup": this.zoomBarUp, + "dblclick": this.doubleClick, + "click": this.doubleClick + }); var sz = new OpenLayers.Size(); sz.h = this.zoomStopHeight * this.map.getNumZoomLevels(); @@ -194,10 +199,12 @@ OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, { this.zoombarDiv = div; this.divEvents = new OpenLayers.Events(this, div, null, true); - this.divEvents.register("mousedown", this, this.divClick); - this.divEvents.register("mousemove", this, this.passEventToSlider); - this.divEvents.register("dblclick", this, this.doubleClick); - this.divEvents.register("click", this, this.doubleClick); + this.divEvents.on({ + "mousedown": this.divClick, + "mousemove": this.passEventToSlider, + "dblclick": this.doubleClick, + "click": this.doubleClick + }); this.div.appendChild(div); @@ -250,8 +257,11 @@ OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, { if (!OpenLayers.Event.isLeftClick(evt)) { return; } - this.map.events.register("mousemove", this, this.passEventToSlider); - this.map.events.register("mouseup", this, this.passEventToSlider); + this.map.events.on({ + "mousemove": this.passEventToSlider, + "mouseup": this.passEventToSlider, + scope: this + }); this.mouseDragStart = evt.xy.clone(); this.zoomStart = evt.xy.clone(); this.div.style.cursor = "move"; @@ -298,8 +308,11 @@ OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, { } if (this.zoomStart) { this.div.style.cursor=""; - this.map.events.unregister("mouseup", this, this.passEventToSlider); - this.map.events.unregister("mousemove", this, this.passEventToSlider); + this.map.events.un({ + "mouseup": this.passEventToSlider, + "mousemove": this.passEventToSlider, + scope: this + }); var deltaY = this.zoomStart.y - evt.xy.y; this.map.zoomTo(this.map.zoom + Math.round(deltaY/this.zoomStopHeight)); this.moveZoomBar(); diff --git a/lib/OpenLayers/Control/Permalink.js b/lib/OpenLayers/Control/Permalink.js index ed566213d8..8f2a727449 100644 --- a/lib/OpenLayers/Control/Permalink.js +++ b/lib/OpenLayers/Control/Permalink.js @@ -115,9 +115,12 @@ OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, { this.element.href=""; this.div.appendChild(this.element); } - this.map.events.register('moveend', this, this.updateLink); - this.map.events.register('changelayer', this, this.updateLink); - this.map.events.register('changebaselayer', this, this.updateLink); + this.map.events.on({ + 'moveend': this.updateLink, + 'changelayer': this.updateLink, + 'changebaselayer': this.updateLink, + scope: this + }); return this.div; }, diff --git a/lib/OpenLayers/Control/SelectFeature.js b/lib/OpenLayers/Control/SelectFeature.js index c31c345c52..5e2292bf2b 100644 --- a/lib/OpenLayers/Control/SelectFeature.js +++ b/lib/OpenLayers/Control/SelectFeature.js @@ -266,6 +266,7 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, { OpenLayers.Util.extend(feature.style, selectStyle); this.layer.drawFeature(feature); + this.layer.events.triggerEvent("featureselected", {feature: feature}); this.onSelect(feature); }, @@ -284,6 +285,7 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, { } this.layer.drawFeature(feature); OpenLayers.Util.removeItem(this.layer.selectedFeatures, feature); + this.layer.events.triggerEvent("featureunselected", {feature: feature}); this.onUnselect(feature); }, diff --git a/lib/OpenLayers/Events.js b/lib/OpenLayers/Events.js index 973998fee5..c618d01d9a 100644 --- a/lib/OpenLayers/Events.js +++ b/lib/OpenLayers/Events.js @@ -481,6 +481,27 @@ OpenLayers.Events = OpenLayers.Class({ // disable dragstart in IE so that mousedown/move/up works normally OpenLayers.Event.observe(element, "dragstart", OpenLayers.Event.stop); }, + + /** + * Method: on + * Convenience method for registering listeners with a common scope. + * + * Example use: + * (code) + * events.on({ + * "loadstart": loadStartListener, + * "loadend": loadEndListener, + * scope: object + * }); + * (end) + */ + on: function(object) { + for(var type in object) { + if(type != "scope") { + this.register(type, object.scope, object[type]); + } + } + }, /** * APIMethod: register @@ -552,6 +573,27 @@ OpenLayers.Events = OpenLayers.Class({ } }, + /** + * Method: un + * Convenience method for unregistering listeners with a common scope. + * + * Example use: + * (code) + * events.un({ + * "loadstart": loadStartListener, + * "loadend": loadEndListener, + * scope: object + * }); + * (end) + */ + un: function(object) { + for(var type in object) { + if(type != "scope") { + this.unregister(type, object.scope, object[type]); + } + } + }, + /** * APIMethod: unregister * @@ -596,13 +638,12 @@ OpenLayers.Events = OpenLayers.Class({ * Parameters: * type - {String} * evt - {Event} - * args - {Array} Optional array of arguments to call the listener with. * * Returns: * {Boolean} The last listener return. If a listener returns false, the * chain of listeners will stop getting called. */ - triggerEvent: function (type, evt, args) { + triggerEvent: function (type, evt) { // prep evt object with object & div references if (evt == null) { @@ -610,13 +651,7 @@ OpenLayers.Events = OpenLayers.Class({ } evt.object = this.object; evt.element = this.element; - - if(!args) { - args = [evt]; - } else { - args.unshift(evt); - } - + // execute all callbacks registered for specified type // get a clone of the listeners array to // allow for splicing during callbacks @@ -627,7 +662,7 @@ OpenLayers.Events = OpenLayers.Class({ for (var i = 0; i < listeners.length; i++) { var callback = listeners[i]; // bind the context to callback.obj - continueChain = callback.func.apply(callback.obj, args); + continueChain = callback.func.apply(callback.obj, [evt]); if ((continueChain != undefined) && (continueChain == false)) { // if callback returns false, execute no more callbacks. diff --git a/lib/OpenLayers/Layer.js b/lib/OpenLayers/Layer.js index 8c452b1b05..cb21388ada 100644 --- a/lib/OpenLayers/Layer.js +++ b/lib/OpenLayers/Layer.js @@ -37,9 +37,26 @@ OpenLayers.Layer = OpenLayers.Class({ */ opacity: null, - /** + /** * Constant: EVENT_TYPES - * {Array(String)} Supported application event types + * {Array(String)} Supported application event types. Register a listener + * for a particular event with the following syntax: + * (code) + * layer.events.register(type, obj, listener); + * (end) + * + * Listeners will be called with a reference to an event object. The + * properties of this event depends on exactly what happened. + * + * All event objects have at least the following properties: + * - *object* {Object} A reference to layer.events.object. + * - *element* {DOMElement} A reference to layer.events.element. + * + * Supported map event types: + * - *loadstart* Triggered when layer loading starts. + * - *loadend* Triggered when layer loading ends. + * - *loadcancel* Triggered when layer loading is canceled. + * - *visibilitychanged* Triggered when layer visibility is changed. */ EVENT_TYPES: ["loadstart", "loadend", "loadcancel", "visibilitychanged"], @@ -327,7 +344,10 @@ OpenLayers.Layer = OpenLayers.Class({ if (newName != this.name) { this.name = newName; if (this.map != null) { - this.map.events.triggerEvent("changelayer"); + this.map.events.triggerEvent("changelayer", { + layer: this, + property: "name" + }); } } }, @@ -530,7 +550,10 @@ OpenLayers.Layer = OpenLayers.Class({ this.display(visibility); this.redraw(); if (this.map != null) { - this.map.events.triggerEvent("changelayer"); + this.map.events.triggerEvent("changelayer", { + layer: this, + property: "visibility" + }); } this.events.triggerEvent("visibilitychanged"); } @@ -576,7 +599,9 @@ OpenLayers.Layer = OpenLayers.Class({ if (isBaseLayer != this.isBaseLayer) { this.isBaseLayer = isBaseLayer; if (this.map != null) { - this.map.events.triggerEvent("changebaselayer"); + this.map.events.triggerEvent("changebaselayer", { + layer: this + }); } } }, diff --git a/lib/OpenLayers/Layer/Grid.js b/lib/OpenLayers/Layer/Grid.js index 43d9f48918..dc41417f82 100644 --- a/lib/OpenLayers/Layer/Grid.js +++ b/lib/OpenLayers/Layer/Grid.js @@ -526,8 +526,11 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, { * tile - {} */ removeTileMonitoringHooks: function(tile) { - tile.events.unregister("loadstart", this, tile.onLoadStart); - tile.events.unregister("loadend", this, tile.onLoadEnd); + tile.events.un({ + "loadstart": tile.onLoadStart, + "loadend": tile.onLoadEnd, + scope: this + }); }, /** diff --git a/lib/OpenLayers/Layer/Vector.js b/lib/OpenLayers/Layer/Vector.js index f48fc63436..f3334dfaa2 100644 --- a/lib/OpenLayers/Layer/Vector.js +++ b/lib/OpenLayers/Layer/Vector.js @@ -19,6 +19,46 @@ */ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, { + /** + * Constant: EVENT_TYPES + * {Array(String)} Supported application event types. Register a listener + * for a particular event with the following syntax: + * (code) + * layer.events.register(type, obj, listener); + * (end) + * + * Listeners will be called with a reference to an event object. The + * properties of this event depends on exactly what happened. + * + * All event objects have at least the following properties: + * - *object* {Object} A reference to layer.events.object. + * - *element* {DOMElement} A reference to layer.events.element. + * + * Supported map event types: + * - *loadstart* Triggered when layer loading starts. + * - *loadend* Triggered when layer loading ends. + * - *loadcancel* Triggered when layer loading is canceled. + * - *visibilitychanged* Triggered when layer visibility is changed. + * - *beforefeatureadded* Triggered before a feature is added. Listeners + * will receive an object with a *feature* property referencing the + * feature to be added. + * - *featureadded* Triggered after a feature is added. The event + * object passed to listeners will have a *feature* property with a + * reference to the added feature. + * - *featuresadded* Triggered after features are added. The event + * object passed to listeners will have a *features* property with a + * reference to an array of added features. + * - *featureselected* Triggered after a feature is selected. Listeners + * will receive an object with a *feature* property referencing the + * selected feature. + * - *featureunselected* Triggered after a feature is unselected. + * Listeners will receive an object with a *feature* property + * referencing the unselected feature. + */ + EVENT_TYPES: ["loadstart", "loadend", "loadcancel", "visibilitychanged", + "beforefeatureadded", "featureadded", + "featuresadded", "featureselected", "featureunselected"], + /** * APIProperty: isBaseLayer * {Boolean} The layer is a base layer. Default is true. Set this property @@ -273,6 +313,9 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, { } if (notify) { + this.events.triggerEvent("beforefeatureadded", { + feature: feature + }); this.preFeatureInsert(feature); } @@ -281,9 +324,16 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, { } if (notify) { + this.events.triggerEvent("featureadded", { + feature: feature + }); this.onFeatureInsert(feature); } } + + if(notify) { + this.events.triggerEvent("featuresadded", {features: features}); + } }, diff --git a/lib/OpenLayers/Layer/WFS.js b/lib/OpenLayers/Layer/WFS.js index 27701f56ee..fc0f642dad 100644 --- a/lib/OpenLayers/Layer/WFS.js +++ b/lib/OpenLayers/Layer/WFS.js @@ -353,8 +353,11 @@ OpenLayers.Layer.WFS = OpenLayers.Class( * tile - {} */ removeTileMonitoringHooks: function(tile) { - tile.events.unregister("loadstart", tile, tile.onLoadStart); - tile.events.unregister("loadend", tile, tile.onLoadEnd); + tile.events.un({ + "loadstart": tile.onLoadStart, + "loadend": tile.onLoadEnd, + scope: tile + }); }, /** diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index 0283135f55..62fb1ad5dc 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -32,11 +32,30 @@ OpenLayers.Map = OpenLayers.Class({ * map.events.register(type, obj, listener); * (end) * + * Listeners will be called with a reference to an event object. The + * properties of this event depends on exactly what happened. + * + * All event objects have at least the following properties: + * - *object* {Object} A reference to map.events.object. + * - *element* {DOMElement} A reference to map.events.element. + * + * Browser events have the following additional properties: + * - *xy* {} The pixel location of the event (relative + * to the the map viewport). + * - other properties that come with browser events + * * Supported map event types: - * - *addlayer* triggered after a layer has been added - * - *removelayer* triggered after a layer has been removed + * - *addlayer* triggered after a layer has been added. The event object + * will include a *layer* property that references the added layer. + * - *removelayer* triggered after a layer has been removed. The event + * object will include a *layer* property that references the removed + * layer. * - *changelayer* triggered after a layer name change, order change, or - * visibility change (due to resolution thresholds) + * visibility change (due to resolution thresholds). Listeners will + * receive an event object with *layer* and *property* properties. The + * *layer* property will be a reference to the changed layer. The + * *property* property will be a key to the changed property (name, + * visibility, or order). * - *movestart* triggered after the start of a drag, pan, or zoom * - *move* triggered after each drag, pan, or zoom * - *moveend* triggered after a drag, pan, or zoom completes @@ -735,7 +754,7 @@ OpenLayers.Map = OpenLayers.Class({ layer.redraw(); } - this.events.triggerEvent("addlayer"); + this.events.triggerEvent("addlayer", {layer: layer}); }, /** @@ -808,7 +827,7 @@ OpenLayers.Map = OpenLayers.Class({ this.resetLayersZIndex(); - this.events.triggerEvent("removelayer"); + this.events.triggerEvent("removelayer", {layer: layer}); }, /** @@ -860,7 +879,9 @@ OpenLayers.Map = OpenLayers.Class({ for (var i = 0; i < this.layers.length; i++) { this.setLayerZIndex(this.layers[i], i); } - this.events.triggerEvent("changelayer"); + this.events.triggerEvent("changelayer", { + layer: layer, property: "order" + }); } }, @@ -933,7 +954,9 @@ OpenLayers.Map = OpenLayers.Class({ this.setCenter(newCenter, newZoom, false, true); } - this.events.triggerEvent("changebaselayer"); + this.events.triggerEvent("changebaselayer", { + layer: this.baseLayer + }); } } }, @@ -1453,7 +1476,9 @@ OpenLayers.Map = OpenLayers.Class({ if (!inRange) { layer.display(false); } - this.events.triggerEvent("changelayer"); + this.events.triggerEvent("changelayer", { + layer: layer, property: "visibility" + }); } if (inRange && layer.visibility) { layer.moveTo(bounds, zoomChanged, dragging); diff --git a/lib/OpenLayers/Popup.js b/lib/OpenLayers/Popup.js index 734df9073a..e3b2927605 100644 --- a/lib/OpenLayers/Popup.js +++ b/lib/OpenLayers/Popup.js @@ -403,12 +403,16 @@ OpenLayers.Popup = OpenLayers.Class({ registerEvents:function() { this.events = new OpenLayers.Events(this, this.div, null, true); - this.events.register("mousedown", this, this.onmousedown); - this.events.register("mousemove", this, this.onmousemove); - this.events.register("mouseup", this, this.onmouseup); - this.events.register("click", this, this.onclick); - this.events.register("mouseout", this, this.onmouseout); - this.events.register("dblclick", this, this.ondblclick); + this.events.on({ + "mousedown": this.onmousedown, + "mousemove": this.onmousemove, + "mouseup": this.onmouseup, + "click": this.onclick, + "mouseout": this.onmouseout, + "dblclick": this.ondblclick, + scope: this + }); + }, /** diff --git a/tests/Control/test_ModifyFeature.html b/tests/Control/test_ModifyFeature.html index 3f4b06026c..855011d4e1 100644 --- a/tests/Control/test_ModifyFeature.html +++ b/tests/Control/test_ModifyFeature.html @@ -5,13 +5,17 @@ function test_ModifyFeature_constructor(t) { t.plan(3); - var layer = "foo"; + var layer = { + events: { + on: function() {} + } + }; var options = { geometryTypes: "bar" }; var control = new OpenLayers.Control.ModifyFeature(layer, options); - t.eq(control.layer, "foo", + t.ok(control.layer == layer, "constructor sets layer correctly"); t.eq(control.selectControl.geometryTypes, "bar", "constructor sets options correctly on feature handler"); @@ -72,7 +76,12 @@ * In the future, feature deletion may be added to the control. */ - var control = new OpenLayers.Control.ModifyFeature({style: null}); + var control = new OpenLayers.Control.ModifyFeature({ + style: null, + events: { + on: function() {} + } + }); var delKey = 46; var dKey = 100; control.deleteCodes = [delKey, dKey]; @@ -197,7 +206,7 @@ layer.destroyFeatures = function(verts) { t.ok(verts == 'b', "Virtual verts destroyed correctly"); } - control.unselectFeature(fakeFeature); + control.unselectFeature({feature: fakeFeature}); t.eq(control.feature, null, "feature is set to null"); } function test_ModifyFeature_selectFeature(t) { @@ -216,7 +225,7 @@ var fakeFeature = {'id':'myFakeFeature','geometry':{'CLASS_NAME':'OpenLayers.Geometry.Point'}}; // Points don't call collectVertices - control.selectFeature(fakeFeature); + control.selectFeature({feature: fakeFeature}); control.collectVertices = function() { t.ok(true, "collectVertices called"); @@ -232,7 +241,7 @@ fakeFeature.geometry.CLASS_NAME='OpenLayers.Geometry.Polygon'; // OnSelect calls collectVertices and passes features to layer - control.selectFeature(fakeFeature); + control.selectFeature({feature: fakeFeature}); control.vertices = ['a']; control.virtualVertices = ['b']; @@ -244,7 +253,7 @@ } // Features are removed whenever they exist - control.selectFeature(fakeFeature); + control.selectFeature({feature: fakeFeature}); } @@ -389,7 +398,7 @@ t.eq(feature.id, testFeature.id, "onModificationStart called with the right feature"); }; - control.selectFeature(testFeature); + control.selectFeature({feature: testFeature}); } function test_ModifyFeature_onModification(t) { @@ -450,7 +459,7 @@ t.eq(feature.id, testFeature.id, "onModificationEnd called with the right feature"); }; - control.unselectFeature(testFeature); + control.unselectFeature({feature: testFeature}); } diff --git a/tests/Control/test_SelectFeature.html b/tests/Control/test_SelectFeature.html index f5eb446ee7..6ba8573c1f 100644 --- a/tests/Control/test_SelectFeature.html +++ b/tests/Control/test_SelectFeature.html @@ -78,7 +78,10 @@ // mock up layer var layer = { selectedFeatures: [], - drawFeature: function() {} + drawFeature: function() {}, + events: { + triggerEvent: function() {} + } }; // mock up active control var control = new OpenLayers.Control.SelectFeature(layer); diff --git a/tests/Layer/test_Grid.html b/tests/Layer/test_Grid.html index d7f75d998d..9df22e38a0 100644 --- a/tests/Layer/test_Grid.html +++ b/tests/Layer/test_Grid.html @@ -499,7 +499,8 @@ events: { unregister: function(name, obj, func) { g_unregistered[name] = [obj, func]; - } + }, + un: OpenLayers.Events.prototype.un } } diff --git a/tests/test_Events.html b/tests/test_Events.html index 750df5270f..03d562b7c9 100644 --- a/tests/test_Events.html +++ b/tests/test_Events.html @@ -205,19 +205,22 @@ eventTypes = ["something"]; events = new OpenLayers.Events(null, null, eventTypes); var instance = {id: Math.random()}; - var listener = function() { + var listener = function(obj) { t.eq(this.id, instance.id, "listener called with proper scope"); - t.eq(arguments[0].id, evt.id, - "listener called with evt as first arg"); - t.eq(arguments[1], "arg1", - "listener called with correct extra arg1"); - t.eq(arguments[2], "arg2", - "listener called with correct extra arg2"); + t.eq(arguments.length, 1, + "listener called with a single argument"); + t.eq(typeof arguments, "object", + "listener called with an object"); + t.eq(obj.foo, evt.foo, + "foo property set on the layer"); }; events.register("something", instance, listener); - var evt = {id: Math.random()}; - events.triggerEvent("something", evt, ["arg1", "arg2", "arg3"]); + var evt = { + id: Math.random(), + "foo": "bar" + }; + events.triggerEvent("something", evt); events.unregister("something", instance, listener); // test return from triggerEvent