2019-06-11 16:01:16 +03:00
|
|
|
import debounce from '../util/debounce.js';
|
|
|
|
import { getMapObjects } from '../api.js';
|
2019-02-07 09:56:52 +03:00
|
|
|
|
2019-06-11 15:16:41 +03:00
|
|
|
export default L.LayerGroup.extend({
|
2019-02-07 09:56:52 +03:00
|
|
|
initialize: function(wsChannel, layerMgr, type) {
|
|
|
|
L.LayerGroup.prototype.initialize.call(this);
|
|
|
|
|
|
|
|
this.layerMgr = layerMgr;
|
|
|
|
this.wsChannel = wsChannel;
|
|
|
|
this.type = type;
|
|
|
|
|
|
|
|
this.onLayerChange = this.onLayerChange.bind(this);
|
|
|
|
this.onMapMove = debounce(this.onMapMove.bind(this), 50);
|
|
|
|
},
|
|
|
|
|
2019-06-11 16:01:16 +03:00
|
|
|
onLayerChange: function(){
|
2019-02-07 09:56:52 +03:00
|
|
|
this.reDraw();
|
|
|
|
},
|
|
|
|
|
|
|
|
onMapMove: function(){
|
|
|
|
this.reDraw();
|
|
|
|
},
|
|
|
|
|
2019-06-11 16:01:16 +03:00
|
|
|
createStyle: function(){
|
2019-04-02 16:00:42 +03:00
|
|
|
//TODO: default style
|
2019-02-25 15:01:15 +03:00
|
|
|
},
|
|
|
|
|
2019-02-07 09:56:52 +03:00
|
|
|
createGeoJson: function(objects){
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var geoJsonLayer = L.geoJSON([], {
|
|
|
|
onEachFeature: function(feature, layer){
|
|
|
|
if (feature.properties && feature.properties.popupContent) {
|
2019-02-08 13:05:22 +03:00
|
|
|
layer.bindPopup(feature.properties.popupContent);
|
|
|
|
}
|
2019-02-25 15:01:15 +03:00
|
|
|
},
|
2019-04-02 16:00:42 +03:00
|
|
|
style: self.createStyle.bind(self)
|
2019-02-07 09:56:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
objects.forEach(function(obj){
|
|
|
|
geoJsonLayer.addData(self.createFeature(obj));
|
|
|
|
});
|
|
|
|
|
|
|
|
return geoJsonLayer;
|
|
|
|
},
|
|
|
|
|
2019-04-03 22:31:36 +03:00
|
|
|
getMaxDisplayedZoom: function(){
|
|
|
|
return 7;
|
|
|
|
},
|
|
|
|
|
2019-04-04 09:45:42 +03:00
|
|
|
getMinDisplayedZoom: function(){
|
|
|
|
return 13;
|
|
|
|
},
|
|
|
|
|
2019-02-07 09:56:52 +03:00
|
|
|
reDraw: function(){
|
|
|
|
var self = this;
|
2019-04-04 09:45:42 +03:00
|
|
|
var zoom = this.map.getZoom();
|
2019-02-07 09:56:52 +03:00
|
|
|
|
2019-04-04 09:45:42 +03:00
|
|
|
if (zoom < this.getMaxDisplayedZoom() || zoom > this.getMinDisplayedZoom()) {
|
2019-02-07 09:56:52 +03:00
|
|
|
this.clearLayers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:05:22 +03:00
|
|
|
var mapLayer = this.layerMgr.getCurrentLayer();
|
2019-02-07 09:56:52 +03:00
|
|
|
var min = this._map.getBounds().getSouthWest();
|
|
|
|
var max = this._map.getBounds().getNorthEast();
|
|
|
|
|
2019-03-29 21:21:27 +03:00
|
|
|
var y1 = parseInt(mapLayer.from);
|
|
|
|
var y2 = parseInt(mapLayer.to);
|
2019-02-22 20:30:38 +03:00
|
|
|
var x1 = parseInt(min.lng/16);
|
|
|
|
var x2 = parseInt(max.lng/16);
|
|
|
|
var z1 = parseInt(min.lat/16);
|
|
|
|
var z2 = parseInt(max.lat/16);
|
2019-02-07 09:56:52 +03:00
|
|
|
|
2019-06-11 16:01:16 +03:00
|
|
|
getMapObjects({
|
2019-02-22 16:57:40 +03:00
|
|
|
pos1: { x:x1, y:y1, z:z1 },
|
|
|
|
pos2: { x:x2, y:y2, z:z2 },
|
|
|
|
type: this.type
|
|
|
|
})
|
2019-02-07 09:56:52 +03:00
|
|
|
.then(function(objects){
|
|
|
|
self.clearLayers();
|
|
|
|
|
2019-02-08 23:00:39 +03:00
|
|
|
var geoJsonLayer = self.createGeoJson(objects);
|
2019-02-07 09:56:52 +03:00
|
|
|
geoJsonLayer.addTo(self);
|
2019-04-04 11:19:29 +03:00
|
|
|
});
|
2019-02-07 09:56:52 +03:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
onAdd: function(map) {
|
2019-02-08 13:05:22 +03:00
|
|
|
this.map = map;
|
2019-02-07 09:56:52 +03:00
|
|
|
map.on("zoomend", this.onMapMove);
|
|
|
|
map.on("moveend", this.onMapMove);
|
|
|
|
this.layerMgr.addListener(this.onLayerChange);
|
2019-04-04 11:19:29 +03:00
|
|
|
this.reDraw(true);
|
2019-02-07 09:56:52 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onRemove: function(map) {
|
|
|
|
this.clearLayers();
|
|
|
|
map.off("zoomend", this.onMapMove);
|
|
|
|
map.off("moveend", this.onMapMove);
|
|
|
|
this.layerMgr.removeListener(this.onLayerChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|