Some refactoring

This commit is contained in:
Willem Dantuma 2020-02-29 11:56:19 +01:00
parent 47f77c2819
commit a151b73d03

View File

@ -261,26 +261,31 @@ export class ItemLayersComponent extends LayerGroupComponent implements OnChange
this.updateLayers(this.itemLayers); this.updateLayers(this.itemLayers);
} }
addOrUpdateOlLayer(itemLayer:IItemLayer,index:number):Layer {
var olLayers = this.instance.getLayers();
var layer = itemLayer.layer;
let olIndex = olLayers.getArray().indexOf(layer);
if (olIndex < 0) {
// New layer: we add it to the map
layer = this.createLayer(itemLayer);
if (layer) {
olLayers.insertAt(index, layer);
}
} else if (index !== olIndex) {
// layer has moved inside the layers list
olLayers.removeAt(olIndex);
olLayers.insertAt(index, layer);
}
return layer;
}
updateLayers(itemLayers: IItemLayer[]) { updateLayers(itemLayers: IItemLayer[]) {
if (itemLayers) { if (itemLayers) {
var olLayers = this.instance.getLayers(); var olLayers = this.instance.getLayers();
itemLayers.forEach((itemLayer, index) => { itemLayers.forEach((itemLayer, index) => {
var layer = this.addOrUpdateOlLayer(itemLayer,index);
var layer = itemLayer.layer;
let olIndex = olLayers.getArray().indexOf(layer);
if (olIndex < 0) {
// New layer: we add it to the map
layer = this.createLayer(itemLayer);
if (layer) {
itemLayer.layer = layer;
olLayers.insertAt(index, layer);
}
} else if (index !== olIndex) {
// layer has moved inside the layers list
olLayers.removeAt(olIndex);
olLayers.insertAt(index, layer);
}
if(layer) { if(layer) {
itemLayer.layer = layer;
layer.setOpacity(itemLayer.opacity); layer.setOpacity(itemLayer.opacity);
layer.setVisible(itemLayer.visible); layer.setVisible(itemLayer.visible);
} }