adt-js-components 1.10.6 → 1.10.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Map/index.js +45 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adt-js-components",
3
- "version": "1.10.6",
3
+ "version": "1.10.8",
4
4
  "description": "JavaScript components for Nette framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/Map/index.js CHANGED
@@ -10,6 +10,7 @@ let siteKey;
10
10
  let markerImg;
11
11
  let mapProvider;
12
12
  const svgCache = new Map();
13
+ const mapInstancesById = new Map();
13
14
  const mapInstances = new WeakMap();
14
15
  const selectedMarkers = new WeakMap();
15
16
  const selectionOrder = new WeakMap();
@@ -58,6 +59,7 @@ async function run(options) {
58
59
  disableClickZoom = false
59
60
  } = JSON.parse(el.dataset.adtMap);
60
61
 
62
+ /** @type {RouteSetting} */
61
63
  const routeSettings = {
62
64
  ...defaultRouteSettings,
63
65
  ...route,
@@ -65,13 +67,20 @@ async function run(options) {
65
67
  mapProvider,
66
68
  };
67
69
 
68
- if (mapInstances.has(el)) {
69
- mapInstances.get(el).remove();
70
- mapInstances.delete(el);
70
+ const elId = el.id;
71
+ if (elId && mapInstancesById.has(elId)) {
72
+ const oldMap = mapInstancesById.get(elId);
73
+ oldMap.remove();
74
+ mapInstancesById.delete(elId);
71
75
  }
72
76
 
73
77
  const map = L.map(el);
74
78
  mapInstances.set(el, map);
79
+
80
+ if (elId) {
81
+ mapInstancesById.set(elId, map);
82
+ }
83
+
75
84
  selectedMarkers.set(map, new Set());
76
85
  selectionOrder.set(map, new Map());
77
86
  markerInstances.set(map, new Map());
@@ -204,9 +213,21 @@ async function run(options) {
204
213
  mutation.addedNodes.forEach(node => {
205
214
  if (node.nodeType === 1) {
206
215
  if (node.hasAttribute("data-adt-map")) {
216
+ const oldMapElement = document.querySelector('[data-adt-map]');
217
+ if (oldMapElement && oldMapElement !== node && mapInstances.has(oldMapElement)) {
218
+ mapInstances.get(oldMapElement).remove();
219
+ mapInstances.delete(oldMapElement);
220
+ }
207
221
  applyEventHandlers(node);
208
222
  }
209
- node.querySelectorAll?.('[data-adt-map]').forEach(el => applyEventHandlers(el));
223
+ node.querySelectorAll?.('[data-adt-map]').forEach(el => {
224
+ const oldMapElement = document.querySelector('[data-adt-map]');
225
+ if (oldMapElement && oldMapElement !== el && mapInstances.has(oldMapElement)) {
226
+ mapInstances.get(oldMapElement).remove();
227
+ mapInstances.delete(oldMapElement);
228
+ }
229
+ applyEventHandlers(el);
230
+ });
210
231
  }
211
232
  });
212
233
  }
@@ -354,7 +375,11 @@ function addMarkers(map, markers, options, selectedOptions, position, selectable
354
375
  }
355
376
 
356
377
  if (!position.length && positionsOfMarkers.length) {
357
- map.fitBounds(positionsOfMarkers);
378
+ const rs = routeSettingsMap.get(map);
379
+ const allBounds = [...positionsOfMarkers];
380
+ if (rs?.startPoint) allBounds.push([rs.startPoint.lat, rs.startPoint.lon]);
381
+ if (rs?.endPoint) allBounds.push([rs.endPoint.lat, rs.endPoint.lon]);
382
+ map.fitBounds(allBounds);
358
383
  }
359
384
  map.addLayer(cluster);
360
385
 
@@ -611,7 +636,7 @@ async function calculateRoute(map, invalidateCache = false) {
611
636
  }
612
637
 
613
638
  if (oldPolyline) {
614
- map.removeLayer(oldPolyline);
639
+ oldPolyline.remove();
615
640
  routePolylines.set(map, null);
616
641
  }
617
642
 
@@ -632,11 +657,12 @@ async function calculateRoute(map, invalidateCache = false) {
632
657
  return;
633
658
  }
634
659
 
660
+ cachedRouteLoaded.set(map, true);
661
+
635
662
  try {
636
663
  const response = await fetch(routeSettings.cachedRouteUrl);
637
664
  console.log('Loading cached route from:', routeSettings.cachedRouteUrl);
638
665
  if (response.ok) {
639
- cachedRouteLoaded.set(map, true);
640
666
  const coords = await response.json();
641
667
 
642
668
  if (coords && coords.length > 0) {
@@ -653,7 +679,10 @@ async function calculateRoute(map, invalidateCache = false) {
653
679
  return;
654
680
  }
655
681
  }
682
+
683
+ cachedRouteLoaded.set(map, false);
656
684
  } catch (e) {
685
+ cachedRouteLoaded.set(map, false);
657
686
  console.error('Error loading cached route:', e);
658
687
  }
659
688
  }
@@ -819,14 +848,14 @@ function addDepotMarker(map, position, depotType) {
819
848
  }
820
849
 
821
850
  function getSelectedMarkers(mapElement) {
822
- const map = mapInstances.get(mapElement);
851
+ const map = mapInstancesById.get(mapElement.id) ?? mapInstances.get(mapElement);
823
852
  if (!map) return [];
824
853
  const order = selectionOrder.get(map);
825
854
  return Array.from(order.entries()).sort((a, b) => a[1] - b[1]).map(e => e[0]);
826
855
  }
827
856
 
828
857
  function clearSelection(mapElement) {
829
- const map = mapInstances.get(mapElement);
858
+ const map = mapInstancesById.get(mapElement.id) ?? mapInstances.get(mapElement);
830
859
  if (!map) return;
831
860
  const selected = selectedMarkers.get(map);
832
861
  const order = selectionOrder.get(map);
@@ -839,7 +868,7 @@ function clearSelection(mapElement) {
839
868
  }
840
869
 
841
870
  async function toggleMarker(mapElement, markerId, selected, recalculate = true) {
842
- const map = mapInstances.get(mapElement);
871
+ const map = mapInstancesById.get(mapElement.id) ?? mapInstances.get(mapElement);
843
872
  const markers = markerInstances.get(map);
844
873
  const marker = markers?.get(markerId);
845
874
  if (!marker) return;
@@ -868,7 +897,7 @@ async function toggleMarker(mapElement, markerId, selected, recalculate = true)
868
897
 
869
898
  function setOrder(mapElement, markerId, orderNumber) {
870
899
  let color = null;
871
- const map = mapInstances.get(mapElement);
900
+ const map = mapInstancesById.get(mapElement.id) ?? mapInstances.get(mapElement);
872
901
  const settings = $(mapElement).data('adt-map');
873
902
  const markers = markerInstances.get(map);
874
903
  const marker = markers?.get(markerId);
@@ -880,19 +909,19 @@ function setOrder(mapElement, markerId, orderNumber) {
880
909
  }
881
910
 
882
911
  function onBeforeRouteCalculation(mapElement, callbackName) {
883
- const map = mapInstances.get(mapElement);
912
+ const map = mapInstancesById.get(mapElement.id) ?? mapInstances.get(mapElement);
884
913
  if (!map) return;
885
914
  onBeforeRouteCalculationMap.set(map, callbackName);
886
915
  }
887
916
 
888
917
  function onAfterRouteCalculation(mapElement, callbackName) {
889
- const map = mapInstances.get(mapElement);
918
+ const map = mapInstancesById.get(mapElement.id) ?? mapInstances.get(mapElement);
890
919
  if (!map) return;
891
920
  onAfterRouteCalculationMap.set(map, callbackName);
892
921
  }
893
922
 
894
923
  function recalculateRoute(mapElement) {
895
- const map = mapInstances.get(mapElement);
924
+ const map = mapInstancesById.get(mapElement.id) ?? mapInstances.get(mapElement);
896
925
 
897
926
  if (!map) return;
898
927
 
@@ -900,7 +929,7 @@ function recalculateRoute(mapElement) {
900
929
  }
901
930
 
902
931
  function removeMarker(mapElement, markerId, recalculate = true) {
903
- const map = mapInstances.get(mapElement);
932
+ const map = mapInstancesById.get(mapElement.id) ?? mapInstances.get(mapElement);
904
933
  if (!map) return;
905
934
 
906
935
  const markers = markerInstances.get(map);
@@ -936,7 +965,7 @@ function removeMarker(mapElement, markerId, recalculate = true) {
936
965
  }
937
966
 
938
967
  function addMarkerAndSelect(mapElement, markerData, recalculate = true) {
939
- const map = mapInstances.get(mapElement);
968
+ const map = mapInstancesById.get(mapElement.id) ?? mapInstances.get(mapElement);
940
969
 
941
970
  if (!map) return;
942
971