adt-js-components 1.9.2 → 1.9.4

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 +134 -26
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adt-js-components",
3
- "version": "1.9.2",
3
+ "version": "1.9.4",
4
4
  "description": "JavaScript components for Nette framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/Map/index.js CHANGED
@@ -17,6 +17,11 @@ const markersDataMap = new WeakMap();
17
17
  const markerClusters = new WeakMap();
18
18
  const onSelectionChangeMap = new WeakMap();
19
19
 
20
+ const DEPOT_TYPE = {
21
+ START: 'depot-start',
22
+ END: 'depot-end'
23
+ };
24
+
20
25
  async function run(options) {
21
26
  siteKey = options.siteKey;
22
27
  markerImg = options.markerImg;
@@ -41,6 +46,7 @@ async function run(options) {
41
46
  const routeSettings = {
42
47
  ...defaultRouteSettings,
43
48
  ...route,
49
+ customMarkers: customMarkers,
44
50
  };
45
51
 
46
52
  if (mapInstances.has(el)) {
@@ -575,6 +581,16 @@ async function calculateRoute(map) {
575
581
  return;
576
582
  }
577
583
 
584
+ const hasCustomStart = routeSettings.startPoint !== null && routeSettings.startPoint !== undefined;
585
+ const hasCustomEnd = routeSettings.endPoint !== null && routeSettings.endPoint !== undefined;
586
+
587
+ if (hasCustomStart) {
588
+ addDepotMarker(map, routeSettings.startPoint, DEPOT_TYPE.START);
589
+ }
590
+ if (hasCustomEnd) {
591
+ addDepotMarker(map, routeSettings.endPoint, DEPOT_TYPE.END);
592
+ }
593
+
578
594
  if (selectedSet && selectedSet.size > 0) {
579
595
  const orderedIds = Array.from(order.entries())
580
596
  .sort((a, b) => a[1] - b[1])
@@ -587,39 +603,131 @@ async function calculateRoute(map) {
587
603
  return;
588
604
  }
589
605
 
590
- const params = new URLSearchParams({
591
- start: `${routeMarkers[0].position.lon},${routeMarkers[0].position.lat}`,
592
- end: `${routeMarkers[routeMarkers.length-1].position.lon},${routeMarkers[routeMarkers.length-1].position.lat}`,
593
- routeType: routeSettings.routeType,
594
- apikey: siteKey
595
- });
606
+ let startPoint = routeMarkers[0].position;
607
+ if (hasCustomStart) {
608
+ startPoint = routeSettings.startPoint;
609
+ }
596
610
 
597
- routeMarkers.slice(1, -1).forEach(m => {
598
- params.append('waypoints', `${m.position.lon},${m.position.lat}`);
599
- });
611
+ let endPoint = routeMarkers[routeMarkers.length - 1].position;
612
+ if (hasCustomEnd) {
613
+ endPoint = routeSettings.endPoint;
614
+ }
615
+
616
+ // API mapy.cz povoluje max 15 waypointů, viz https://developer.mapy.com/cs/rest-api/funkce/planovani/
617
+ const WAYPOINTS_LIMIT = 15;
618
+ const allCoords = [];
619
+
620
+ if (routeMarkers.length <= WAYPOINTS_LIMIT) {
621
+ // Pokud je markerů méně nebo rovno 15, použijeme původní logiku
622
+ const params = new URLSearchParams({
623
+ start: `${startPoint.lon},${startPoint.lat}`,
624
+ end: `${endPoint.lon},${endPoint.lat}`,
625
+ routeType: routeSettings.routeType,
626
+ apikey: siteKey
627
+ });
628
+
629
+ routeMarkers.forEach(m => {
630
+ params.append('waypoints', `${m.position.lon},${m.position.lat}`);
631
+ });
632
+
633
+ try {
634
+ const response = await fetch(
635
+ `https://api.mapy.cz/v1/routing/route?${params.toString()}`
636
+ );
637
+ const data = await response.json();
638
+
639
+ if (data.geometry?.geometry?.coordinates) {
640
+ const coords = data.geometry.geometry.coordinates.map(c => [c[1], c[0]]);
641
+ allCoords.push(...coords);
642
+ }
643
+ } catch (error) {
644
+ console.error('Error calculating route:', error);
645
+ return;
646
+ }
647
+ } else {
648
+ const chunks = [];
649
+ for (let i = 0; i < routeMarkers.length; i += WAYPOINTS_LIMIT) {
650
+ chunks.push(routeMarkers.slice(i, i + WAYPOINTS_LIMIT));
651
+ }
652
+
653
+ for (let i = 0; i < chunks.length; i++) {
654
+ const chunk = chunks[i];
655
+ const isFirstChunk = i === 0;
656
+ const isLastChunk = i === chunks.length - 1;
600
657
 
601
- try {
602
- const response = await fetch(
603
- `https://api.mapy.cz/v1/routing/route?${params.toString()}`
604
- );
605
- const data = await response.json();
606
-
607
- if (data.geometry?.geometry?.coordinates) {
608
- const coords = data.geometry.geometry.coordinates.map(c => [c[1], c[0]]);
609
- const polyline = L.polyline(coords, {
610
- color: routeSettings.color,
611
- weight: routeSettings.weight,
612
- opacity: routeSettings.opacity
613
- }).addTo(map);
614
-
615
- routePolylines.set(map, polyline);
658
+ let chunkStart, chunkEnd;
659
+
660
+ if (isFirstChunk) {
661
+ chunkStart = startPoint; // Použije custom start nebo první marker
662
+ chunkEnd = isLastChunk ? endPoint : chunk[chunk.length - 1].position;
663
+ } else if (isLastChunk) {
664
+ chunkStart = chunks[i - 1][chunks[i - 1].length - 1].position;
665
+ chunkEnd = endPoint; // Použije custom end nebo poslední marker
666
+ } else {
667
+ chunkStart = chunks[i - 1][chunks[i - 1].length - 1].position;
668
+ chunkEnd = chunk[chunk.length - 1].position;
669
+ }
670
+
671
+ const params = new URLSearchParams({
672
+ start: `${chunkStart.lon},${chunkStart.lat}`,
673
+ end: `${chunkEnd.lon},${chunkEnd.lat}`,
674
+ routeType: routeSettings.routeType,
675
+ apikey: siteKey
676
+ });
677
+
678
+ const markersToAdd = isFirstChunk ? chunk : chunk.slice(1);
679
+
680
+ markersToAdd.forEach(m => {
681
+ params.append('waypoints', `${m.position.lon},${m.position.lat}`);
682
+ });
683
+
684
+ try {
685
+ const response = await fetch(
686
+ `https://api.mapy.cz/v1/routing/route?${params.toString()}`
687
+ );
688
+ const data = await response.json();
689
+
690
+ if (data.geometry?.geometry?.coordinates) {
691
+ const coords = data.geometry.geometry.coordinates.map(c => [c[1], c[0]]);
692
+ allCoords.push(...coords);
693
+ }
694
+ } catch (error) {
695
+ console.error(`Error calculating route chunk ${i + 1}:`, error);
696
+ return;
697
+ }
616
698
  }
617
- } catch (error) {
618
- console.error('Error calculating route:', error);
699
+ }
700
+
701
+ if (allCoords.length > 0) {
702
+ const polyline = L.polyline(allCoords, {
703
+ color: routeSettings.color,
704
+ weight: routeSettings.weight,
705
+ opacity: routeSettings.opacity
706
+ }).addTo(map);
707
+
708
+ routePolylines.set(map, polyline);
619
709
  }
620
710
  }
621
711
  }
622
712
 
713
+ function addDepotMarker(map, position, depotType) {
714
+ const routeSettings = routeSettingsMap.get(map);
715
+ const customMarkers = routeSettings.customMarkers || {};
716
+ const iconUrl = customMarkers.depot || markerImg;
717
+
718
+ const img = new Image();
719
+ img.src = iconUrl;
720
+ img.onload = function () {
721
+ const icon = L.icon({
722
+ iconUrl: iconUrl,
723
+ iconSize: [img.width, img.height],
724
+ iconAnchor: [img.width / 2, img.height]
725
+ });
726
+
727
+ L.marker(position, { icon: icon }).addTo(map);
728
+ };
729
+ }
730
+
623
731
  function getSelectedMarkers(mapElement) {
624
732
  const map = mapInstances.get(mapElement);
625
733
  if (!map) return [];