Create redraw() function. This function does all of the drawing in the control: the draw function merely calls out to it. In addition, draw now does initialization of an event listener, listening for layers being added. This allows you to add the layer control before you've added all your layers, which was not possible before. It also allows, if desired, for us to add the LayerSwitcher control to the default controls, since it now will draw the layer switchers as they are added.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@67 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2006-05-16 23:54:34 +00:00
parent ed9b57b6df
commit a06bc351e4

View File

@@ -8,16 +8,32 @@ OpenLayers.Control.LayerSwitcher.prototype =
draw: function() {
// initialize our internal div
OpenLayers.Control.prototype.draw.apply(this);
this.map.events.register("addlayer", this, this.redraw);
return this.redraw();
},
redraw: function() {
var pixel = new OpenLayers.Pixel(this.map.div.clientWidth-200,4);
this.div.innerHTML = "";
for(i=0; i<this.map.layers.length; i++) {
var div = OpenLayers.Util.createDiv("LayerControl_layer"+i,pixel,new OpenLayers.Size(200,20));
div.innerHTML = this.map.layers[i].name;
div.style.backgroundColor="white";
div.style.padding="5px";
div.layerid = i;
div.map = this.map;
div.ondblclick = this.doubleClick.bindAsEventListener(div);
div.onmousedown = this.singleClick.bindAsEventListener(div);
var div = $('LayerControl_layer'+i);
if (!div) {
div = OpenLayers.Util.createDiv("LayerControl_layer"+i,pixel,new OpenLayers.Size(200,20));
div.innerHTML = this.map.layers[i].name;
var status = this.map.layers[i].getVisibility();
if (!status) {
div.style.backgroundColor="black";
div.style.color="white";
} else {
div.style.backgroundColor="white";
div.style.color="black";
}
div.style.padding="5px";
div.layerid = i;
div.map = this.map;
div.ondblclick = this.doubleClick.bindAsEventListener(div);
div.onmousedown = this.singleClick.bindAsEventListener(div);
}
this.div.appendChild(div);
pixel = pixel.add(0, 35);
}