diff --git a/lib/OpenLayers/Control.js b/lib/OpenLayers/Control.js index 2d83af8db2..2b9dc8f0db 100644 --- a/lib/OpenLayers/Control.js +++ b/lib/OpenLayers/Control.js @@ -139,6 +139,15 @@ OpenLayers.Control = OpenLayers.Class({ this.handler.destroy(); this.handler = null; } + if(this.handlers) { + for(var key in this.handlers) { + if(this.handlers.hasOwnProperty(key) && + typeof this.handlers[key].destroy == "function") { + this.handlers[key].destroy(); + } + } + this.handlers = null; + } if (this.map) { this.map.removeControl(this); this.map = null; diff --git a/lib/OpenLayers/Control/DragFeature.js b/lib/OpenLayers/Control/DragFeature.js index 45f7f7da71..19ca3a6b6e 100644 --- a/lib/OpenLayers/Control/DragFeature.js +++ b/lib/OpenLayers/Control/DragFeature.js @@ -76,24 +76,12 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { */ feature: null, - /** - * Property: dragHandler - * {} - */ - dragHandler: null, - /** * Property: dragCallbacks * {Object} The functions that are sent to the drag handler for callback. */ dragCallbacks: {}, - /** - * Property: featureHandler - * {} - */ - featureHandler: null, - /** * Property: featureCallbacks * {Object} The functions that are sent to the feature handler for callback. @@ -119,20 +107,24 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { initialize: function(layer, options) { OpenLayers.Control.prototype.initialize.apply(this, [options]); this.layer = layer; - this.dragCallbacks = OpenLayers.Util.extend({down: this.downFeature, - move: this.moveFeature, - up: this.upFeature, - out: this.cancel, - done: this.doneDragging - }, this.dragCallbacks); - this.dragHandler = new OpenLayers.Handler.Drag(this, this.dragCallbacks); - this.featureCallbacks = OpenLayers.Util.extend({over: this.overFeature, - out: this.outFeature - }, this.featureCallbacks); - var handlerOptions = {geometryTypes: this.geometryTypes}; - this.featureHandler = new OpenLayers.Handler.Feature(this, this.layer, - this.featureCallbacks, - handlerOptions); + this.handlers = { + drag: new OpenLayers.Handler.Drag( + this, OpenLayers.Util.extend({ + down: this.downFeature, + move: this.moveFeature, + up: this.upFeature, + out: this.cancel, + done: this.doneDragging + }, this.dragCallbacks) + ), + feature: new OpenLayers.Handler.Feature( + this, this.layer, OpenLayers.Util.extend({ + over: this.overFeature, + out: this.outFeature + }, this.featureCallbacks), + {geometryTypes: this.geometryTypes} + ) + }; }, /** @@ -141,8 +133,6 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { */ destroy: function() { this.layer = null; - this.dragHandler.destroy(); - this.featureHandler.destroy(); OpenLayers.Control.prototype.destroy.apply(this, []); }, @@ -154,7 +144,7 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { * {Boolean} Successfully activated the control and feature handler. */ activate: function() { - return (this.featureHandler.activate() && + return (this.handlers.feature.activate() && OpenLayers.Control.prototype.activate.apply(this, arguments)); }, @@ -167,8 +157,8 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { */ deactivate: function() { // the return from the handlers is unimportant in this case - this.dragHandler.deactivate(); - this.featureHandler.deactivate(); + this.handlers.drag.deactivate(); + this.handlers.feature.deactivate(); this.feature = null; this.dragging = false; this.lastPixel = null; @@ -184,9 +174,9 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { * feature - {} The selected feature. */ overFeature: function(feature) { - if(!this.dragHandler.dragging) { + if(!this.handlers.drag.dragging) { this.feature = feature; - this.dragHandler.activate(); + this.handlers.drag.activate(); this.over = true; // TBD replace with CSS classes this.map.div.style.cursor = "move"; @@ -238,7 +228,7 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { */ upFeature: function(pixel) { if(!this.over) { - this.dragHandler.deactivate(); + this.handlers.drag.deactivate(); this.feature = null; // TBD replace with CSS classes this.map.div.style.cursor = "default"; @@ -265,9 +255,9 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { * feature - {} The feature that the mouse left. */ outFeature: function(feature) { - if(!this.dragHandler.dragging) { + if(!this.handlers.drag.dragging) { this.over = false; - this.dragHandler.deactivate(); + this.handlers.drag.deactivate(); // TBD replace with CSS classes this.map.div.style.cursor = "default"; this.feature = null; @@ -283,7 +273,7 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { * Called when the drag handler detects a mouse-out (from the map viewport). */ cancel: function() { - this.dragHandler.deactivate(); + this.handlers.drag.deactivate(); this.over = false; }, @@ -295,8 +285,8 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { * map - {} The control's map. */ setMap: function(map) { - this.dragHandler.setMap(map); - this.featureHandler.setMap(map); + this.handlers.drag.setMap(map); + this.handlers.feature.setMap(map); OpenLayers.Control.prototype.setMap.apply(this, arguments); }, diff --git a/lib/OpenLayers/Control/ModifyFeature.js b/lib/OpenLayers/Control/ModifyFeature.js index d5823c08aa..14bfffa180 100644 --- a/lib/OpenLayers/Control/ModifyFeature.js +++ b/lib/OpenLayers/Control/ModifyFeature.js @@ -83,10 +83,10 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { dragControl: null, /** - * Property: keyboardHandler - * {} + * Property: handlers + * {Object} */ - keyboardHandler: null, + handlers: null, /** * APIProperty: deleteCodes @@ -216,9 +216,9 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { var keyboardOptions = { keypress: this.handleKeypress }; - this.keyboardHandler = new OpenLayers.Handler.Keyboard( - this, keyboardOptions - ); + this.handlers = { + keyboard: new OpenLayers.Handler.Keyboard(this, keyboardOptions) + }; }, /** @@ -229,7 +229,6 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { this.layer = null; this.selectControl.destroy(); this.dragControl.destroy(); - this.keyboardHandler.destroy(); OpenLayers.Control.prototype.destroy.apply(this, []); }, @@ -242,7 +241,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { */ activate: function() { return (this.selectControl.activate() && - this.keyboardHandler.activate() && + this.handlers.keyboard.activate() && OpenLayers.Control.prototype.activate.apply(this, arguments)); }, @@ -266,7 +265,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { [this.feature]); } this.selectControl.deactivate(); - this.keyboardHandler.deactivate(); + this.handlers.keyboard.deactivate(); deactivated = true; } return deactivated; @@ -352,9 +351,9 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { this.dragControl.overFeature.apply(this.dragControl, [feature]); this.dragControl.lastPixel = pixel; - this.dragControl.dragHandler.started = true; - this.dragControl.dragHandler.start = pixel; - this.dragControl.dragHandler.last = pixel; + this.dragControl.handlers.drag.started = true; + this.dragControl.handlers.drag.start = pixel; + this.dragControl.handlers.drag.last = pixel; } } }, @@ -486,7 +485,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { var vertex = this.dragControl.feature; if(vertex && OpenLayers.Util.indexOf(this.vertices, vertex) != -1 && - !this.dragControl.dragHandler.dragging && + !this.dragControl.handlers.drag.dragging && vertex.geometry.parent) { // remove the vertex vertex.geometry.parent.removeComponent(vertex.geometry); diff --git a/lib/OpenLayers/Control/Navigation.js b/lib/OpenLayers/Control/Navigation.js index 0cd1e67688..0506e22ece 100644 --- a/lib/OpenLayers/Control/Navigation.js +++ b/lib/OpenLayers/Control/Navigation.js @@ -36,12 +36,6 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, { */ zoomBox: null, - /** - * Property: wheelHandler - * {} - */ - wheelHandler: null, - /** * Constructor: OpenLayers.Control.Navigation * Create a new navigation control @@ -51,6 +45,7 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, { * the control */ initialize: function(options) { + this.handlers = {}; OpenLayers.Control.prototype.initialize.apply(this, arguments); }, @@ -61,8 +56,6 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, { * to prevent memory leaks. */ destroy: function() { - OpenLayers.Control.prototype.destroy.apply(this,arguments); - this.deactivate(); if (this.dragPan) { @@ -70,20 +63,11 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, { } this.dragPan = null; - if (this.wheelHandler) { - this.wheelHandler.destroy(); - } - this.wheelHandler = null; - - if (this.clickHandler) { - this.clickHandler.destroy(); - } - this.clickHandler = null; - if (this.zoomBox) { this.zoomBox.destroy(); } this.zoomBox = null; + OpenLayers.Control.prototype.destroy.apply(this,arguments); }, /** @@ -91,8 +75,8 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, { */ activate: function() { this.dragPan.activate(); - this.wheelHandler.activate(); - this.clickHandler.activate(); + this.handlers.wheel.activate(); + this.handlers.click.activate(); this.zoomBox.activate(); return OpenLayers.Control.prototype.activate.apply(this,arguments); }, @@ -103,8 +87,8 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, { deactivate: function() { this.zoomBox.deactivate(); this.dragPan.deactivate(); - this.clickHandler.deactivate(); - this.wheelHandler.deactivate(); + this.handlers.click.deactivate(); + this.handlers.wheel.deactivate(); return OpenLayers.Control.prototype.deactivate.apply(this,arguments); }, @@ -112,7 +96,7 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, { * Method: draw */ draw: function() { - this.clickHandler = new OpenLayers.Handler.Click(this, + this.handlers.click = new OpenLayers.Handler.Click(this, { 'dblclick': this.defaultDblClick }, { 'double': true, @@ -123,7 +107,7 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, { {map: this.map, keyMask: OpenLayers.Handler.MOD_SHIFT}); this.dragPan.draw(); this.zoomBox.draw(); - this.wheelHandler = new OpenLayers.Handler.MouseWheel( + this.handlers.wheel = new OpenLayers.Handler.MouseWheel( this, {"up" : this.wheelUp, "down": this.wheelDown} ); this.activate(); diff --git a/lib/OpenLayers/Control/OverviewMap.js b/lib/OpenLayers/Control/OverviewMap.js index 1e0c4ed6a5..cbad34c044 100644 --- a/lib/OpenLayers/Control/OverviewMap.js +++ b/lib/OpenLayers/Control/OverviewMap.js @@ -98,10 +98,10 @@ OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, { mapOptions: null, /** - * Property: dragHandler - * {} A handler for dragging the extent rectangle. + * Property: handlers + * {Object} */ - dragHandler: null, + handlers: null, /** * Constructor: OpenLayers.Control.OverviewMap @@ -114,6 +114,7 @@ OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, { */ initialize: function(options) { this.layers = []; + this.handlers = {}; OpenLayers.Control.prototype.initialize.apply(this, [options]); }, @@ -125,8 +126,7 @@ OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, { if (!this.mapDiv) { // we've already been destroyed return; } - this.dragHandler.destroy(); - this.clickHandler.destroy(); + this.handlers.click.destroy(); this.mapDiv.removeChild(this.extentRectangle); this.extentRectangle = null; @@ -280,8 +280,8 @@ OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, { * px - {} The pixel location of the drag. */ rectDrag: function(px) { - var deltaX = this.dragHandler.last.x - px.x; - var deltaY = this.dragHandler.last.y - px.y; + var deltaX = this.handlers.drag.last.x - px.x; + var deltaY = this.handlers.drag.last.y - px.y; if(deltaX != 0 || deltaY != 0) { var rectTop = this.rectPxBounds.top; var rectLeft = this.rectPxBounds.left; @@ -456,11 +456,11 @@ OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, { 'border-bottom-width')); this.hComp = (this.hComp) ? this.hComp : 2; - this.dragHandler = new OpenLayers.Handler.Drag( + this.handlers.drag = new OpenLayers.Handler.Drag( this, {move: this.rectDrag, done: this.updateMapToRect}, {map: this.ovmap} ); - this.clickHandler = new OpenLayers.Handler.Click( + this.handlers.click = new OpenLayers.Handler.Click( this, { "click": this.mapDivClick },{ @@ -470,18 +470,18 @@ OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, { map: this.ovmap } ); - this.clickHandler.activate(); + this.handlers.click.activate(); this.rectEvents = new OpenLayers.Events(this, this.extentRectangle, null, true); this.rectEvents.register("mouseover", this, function(e) { - if(!this.dragHandler.active && !this.map.dragging) { - this.dragHandler.activate(); + if(!this.handlers.drag.active && !this.map.dragging) { + this.handlers.drag.activate(); } }); this.rectEvents.register("mouseout", this, function(e) { - if(!this.dragHandler.dragging) { - this.dragHandler.deactivate(); + if(!this.handlers.drag.dragging) { + this.handlers.drag.deactivate(); } }); diff --git a/tests/Control/test_DragFeature.html b/tests/Control/test_DragFeature.html index b531bbd586..50f74c3971 100644 --- a/tests/Control/test_DragFeature.html +++ b/tests/Control/test_DragFeature.html @@ -14,7 +14,7 @@ "new OpenLayers.Control.DragFeature returns an instance"); t.eq(control.layer, "bar", "constructor sets layer correctly"); - t.eq(control.featureHandler.geometryTypes, "foo", + t.eq(control.handlers.feature.geometryTypes, "foo", "constructor sets options correctly on feature handler"); } @@ -24,14 +24,15 @@ var layer = new OpenLayers.Layer.Vector(); map.addLayer(layer); var control = new OpenLayers.Control.DragFeature(layer); - control.dragHandler.destroy = function() { + control.handlers.drag.destroy = function() { t.ok(true, "control.destroy calls destroy on drag handler"); } - control.featureHandler.destroy = function() { + control.handlers.feature.destroy = function() { t.ok(true, "control.destroy calls destroy on feature handler"); } + control.destroy(); } @@ -43,10 +44,10 @@ map.addLayer(layer); var control = new OpenLayers.Control.DragFeature(layer); map.addControl(control); - t.ok(!control.featureHandler.active, + t.ok(!control.handlers.feature.active, "feature handler is not active prior to activating control"); control.activate(); - t.ok(control.featureHandler.active, + t.ok(control.handlers.feature.active, "feature handler is active after activating control"); } @@ -58,11 +59,11 @@ var control = new OpenLayers.Control.DragFeature(layer); map.addControl(control); - control.dragHandler.deactivate = function() { + control.handlers.drag.deactivate = function() { t.ok(true, "control.deactivate calls deactivate on drag handler"); } - control.featureHandler.deactivate = function() { + control.handlers.feature.deactivate = function() { t.ok(true, "control.deactivate calls deactivate on feature handler"); } @@ -78,7 +79,7 @@ map.addControl(control); control.activate(); - t.ok(!control.dragHandler.active, + t.ok(!control.handlers.drag.active, "drag handler is not active before over a feature"); // simulate a mouseover on a feature @@ -89,7 +90,7 @@ t.eq(control.feature, "foo", "control gets the proper feature from the feature handler"); - t.ok(control.dragHandler.active, + t.ok(control.handlers.drag.active, "drag handler activated when over a feature"); } diff --git a/tests/Control/test_ModifyFeature.html b/tests/Control/test_ModifyFeature.html index dc2a788425..3f4b06026c 100644 --- a/tests/Control/test_ModifyFeature.html +++ b/tests/Control/test_ModifyFeature.html @@ -172,7 +172,7 @@ control.handleKeypress(dKey); // now make sure nothing happens if the vertex is mid-drag - control.dragControl.dragHandler.dragging = true; + control.dragControl.handlers.drag.dragging = true; control.handleKeypress(delKey); // reset modified methods @@ -305,11 +305,11 @@ control.dragControl.map.div = {}; control.dragControl.map.div.style = {}; control.dragControl.map.div.cursor = "foo"; - control.dragControl.dragHandler.deactivate = function() { + control.dragControl.handlers.drag.deactivate = function() { this.active = false; } control.resetVertices(); - t.ok(!control.dragControl.dragHandler.active, "resetVertices deactivates drag handler"); + t.ok(!control.dragControl.handlers.drag.active, "resetVertices deactivates drag handler"); control.dragControl.map = null; control.destroy(); diff --git a/tests/Control/test_Navigation.html b/tests/Control/test_Navigation.html index 6c390198b5..f337b84bed 100644 --- a/tests/Control/test_Navigation.html +++ b/tests/Control/test_Navigation.html @@ -5,61 +5,63 @@ function test_Control_Navigation_constructor (t) { t.plan( 2 ); - var temp = OpenLayers.Control.prototype.initialize; - OpenLayers.Control.prototype.initialize = function() { - t.ok(true, "OpenLayers.Control's constructor called"); - }; + var temp = OpenLayers.Control.prototype.initialize; + OpenLayers.Control.prototype.initialize = function() { + t.ok(true, "OpenLayers.Control's constructor called"); + }; var control = new OpenLayers.Control.Navigation(); t.ok( control instanceof OpenLayers.Control.Navigation, "new OpenLayers.Control returns object" ); - OpenLayers.Control.prototype.initialize = temp; + OpenLayers.Control.prototype.initialize = temp; } function test_Control_Navigation_destroy (t) { - t.plan(10); - - var temp = OpenLayers.Control.prototype.destroy; - OpenLayers.Control.prototype.destroy = function() { - t.ok(true, "OpenLayers.Control's destroy called"); - }; + t.plan(9); + + var temp = OpenLayers.Control.prototype.destroy; + OpenLayers.Control.prototype.destroy = function() { + t.ok(true, "OpenLayers.Control's destroy called"); + temp.call(this); + }; - var control = { - 'deactivate': function() { - t.ok(true, "navigation control deactivated before being destroyed"); - }, - 'dragPan': { - 'destroy': function() { - t.ok(true, "dragPan destroyed"); - } - }, - 'zoomBox': { - 'destroy': function() { - t.ok(true, "zoomBox destroyed"); - } - }, - 'wheelHandler': { - 'destroy': function() { - t.ok(true, "wheelHandler destroyed"); - } - }, - 'clickHandler': { - 'destroy': function() { - t.ok(true, "clickHandler destroyed"); - } - } - }; + var control = { + 'deactivate': function() { + t.ok(true, "navigation control deactivated before being destroyed"); + }, + 'dragPan': { + 'destroy': function() { + t.ok(true, "dragPan destroyed"); + } + }, + 'zoomBox': { + 'destroy': function() { + t.ok(true, "zoomBox destroyed"); + } + }, + handlers: { + 'wheel': { + 'destroy': function() { + t.ok(true, "wheelHandler destroyed"); + } + }, + 'click': { + 'destroy': function() { + t.ok(true, "clickHandler destroyed"); + } + } + } + }; - //this will also trigger one test by calling OpenLayers.Control's destroy - // and three more for the destruction of dragPan, zoomBox, and wheelHandler - OpenLayers.Control.Navigation.prototype.destroy.apply(control, []); + //this will also trigger one test by calling OpenLayers.Control's destroy + // and three more for the destruction of dragPan, zoomBox, and wheelHandler + OpenLayers.Control.Navigation.prototype.destroy.apply(control, []); - t.eq(control.dragPan, null, "'dragPan' set to null"); - t.eq(control.zoomBox, null, "'zoomBox' set to null"); - t.eq(control.wheelHandler, null, "'wheelHandler' set to null"); - t.eq(control.clickHandler, null, "'clickHandler' set to null"); + t.eq(control.dragPan, null, "'dragPan' set to null"); + t.eq(control.zoomBox, null, "'zoomBox' set to null"); + t.eq(control.handlers, null, "handlers set to null"); - OpenLayers.Control.prototype.destroy = temp; + OpenLayers.Control.prototype.destroy = temp; } diff --git a/tests/Control/test_OverviewMap.html b/tests/Control/test_OverviewMap.html index 07231c6f73..0caa0e03be 100644 --- a/tests/Control/test_OverviewMap.html +++ b/tests/Control/test_OverviewMap.html @@ -51,7 +51,7 @@ t.eq(cent.lon, -71.3515625, "Clicking on the Overview Map has the correct effect on map lon"); t.eq(cent.lat, 42.17578125, "Clicking on the Overview Map has the correct effect on map lat"); - control.dragHandler = { + control.handlers.drag = { last: new OpenLayers.Pixel(5,5), destroy: function() {} };