-
Notifications
You must be signed in to change notification settings - Fork 11
/
Layer.vue
124 lines (108 loc) · 3.93 KB
/
Layer.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!-- eslint-disable vue/multi-word-component-names -->
<script setup lang="ts">
import {type AnyLayer, type Layer, MapMouseEvent } from "mapbox-gl";
import { inject } from "vue";
import { computed, onUnmounted, watch, useMapboxInstance, useMapbox, useMapboxBeforeLoad, useAttrs, useMapboxRef } from "#imports";
import { whenever } from "@vueuse/core";
interface Props {
layer: AnyLayer;
beforeLayer?: string;
onMousedown?: Function;
onMouseup?: Function;
onMouseover?: Function;
onMousemove?: Function;
onClick?: Function;
onDblclick?: Function;
onMouseenter?: Function;
onMouseleave?: Function;
}
const props = defineProps<Props>();
const emit = defineEmits<{
(e: "mousedown", event: MapMouseEvent): void;
(e: "mouseup", event: MapMouseEvent): void;
(e: "mouseover", event: MapMouseEvent): void;
(e: "mousemove", event: MapMouseEvent): void;
(e: "click", event: MapMouseEvent): void;
(e: "dblclick", event: MapMouseEvent): void;
(e: "mouseenter", event: MapMouseEvent): void;
(e: "mouseleave", event: MapMouseEvent): void;
}>();
const mapId = inject<string>("MapID");
if (!mapId) throw "Mapbox Controls must be placed inside a Map component";
const attrs = useAttrs();
if (attrs.source || attrs.sourceId) throw "source & sourceId props in MapboxLayer are deprecated";
watch(() => {
return Object.entries(props).filter((arr) => arr[0].toString().startsWith('on'))
}, (value) => {
for (const [key, v] of value) {
if (v) {
const eventName = key.toString().replace('on', '').toLowerCase();
useMapboxBeforeLoad(mapId, (map) => {
map.on(eventName as keyof typeof emit, props.layer.id, (e) => {
emit(eventName as keyof typeof emit, e);
})
})
}
}
}, {immediate: true});
const mapRef = useMapboxInstance(mapId);
const sourceExists = computed(() => {
return !!mapRef.value?.getSource(
(props.layer as Layer).source?.toString() || ""
);
});
useMapbox(mapId, (map) => {
function addLayer() {
if (!sourceExists.value) {
// This causes layer to be reloaded whenever the source is.
whenever(sourceExists, addLayer);
return;
}
if (props.beforeLayer && map.getLayer(props.beforeLayer)) {
map?.addLayer(props.layer, props.beforeLayer);
} else {
if (props.beforeLayer) {
watch(() => map.getLayer(props.beforeLayer || ''), () => {
map?.moveLayer(props.layer.id, props.beforeLayer);
})
}
map?.addLayer(props.layer);
}
}
addLayer();
});
watch(() => props.layer, (newLayer, oldLayer) => {
const map = useMapboxRef(mapId);
if (newLayer) {
if ((newLayer as Layer).minzoom) {
map.value?.setLayerZoomRange(newLayer.id, (newLayer as Layer).minzoom || 0, (newLayer as Layer).maxzoom || 0);
}
if ((newLayer as Layer).maxzoom) {
map.value?.setLayerZoomRange(newLayer.id, (newLayer as Layer).minzoom || 0, (newLayer as Layer).maxzoom || 0);
}
if ((newLayer as Layer).paint) {
for (const prop of Object.keys((newLayer as Layer).paint || {})) {
map.value?.setPaintProperty(newLayer.id, prop, (newLayer as Layer).paint?.[prop as keyof Layer["paint"]]);
}
}
if ((newLayer as Layer).layout) {
for (const prop of Object.keys((newLayer as Layer).layout || {})) {
map.value?.setLayoutProperty(newLayer.id, prop, (newLayer as Layer).layout?.[prop as keyof Layer["layout"]]);
}
}
if ((newLayer as Layer).filter) {
map.value?.setFilter(newLayer.id, (newLayer as Layer).filter)
}
}
});
onUnmounted(() => {
useMapbox(mapId, (map) => {
map?.removeLayer(props.layer.id);
});
});
</script>
<template>
<div>
<slot />
</div>
</template>