From ed2e943e52792cfbfb0e1ff181a8c32e8ebddd59 Mon Sep 17 00:00:00 2001 From: crschmidt Date: Sat, 21 Aug 2010 22:45:32 +0000 Subject: [PATCH] Update MousePositoin control to support activate/deactivate. Patch by jorix, tests pass, and controls.html example still works the same as before. (Closes #2520) git-svn-id: http://svn.openlayers.org/trunk/openlayers@10669 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Control/MousePosition.js | 51 ++++++++++++++++++------- tests/Control/MousePosition.html | 33 ++++++++++++++++ 2 files changed, 70 insertions(+), 14 deletions(-) diff --git a/lib/OpenLayers/Control/MousePosition.js b/lib/OpenLayers/Control/MousePosition.js index a764b8da52..387a98dc43 100644 --- a/lib/OpenLayers/Control/MousePosition.js +++ b/lib/OpenLayers/Control/MousePosition.js @@ -17,6 +17,13 @@ */ OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, { + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + /** * Property: element * {DOMElement} @@ -87,12 +94,38 @@ OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, { * Method: destroy */ destroy: function() { - if (this.map) { - this.map.events.unregister('mousemove', this, this.redraw); - } + this.deactivate(); OpenLayers.Control.prototype.destroy.apply(this, arguments); }, + /** + * APIMethod: activate + */ + activate: function() { + if (OpenLayers.Control.prototype.activate.apply(this, arguments)) { + this.map.events.register('mousemove', this, this.redraw); + this.map.events.register('mouseout', this, this.reset); + this.redraw(); + return true; + } else { + return false; + } + }, + + /** + * APIMethod: deactivate + */ + deactivate: function() { + if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { + this.map.events.unregister('mousemove', this, this.redraw); + this.map.events.unregister('mouseout', this, this.reset); + this.element.innerHTML = ""; + return true; + } else { + return false; + } + }, + /** * Method: draw * {DOMElement} @@ -106,7 +139,6 @@ OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, { this.element = this.div; } - this.redraw(); return this.div; }, @@ -174,16 +206,7 @@ OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, { lonLat.lat.toFixed(digits) + this.suffix; return newHtml; - }, - - /** - * Method: setMap - */ - setMap: function() { - OpenLayers.Control.prototype.setMap.apply(this, arguments); - this.map.events.register( 'mousemove', this, this.redraw); - this.map.events.register( 'mouseout', this, this.reset); - }, + }, CLASS_NAME: "OpenLayers.Control.MousePosition" }); diff --git a/tests/Control/MousePosition.html b/tests/Control/MousePosition.html index 2e895bcc15..2858167d29 100644 --- a/tests/Control/MousePosition.html +++ b/tests/Control/MousePosition.html @@ -68,6 +68,39 @@ var val = control.formatOutput(lonlat); t.eq(val, 'prefix0.757separator0.374suffix', 'formatOutput correctly formats the mouse position output'); } + function test_deactivate(t) { + t.plan(4); + var map = new OpenLayers.Map('map'); + var layer = new OpenLayers.Layer(null, {isBaseLayer: true}); + map.addLayer(layer); + map.zoomToMaxExtent(); + // Auxiliary function + function trigger(type, x, y) { + map.events.triggerEvent(type, { + xy: new OpenLayers.Pixel(x, y) + }) + }; + + var control = new OpenLayers.Control.MousePosition(); + map.addControl(control); + trigger("mousemove", 0, 0); + + trigger("mousemove", 0, 1); + t.ok(control.div.innerHTML != "", + "Shows the position after add control (with autoActivate) and move"); + control.deactivate(); + t.ok(control.div.innerHTML == "", + "Position is not displayed after deactivate and move"); + trigger("mousemove", 0, 2); + t.ok(control.div.innerHTML == "", + "Position is not displayed after move when deactivate"); + control.activate(); + trigger("mousemove", 0, 3); + t.ok(control.div.innerHTML != "", + "Shows the position after activate and move"); + + map.destroy(); + }