Handle marker set updates
This commit is contained in:
parent
7b1afa1c20
commit
11fbc069be
39
src/api.ts
39
src/api.ts
@ -185,6 +185,18 @@ function buildComponents(response: any): DynmapComponentConfig {
|
|||||||
return components;
|
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> {
|
function buildMarkers(data: any): Map<string, DynmapMarker> {
|
||||||
const markers = Object.freeze(new Map()) as Map<string, DynmapMarker>;
|
const markers = Object.freeze(new Map()) as Map<string, DynmapMarker>;
|
||||||
|
|
||||||
@ -352,7 +364,10 @@ function buildUpdates(data: Array<any>): DynmapUpdates {
|
|||||||
continue;
|
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++;
|
dropped.noSet++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -362,22 +377,26 @@ function buildUpdates(data: Array<any>): DynmapUpdates {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!updates.markerSets.has(entry.set)) {
|
if(!updates.markerSets.has(set)) {
|
||||||
updates.markerSets.set(entry.set, {
|
updates.markerSets.set(set, {
|
||||||
areaUpdates: [],
|
areaUpdates: [],
|
||||||
markerUpdates: [],
|
markerUpdates: [],
|
||||||
lineUpdates: [],
|
lineUpdates: [],
|
||||||
circleUpdates: [],
|
circleUpdates: [],
|
||||||
|
removed: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const markerSetUpdates = updates.markerSets.get(entry.set),
|
const markerSetUpdates = updates.markerSets.get(set),
|
||||||
update: DynmapUpdate = {
|
update: DynmapUpdate = {
|
||||||
id: entry.id,
|
id: entry.id,
|
||||||
removed: entry.msg.endsWith('deleted'),
|
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);
|
update.payload = update.removed ? undefined : buildMarker(entry);
|
||||||
markerSetUpdates!.markerUpdates.push(Object.freeze(update));
|
markerSetUpdates!.markerUpdates.push(Object.freeze(update));
|
||||||
} else if(entry.msg.startsWith("area")) {
|
} else if(entry.msg.startsWith("area")) {
|
||||||
@ -565,17 +584,11 @@ export default {
|
|||||||
lines = buildLines(set.lines || {});
|
lines = buildLines(set.lines || {});
|
||||||
|
|
||||||
sets.set(key, {
|
sets.set(key, {
|
||||||
id: key,
|
...buildMarkerSet(key, set),
|
||||||
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,
|
|
||||||
markers,
|
markers,
|
||||||
|
circles,
|
||||||
areas,
|
areas,
|
||||||
lines,
|
lines,
|
||||||
circles,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +73,6 @@ export default defineComponent({
|
|||||||
markerSet: {
|
markerSet: {
|
||||||
handler(newValue) {
|
handler(newValue) {
|
||||||
if(newValue && this.layerGroup) {
|
if(newValue && this.layerGroup) {
|
||||||
console.log('Updating layer group');
|
|
||||||
this.layerGroup.update({
|
this.layerGroup.update({
|
||||||
id: this.markerSet.id,
|
id: this.markerSet.id,
|
||||||
minZoom: this.markerSet.minZoom,
|
minZoom: this.markerSet.minZoom,
|
||||||
@ -81,6 +80,12 @@ export default defineComponent({
|
|||||||
showLabels: this.markerSet.showLabels || useStore().state.components.markers.showLabels,
|
showLabels: this.markerSet.showLabels || useStore().state.components.markers.showLabels,
|
||||||
priority: this.markerSet.priority,
|
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,
|
deep: true,
|
||||||
|
9
src/dynmap.d.ts
vendored
9
src/dynmap.d.ts
vendored
@ -250,6 +250,15 @@ interface DynmapMarkerSetUpdates {
|
|||||||
areaUpdates: Array<DynmapAreaUpdate>
|
areaUpdates: Array<DynmapAreaUpdate>
|
||||||
circleUpdates: Array<DynmapCircleUpdate>
|
circleUpdates: Array<DynmapCircleUpdate>
|
||||||
lineUpdates: Array<DynmapLineUpdate>
|
lineUpdates: Array<DynmapLineUpdate>
|
||||||
|
removed?: boolean
|
||||||
|
payload?: {
|
||||||
|
showLabels: boolean;
|
||||||
|
hidden: boolean;
|
||||||
|
minZoom: number;
|
||||||
|
maxZoom: number;
|
||||||
|
priority: number;
|
||||||
|
label: string;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface DynmapUpdate {
|
interface DynmapUpdate {
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
* limitations under the License.
|
* 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 layers from '@/assets/icons/layers.svg';
|
||||||
import LayersObject = Control.LayersObject;
|
import LayersObject = Control.LayersObject;
|
||||||
import LayersOptions = Control.LayersOptions;
|
import LayersOptions = Control.LayersOptions;
|
||||||
@ -48,6 +48,11 @@ export class DynmapLayerControl extends Control.Layers {
|
|||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hasLayer(layer: Layer): boolean {
|
||||||
|
// @ts-ignore
|
||||||
|
return !!super._getLayer(Util.stamp(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'),
|
||||||
|
@ -34,10 +34,15 @@ 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);
|
||||||
|
|
||||||
if(showInControl) {
|
if(showInControl) {
|
||||||
|
if(this.layerControl.hasLayer(layer)) {
|
||||||
|
this.layerControl.removeLayer(layer);
|
||||||
|
}
|
||||||
|
|
||||||
this.layerControl.addOverlay(layer, name);
|
this.layerControl.addOverlay(layer, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
[MutationTypes.SET_COMPONENTS](state: State, components: DynmapComponentConfig) {
|
||||||
state.components = components;
|
state.components = components;
|
||||||
},
|
},
|
||||||
@ -140,13 +140,55 @@ export const mutations: MutationTree<State> & Mutations = {
|
|||||||
[MutationTypes.ADD_MARKER_SET_UPDATES](state: State, updates: Map<string, DynmapMarkerSetUpdates>) {
|
[MutationTypes.ADD_MARKER_SET_UPDATES](state: State, updates: Map<string, DynmapMarkerSetUpdates>) {
|
||||||
for(const entry of updates) {
|
for(const entry of updates) {
|
||||||
if(!state.markerSets.has(entry[0])) {
|
if(!state.markerSets.has(entry[0])) {
|
||||||
|
|
||||||
|
//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`);
|
console.warn(`ADD_MARKER_SET_UPDATES: Marker set ${entry[0]} doesn't exist`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const set = state.markerSets.get(entry[0]) as DynmapMarkerSet,
|
const set = state.markerSets.get(entry[0]) as DynmapMarkerSet,
|
||||||
setUpdates = state.pendingSetUpdates.get(entry[0]) as DynmapMarkerSetUpdates;
|
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
|
//Update non-reactive lists
|
||||||
for(const update of entry[1].markerUpdates) {
|
for(const update of entry[1].markerUpdates) {
|
||||||
if(update.removed) {
|
if(update.removed) {
|
||||||
|
Loading…
Reference in New Issue
Block a user