Handle marker set updates

This commit is contained in:
James Lyne 2020-12-19 01:09:31 +00:00
parent 7b1afa1c20
commit 11fbc069be
6 changed files with 97 additions and 18 deletions

View File

@ -185,6 +185,18 @@ function buildComponents(response: any): DynmapComponentConfig {
return components;
}
function buildMarkerSet(id: string, data: any): any {
return {
id,
label: data.label || "Unnamed set",
hidden: data.hide || false,
priority: data.layerprio || 0,
showLabels: data.showlabels || undefined,
minZoom: typeof data.minzoom !== 'undefined' && data.minzoom > -1 ? data.minzoom : undefined,
maxZoom: typeof data.maxzoom !== 'undefined' && data.maxzoom > -1 ? data.maxzoom : undefined,
}
}
function buildMarkers(data: any): Map<string, DynmapMarker> {
const markers = Object.freeze(new Map()) as Map<string, DynmapMarker>;
@ -352,7 +364,10 @@ function buildUpdates(data: Array<any>): DynmapUpdates {
continue;
}
if(!entry.set) {
//Set updates don't have a set field, the id is the set
const set = entry.msg.startsWith("set") ? entry.id : entry.set;
if(!set) {
dropped.noSet++;
continue;
}
@ -362,22 +377,26 @@ function buildUpdates(data: Array<any>): DynmapUpdates {
continue;
}
if(!updates.markerSets.has(entry.set)) {
updates.markerSets.set(entry.set, {
if(!updates.markerSets.has(set)) {
updates.markerSets.set(set, {
areaUpdates: [],
markerUpdates: [],
lineUpdates: [],
circleUpdates: [],
removed: false,
});
}
const markerSetUpdates = updates.markerSets.get(entry.set),
const markerSetUpdates = updates.markerSets.get(set),
update: DynmapUpdate = {
id: entry.id,
removed: entry.msg.endsWith('deleted'),
};
if(entry.msg.startsWith("marker")) {
if(entry.msg.startsWith("set")) {
markerSetUpdates!.removed = update.removed;
markerSetUpdates!.payload = update.removed ? undefined : buildMarkerSet(set, entry);
} else if(entry.msg.startsWith("marker")) {
update.payload = update.removed ? undefined : buildMarker(entry);
markerSetUpdates!.markerUpdates.push(Object.freeze(update));
} else if(entry.msg.startsWith("area")) {
@ -565,17 +584,11 @@ export default {
lines = buildLines(set.lines || {});
sets.set(key, {
id: key,
label: set.label || "Unnamed set",
hidden: set.hide || false,
priority: set.layerprio || 0,
showLabels: set.showlabels || undefined,
minZoom: typeof set.minzoom !== 'undefined' && set.minzoom > -1 ? set.minzoom : undefined,
maxZoom: typeof set.maxzoom !== 'undefined' && set.maxzoom > -1 ? set.maxzoom : undefined,
...buildMarkerSet(key, set),
markers,
circles,
areas,
lines,
circles,
});
}

View File

@ -73,7 +73,6 @@ export default defineComponent({
markerSet: {
handler(newValue) {
if(newValue && this.layerGroup) {
console.log('Updating layer group');
this.layerGroup.update({
id: this.markerSet.id,
minZoom: this.markerSet.minZoom,
@ -81,6 +80,12 @@ export default defineComponent({
showLabels: this.markerSet.showLabels || useStore().state.components.markers.showLabels,
priority: this.markerSet.priority,
});
if(newValue.hidden) {
this.leaflet.getLayerManager().addHiddenLayer(this.layerGroup, newValue.label, 1);
} else {
this.leaflet.getLayerManager().addLayer(this.layerGroup, true, newValue.label, 1);
}
}
},
deep: true,

9
src/dynmap.d.ts vendored
View File

@ -250,6 +250,15 @@ interface DynmapMarkerSetUpdates {
areaUpdates: Array<DynmapAreaUpdate>
circleUpdates: Array<DynmapCircleUpdate>
lineUpdates: Array<DynmapLineUpdate>
removed?: boolean
payload?: {
showLabels: boolean;
hidden: boolean;
minZoom: number;
maxZoom: number;
priority: number;
label: string;
}
}
interface DynmapUpdate {

View File

@ -17,7 +17,7 @@
* limitations under the License.
*/
import {Util, Control, DomEvent, LeafletEvent, Map} from 'leaflet';
import {Util, Control, DomEvent, LeafletEvent, Map, Layer} from 'leaflet';
import layers from '@/assets/icons/layers.svg';
import LayersObject = Control.LayersObject;
import LayersOptions = Control.LayersOptions;
@ -48,6 +48,11 @@ export class DynmapLayerControl extends Control.Layers {
return element;
}
hasLayer(layer: Layer): boolean {
// @ts-ignore
return !!super._getLayer(Util.stamp(layer));
}
_addItem(obj: any) {
const container = obj.overlay ? this._overlaysList : this._baseLayersList,
item = document.createElement('label'),

View File

@ -34,10 +34,15 @@ export default class LayerManager {
}
}
//TODO: Respect position
addLayer(layer: Layer, showInControl: boolean, name: string, position: number) {
this.map.addLayer(layer);
if(showInControl) {
if(this.layerControl.hasLayer(layer)) {
this.layerControl.removeLayer(layer);
}
this.layerControl.addOverlay(layer, name);
}
}

View File

@ -102,7 +102,7 @@ export const mutations: MutationTree<State> & Mutations = {
});
},
//Sets the state and settings of optional compontents, from the initial config fetch
//Sets the state and settings of optional components, from the initial config fetch
[MutationTypes.SET_COMPONENTS](state: State, components: DynmapComponentConfig) {
state.components = components;
},
@ -140,13 +140,55 @@ export const mutations: MutationTree<State> & Mutations = {
[MutationTypes.ADD_MARKER_SET_UPDATES](state: State, updates: Map<string, DynmapMarkerSetUpdates>) {
for(const entry of updates) {
if(!state.markerSets.has(entry[0])) {
console.warn(`ADD_MARKER_SET_UPDATES: Marker set ${entry[0]} doesn't exist`);
continue;
//Create marker set if it doesn't exist
if(entry[1].payload) {
state.markerSets.set(entry[0], {
id: entry[0],
showLabels: entry[1].payload.showLabels,
minZoom: entry[1].payload.minZoom,
maxZoom: entry[1].payload.maxZoom,
priority: entry[1].payload.priority,
label: entry[1].payload.label,
hidden: entry[1].payload.hidden,
markers: Object.freeze(new Map()) as Map<string, DynmapMarker>,
areas: Object.freeze(new Map()) as Map<string, DynmapArea>,
circles: Object.freeze(new Map()) as Map<string, DynmapCircle>,
lines: Object.freeze(new Map()) as Map<string, DynmapLine>,
});
state.pendingSetUpdates.set(entry[0], {
markerUpdates: [],
areaUpdates: [],
circleUpdates: [],
lineUpdates: [],
});
} else {
console.warn(`ADD_MARKER_SET_UPDATES: Marker set ${entry[0]} doesn't exist`);
continue;
}
}
const set = state.markerSets.get(entry[0]) as DynmapMarkerSet,
setUpdates = state.pendingSetUpdates.get(entry[0]) as DynmapMarkerSetUpdates;
//Delete the set if it has been deleted
if(entry[1].removed) {
state.markerSets.delete(entry[0]);
state.pendingSetUpdates.delete(entry[0]);
continue;
}
//Update the set itself if a payload exists
if(entry[1].payload) {
set.showLabels = entry[1].payload.showLabels;
set.minZoom = entry[1].payload.minZoom;
set.maxZoom = entry[1].payload.maxZoom;
set.priority = entry[1].payload.priority;
set.label = entry[1].payload.label;
set.hidden = entry[1].payload.hidden;
}
//Update non-reactive lists
for(const update of entry[1].markerUpdates) {
if(update.removed) {