fix for #742 -- Adds getControl() and removeControl() methods to the map

git-svn-id: http://svn.openlayers.org/trunk/openlayers@3282 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2007-06-07 18:01:52 +00:00
parent 6da7fb6851
commit 22c8e3ded4
2 changed files with 97 additions and 0 deletions

View File

@@ -598,6 +598,41 @@ OpenLayers.Map.prototype = {
} }
}, },
/**
* @param {String} id ID of the control to return
*
* @returns The control from the map's list of controls which has a
* matching 'id'. If none found, returns null.
* @type OpenLayers.Control
*/
getControl: function (id) {
var returnControl = null;
for(var i=0; i < this.controls.length; i++) {
var control = this.controls[i];
if (control.id == id) {
returnControl = control;
break;
}
}
return returnControl;
},
/** Remove a control from the map. Removes the control both from the map
* object's internal array of controls, as well as from the map's
* viewPort (assuming the control was not added outsideViewport)
*
* @param {String} id ID of the control to remove
*/
removeControl: function (id) {
var control = this.getControl(id);
if (control) {
if (!control.outsideViewport) {
this.viewPortDiv.removeChild(control.div)
}
OpenLayers.Util.removeItem(this.controls, control);
}
},
/********************************************************/ /********************************************************/
/* */ /* */
/* Popup Functions */ /* Popup Functions */

View File

@@ -391,6 +391,68 @@
t.eq(nodeCount, head.childNodes.length, "with no theme, a node is not added to document head" ); t.eq(nodeCount, head.childNodes.length, "with no theme, a node is not added to document head" );
} }
function test_19_Map_getControl(t) {
t.plan(2);
var map1 = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map1.addControl(control);
var gotControl = map1.getControl(control.id);
t.ok(gotControl == control, "got right control");
gotControl = map1.getControl("bogus id");
t.ok(gotControl == null, "getControl() for bad id returns null");
}
function test_19_Map_removeControl(t) {
t.plan(6);
var oldNumControls, newNumControls;
var map1 = new OpenLayers.Map('map');
oldNumControls = map1.controls.length;
var control = new OpenLayers.Control();
map1.addControl(control);
//add control
newNumControls = map1.controls.length;
t.ok( newNumControls = oldNumControls + 1, "adding a control increases control count")
var foundDiv = false;
for(var i=0; i < map1.viewPortDiv.childNodes.length; i++) {
var childNode = map1.viewPortDiv.childNodes[i];
if (childNode == control.div) {
foundDiv = true;
}
}
t.ok(foundDiv, "new control's div correctly added to viewPort");
//remove control
map1.removeControl(control.id)
newNumControls = map1.controls.length;
t.ok( newNumControls == oldNumControls, "removing the control decreases control count")
var gotControl = map1.getControl(control.id);
t.ok( gotControl == null, "control no longer in map's controls array");
var foundDiv = false;
for(var i=0; i < map1.viewPortDiv.childNodes.length; i++) {
var childNode = map1.viewPortDiv.childNodes[i];
if (childNode == control.div) {
foundDiv = true;
}
}
t.ok(!foundDiv, "control no longer child of viewPort");
//remove bogus
map1.removeControl("bogus id");
newNumControls = map1.controls.length;
t.ok( newNumControls == oldNumControls, "removing bad controlid doesnt crash or decrease control count")
}
function test_99_Map_destroy (t) { function test_99_Map_destroy (t) {
t.plan( 3 ); t.plan( 3 );
map = new OpenLayers.Map('map'); map = new OpenLayers.Map('map');