2019-04-04 11:19:29 +03:00
|
|
|
/* exported LayerManager */
|
2019-04-21 22:51:43 +03:00
|
|
|
/* globals RealtimeTileLayer: true */
|
2019-01-29 23:07:29 +03:00
|
|
|
|
2019-04-22 18:17:41 +03:00
|
|
|
function LayerManager(wsChannel, layers, map, currentLayerId){
|
2019-02-01 15:28:26 +03:00
|
|
|
this.listeners = [];
|
|
|
|
this.currentLayer = layers[0];
|
2019-02-15 19:33:49 +03:00
|
|
|
this.layers = layers;
|
2019-04-21 22:51:43 +03:00
|
|
|
this.map = map;
|
|
|
|
this.layerObjects = {};
|
2019-02-01 15:20:50 +03:00
|
|
|
|
2019-03-13 23:28:14 +03:00
|
|
|
var self = this;
|
|
|
|
|
2019-04-21 22:51:43 +03:00
|
|
|
//All layers
|
|
|
|
layers.forEach(function(layer){
|
|
|
|
var tileLayer = new RealtimeTileLayer(wsChannel, layer.id, map);
|
|
|
|
self.layerObjects[layer.name] = tileLayer;
|
2019-04-22 18:17:41 +03:00
|
|
|
if (layer.id == currentLayerId){
|
|
|
|
tileLayer.addTo(map);
|
|
|
|
self.currentLayer = layer;
|
|
|
|
}
|
2019-04-21 22:51:43 +03:00
|
|
|
});
|
|
|
|
|
2019-02-01 15:20:50 +03:00
|
|
|
map.on('baselayerchange', function (e) {
|
2019-03-13 23:28:14 +03:00
|
|
|
self.setLayerId(e.layer.layerId);
|
2019-02-01 15:20:50 +03:00
|
|
|
});
|
2019-02-01 15:28:26 +03:00
|
|
|
|
2019-02-01 14:58:26 +03:00
|
|
|
}
|
2019-01-29 23:07:29 +03:00
|
|
|
|
2019-04-21 22:51:43 +03:00
|
|
|
LayerManager.prototype.switchLayer = function(layerId){
|
|
|
|
var self = this;
|
|
|
|
Object.keys(this.layerObjects).forEach(function(key){
|
|
|
|
var layerObj = self.layerObjects[key];
|
|
|
|
if (self.map.hasLayer(layerObj)){
|
|
|
|
self.map.removeLayer(layerObj);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(this.layerObjects).forEach(function(key){
|
|
|
|
var layerObj = self.layerObjects[key];
|
|
|
|
if (layerObj.layerId == layerId){
|
|
|
|
self.map.addLayer(layerObj);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-02-15 17:51:11 +03:00
|
|
|
LayerManager.prototype.setLayerId = function(layerId){
|
|
|
|
var self = this;
|
|
|
|
this.layers.forEach(function(layer){
|
|
|
|
if (layer.id == layerId){
|
|
|
|
self.currentLayer = layer;
|
2019-03-13 23:28:14 +03:00
|
|
|
self.listeners.forEach(function(listener){
|
|
|
|
listener(layer);
|
|
|
|
});
|
|
|
|
return;
|
2019-02-15 17:51:11 +03:00
|
|
|
}
|
|
|
|
});
|
2019-04-04 11:19:29 +03:00
|
|
|
};
|
2019-02-15 17:51:11 +03:00
|
|
|
|
2019-04-14 22:35:59 +03:00
|
|
|
LayerManager.prototype.getLayerByY = function(y){
|
|
|
|
return this.layers.find(function(layer){
|
|
|
|
return (y >= (layer.from*16) && y <= (layer.to*16));
|
|
|
|
});
|
|
|
|
};
|
2019-02-15 17:51:11 +03:00
|
|
|
|
2019-02-01 14:58:26 +03:00
|
|
|
LayerManager.prototype.addListener = function(listener){
|
2019-02-01 15:28:26 +03:00
|
|
|
this.listeners.push(listener);
|
|
|
|
};
|
|
|
|
|
|
|
|
LayerManager.prototype.removeListener = function(listener){
|
|
|
|
this.listeners = this.listeners.filter(function(el){
|
|
|
|
return el != listener;
|
|
|
|
});
|
2019-02-01 14:58:26 +03:00
|
|
|
};
|
2019-01-29 23:07:29 +03:00
|
|
|
|
2019-02-01 14:58:26 +03:00
|
|
|
LayerManager.prototype.getCurrentLayer = function(){
|
2019-02-01 15:28:26 +03:00
|
|
|
return this.currentLayer;
|
2019-02-01 14:46:38 +03:00
|
|
|
};
|