Added Map::addControls method. Yet another quality patch from Marc, with everything done right plus unit tests. p=marcjansen, r=me (closes #2399)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10085 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2010-03-02 21:39:23 +00:00
parent a27c552554
commit 8adcc155fb
2 changed files with 53 additions and 0 deletions

View File

@@ -1167,6 +1167,8 @@ OpenLayers.Map = OpenLayers.Class({
/**
* APIMethod: addControl
* Add the passed over control to the map. Optionally
* position the control at the given pixel.
*
* Parameters:
* control - {<OpenLayers.Control>}
@@ -1176,6 +1178,28 @@ OpenLayers.Map = OpenLayers.Class({
this.controls.push(control);
this.addControlToMap(control, px);
},
/**
* APIMethod: addControls
* Add all of the passed over controls to the map.
* You can pass over an optional second array
* with pixel-objects to position the controls.
* The indices of the two arrays should match and
* you can add null as pixel for those controls
* you want to be autopositioned.
*
* Parameters:
* controls - {Array(<OpenLayers.Control>)}
* pixels - {Array(<OpenLayers.Pixel>)}
*/
addControls: function (controls, pixels) {
var pxs = (arguments.length === 1) ? [] : pixels;
for (var i=0, len=controls.length; i<len; i++) {
var ctrl = controls[i];
var px = (pxs[i]) ? pxs[i] : null;
this.addControl( ctrl, px );
}
},
/**
* Method: addControlToMap

View File

@@ -921,6 +921,35 @@
map.destroy();
}
function test_Map_addControls(t) {
t.plan(5);
var map = new OpenLayers.Map('map', {
controls: []
});
var controls = [
new OpenLayers.Control({id:'firstctrl'}),
new OpenLayers.Control({id:'secondctrl'})
];
map.addControls(controls);
t.eq(map.controls.length, 2, "two controls were added by map.addControls without a px-array");
t.eq(map.controls[0].id, 'firstctrl', "control with index 0 has id 'firstctrl'");
t.eq(map.controls[1].id, 'secondctrl', "control with index 1 has id 'secondctrl'");
var controls2 = [
new OpenLayers.Control({id:'thirdctrl'}),
new OpenLayers.Control({id:'fourthctrl'}),
new OpenLayers.Control({id:'fifthctrl'})
];
// this array is intentionally one element shorter than the above
var pixels2 = [
null,
new OpenLayers.Pixel(27,11)
];
map.addControls(controls2, pixels2);
t.eq(map.controls.length, 5, "three additional controls were added by map.addControls with a px-array");
t.eq(map.controls[3].position.toString(), pixels2[1].toString(), "control 'fourthctrl' has position set to given px");
}
function test_Map_getControl(t) {
t.plan(2);