Skip to content

Commit

Permalink
♻️ [leafletMap] rewrite to composition API
Browse files Browse the repository at this point in the history
  • Loading branch information
jxn-30 committed Aug 19, 2023
1 parent f7f8a4d commit 5aa6973
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 151 deletions.
80 changes: 80 additions & 0 deletions src/components/LeafletMap.vue
@@ -0,0 +1,80 @@
<template>
<div :id="mapId"></div>
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
import { useEventStore } from '@stores/event';
import type { FeatureGroup, Layer, Map as LMap } from 'leaflet';
const leafletMap = ref<LMap>();
const props = withDefaults(
defineProps<{
id: string;
height?: string;
startLat?: number;
startLong?: number;
startZoom?: number;
layers?: Layer[];
centerGroup?: FeatureGroup;
}>(),
{
height: '500px',
startLat: 48.783_200_898_738_78,
startLong: 9.180_364_608_764_65,
startZoom: 15,
layers: () => [],
centerGroup: undefined,
}
);
const mapId = computed(() => `map-${props.id}`);
const redraw = () => {
leafletMap.value?.remove();
const map = window.L.map(mapId.value, {
layers: props.layers,
}).setView([props.startLat, props.startLong], props.startZoom);
leafletMap.value = map;
window.L.tileLayer('https://{s}/{z}/{x}/{y}.png', {
attribution:
'&copy; <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors',
subdomains: ['maps.leitstellenspiel.de/tiles/osm'],
maxZoom: 17,
noWrap: true,
}).addTo(map);
if (props.centerGroup) map.fitBounds(props.centerGroup.getBounds());
useEventStore().createAndDispatchEvent({
name: 'leaflet-map-loaded',
detail: { id: mapId.value, map },
});
return map;
};
const setView = (lat: number, long: number, zoom: number = undefined) =>
leafletMap.value?.setView(
[lat, long],
zoom ?? leafletMap.value?.getZoom() ?? 15
);
const $emit = defineEmits<{ (event: 'mounted', map: LMap): void }>();
defineExpose({ map: leafletMap, redraw, setView });
onMounted(() => {
$emit('mounted', redraw());
});
</script>

<style scoped lang="sass">
div
height: v-bind(height)
</style>
97 changes: 0 additions & 97 deletions src/components/leaflet-map.vue

This file was deleted.

4 changes: 2 additions & 2 deletions src/components/setting/location/LocationSelect.vue
Expand Up @@ -10,7 +10,7 @@
:start-lat="location[0]"
:start-long="location[1]"
:start-zoom="location[2]"
:layers="[locationMarker]"
:layers="locationMarker ? [locationMarker] : []"
@mounted="mapMounted"
></leaflet-map>
</lightbox>
Expand All @@ -21,7 +21,7 @@ import { computed, onBeforeMount, ref } from 'vue';
import { useRootStore } from '@stores/index';
import LeafletMap from '../../leaflet-map.vue';
import LeafletMap from '../../LeafletMap.vue';
import Lightbox from '../../LightboxWrapper.vue';
import { useI18n } from '../../../i18n';
Expand Down
Expand Up @@ -28,7 +28,7 @@ export default Vue.extend<
components: {
LeafletMap: () =>
import(
/* webpackChunkName: "components/leaflet-map" */ '../../../../components/leaflet-map.vue'
/* webpackChunkName: "components/leafletMap" */ '../../../../components/LeafletMap.vue'
),
},
props: {
Expand Down
13 changes: 6 additions & 7 deletions src/modules/generalExtensions/assets/mapSearches.ts
Expand Up @@ -125,13 +125,12 @@ export default (
window.map.addEventListener('moveend', resetNewBuildingMarker);
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.addEventListener(
'lssmv4-map-loaded',
({ detail: { id, map } }: CustomEvent<{ id: string; map: LMap }>) =>
addToMap(map, id)
);
LSSM.$stores.event.addListener({
name: 'leaflet-map-loaded',
listener: ({
detail: { id, map },
}: CustomEvent<{ id: string; map: LMap }>) => addToMap(map, id),
});

if (window.location.pathname === '/' && !mapSearchOnMap) return;

Expand Down
13 changes: 6 additions & 7 deletions src/modules/heatmap/main.ts
Expand Up @@ -307,12 +307,11 @@ export default <ModuleMainFunction>(async ({
if (window.location.pathname.startsWith(`/profile/${window.user_id}`)) {
if (window.map) initHeatmap(window.map);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.addEventListener(
'lssmv4-map-loaded',
({ detail: { map } }: CustomEvent<{ id: string; map: Map }>) =>
initHeatmap(map)
);
LSSM.$stores.event.addListener({
name: 'leaflet-map-loaded',
listener: ({
detail: { map },
}: CustomEvent<{ id: string; map: Map }>) => initHeatmap(map),
});
}
});
2 changes: 1 addition & 1 deletion src/modules/redesign/components/profile.vue
Expand Up @@ -682,7 +682,7 @@ export default Vue.extend<
components: {
LeafletMap: () =>
import(
/* webpackChunkName: "components/leaflet-map" */ '../../../components/leaflet-map.vue'
/* webpackChunkName: "components/leafletMap" */ '../../../components/LeafletMap.vue'
),
},
data() {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/redesign/components/verband/bsr.vue
Expand Up @@ -85,7 +85,7 @@ export default Vue.extend<
components: {
LeafletMap: () =>
import(
/* webpackChunkName: "components/leaflet-map" */ '../../../../components/leaflet-map.vue'
/* webpackChunkName: "components/leafletMap" */ '../../../../components/LeafletMap.vue'
),
EnhancedTable: () =>
import(
Expand Down
2 changes: 1 addition & 1 deletion src/modules/redesign/components/verband/gebauede.vue
Expand Up @@ -114,7 +114,7 @@ export default Vue.extend<
components: {
LeafletMap: () =>
import(
/* webpackChunkName: "components/leaflet-map" */ '../../../../components/leaflet-map.vue'
/* webpackChunkName: "components/leafletMap" */ '../../../../components/LeafletMap.vue'
),
EnhancedTable: () =>
import(
Expand Down
34 changes: 0 additions & 34 deletions typings/components/LeafletMap.ts

This file was deleted.

0 comments on commit 5aa6973

Please sign in to comment.