adt-js-components 1.10.0 → 1.10.1

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 +67 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adt-js-components",
3
- "version": "1.10.0",
3
+ "version": "1.10.1",
4
4
  "description": "JavaScript components for Nette framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/Map/index.js CHANGED
@@ -139,6 +139,7 @@ async function run(options) {
139
139
  enableRectangleSelection(map, onSelectionChange, showSelectionOrder);
140
140
  }
141
141
 
142
+ /** @type {RouteSetting} */
142
143
  const markerOptions = {};
143
144
  const customMarkerOptions = {};
144
145
  const normalImg = customMarkers.normal || markerImg;
@@ -346,11 +347,25 @@ function addMarkers(map, markers, options, selectedOptions, position, selectable
346
347
  }
347
348
 
348
349
  function checkAndApplyPreselection(map, markers, showSelectionOrder, onSelectionChange, selectable) {
349
- if (selectable && showSelectionOrder) {
350
+ if (showSelectionOrder) {
350
351
  applyPreselectedMarkers(map, markers, showSelectionOrder, onSelectionChange);
352
+ } else if (!selectable) {
353
+ applyPreselectedMarkersVisualOnly(map, markers);
351
354
  }
352
355
  }
353
356
 
357
+ function applyPreselectedMarkersVisualOnly(map, markersData) {
358
+ const markerMap = markerInstances.get(map);
359
+ const preselected = markersData.filter(m => m.selected === true);
360
+
361
+ preselected.forEach((markerData) => {
362
+ const markerInstance = markerMap.get(markerData.id);
363
+ if (markerInstance && markerInstance._selectedIcon) {
364
+ markerInstance.setIcon(markerInstance._selectedIcon);
365
+ }
366
+ });
367
+ }
368
+
354
369
  function createMarker(marker, options, selectedOptions, cluster = null, selectable = false, onSelectionChange = null, map = null, showSelectionOrder = false, markerInfoCallback = null) {
355
370
  const mapMarker = L.marker(marker.position, {...options, id: marker.id});
356
371
  mapMarker._normalIcon = options.icon;
@@ -426,6 +441,11 @@ function createMarker(marker, options, selectedOptions, cluster = null, selectab
426
441
  async function selectMarker(marker, map, showSelectionOrder) {
427
442
  const selected = selectedMarkers.get(map);
428
443
  const order = selectionOrder.get(map);
444
+
445
+ if (selected.has(marker.options.id)) {
446
+ return;
447
+ }
448
+
429
449
  selected.add(marker.options.id);
430
450
  const maxOrder = order.size > 0 ? Math.max(...order.values()) : 0;
431
451
  const newOrder = maxOrder + 1;
@@ -512,21 +532,21 @@ async function createMarkerIcon(map, iconUrl, orderNumber, color = null) {
512
532
  });
513
533
  }
514
534
 
515
- function applyPreselectedMarkers(map, markersData, showSelectionOrder, onSelectionChange) {
535
+ async function applyPreselectedMarkers(map, markersData, showSelectionOrder, onSelectionChange) {
516
536
  const markerMap = markerInstances.get(map);
517
537
  const order = selectionOrder.get(map);
518
538
  const preselected = markersData
519
539
  .filter(m => m.selected === true)
520
540
  .sort((a, b) => (a.selectionOrder || 0) - (b.selectionOrder || 0));
521
541
 
522
- preselected.forEach((markerData) => {
542
+ for (const markerData of preselected) {
523
543
  const markerInstance = markerMap.get(markerData.id);
524
544
  if (markerInstance) {
525
- selectMarker(markerInstance, map, showSelectionOrder);
545
+ await selectMarker(markerInstance, map, showSelectionOrder);
526
546
  } else {
527
547
  console.warn('Marker instance NOT found for ID:', markerData.id);
528
548
  }
529
- });
549
+ }
530
550
 
531
551
  if (onSelectionChange && window[onSelectionChange] && order.size > 0) {
532
552
  const orderedIds = Array.from(order.entries()).sort((a, b) => a[1] - b[1]).map(e => e[0]);
@@ -534,7 +554,8 @@ function applyPreselectedMarkers(map, markersData, showSelectionOrder, onSelecti
534
554
  }
535
555
 
536
556
  const settings = routeSettingsMap.get(map);
537
- if (settings && settings.enabled && order.size >= 2) {
557
+ const hasDepot = settings?.startPoint !== null && settings?.startPoint !== undefined || settings?.endPoint !== null && settings?.endPoint !== undefined;
558
+ if (settings && settings.enabled && order.size >= (hasDepot ? 1 : 2)) {
538
559
  calculateRoute(map);
539
560
  }
540
561
  }
@@ -568,7 +589,7 @@ async function calculateRoute(map) {
568
589
  const orderedIds = Array.from(order.entries()).sort((a, b) => a[1] - b[1]).map(e => e[0]);
569
590
  const routeMarkers = orderedIds.map(id => markers.find(m => m.id === id)).filter(m => m !== undefined);
570
591
 
571
- if (routeMarkers.length < 2) return;
592
+ if (routeMarkers.length < (hasCustomStart || hasCustomEnd ? 1 : 2)) return;
572
593
 
573
594
  const beforeCallback = onBeforeRouteCalculationMap.get(map);
574
595
  if (beforeCallback && window[beforeCallback]) {
@@ -586,7 +607,7 @@ async function calculateRoute(map) {
586
607
  if (isHere) {
587
608
  await calculateRouteHere({ routeMarkers, startPoint, endPoint, routeSettings, allCoords, allParts });
588
609
  } else {
589
- await calculateRouteMapy({ routeMarkers, startPoint, endPoint, routeSettings, allCoords, allParts });
610
+ await calculateRouteMapyCz({ routeMarkers, startPoint, endPoint, routeSettings, allCoords, allParts });
590
611
  }
591
612
  } catch (error) {
592
613
  console.error('Error calculating route:', error);
@@ -604,6 +625,7 @@ async function calculateRoute(map) {
604
625
  opacity: routeSettings.opacity
605
626
  }).addTo(map);
606
627
  routePolylines.set(map, polyline);
628
+ map.fitBounds(polyline.getBounds(), { padding: [32, 32] });
607
629
  }
608
630
 
609
631
  const totalDuration = allParts.reduce((s, p) => s + (p.duration || 0), 0);
@@ -622,7 +644,7 @@ async function calculateRoute(map) {
622
644
  }
623
645
  }
624
646
 
625
- async function calculateRouteMapy({ routeMarkers, startPoint, endPoint, routeSettings, allCoords, allParts }) {
647
+ async function calculateRouteMapyCz({ routeMarkers, startPoint, endPoint, routeSettings, allCoords, allParts }) {
626
648
  const WAYPOINTS_LIMIT = 15;
627
649
 
628
650
  const chunks = [];
@@ -797,6 +819,41 @@ function recalculateRoute(mapElement) {
797
819
  calculateRoute(map);
798
820
  }
799
821
 
822
+ function removeMarker(mapElement, markerId) {
823
+ const map = mapInstances.get(mapElement);
824
+ if (!map) return;
825
+
826
+ const markers = markerInstances.get(map);
827
+ const marker = markers?.get(markerId);
828
+ if (!marker) return;
829
+
830
+ const cluster = markerClusters.get(map);
831
+ if (cluster) {
832
+ cluster.removeLayer(marker);
833
+ } else {
834
+ map.removeLayer(marker);
835
+ }
836
+
837
+ markers.delete(markerId);
838
+
839
+ const selected = selectedMarkers.get(map);
840
+ const order = selectionOrder.get(map);
841
+ if (selected.has(markerId)) {
842
+ const removedOrder = order.get(markerId);
843
+ selected.delete(markerId);
844
+ order.delete(markerId);
845
+ order.forEach((value, key) => { if (value > removedOrder) order.set(key, value - 1); });
846
+ }
847
+
848
+ const markersData = markersDataMap.get(map);
849
+ const idx = markersData.findIndex(m => m.id === markerId);
850
+ if (idx !== -1) markersData.splice(idx, 1);
851
+
852
+ const settings = routeSettingsMap.get(map);
853
+ if (settings?.enabled) {
854
+ calculateRoute(map);
855
+ }
856
+ }
800
857
 
801
858
  function addMarkerAndSelect(mapElement, markerData) {
802
859
  const map = mapInstances.get(mapElement);
@@ -877,4 +934,5 @@ export default {
877
934
  onAfterRouteCalculation,
878
935
  recalculateRoute,
879
936
  addMarkerAndSelect,
937
+ removeMarker,
880
938
  }