2019-08-30 10:59:55 +02:00
|
|
|
import debounce from '../../util/debounce.js';
|
2019-09-18 11:53:58 +02:00
|
|
|
import wsChannel from '../../WebSocketChannel.js';
|
|
|
|
import layerMgr from '../../LayerManager.js';
|
|
|
|
|
2019-08-30 10:59:55 +02:00
|
|
|
import { getMapObjects } from '../../api.js';
|
2019-02-05 09:09:35 +01:00
|
|
|
|
2019-06-11 14:16:41 +02:00
|
|
|
export default L.LayerGroup.extend({
|
2019-09-18 11:53:58 +02:00
|
|
|
initialize: function(type, icon) {
|
2019-02-05 09:09:35 +01:00
|
|
|
L.LayerGroup.prototype.initialize.call(this);
|
|
|
|
|
|
|
|
this.type = type;
|
|
|
|
this.icon = icon;
|
|
|
|
|
|
|
|
this.currentObjects = {};
|
|
|
|
|
|
|
|
this.onLayerChange = this.onLayerChange.bind(this);
|
2019-02-08 14:35:29 +01:00
|
|
|
this.onMapObjectUpdated = this.onMapObjectUpdated.bind(this);
|
2019-02-05 09:09:35 +01:00
|
|
|
this.onMapMove = debounce(this.onMapMove.bind(this), 50);
|
|
|
|
},
|
|
|
|
|
2019-02-08 14:35:29 +01:00
|
|
|
//websocket update
|
|
|
|
onMapObjectUpdated: function(obj){
|
2019-02-08 17:41:09 +01:00
|
|
|
var hash = this.hashPos(obj.x, obj.y, obj.z);
|
|
|
|
var marker = this.currentObjects[hash];
|
2019-02-08 14:35:29 +01:00
|
|
|
|
|
|
|
if (marker) {
|
|
|
|
//marker exists
|
2019-02-08 17:41:09 +01:00
|
|
|
var popup = this.createPopup(obj);
|
2019-02-08 15:12:40 +01:00
|
|
|
if (popup)
|
|
|
|
marker.setPopupContent(popup);
|
|
|
|
|
|
|
|
marker.setIcon(this.getIcon(obj));
|
2019-02-08 14:35:29 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-05 09:09:35 +01:00
|
|
|
hashPos: function(x,y,z){
|
2019-02-05 21:17:14 +01:00
|
|
|
return x + "/" + y + "/" + z;
|
2019-02-05 09:09:35 +01:00
|
|
|
},
|
|
|
|
|
2019-06-11 15:01:16 +02:00
|
|
|
onLayerChange: function(/*layer*/){
|
2019-02-05 09:09:35 +01:00
|
|
|
this.reDraw(true);
|
|
|
|
},
|
|
|
|
|
|
|
|
onMapMove: function(){
|
|
|
|
this.reDraw(false);
|
|
|
|
},
|
|
|
|
|
2019-06-11 15:01:16 +02:00
|
|
|
getIcon: function(/*ob*/){
|
2019-02-08 13:53:16 +01:00
|
|
|
return this.icon;
|
|
|
|
},
|
|
|
|
|
2019-03-31 18:19:41 +02:00
|
|
|
getMaxDisplayedZoom: function(){
|
|
|
|
return 10;
|
|
|
|
},
|
|
|
|
|
2019-02-05 09:09:35 +01:00
|
|
|
reDraw: function(full){
|
|
|
|
var self = this;
|
|
|
|
|
2019-03-31 18:19:41 +02:00
|
|
|
if (this.map.getZoom() < this.getMaxDisplayedZoom()) {
|
2019-02-05 09:09:35 +01:00
|
|
|
this.clearLayers();
|
|
|
|
this.currentObjects = {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (full){
|
|
|
|
this.clearLayers();
|
|
|
|
this.currentObjects = {};
|
|
|
|
}
|
|
|
|
|
2019-09-18 11:53:58 +02:00
|
|
|
var mapLayer = layerMgr.getCurrentLayer();
|
2019-02-08 14:48:06 +01:00
|
|
|
var min = this.map.getBounds().getSouthWest();
|
|
|
|
var max = this.map.getBounds().getNorthEast();
|
2019-02-05 09:09:35 +01:00
|
|
|
|
2019-03-29 19:21:27 +01:00
|
|
|
var y1 = parseInt(mapLayer.from);
|
|
|
|
var y2 = parseInt(mapLayer.to);
|
2019-02-22 18:30:38 +01: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-05 09:09:35 +01:00
|
|
|
|
2019-06-11 15:01:16 +02:00
|
|
|
getMapObjects({
|
2019-02-22 14:57:40 +01:00
|
|
|
pos1: { x:x1, y:y1, z:z1 },
|
|
|
|
pos2: { x:x2, y:y2, z:z2 },
|
|
|
|
type: this.type
|
|
|
|
})
|
2019-02-05 09:09:35 +01:00
|
|
|
.then(function(objects){
|
2019-02-05 12:54:42 +01:00
|
|
|
//TODO: remove non-existing markers
|
2019-02-05 09:09:35 +01:00
|
|
|
|
|
|
|
objects.forEach(function(obj){
|
|
|
|
var hash = self.hashPos(obj.x, obj.y, obj.z);
|
2019-02-08 14:35:29 +01:00
|
|
|
var marker = self.currentObjects[hash];
|
2019-06-09 18:32:36 +02:00
|
|
|
var popup, icon;
|
2019-02-05 12:54:42 +01:00
|
|
|
|
2019-02-08 14:35:29 +01:00
|
|
|
if (marker) {
|
2019-02-05 12:54:42 +01:00
|
|
|
//marker exists
|
2019-06-09 18:32:36 +02:00
|
|
|
icon = self.getIcon(obj);
|
2019-02-08 15:12:40 +01:00
|
|
|
|
2019-06-09 18:31:05 +02:00
|
|
|
if (!icon) {
|
|
|
|
//icon does not wanna be displayed anymore
|
|
|
|
marker.remove();
|
|
|
|
return;
|
|
|
|
}
|
2019-02-08 15:12:40 +01:00
|
|
|
//set popup, if changed
|
2019-04-04 10:49:55 +02:00
|
|
|
popup = self.createPopup(obj);
|
2019-02-08 15:12:40 +01:00
|
|
|
if (popup)
|
|
|
|
marker.setPopupContent(popup);
|
|
|
|
|
|
|
|
//redraw icon, if changed
|
2019-06-09 18:31:05 +02:00
|
|
|
marker.setIcon(icon);
|
2019-02-05 12:54:42 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
//marker does not exist
|
2019-06-09 18:32:36 +02:00
|
|
|
icon = self.getIcon(obj);
|
2019-06-09 18:31:05 +02:00
|
|
|
|
|
|
|
if (!icon) {
|
|
|
|
//icon does not want to be displayed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-07 01:23:14 +08:00
|
|
|
marker = L.marker([obj.z + 0.5, obj.x + 0.5], {icon: icon});
|
2019-04-04 10:49:55 +02:00
|
|
|
popup = self.createPopup(obj);
|
2019-02-08 15:12:40 +01:00
|
|
|
if (popup)
|
|
|
|
marker.bindPopup(popup);
|
2019-02-05 12:54:42 +01:00
|
|
|
marker.addTo(self);
|
|
|
|
|
|
|
|
self.currentObjects[hash] = marker;
|
|
|
|
|
|
|
|
}
|
2019-02-05 09:09:35 +01:00
|
|
|
});
|
2019-04-04 10:49:55 +02:00
|
|
|
});
|
2019-02-05 09:09:35 +01:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
onAdd: function(map) {
|
2019-02-08 14:48:06 +01:00
|
|
|
this.map = map;
|
2019-02-05 09:09:35 +01:00
|
|
|
map.on("zoomend", this.onMapMove);
|
|
|
|
map.on("moveend", this.onMapMove);
|
2019-09-18 11:53:58 +02:00
|
|
|
wsChannel.addListener("mapobject-created", this.onMapObjectUpdated);
|
2019-04-04 10:49:55 +02:00
|
|
|
this.reDraw(true);
|
2019-02-05 09:09:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
onRemove: function(map) {
|
|
|
|
this.clearLayers();
|
|
|
|
map.off("zoomend", this.onMapMove);
|
|
|
|
map.off("moveend", this.onMapMove);
|
2019-09-18 11:53:58 +02:00
|
|
|
wsChannel.removeListener("mapobject-created", this.onMapObjectUpdated);
|
2019-02-05 09:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|