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
This commit is contained in:
crschmidt
2010-08-21 22:45:32 +00:00
parent c5f233ab8c
commit ed2e943e52
2 changed files with 70 additions and 14 deletions

View File

@@ -17,6 +17,13 @@
*/ */
OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, { 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 * Property: element
* {DOMElement} * {DOMElement}
@@ -87,12 +94,38 @@ OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, {
* Method: destroy * Method: destroy
*/ */
destroy: function() { destroy: function() {
if (this.map) { this.deactivate();
this.map.events.unregister('mousemove', this, this.redraw);
}
OpenLayers.Control.prototype.destroy.apply(this, arguments); 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 * Method: draw
* {DOMElement} * {DOMElement}
@@ -106,7 +139,6 @@ OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, {
this.element = this.div; this.element = this.div;
} }
this.redraw();
return this.div; return this.div;
}, },
@@ -174,16 +206,7 @@ OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, {
lonLat.lat.toFixed(digits) + lonLat.lat.toFixed(digits) +
this.suffix; this.suffix;
return newHtml; 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" CLASS_NAME: "OpenLayers.Control.MousePosition"
}); });

View File

@@ -68,6 +68,39 @@
var val = control.formatOutput(lonlat); var val = control.formatOutput(lonlat);
t.eq(val, 'prefix0.757separator0.374suffix', 'formatOutput correctly formats the mouse position output'); 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();
}
</script> </script>
</head> </head>
<body> <body>