From 23b00bd19d05128251706a3069c7507403a87438 Mon Sep 17 00:00:00 2001 From: James Lyne Date: Mon, 8 Feb 2021 00:08:29 +0000 Subject: [PATCH] Respect dynmap layer priorities --- src/components/map/layer/MarkerSetLayer.vue | 12 ++++--- src/components/map/layer/PlayersLayer.vue | 6 +++- src/leaflet/control/DynmapLayerControl.ts | 37 ++++++++++++++++++--- src/leaflet/layer/LayerManager.ts | 13 ++++++-- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/components/map/layer/MarkerSetLayer.vue b/src/components/map/layer/MarkerSetLayer.vue index 26a875e..77eb780 100644 --- a/src/components/map/layer/MarkerSetLayer.vue +++ b/src/components/map/layer/MarkerSetLayer.vue @@ -82,9 +82,11 @@ export default defineComponent({ }); if(newValue.hidden) { - this.leaflet.getLayerManager().addHiddenLayer(this.layerGroup, newValue.label, 1); + this.leaflet.getLayerManager() + .addHiddenLayer(this.layerGroup, newValue.label, this.markerSet.priority); } else { - this.leaflet.getLayerManager().addLayer(this.layerGroup, true, newValue.label, 1); + this.leaflet.getLayerManager() + .addLayer(this.layerGroup, true, newValue.label, this.markerSet.priority); } } }, @@ -94,9 +96,11 @@ export default defineComponent({ mounted() { if(this.markerSet.hidden) { - this.leaflet.getLayerManager().addHiddenLayer(this.layerGroup, this.markerSet.label, 1); + this.leaflet.getLayerManager() + .addHiddenLayer(this.layerGroup, this.markerSet.label, this.markerSet.priority); } else { - this.leaflet.getLayerManager().addLayer(this.layerGroup, true, this.markerSet.label, 1); + this.leaflet.getLayerManager() + .addLayer(this.layerGroup, true, this.markerSet.label, this.markerSet.priority); } }, diff --git a/src/components/map/layer/PlayersLayer.vue b/src/components/map/layer/PlayersLayer.vue index ac2e23e..12451bd 100644 --- a/src/components/map/layer/PlayersLayer.vue +++ b/src/components/map/layer/PlayersLayer.vue @@ -58,7 +58,11 @@ export default defineComponent({ mounted() { if(!this.componentSettings!.hideByDefault) { - this.leaflet.getLayerManager().addLayer(this.layerGroup, true, useStore().state.messages.players, 1); + this.leaflet.getLayerManager().addLayer( + this.layerGroup, + true, + useStore().state.messages.players, + this.componentSettings!.layerPriority); } }, diff --git a/src/leaflet/control/DynmapLayerControl.ts b/src/leaflet/control/DynmapLayerControl.ts index 6b1f006..ba54c87 100644 --- a/src/leaflet/control/DynmapLayerControl.ts +++ b/src/leaflet/control/DynmapLayerControl.ts @@ -17,7 +17,7 @@ * limitations under the License. */ -import {Util, Control, DomEvent, LeafletEvent, Map, Layer, DomUtil} from 'leaflet'; +import {Util, Control, DomEvent, LeafletEvent, Map as LeafletMap, Layer, DomUtil} from 'leaflet'; import layers from '@/assets/icons/layers.svg'; import LayersObject = Control.LayersObject; import LayersOptions = Control.LayersOptions; @@ -27,16 +27,30 @@ import checkbox from '@/assets/icons/checkbox.svg'; export class DynmapLayerControl extends Control.Layers { private _layersLink?: HTMLElement; - private _map ?: Map; + private _map ?: LeafletMap; private _overlaysList?: HTMLElement; private _baseLayersList?: HTMLElement; private _layerControlInputs?: HTMLElement[]; + private _layerPositions: Map; constructor(baseLayers?: LayersObject, overlays?: LayersObject, options?: LayersOptions) { - super(baseLayers, overlays, options); + super(baseLayers, overlays, Object.assign(options, { + sortLayers: true, + sortFunction: (layer1: Layer, layer2: Layer, name1: string, name2: string) => { + const priority1 = this._layerPositions.get(layer1) || 0, + priority2 = this._layerPositions.get(layer2) || 0; + + if(priority1 !== priority2) { + return priority1 - priority2; + } + + return ((name1 < name2) ? -1 : ((name1 > name2) ? 1 : 0)); + } + })); + this._layerPositions = new Map(); } - onAdd(map: Map) { + onAdd(map: LeafletMap) { // @ts-ignore const element = super.onAdd(map); @@ -62,6 +76,21 @@ export class DynmapLayerControl extends Control.Layers { return this; } + addOverlayAtPosition(layer: Layer, name: string, position: number): this { + this._layerPositions.set(layer, position); + return super.addOverlay(layer, name); + } + + addOverlay(layer: Layer, name: string): this { + this._layerPositions.set(layer, 0); + return super.addOverlay(layer, name); + } + + removeLayer(layer: Layer): this { + this._layerPositions.delete(layer); + return super.removeLayer(layer); + } + _addItem(obj: any) { const container = obj.overlay ? this._overlaysList : this._baseLayersList, item = document.createElement('label'), diff --git a/src/leaflet/layer/LayerManager.ts b/src/leaflet/layer/LayerManager.ts index 0d442a9..46a89bf 100644 --- a/src/leaflet/layer/LayerManager.ts +++ b/src/leaflet/layer/LayerManager.ts @@ -34,7 +34,6 @@ export default class LayerManager { } } - //TODO: Respect position addLayer(layer: Layer, showInControl: boolean, name: string, position: number) { this.map.addLayer(layer); @@ -43,12 +42,20 @@ export default class LayerManager { this.layerControl.removeLayer(layer); } - this.layerControl.addOverlay(layer, name); + if(typeof position !== 'undefined') { + this.layerControl.addOverlayAtPosition(layer, name, position); + } else { + this.layerControl.addOverlay(layer, name); + } } } addHiddenLayer(layer: Layer, name: string, position: number) { - this.layerControl.addOverlay(layer, name); + if(typeof position !== 'undefined') { + this.layerControl.addOverlayAtPosition(layer, name, position); + } else { + this.layerControl.addOverlay(layer, name); + } } removeLayer(layer: Layer) {