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:
@@ -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,15 +206,6 @@ 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"
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
Reference in New Issue
Block a user