diff --git a/lib/OpenLayers/Events.js b/lib/OpenLayers/Events.js index 8e1e3a5654..b2140c1d51 100644 --- a/lib/OpenLayers/Events.js +++ b/lib/OpenLayers/Events.js @@ -1,41 +1,46 @@ OpenLayers.Events = Class.create(); OpenLayers.Events.prototype = { - // hash of Array(Function) - listeners: null, - - // OpenLayers.Map - map: null, - - // DOMElement - div: null, - - // supported events + // Array: supported events BROWSER_EVENTS: [ "mouseover", "mouseout", "mousedown", "mouseup", "mousemove", "click", "dblclick" ], - MAP_EVENTS: [ - "mouseover", "mouseout", - "mousedown", "mouseup", "mousemove", - "click", "dblclick" - ], + + // hash of Array(Function): events listener functions + listeners: null, + + // Object: the code object issuing application events + object: null, + + // DOMElement: the DOM element receiving browser events + div: null, + + // Array: list of support application events + eventTypes: null, /** * @param {OpenLayers.Map} map * @param {DOMElement} div */ - initialize: function (map, div) { - this.listeners = {}; - this.map = map; - this.div = div; - for (var i = 0; i < this.MAP_EVENTS.length; i++) { - this.listeners[ this.MAP_EVENTS[i] ] = []; + initialize: function (object, div, eventTypes) { + this.listeners = {}; + this.object = object; + this.div = div; + this.eventTypes = eventTypes; + for (var i = 0; i < this.eventTypes.length; i++) { + // create a listener list for every custom application event + this.listeners[ this.eventTypes[i] ] = []; } for (var i = 0; i < this.BROWSER_EVENTS.length; i++) { - var eventName = this.BROWSER_EVENTS[i]; - Event.observe(div, eventName, + var eventType = this.BROWSER_EVENTS[i]; + + // every browser event has a corresponding application event + // (whether it's listened for or not). + this.listeners[ eventType ] = []; + + Event.observe(div, eventType, this.handleBrowserEvent.bindAsEventListener(this)); } }, @@ -55,7 +60,7 @@ OpenLayers.Events.prototype = { */ handleBrowserEvent: function (evt) { evt.xy = this.getMousePosition(evt); - this.triggerMapEvent(evt.type, evt) + this.triggerEvent(evt.type, evt) }, /** @@ -74,8 +79,11 @@ OpenLayers.Events.prototype = { * @param {str} type * @param {event} evt */ - triggerMapEvent: function (type, evt) { - evt.map = this.map; + triggerEvent: function (type, evt) { + if (evt == null) { + evt = {}; + } + evt.object = this.object; var listeners = this.listeners[type]; for (var i = 0; i < listeners.length; i++) { diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index 4ea6d3cd3a..bc22810334 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -9,6 +9,13 @@ OpenLayers.Map.prototype = { // Hash: base z-indexes for different classes of thing Z_INDEX_BASE: { Layer: 100, Popup: 200, Control: 250 }, + // Array: supported application event types + EVENT_TYPES: [ + "addlayer", "removelayer", "movestart", "move", "moveend", + "zoomend", "layerchanged", "popupopen", "popupclose", + "addmarker", "removemarker", "clearmarkers", "mouseover", + "mouseout", "mousemove", "dragstart", "drag", "dragend" ], + // int: zoom levels, used to draw zoom dragging control and limit zooming maxZoomLevel: 16, @@ -56,7 +63,7 @@ OpenLayers.Map.prototype = { initialize: function (div, options) { Object.extend(this, options); - this.div = $(div); + this.div = div = $(div); this.viewPortDiv = OpenLayers.Util.createDiv( div.id + "_OpenLayers_ViewPort" ); @@ -75,12 +82,13 @@ OpenLayers.Map.prototype = { div.id + "_OpenLayers_Container" ); this.viewPortDiv.appendChild(this.layerContainerDiv); + this.events = new OpenLayers.Events(this, div, this.EVENT_TYPES); + this.layers = []; this.controls = []; this.addControl( new OpenLayers.Control.PanZoom() ); - this.events = new OpenLayers.Events(this, div); this.events.register( "dblclick", this, this.defaultDblClick ); this.events.register( "mousedown", this, this.defaultMouseDown ); this.events.register( "mouseup", this, this.defaultMouseUp ); diff --git a/tests/list-tests.html b/tests/list-tests.html index c4b52b98cc..cb0882209b 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -2,6 +2,7 @@