@dative-gpi/foundation-shared-components 1.0.156-maps → 1.0.156-maps2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,9 +3,9 @@
3
3
  </template>
4
4
 
5
5
  <script lang="ts">
6
- import { inject, type PropType, onMounted, type Ref, watch, ref, onUnmounted } from 'vue';
6
+ import { inject, type PropType, type Ref, watch, ref, onUnmounted, onMounted } from 'vue';
7
7
 
8
- import { type Map, type DivIcon, divIcon, type LatLng, marker, type Marker, type MarkerClusterGroup } from 'leaflet';
8
+ import { type Map, divIcon, type LatLng, marker, type Marker, type MarkerClusterGroup, type LeafletMouseEvent } from 'leaflet';
9
9
 
10
10
  import { useColors } from "../../composables";
11
11
 
@@ -49,87 +49,90 @@ export default {
49
49
  const markerClusterGroup = inject<Ref<MarkerClusterGroup | null>>(MARKERCLUSTERGROUP, ref(null));
50
50
 
51
51
  const { getColors } = useColors();
52
-
53
- const lastMarker = ref<Marker | null>(null);
54
-
55
- if(!map) {
56
- throw new Error('FSMapTileLayer must be used inside a FSMap component');
57
- }
58
-
59
- if(!map.value) {
60
- throw new Error('FSMapTileLayer must be used inside a FSMap component with a map');
61
- }
62
-
63
- const updateMarker = () => {
64
- if(!map.value || !props.latlng) {
65
- return;
66
- }
67
-
68
- if(lastMarker.value) {
69
- if(markerClusterGroup && markerClusterGroup.value) {
70
- markerClusterGroup.value.removeLayer(lastMarker.value as Marker);
71
- } else {
72
- map.value.removeLayer(lastMarker.value as Marker);
73
- }
74
- }
75
-
76
- let icon: DivIcon | null = null;
52
+ const getMarkerIcon = () => {
77
53
  if(props.variant === 'gps') {
78
54
  const size = 16;
79
- icon = divIcon({
55
+ return divIcon({
80
56
  html: gpsMarkerHtml(),
81
57
  className: 'fs-map-mylocation',
82
58
  iconSize: [size, size],
83
59
  iconAnchor: [size / 2, size / 2],
84
60
  });
85
- } else if(props.variant === 'location') {
61
+ }
62
+
63
+ if(props.variant === 'location') {
86
64
  const size = 36;
87
- icon = divIcon({
65
+ return divIcon({
88
66
  html: locationMarkerHtml(props.icon ?? "mdi-map-marker", getColors(props.color).base, props.label),
89
67
  iconSize: [size, size],
90
68
  className: props.selected ? 'fs-map-marker fs-map-location fs-map-location-selected' : 'fs-map-marker fs-map-location',
91
69
  iconAnchor: [size / 2, size / 2],
92
70
  });
93
- } else {
94
- const size = 16;
95
- icon = divIcon({
96
- html: pinMarkerHtml(getColors(props.color).base, props.label),
97
- iconSize: [size, size],
98
- className: props.selected ? 'fs-map-marker fs-map-pin fs-map-pin-selected' : 'fs-map-marker fs-map-pin',
99
- iconAnchor: [size / 2, size / 2],
100
- });
101
71
  }
102
-
103
- lastMarker.value = marker(props.latlng, { icon });
104
- lastMarker.value.on('click', (e) => {
105
- emit('click', e);
72
+
73
+ const size = 16;
74
+ return divIcon({
75
+ html: pinMarkerHtml(getColors(props.color).base, props.label),
76
+ iconSize: [size, size],
77
+ className: props.selected ? 'fs-map-marker fs-map-pin fs-map-pin-selected' : 'fs-map-marker fs-map-pin',
78
+ iconAnchor: [size / 2, size / 2],
106
79
  });
80
+ }
81
+
82
+ const actualMarker = ref(marker(props.latlng ?? [0, 0], { icon: getMarkerIcon() }));
83
+
84
+ if(!map) {
85
+ throw new Error('FSMapTileLayer must be used inside a FSMap component');
86
+ }
87
+
88
+ if(!map.value) {
89
+ throw new Error('FSMapTileLayer must be used inside a FSMap component with a map');
90
+ }
91
+
92
+ watch(map, () => {
93
+ if(!map.value) {
94
+ return;
95
+ }
107
96
 
108
97
  if(markerClusterGroup && markerClusterGroup.value) {
109
- lastMarker.value.addTo(markerClusterGroup.value);
98
+ actualMarker.value.addTo(markerClusterGroup.value);
110
99
  } else {
111
- lastMarker.value.addTo(map.value);
100
+ actualMarker.value.addTo(map.value);
112
101
  }
113
- };
102
+ }, { immediate: true });
103
+
104
+ watch([() => props.variant, () => props.color, () => props.selected], () => {
105
+ if(!actualMarker.value || !map.value) {
106
+ return;
107
+ }
108
+
109
+ const icon = getMarkerIcon();
110
+ actualMarker.value?.setIcon(icon);
111
+ });
112
+
113
+ watch([() => props.latlng?.lat, () => props.latlng?.lng], () => {
114
+ if(!actualMarker.value || !map.value || !props.latlng) {
115
+ return;
116
+ }
117
+
118
+ actualMarker.value.setLatLng(props.latlng);
119
+ });
114
120
 
115
121
  onMounted(() => {
116
- updateMarker();
122
+ actualMarker.value.on('click', (event: LeafletMouseEvent) => {
123
+ emit('click', event);
124
+ });
117
125
  });
118
126
 
119
127
  onUnmounted(() => {
120
- if(lastMarker.value && map.value) {
128
+ if(actualMarker.value && map.value) {
121
129
  if(markerClusterGroup && markerClusterGroup.value) {
122
- markerClusterGroup.value.removeLayer(lastMarker.value as Marker);
130
+ markerClusterGroup.value.removeLayer(actualMarker.value as Marker);
123
131
  } else {
124
- map.value.removeLayer(lastMarker.value as Marker);
132
+ map.value.removeLayer(actualMarker.value as Marker);
125
133
  }
126
134
  }
127
- lastMarker.value = null;
128
135
  })
129
-
130
- watch([() => props.variant, () => props.color, () => props.latlng?.lat, () => props.latlng?.lng, () => props.selected],
131
- updateMarker,
132
- );
133
136
  }
134
137
  };
135
138
  </script>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dative-gpi/foundation-shared-components",
3
3
  "sideEffects": false,
4
- "version": "1.0.156-maps",
4
+ "version": "1.0.156-maps2",
5
5
  "description": "",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -10,8 +10,8 @@
10
10
  "author": "",
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
- "@dative-gpi/foundation-shared-domain": "1.0.156-maps",
14
- "@dative-gpi/foundation-shared-services": "1.0.156-maps"
13
+ "@dative-gpi/foundation-shared-domain": "1.0.156-maps2",
14
+ "@dative-gpi/foundation-shared-services": "1.0.156-maps2"
15
15
  },
16
16
  "peerDependencies": {
17
17
  "@dative-gpi/bones-ui": "^1.0.0",
@@ -35,5 +35,5 @@
35
35
  "sass": "1.71.1",
36
36
  "sass-loader": "13.3.2"
37
37
  },
38
- "gitHead": "9bb7e7b6d47153dd9b68502b342c2b81a5a960d6"
38
+ "gitHead": "e47363fc9a738d138f94cbf4948da4947d35e0df"
39
39
  }