Respect dynmap layer priorities

This commit is contained in:
James Lyne 2021-02-08 00:08:29 +00:00
parent 3b4e7e47c8
commit 23b00bd19d
4 changed files with 56 additions and 12 deletions

View File

@ -82,9 +82,11 @@ export default defineComponent({
}); });
if(newValue.hidden) { if(newValue.hidden) {
this.leaflet.getLayerManager().addHiddenLayer(this.layerGroup, newValue.label, 1); this.leaflet.getLayerManager()
.addHiddenLayer(this.layerGroup, newValue.label, this.markerSet.priority);
} else { } 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() { mounted() {
if(this.markerSet.hidden) { 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 { } 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);
} }
}, },

View File

@ -58,7 +58,11 @@ export default defineComponent({
mounted() { mounted() {
if(!this.componentSettings!.hideByDefault) { 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);
} }
}, },

View File

@ -17,7 +17,7 @@
* limitations under the License. * 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 layers from '@/assets/icons/layers.svg';
import LayersObject = Control.LayersObject; import LayersObject = Control.LayersObject;
import LayersOptions = Control.LayersOptions; import LayersOptions = Control.LayersOptions;
@ -27,16 +27,30 @@ import checkbox from '@/assets/icons/checkbox.svg';
export class DynmapLayerControl extends Control.Layers { export class DynmapLayerControl extends Control.Layers {
private _layersLink?: HTMLElement; private _layersLink?: HTMLElement;
private _map ?: Map; private _map ?: LeafletMap;
private _overlaysList?: HTMLElement; private _overlaysList?: HTMLElement;
private _baseLayersList?: HTMLElement; private _baseLayersList?: HTMLElement;
private _layerControlInputs?: HTMLElement[]; private _layerControlInputs?: HTMLElement[];
private _layerPositions: Map<Layer, number>;
constructor(baseLayers?: LayersObject, overlays?: LayersObject, options?: LayersOptions) { 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<Layer, number>();
} }
onAdd(map: Map) { onAdd(map: LeafletMap) {
// @ts-ignore // @ts-ignore
const element = super.onAdd(map); const element = super.onAdd(map);
@ -62,6 +76,21 @@ export class DynmapLayerControl extends Control.Layers {
return this; 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) { _addItem(obj: any) {
const container = obj.overlay ? this._overlaysList : this._baseLayersList, const container = obj.overlay ? this._overlaysList : this._baseLayersList,
item = document.createElement('label'), item = document.createElement('label'),

View File

@ -34,7 +34,6 @@ export default class LayerManager {
} }
} }
//TODO: Respect position
addLayer(layer: Layer, showInControl: boolean, name: string, position: number) { addLayer(layer: Layer, showInControl: boolean, name: string, position: number) {
this.map.addLayer(layer); this.map.addLayer(layer);
@ -43,12 +42,20 @@ export default class LayerManager {
this.layerControl.removeLayer(layer); 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) { 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) { removeLayer(layer: Layer) {