From 97d32a52109384d09e7554d58c0a22cb53c6e853 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 8 Feb 2019 14:35:29 +0100 Subject: [PATCH] realtime updates of markers --- server/static/js/main.js | 2 +- .../static/js/overlays/AbstractIconOverlay.js | 21 ++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/server/static/js/main.js b/server/static/js/main.js index 88526d6..051f1c0 100644 --- a/server/static/js/main.js +++ b/server/static/js/main.js @@ -38,7 +38,7 @@ api.getConfig().then(function(cfg){ overlays["Lua Controller"] = new LuacontrollerOverlay(wsChannel, layerMgr); overlays["Technic Anchor"] = new TechnicAnchorOverlay(wsChannel, layerMgr); overlays["Technic Quarry"] = new TechnicQuarryOverlay(wsChannel, layerMgr); - //overlays["Protector"] = new ProtectorOverlay(wsChannel, layerMgr); + overlays["Protector"] = new ProtectorOverlay(wsChannel, layerMgr); //Default enabled overlays map.addLayer(overlays["Player"]); diff --git a/server/static/js/overlays/AbstractIconOverlay.js b/server/static/js/overlays/AbstractIconOverlay.js index 1b5f79e..1ebb86f 100644 --- a/server/static/js/overlays/AbstractIconOverlay.js +++ b/server/static/js/overlays/AbstractIconOverlay.js @@ -12,9 +12,21 @@ var AbstractIconOverlay = L.LayerGroup.extend({ this.currentObjects = {}; this.onLayerChange = this.onLayerChange.bind(this); + this.onMapObjectUpdated = this.onMapObjectUpdated.bind(this); this.onMapMove = debounce(this.onMapMove.bind(this), 50); }, + //websocket update + onMapObjectUpdated: function(obj){ + var hash = self.hashPos(obj.x, obj.y, obj.z); + var marker = self.currentObjects[hash]; + + if (marker) { + //marker exists + marker.setPopupContent(self.createPopup(obj)); + } + }, + hashPos: function(x,y,z){ return x + "/" + y + "/" + z; }, @@ -65,14 +77,15 @@ var AbstractIconOverlay = L.LayerGroup.extend({ objects.forEach(function(obj){ var hash = self.hashPos(obj.x, obj.y, obj.z); + var marker = self.currentObjects[hash]; - if (self.currentObjects[hash]) { + if (marker) { //marker exists - //TODO: update popup + marker.setPopupContent(self.createPopup(obj)); } else { //marker does not exist - var marker = L.marker([obj.z, obj.x], {icon: self.getIcon(obj)}); + marker = L.marker([obj.z, obj.x], {icon: self.getIcon(obj)}); marker.bindPopup(self.createPopup(obj)); marker.addTo(self); @@ -88,6 +101,7 @@ var AbstractIconOverlay = L.LayerGroup.extend({ map.on("zoomend", this.onMapMove); map.on("moveend", this.onMapMove); this.layerMgr.addListener(this.onLayerChange); + this.wsChannel.addListener("mapobject-created", this.onMapObjectUpdated); this.reDraw(true) }, @@ -96,6 +110,7 @@ var AbstractIconOverlay = L.LayerGroup.extend({ map.off("zoomend", this.onMapMove); map.off("moveend", this.onMapMove); this.layerMgr.removeListener(this.onLayerChange); + this.wsChannel.removeListener("mapobject-created", this.onMapObjectUpdated); } });