maplibre-gl-js-amplify 1.3.0 → 1.4.0

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 (75) hide show
  1. package/dist/maplibre-gl-js-amplify.umd.js +1128 -59
  2. package/dist/maplibre-gl-js-amplify.umd.js.map +1 -1
  3. package/dist/maplibre-gl-js-amplify.umd.min.js +1 -1
  4. package/dist/maplibre-gl-js-amplify.umd.min.js.gz +0 -0
  5. package/dist/maplibre-gl-js-amplify.umd.min.js.map +1 -1
  6. package/dist/public/amplify-ctrl-geofence.css +439 -0
  7. package/lib/cjs/AmplifyGeofenceControl/AmplifyMapDraw.d.ts +30 -0
  8. package/lib/cjs/AmplifyGeofenceControl/AmplifyMapDraw.js +131 -0
  9. package/lib/cjs/AmplifyGeofenceControl/AmplifyMapDraw.js.map +1 -0
  10. package/lib/cjs/AmplifyGeofenceControl/icons.d.ts +9 -0
  11. package/lib/cjs/AmplifyGeofenceControl/icons.js +174 -0
  12. package/lib/cjs/AmplifyGeofenceControl/icons.js.map +1 -0
  13. package/lib/cjs/AmplifyGeofenceControl/index.d.ts +56 -0
  14. package/lib/cjs/AmplifyGeofenceControl/index.js +287 -0
  15. package/lib/cjs/AmplifyGeofenceControl/index.js.map +1 -0
  16. package/lib/cjs/AmplifyGeofenceControl/ui.d.ts +18 -0
  17. package/lib/cjs/AmplifyGeofenceControl/ui.js +397 -0
  18. package/lib/cjs/AmplifyGeofenceControl/ui.js.map +1 -0
  19. package/lib/cjs/AmplifyMapLibreGeocoder.d.ts +0 -3
  20. package/lib/cjs/AmplifyMapLibreGeocoder.js +0 -16
  21. package/lib/cjs/AmplifyMapLibreGeocoder.js.map +1 -1
  22. package/lib/cjs/constants.d.ts +4 -0
  23. package/lib/cjs/constants.js +5 -1
  24. package/lib/cjs/constants.js.map +1 -1
  25. package/lib/cjs/drawGeofences.d.ts +4 -0
  26. package/lib/cjs/drawGeofences.js +35 -33
  27. package/lib/cjs/drawGeofences.js.map +1 -1
  28. package/lib/cjs/geofenceUtils.d.ts +14 -0
  29. package/lib/cjs/geofenceUtils.js +117 -0
  30. package/lib/cjs/geofenceUtils.js.map +1 -0
  31. package/lib/cjs/index.d.ts +2 -1
  32. package/lib/cjs/index.js +3 -1
  33. package/lib/cjs/index.js.map +1 -1
  34. package/lib/cjs/public/amplify-ctrl-geofence.css +439 -0
  35. package/lib/cjs/public/amplify-geocoder.css +22 -0
  36. package/lib/cjs/public/amplify-map.css +15 -0
  37. package/lib/cjs/types.d.ts +3 -3
  38. package/lib/cjs/utils.d.ts +6 -1
  39. package/lib/cjs/utils.js +42 -2
  40. package/lib/cjs/utils.js.map +1 -1
  41. package/lib/esm/AmplifyGeofenceControl/AmplifyMapDraw.d.ts +30 -0
  42. package/lib/esm/AmplifyGeofenceControl/AmplifyMapDraw.js +124 -0
  43. package/lib/esm/AmplifyGeofenceControl/AmplifyMapDraw.js.map +1 -0
  44. package/lib/esm/AmplifyGeofenceControl/icons.d.ts +9 -0
  45. package/lib/esm/AmplifyGeofenceControl/icons.js +162 -0
  46. package/lib/esm/AmplifyGeofenceControl/icons.js.map +1 -0
  47. package/lib/esm/AmplifyGeofenceControl/index.d.ts +56 -0
  48. package/lib/esm/AmplifyGeofenceControl/index.js +280 -0
  49. package/lib/esm/AmplifyGeofenceControl/index.js.map +1 -0
  50. package/lib/esm/AmplifyGeofenceControl/ui.d.ts +18 -0
  51. package/lib/esm/AmplifyGeofenceControl/ui.js +393 -0
  52. package/lib/esm/AmplifyGeofenceControl/ui.js.map +1 -0
  53. package/lib/esm/AmplifyMapLibreGeocoder.d.ts +0 -3
  54. package/lib/esm/AmplifyMapLibreGeocoder.js +0 -16
  55. package/lib/esm/AmplifyMapLibreGeocoder.js.map +1 -1
  56. package/lib/esm/constants.d.ts +4 -0
  57. package/lib/esm/constants.js +4 -0
  58. package/lib/esm/constants.js.map +1 -1
  59. package/lib/esm/drawGeofences.d.ts +4 -0
  60. package/lib/esm/drawGeofences.js +35 -33
  61. package/lib/esm/drawGeofences.js.map +1 -1
  62. package/lib/esm/geofenceUtils.d.ts +14 -0
  63. package/lib/esm/geofenceUtils.js +103 -0
  64. package/lib/esm/geofenceUtils.js.map +1 -0
  65. package/lib/esm/index.d.ts +2 -1
  66. package/lib/esm/index.js +2 -1
  67. package/lib/esm/index.js.map +1 -1
  68. package/lib/esm/public/amplify-ctrl-geofence.css +439 -0
  69. package/lib/esm/public/amplify-geocoder.css +22 -0
  70. package/lib/esm/public/amplify-map.css +15 -0
  71. package/lib/esm/types.d.ts +3 -3
  72. package/lib/esm/utils.d.ts +6 -1
  73. package/lib/esm/utils.js +36 -1
  74. package/lib/esm/utils.js.map +1 -1
  75. package/package.json +24 -8
@@ -1,13 +1,17 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@aws-amplify/core'), require('@aws-amplify/geo'), require('maplibre-gl'), require('@maplibre/maplibre-gl-geocoder')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@aws-amplify/core', '@aws-amplify/geo', 'maplibre-gl', '@maplibre/maplibre-gl-geocoder'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.AmplifyMapLibre = {}, global.aws_amplify_core, global.aws_amplify_geo, global.maplibregl, global.MaplibreGeocoder));
5
- })(this, (function (exports, core, geo, maplibregl, MaplibreGeocoder) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@aws-amplify/core'), require('@aws-amplify/geo'), require('maplibre-gl'), require('@maplibre/maplibre-gl-geocoder'), require('@turf/along'), require('@turf/circle'), require('@turf/length'), require('@turf/helpers'), require('debounce'), require('@mapbox/mapbox-gl-draw'), require('maplibre-gl-draw-circle')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@aws-amplify/core', '@aws-amplify/geo', 'maplibre-gl', '@maplibre/maplibre-gl-geocoder', '@turf/along', '@turf/circle', '@turf/length', '@turf/helpers', 'debounce', '@mapbox/mapbox-gl-draw', 'maplibre-gl-draw-circle'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.AmplifyMapLibre = {}, global.aws_amplify_core, global.aws_amplify_geo, global.maplibregl, global.MaplibreGeocoder, global.along, global.circle, global.length, global.helpers, global.debounce, global.MapboxDraw, global.maplibreGlDrawCircle));
5
+ })(this, (function (exports, core, geo, maplibregl, MaplibreGeocoder, along, circle, length, helpers, debounce, MapboxDraw, maplibreGlDrawCircle) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
9
9
  var maplibregl__default = /*#__PURE__*/_interopDefaultLegacy(maplibregl);
10
10
  var MaplibreGeocoder__default = /*#__PURE__*/_interopDefaultLegacy(MaplibreGeocoder);
11
+ var along__default = /*#__PURE__*/_interopDefaultLegacy(along);
12
+ var circle__default = /*#__PURE__*/_interopDefaultLegacy(circle);
13
+ var length__default = /*#__PURE__*/_interopDefaultLegacy(length);
14
+ var MapboxDraw__default = /*#__PURE__*/_interopDefaultLegacy(MapboxDraw);
11
15
 
12
16
  function isCoordinates(array) {
13
17
  return (Array.isArray(array) &&
@@ -28,9 +32,18 @@
28
32
  }
29
33
  function isGeofence(object) {
30
34
  return (object &&
31
- typeof object.id === "string" &&
35
+ typeof object.geofenceId === "string" &&
32
36
  typeof object.geometry === "object");
33
37
  }
38
+ function isGeofenceArray(array) {
39
+ return Array.isArray(array) && isGeofence(array[0]);
40
+ }
41
+ function isPolygon(object) {
42
+ return Array.isArray(object) && isCoordinatesArray(object[0]);
43
+ }
44
+ function isPolygonArray(array) {
45
+ return Array.isArray(array) && isPolygon(array[0]);
46
+ }
34
47
  function isGeoJsonSource(source) {
35
48
  return source.type === "geojson";
36
49
  }
@@ -63,8 +76,37 @@
63
76
  const urlEncodePeriods = (str) => {
64
77
  return str.replace(/\./g, "%2E");
65
78
  };
79
+ function validateCoordinates(coordinates) {
80
+ const [lng, lat] = coordinates;
81
+ if (!Number.isFinite(lng) || !Number.isFinite(lat)) {
82
+ throw new Error(`Invalid coordinates: [${lng},${lat}]`);
83
+ }
84
+ if (lat < -90 || lat > 90) {
85
+ const errorString = "Latitude must be between -90 and 90 degrees inclusive.";
86
+ console.warn(errorString);
87
+ throw new Error(errorString);
88
+ }
89
+ else if (lng < -180 || lng > 180) {
90
+ const errorString = "Longitude must be between -180 and 180 degrees inclusive.";
91
+ console.warn(errorString);
92
+ throw new Error(errorString);
93
+ }
94
+ }
95
+ function createElement(tagName, className, container) {
96
+ const el = window.document.createElement(tagName);
97
+ if (className !== undefined)
98
+ el.className = className;
99
+ if (container)
100
+ container.appendChild(el);
101
+ return el;
102
+ }
103
+ function removeElement(node) {
104
+ if (node.parentNode) {
105
+ node.parentNode.removeChild(node);
106
+ }
107
+ }
66
108
 
67
- var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
109
+ var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
68
110
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
69
111
  return new (P || (P = Promise))(function (resolve, reject) {
70
112
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -94,7 +136,7 @@
94
136
  */
95
137
  class AmplifyMapLibreRequest {
96
138
  constructor(currentCredentials, region) {
97
- this.refreshCredentials = () => __awaiter$1(this, void 0, void 0, function* () {
139
+ this.refreshCredentials = () => __awaiter$3(this, void 0, void 0, function* () {
98
140
  try {
99
141
  this.credentials = yield core.Amplify.Auth.currentCredentials();
100
142
  }
@@ -103,7 +145,7 @@
103
145
  throw e;
104
146
  }
105
147
  });
106
- this.refreshCredentialsWithRetry = () => __awaiter$1(this, void 0, void 0, function* () {
148
+ this.refreshCredentialsWithRetry = () => __awaiter$3(this, void 0, void 0, function* () {
107
149
  try {
108
150
  const MAX_DELAY_MS = 5 * 60 * 1000; // 5 minutes
109
151
  yield core.jitteredExponentialRetry(this.refreshCredentials, [], MAX_DELAY_MS);
@@ -158,7 +200,7 @@
158
200
  });
159
201
  }
160
202
  }
161
- AmplifyMapLibreRequest.createMapLibreMap = (options) => __awaiter$1(void 0, void 0, void 0, function* () {
203
+ AmplifyMapLibreRequest.createMapLibreMap = (options) => __awaiter$3(void 0, void 0, void 0, function* () {
162
204
  const { region, mapConstructor = maplibregl.Map } = options, maplibreOption = __rest$2(options, ["region", "mapConstructor"]);
163
205
  const defaultMap = geo.Geo.getDefaultMap();
164
206
  const amplifyRequest = new AmplifyMapLibreRequest(yield core.Amplify.Auth.currentCredentials(), region || defaultMap.region);
@@ -167,7 +209,7 @@
167
209
  transformRequest }));
168
210
  return map;
169
211
  });
170
- const createMap = (options) => __awaiter$1(void 0, void 0, void 0, function* () {
212
+ const createMap = (options) => __awaiter$3(void 0, void 0, void 0, function* () {
171
213
  return AmplifyMapLibreRequest.createMapLibreMap(options);
172
214
  });
173
215
 
@@ -176,6 +218,9 @@
176
218
  const MARKER_COLOR = "#5d8aff";
177
219
  const ACTIVE_MARKER_COLOR = "#ff9900";
178
220
  const POPUP_BORDER_COLOR = "#0000001f";
221
+ const GEOFENCE_COLOR = "#2194f3";
222
+ const GEOFENCE_BORDER_COLOR = "#003560";
223
+ const GEOFENCE_VERTEX_COLOR = "#FF9900";
179
224
  const LOCATION_MARKER = "M24.8133 38.533C18.76 31.493 13 28.8264 13 20.8264C13.4827 14.9864 16.552 9.67169 21.368 6.33302C33.768 -2.26165 50.824 5.78902 52.0667 20.8264C52.0667 28.613 46.5733 31.6797 40.6533 38.373C32.4933 47.5464 35.4 63.093 32.4933 63.093C29.72 63.093 32.4933 47.5464 24.8133 38.533ZM32.4933 8.23969C26.5573 8.23969 21.7467 13.0504 21.7467 18.9864C21.7467 24.9224 26.5573 29.733 32.4933 29.733C38.4293 29.733 43.24 24.9224 43.24 18.9864C43.24 13.0504 38.4293 8.23969 32.4933 8.23969Z";
180
225
  // Map styles exist due to an issue with Amazon Location Service not supporting the default set of maplibre fonts
181
226
  var MAP_STYLES;
@@ -568,7 +613,7 @@
568
613
  return customIcon;
569
614
  }
570
615
 
571
- var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
616
+ var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
572
617
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
573
618
  return new (P || (P = Promise))(function (resolve, reject) {
574
619
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -589,7 +634,7 @@
589
634
  return t;
590
635
  };
591
636
  const AmplifyGeocoderAPI = {
592
- forwardGeocode: (config) => __awaiter(void 0, void 0, void 0, function* () {
637
+ forwardGeocode: (config) => __awaiter$2(void 0, void 0, void 0, function* () {
593
638
  const features = [];
594
639
  try {
595
640
  const data = yield geo.Geo.searchByText(config.query, {
@@ -617,7 +662,7 @@
617
662
  }
618
663
  return { features };
619
664
  }),
620
- reverseGeocode: (config) => __awaiter(void 0, void 0, void 0, function* () {
665
+ reverseGeocode: (config) => __awaiter$2(void 0, void 0, void 0, function* () {
621
666
  const features = [];
622
667
  try {
623
668
  const data = yield geo.Geo.searchByCoordinates(config.query, {
@@ -640,22 +685,6 @@
640
685
  }
641
686
  return { features };
642
687
  }),
643
- getSuggestions: (config) => __awaiter(void 0, void 0, void 0, function* () {
644
- const suggestions = [];
645
- try {
646
- const response = yield geo.Geo.searchForSuggestions(config.query, {
647
- biasPosition: config.bbox ? undefined : config.proximity,
648
- searchAreaConstraints: config.bbox,
649
- countries: config.countries,
650
- maxResults: config.limit,
651
- });
652
- suggestions.push(...response);
653
- }
654
- catch (e) {
655
- console.error(`Failed to get suggestions with error: ${e}`);
656
- }
657
- return { suggestions };
658
- }),
659
688
  };
660
689
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
661
690
  function createAmplifyGeocoder(options) {
@@ -664,6 +693,101 @@
664
693
  showResultsWhileTyping: options === null || options === void 0 ? void 0 : options.autocomplete }, options));
665
694
  }
666
695
 
696
+ const GEOFENCE_ID_REGEX = /^[-._\p{L}\p{N}]+$/iu;
697
+ const getGeofenceFeatureArray = (data) => {
698
+ const coordinates = isGeofenceArray(data)
699
+ ? data.map((geofence) => geofence.geometry.polygon)
700
+ : data;
701
+ return {
702
+ type: "Feature",
703
+ geometry: {
704
+ type: "MultiPolygon",
705
+ coordinates,
706
+ },
707
+ properties: {},
708
+ };
709
+ };
710
+ const getGeofenceFeatureFromPolygon = (polygon) => {
711
+ return {
712
+ type: "Feature",
713
+ geometry: {
714
+ type: "Polygon",
715
+ coordinates: polygon,
716
+ },
717
+ properties: {},
718
+ };
719
+ };
720
+ // Measures distance between the coordinate bounds and takes two points 1/4 from each coordinate to create a polygon
721
+ const getPolygonFeatureFromBounds = (id, bounds) => {
722
+ const swCoordinate = bounds.getSouthWest().toArray();
723
+ const neCoordinate = bounds.getNorthEast().toArray();
724
+ const center = bounds.getCenter().toArray();
725
+ const line = helpers.lineString([swCoordinate, center, neCoordinate]);
726
+ const distanceInMiles = length__default["default"](line, { units: "miles" });
727
+ // Gets coordinates 1/4 along the line from each coordinate
728
+ const southWestCoordinate = along__default["default"](line, distanceInMiles / 4, {
729
+ units: "miles",
730
+ }).geometry.coordinates;
731
+ const northeastCoordinate = along__default["default"](line, distanceInMiles * (3 / 4), {
732
+ units: "miles",
733
+ }).geometry.coordinates;
734
+ // Creates a polygon from the coordinates found along the line between the bounding coordinates in counter clockwise order starting from northeast most coordinate
735
+ const polygon = [
736
+ [
737
+ northeastCoordinate,
738
+ [southWestCoordinate[0], northeastCoordinate[1]],
739
+ southWestCoordinate,
740
+ [northeastCoordinate[0], southWestCoordinate[1]],
741
+ northeastCoordinate,
742
+ ],
743
+ ];
744
+ return {
745
+ id,
746
+ type: "Feature",
747
+ geometry: {
748
+ type: "Polygon",
749
+ coordinates: polygon,
750
+ },
751
+ properties: {},
752
+ };
753
+ };
754
+ const getCircleFeatureFromCoords = (id, center, { bounds, radius }) => {
755
+ if (!bounds && !radius) {
756
+ throw new Error("Circle requires a bounds or a radius");
757
+ }
758
+ validateCoordinates(center);
759
+ const circleRadius = radius !== null && radius !== void 0 ? radius : getDistanceFromBounds(bounds) / 8;
760
+ const circleFeature = circle__default["default"](center, circleRadius, { units: "miles" });
761
+ return {
762
+ id,
763
+ type: "Feature",
764
+ properties: {
765
+ isCircle: true,
766
+ center,
767
+ },
768
+ geometry: {
769
+ type: "Polygon",
770
+ coordinates: circleFeature.geometry.coordinates,
771
+ },
772
+ };
773
+ };
774
+ const getDistanceFromBounds = (bounds) => {
775
+ const swCoordinate = bounds.getSouthWest().toArray();
776
+ const neCoordinate = bounds.getNorthEast().toArray();
777
+ const center = bounds.getCenter().toArray();
778
+ const line = helpers.lineString([swCoordinate, center, neCoordinate]);
779
+ return length__default["default"](line, { units: "miles" });
780
+ };
781
+ const doesGeofenceExist = (id, loadedGeofences) => {
782
+ return !!loadedGeofences[id];
783
+ };
784
+ const isValidGeofenceId = (id) => {
785
+ return !!id.match(GEOFENCE_ID_REGEX);
786
+ };
787
+ const isExistingGeofenceId = (id, loadedGeofences) => {
788
+ return !doesGeofenceExist(id, loadedGeofences);
789
+ };
790
+
667
791
  const FILL_OPACITY = 0.3;
668
792
  const BORDER_OPACITY = 0.5;
669
793
  const BORDER_WIDTH = 4;
@@ -671,43 +795,40 @@
671
795
  * DrawPoints utility function for adding points to a map based on coordinate data or a FeatureCollection. Will add clustered points and styled markers by default with options for popups and other styles
672
796
  */
673
797
  function drawGeofences(sourceName, data, map, options = {}) {
674
- var _a, _b, _c, _d, _e;
798
+ var _a, _b, _c, _d, _e, _f, _g, _h;
675
799
  if (!map ||
676
800
  typeof map.addSource !== "function" ||
677
801
  typeof map.addLayer !== "function") {
678
802
  throw new Error("Please use a maplibre map");
679
803
  }
680
- /*
681
- * Convert data passed in as coordinates into features
682
- */
683
- const features = getGeofenceFeaturesFromData(data);
804
+ if (data.length > 0 && !isGeofenceArray(data) && !isPolygonArray(data)) {
805
+ throw new Error("Please pass in an array of Geofences or an array of Polygons");
806
+ }
684
807
  /*
685
808
  * Data source for features
809
+ * Convert data passed in as coordinates into feature data
686
810
  */
687
811
  const sourceId = `${sourceName}-source`;
688
812
  map.addSource(sourceId, {
689
813
  type: "geojson",
690
- data: {
691
- type: "FeatureCollection",
692
- features,
693
- },
814
+ data: getGeofenceFeatureArray(data),
694
815
  generateId: true,
695
816
  });
817
+ const initialVisiblity = ((_a = options.visible) !== null && _a !== void 0 ? _a : true) ? "visible" : "none";
696
818
  /*
697
819
  * Draw ui layers for source data
698
820
  */
699
- // Add a new layer to visualize the polygon.
700
821
  const fillLayerId = `${sourceName}-fill-layer`;
701
822
  map.addLayer({
702
823
  id: fillLayerId,
703
824
  type: "fill",
704
825
  source: sourceId,
705
826
  layout: {
706
- visibility: "visible",
827
+ visibility: initialVisiblity,
707
828
  },
708
829
  paint: {
709
- "fill-color": (_a = options.fillColor) !== null && _a !== void 0 ? _a : COLOR_BLACK,
710
- "fill-opacity": (_b = options.fillOpacity) !== null && _b !== void 0 ? _b : FILL_OPACITY,
830
+ "fill-color": (_b = options.fillColor) !== null && _b !== void 0 ? _b : COLOR_BLACK,
831
+ "fill-opacity": (_c = options.fillOpacity) !== null && _c !== void 0 ? _c : FILL_OPACITY,
711
832
  },
712
833
  });
713
834
  // Add a black outline around the polygon.
@@ -717,12 +838,13 @@
717
838
  type: "line",
718
839
  source: sourceId,
719
840
  layout: {
720
- visibility: "visible",
841
+ visibility: initialVisiblity,
721
842
  },
722
843
  paint: {
723
- "line-color": (_c = options.borderColor) !== null && _c !== void 0 ? _c : COLOR_BLACK,
724
- "line-opacity": (_d = options.borderOpacity) !== null && _d !== void 0 ? _d : BORDER_OPACITY,
725
- "line-width": (_e = options.borderWidth) !== null && _e !== void 0 ? _e : BORDER_WIDTH,
844
+ "line-color": (_d = options.borderColor) !== null && _d !== void 0 ? _d : COLOR_BLACK,
845
+ "line-opacity": (_e = options.borderOpacity) !== null && _e !== void 0 ? _e : BORDER_OPACITY,
846
+ "line-width": (_f = options.borderWidth) !== null && _f !== void 0 ? _f : BORDER_WIDTH,
847
+ "line-offset": (_g = options.borderOffset) !== null && _g !== void 0 ? _g : (((_h = options.borderWidth) !== null && _h !== void 0 ? _h : BORDER_WIDTH) / 2) * -1,
726
848
  },
727
849
  });
728
850
  // utility function for setting layer visibility to none
@@ -735,24 +857,971 @@
735
857
  map.setLayoutProperty(fillLayerId, "visibility", "visible");
736
858
  map.setLayoutProperty(outlineLayerId, "visibility", "visible");
737
859
  };
738
- return { sourceId, outlineLayerId, fillLayerId, show, hide };
739
- }
740
- const getGeofenceFeaturesFromData = (data) => {
741
- const features = data.map((item) => {
742
- const coordinates = isGeofence(item) ? item.geometry.polygon : item;
743
- return {
744
- type: "Feature",
745
- geometry: {
746
- type: "Polygon",
747
- coordinates,
748
- },
749
- properties: {},
860
+ // utility function for checking layer visibility
861
+ const isVisible = () => {
862
+ const visibility = map.getLayoutProperty(fillLayerId, "visibility");
863
+ return visibility === "visible";
864
+ };
865
+ // utility function for setting layer visibility to visible
866
+ const setData = (data) => {
867
+ map.getSource(sourceId).setData(data);
868
+ };
869
+ return {
870
+ sourceId,
871
+ outlineLayerId,
872
+ fillLayerId,
873
+ show,
874
+ hide,
875
+ isVisible,
876
+ setData,
877
+ };
878
+ }
879
+
880
+ const EDIT_ICON_PATH = "M0.5 12.375V15.5H3.625L12.8417 6.28333L9.71667 3.15833L0.5 12.375ZM2.93333 13.8333H2.16667V13.0667L9.71667 5.51667L10.4833 6.28333L2.93333 13.8333ZM15.2583 2.69167L13.3083 0.741667C13.1417 0.575 12.9333 0.5 12.7167 0.5C12.5 0.5 12.2917 0.583333 12.1333 0.741667L10.6083 2.26667L13.7333 5.39167L15.2583 3.86667C15.5833 3.54167 15.5833 3.01667 15.2583 2.69167Z";
881
+ function createEditIcon() {
882
+ const customIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
883
+ const iconPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
884
+ customIcon.setAttribute("viewBox", "0 0 16 16");
885
+ customIcon.setAttribute("width", "16");
886
+ customIcon.setAttribute("height", "16");
887
+ customIcon.setAttribute("fill", "none");
888
+ iconPath.setAttribute("d", EDIT_ICON_PATH);
889
+ iconPath.setAttribute("fill", "white");
890
+ customIcon.appendChild(iconPath);
891
+ return customIcon;
892
+ }
893
+ const TRASH_ICON_PATH = "M9.33317 5.5V13.8333H2.6665V5.5H9.33317ZM8.08317 0.5H3.9165L3.08317 1.33333H0.166504V3H11.8332V1.33333H8.9165L8.08317 0.5ZM10.9998 3.83333H0.999837V13.8333C0.999837 14.75 1.74984 15.5 2.6665 15.5H9.33317C10.2498 15.5 10.9998 14.75 10.9998 13.8333V3.83333Z";
894
+ function createTrashIcon() {
895
+ const customIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
896
+ const iconPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
897
+ customIcon.setAttribute("viewBox", "0 0 12 16");
898
+ customIcon.setAttribute("width", "12");
899
+ customIcon.setAttribute("height", "16");
900
+ customIcon.setAttribute("fill", "none");
901
+ iconPath.setAttribute("d", TRASH_ICON_PATH);
902
+ iconPath.setAttribute("fill", "white");
903
+ customIcon.appendChild(iconPath);
904
+ return customIcon;
905
+ }
906
+ function createPopupStep1Icon() {
907
+ const customIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
908
+ customIcon.setAttribute("viewBox", "0 0 38 38");
909
+ customIcon.setAttribute("width", "38");
910
+ customIcon.setAttribute("height", "38");
911
+ customIcon.setAttribute("fill", "none");
912
+ const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
913
+ circle.setAttribute("cx", "19");
914
+ circle.setAttribute("cy", "18");
915
+ circle.setAttribute("r", "8");
916
+ circle.setAttribute("fill", "#FF9900");
917
+ const path1 = document.createElementNS("http://www.w3.org/2000/svg", "path");
918
+ path1.setAttribute("d", "M19 0L23.3302 7.5H14.6699L19 0Z");
919
+ path1.setAttribute("fill", "#003560");
920
+ const path2 = document.createElementNS("http://www.w3.org/2000/svg", "path");
921
+ path2.setAttribute("d", "M19 36.5L14.6698 29H23.3301L19 36.5Z");
922
+ path2.setAttribute("fill", "#003560");
923
+ const path3 = document.createElementNS("http://www.w3.org/2000/svg", "path");
924
+ path3.setAttribute("d", "M0 18.33L7.5 13.9999L7.5 22.6602L0 18.33Z");
925
+ path3.setAttribute("fill", "#003560");
926
+ const path4 = document.createElementNS("http://www.w3.org/2000/svg", "path");
927
+ path4.setAttribute("d", "M37.5 18.3301L30 22.6603V14L37.5 18.3301Z");
928
+ path4.setAttribute("fill", "#003560");
929
+ customIcon.appendChild(circle);
930
+ customIcon.appendChild(path1);
931
+ customIcon.appendChild(path2);
932
+ customIcon.appendChild(path3);
933
+ customIcon.appendChild(path4);
934
+ return customIcon;
935
+ }
936
+ function createPopupStep2Icon() {
937
+ const customIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
938
+ customIcon.setAttribute("viewBox", "0 0 42 27");
939
+ customIcon.setAttribute("width", "42");
940
+ customIcon.setAttribute("height", "27");
941
+ customIcon.setAttribute("fill", "none");
942
+ const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
943
+ line.setAttribute("y1", "8");
944
+ line.setAttribute("x2", "42");
945
+ line.setAttribute("y2", "8");
946
+ line.setAttribute("stroke", "black");
947
+ line.setAttribute("stroke-width", "2");
948
+ const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
949
+ circle.setAttribute("cx", "21");
950
+ circle.setAttribute("cy", "8");
951
+ circle.setAttribute("r", "8");
952
+ circle.setAttribute("fill", "#FF9900");
953
+ const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
954
+ path.setAttribute("d", "M21 6.17822V22.488L24.6951 19.4356L27.172 26.1782L29.0399 25.3582L26.6035 18.57L31.4762 17.9322L21 6.17822Z");
955
+ path.setAttribute("fill", "#003560");
956
+ customIcon.appendChild(line);
957
+ customIcon.appendChild(circle);
958
+ customIcon.appendChild(path);
959
+ return customIcon;
960
+ }
961
+ function createPopupStep3Icon() {
962
+ const customIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
963
+ customIcon.setAttribute("viewBox", "0 0 32 32");
964
+ customIcon.setAttribute("width", "32");
965
+ customIcon.setAttribute("height", "32");
966
+ customIcon.setAttribute("fill", "none");
967
+ const rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
968
+ rect.setAttribute("y", "1");
969
+ rect.setAttribute("x", "1");
970
+ rect.setAttribute("width", "30");
971
+ rect.setAttribute("height", "30");
972
+ rect.setAttribute("fill", "#2196F3");
973
+ rect.setAttribute("fill-opacity", "0.4");
974
+ rect.setAttribute("stroke", "#003560");
975
+ rect.setAttribute("stroke-width", "2");
976
+ const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
977
+ path.setAttribute("d", "M11 6V22.3098L14.6951 19.2574L17.172 26L19.0399 25.18L16.6035 18.3918L21.4762 17.754L11 6Z");
978
+ path.setAttribute("fill", "#003560");
979
+ customIcon.appendChild(rect);
980
+ customIcon.appendChild(path);
981
+ return customIcon;
982
+ }
983
+ function createPopupStep4Icon() {
984
+ const customIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
985
+ customIcon.setAttribute("viewBox", "0 0 64 20");
986
+ customIcon.setAttribute("width", "64");
987
+ customIcon.setAttribute("height", "20");
988
+ customIcon.setAttribute("fill", "none");
989
+ const rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
990
+ rect.setAttribute("y", "0.5");
991
+ rect.setAttribute("x", "0.5");
992
+ rect.setAttribute("width", "63");
993
+ rect.setAttribute("height", "19");
994
+ rect.setAttribute("rx", "3.5");
995
+ rect.setAttribute("stroke", "#014478");
996
+ const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
997
+ path.setAttribute("d", "M14.4148 15C17.517 15 19.3118 13.076 19.3118 9.89418C19.3118 6.72727 17.517 4.81818 14.5192 4.81818H11.1186V15H14.4148ZM12.6548 13.6577V6.16051H14.4247C16.652 6.16051 17.8004 7.4929 17.8004 9.89418C17.8004 12.3054 16.652 13.6577 14.3303 13.6577H12.6548ZM24.394 15.1541C26.0595 15.1541 27.2377 14.3338 27.5758 13.0909L26.1689 12.8374C25.9004 13.5582 25.2541 13.9261 24.4089 13.9261C23.1362 13.9261 22.2811 13.1009 22.2413 11.6293H27.6703V11.1023C27.6703 8.34304 26.0197 7.2642 24.2896 7.2642C22.1618 7.2642 20.7598 8.88494 20.7598 11.2315C20.7598 13.603 22.1419 15.1541 24.394 15.1541ZM22.2463 10.5156C22.3059 9.43182 23.0914 8.49219 24.2995 8.49219C25.4529 8.49219 26.2086 9.3473 26.2136 10.5156H22.2463ZM30.8061 4.81818H29.3196V15H30.8061V4.81818ZM36.0971 15.1541C37.7626 15.1541 38.9409 14.3338 39.2789 13.0909L37.872 12.8374C37.6035 13.5582 36.9572 13.9261 36.112 13.9261C34.8393 13.9261 33.9842 13.1009 33.9444 11.6293H39.3734V11.1023C39.3734 8.34304 37.7228 7.2642 35.9927 7.2642C33.8649 7.2642 32.4629 8.88494 32.4629 11.2315C32.4629 13.603 33.845 15.1541 36.0971 15.1541ZM33.9494 10.5156C34.0091 9.43182 34.7946 8.49219 36.0027 8.49219C37.1561 8.49219 37.9118 9.3473 37.9167 10.5156H33.9494ZM44.5874 7.36364H43.0213V5.53409H41.5348V7.36364H40.4162V8.55682H41.5348V13.0661C41.5298 14.4531 42.5888 15.1243 43.7621 15.0994C44.2344 15.0945 44.5526 15.005 44.7266 14.9403L44.4581 13.7124C44.3587 13.7322 44.1747 13.777 43.9361 13.777C43.4538 13.777 43.0213 13.6179 43.0213 12.7578V8.55682H44.5874V7.36364ZM49.4409 15.1541C51.1064 15.1541 52.2846 14.3338 52.6227 13.0909L51.2157 12.8374C50.9473 13.5582 50.301 13.9261 49.4558 13.9261C48.1831 13.9261 47.3279 13.1009 47.2882 11.6293H52.7172V11.1023C52.7172 8.34304 51.0666 7.2642 49.3365 7.2642C47.2086 7.2642 45.8066 8.88494 45.8066 11.2315C45.8066 13.603 47.1887 15.1541 49.4409 15.1541ZM47.2931 10.5156C47.3528 9.43182 48.1383 8.49219 49.3464 8.49219C50.4998 8.49219 51.2555 9.3473 51.2605 10.5156H47.2931Z");
998
+ path.setAttribute("fill", "black");
999
+ customIcon.appendChild(rect);
1000
+ customIcon.appendChild(path);
1001
+ return customIcon;
1002
+ }
1003
+ function createDeleteSuccessIcon() {
1004
+ const customIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
1005
+ customIcon.setAttribute("viewBox", "0 0 12 12");
1006
+ customIcon.setAttribute("width", "12");
1007
+ customIcon.setAttribute("height", "12");
1008
+ customIcon.setAttribute("fill", "none");
1009
+ const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
1010
+ path.setAttribute("d", "M6 0C2.688 0 0 2.688 0 6C0 9.312 2.688 12 6 12C9.312 12 12 9.312 12 6C12 2.688 9.312 0 6 0ZM6 10.8C3.354 10.8 1.2 8.646 1.2 6C1.2 3.354 3.354 1.2 6 1.2C8.646 1.2 10.8 3.354 10.8 6C10.8 8.646 8.646 10.8 6 10.8ZM8.754 3.348L4.8 7.302L3.246 5.754L2.4 6.6L4.8 9L9.6 4.2L8.754 3.348Z");
1011
+ path.setAttribute("fill", "white");
1012
+ customIcon.appendChild(path);
1013
+ return customIcon;
1014
+ }
1015
+ function createCloseIcon() {
1016
+ const customIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
1017
+ customIcon.setAttribute("viewBox", "0 0 12 12");
1018
+ customIcon.setAttribute("width", "12");
1019
+ customIcon.setAttribute("height", "12");
1020
+ customIcon.setAttribute("fill", "none");
1021
+ const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
1022
+ path.setAttribute("d", "M10.773 2.28762L9.71231 1.22696L6 4.93927L2.28769 1.22696L1.22703 2.28762L4.93934 5.99993L1.22703 9.71224L2.28769 10.7729L6 7.06059L9.71231 10.7729L10.773 9.71224L7.06066 5.99993L10.773 2.28762Z");
1023
+ path.setAttribute("fill", "white");
1024
+ path.setAttribute("fill-rule", "evenodd");
1025
+ path.setAttribute("clip-rule", "evenodd");
1026
+ customIcon.appendChild(path);
1027
+ return customIcon;
1028
+ }
1029
+ function createErrorIcon() {
1030
+ const customIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
1031
+ customIcon.setAttribute("viewBox", "0 0 12 10");
1032
+ customIcon.setAttribute("width", "12");
1033
+ customIcon.setAttribute("height", "10");
1034
+ customIcon.setAttribute("fill", "none");
1035
+ const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
1036
+ path.setAttribute("d", "M0 10H11.5789L5.78947 0L0 10ZM6.31579 8.42105H5.26316V7.36842H6.31579V8.42105ZM6.31579 6.31579H5.26316V4.21053H6.31579V6.31579Z");
1037
+ path.setAttribute("fill", "#FF5050");
1038
+ customIcon.appendChild(path);
1039
+ return customIcon;
1040
+ }
1041
+
1042
+ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
1043
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1044
+ return new (P || (P = Promise))(function (resolve, reject) {
1045
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1046
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1047
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1048
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
1049
+ });
1050
+ };
1051
+ function AmplifyGeofenceControlUI(geofenceControl, geofenceControlContainer) {
1052
+ let _addGeofenceContainer;
1053
+ let _deleteGeofenceContainer;
1054
+ let _addGeofencebutton;
1055
+ let _checkboxAll;
1056
+ let _geofenceList;
1057
+ let _createContainer;
1058
+ let _geofenceTitle;
1059
+ let _checkBoxAllAndCreateContainer;
1060
+ let _checkBoxAllContainer;
1061
+ let _circleModeContainer;
1062
+ let _polygonModeContainer;
1063
+ let _deletePopdownContainer;
1064
+ let _errorDiv;
1065
+ function registerControlPosition(map, positionName) {
1066
+ if (map._controlPositions[positionName]) {
1067
+ return;
1068
+ }
1069
+ const positionContainer = document.createElement("div");
1070
+ positionContainer.className = `maplibregl-ctrl-${positionName}`;
1071
+ map._controlContainer.appendChild(positionContainer);
1072
+ map._controlPositions[positionName] = positionContainer;
1073
+ }
1074
+ /************************************************************
1075
+ * Create Geofence Controls
1076
+ *************************************************************/
1077
+ function createGeofenceCreateContainer(isCircle) {
1078
+ const container = createElement("div", "geofence-ctrl-create-prompt-container", geofenceControlContainer);
1079
+ _createContainer = createElement("div", "geofence-ctrl-create-prompt", container);
1080
+ if (isCircle) {
1081
+ /* Create buttons to switch between different modes */
1082
+ const buttonContainer = createElement("div", "geofence-ctrl-create-prompt-buttons", _createContainer);
1083
+ const circleModeButton = createElement("div", "geofence-ctrl-create-prompt-button-circle geofence-ctrl-create-prompt-button", buttonContainer);
1084
+ circleModeButton.addEventListener("click", () => {
1085
+ geofenceControl.changeMode("draw_circle");
1086
+ // Change button selected style
1087
+ circleModeButton.classList.add("geofence-ctrl-create-prompt-selected");
1088
+ polygonModeButton.classList.remove("geofence-ctrl-create-prompt-selected");
1089
+ // Switch info box mode
1090
+ if (_polygonModeContainer) {
1091
+ removeElement(_polygonModeContainer);
1092
+ _polygonModeContainer = undefined;
1093
+ }
1094
+ if (!_circleModeContainer)
1095
+ createCircleModeCreateContainer(_createContainer);
1096
+ });
1097
+ circleModeButton.innerHTML = "Circle";
1098
+ const polygonModeButton = createElement("div", "geofence-ctrl-create-prompt-button-polygon geofence-ctrl-create-prompt-button", buttonContainer);
1099
+ polygonModeButton.addEventListener("click", () => {
1100
+ geofenceControl.changeMode("draw_polygon");
1101
+ // Change button selected style
1102
+ polygonModeButton.classList.add("geofence-ctrl-create-prompt-selected");
1103
+ circleModeButton.classList.remove("geofence-ctrl-create-prompt-selected");
1104
+ // Switch info box mode
1105
+ if (_circleModeContainer) {
1106
+ removeElement(_circleModeContainer);
1107
+ _circleModeContainer = undefined;
1108
+ }
1109
+ if (!_polygonModeContainer)
1110
+ createPolygonModeCreateContainer(_createContainer);
1111
+ });
1112
+ polygonModeButton.innerHTML = "Custom";
1113
+ circleModeButton.classList.add("geofence-ctrl-create-prompt-selected");
1114
+ createCircleModeCreateContainer(_createContainer);
1115
+ }
1116
+ else {
1117
+ createPolygonModeCreateContainer(_createContainer);
1118
+ }
1119
+ }
1120
+ function createCircleModeCreateContainer(container) {
1121
+ _circleModeContainer = createElement("div", "geofence-ctrl-create-circle-mode-container", container);
1122
+ const radiusTitle = createElement("div", "geofence-ctrl-create-circle-mode-title", _circleModeContainer);
1123
+ radiusTitle.innerHTML = "Radius";
1124
+ const geofenceCreateInput = createElement("input", "geofence-ctrl-create-circle-mode-input", _circleModeContainer);
1125
+ geofenceCreateInput.addEventListener("keydown", debounce.debounce(geofenceControl.updateInputRadius, 200));
1126
+ }
1127
+ function createPolygonModeCreateContainer(container) {
1128
+ _polygonModeContainer = createElement("div", "geofence-ctrl-create-polygon-mode-container", container);
1129
+ const moreInfoContainer = createElement("div", "geofence-ctrl-create-polygon-mode-info-container", _polygonModeContainer);
1130
+ const moreInfoIcon = createElement("div", "geofence-ctrl-create-polygon-mode-icon", moreInfoContainer);
1131
+ const letterI = createElement("div", "geofence-ctrl-create-polygon-mode-info-icon", moreInfoIcon);
1132
+ letterI.innerHTML = "i";
1133
+ const moreInfo = createElement("div", "geofence-ctrl-create-polygon-mode-title", moreInfoContainer);
1134
+ moreInfo.innerHTML = "How it works?";
1135
+ const resetButton = createElement("div", "geofence-ctrl-create-polygon-mode-reset-button geofence-ctrl-button", _polygonModeContainer);
1136
+ resetButton.innerHTML = "Reset";
1137
+ resetButton.addEventListener("click", () => {
1138
+ geofenceControl.changeMode("draw_polygon");
1139
+ });
1140
+ // Add popup onClick
1141
+ const popup = createPolygonModeInfoPopup(moreInfoIcon);
1142
+ moreInfoContainer.addEventListener("click", () => {
1143
+ popup.classList.toggle("show");
1144
+ });
1145
+ }
1146
+ function createPolygonModeInfoPopup(container) {
1147
+ const popupContainer = createElement("div", "geofence-ctrl-create-polygon-mode-popup-container", container);
1148
+ const popup = createElement("div", "geofence-ctrl-create-polygon-mode-popup", popupContainer);
1149
+ createPopupStep(popup, "Move dots to desired position", createPopupStep1Icon());
1150
+ createPopupStep(popup, "Click on a border to create a dot", createPopupStep2Icon());
1151
+ createPopupStep(popup, "Click into shape to move", createPopupStep3Icon());
1152
+ createPopupStep(popup, "Press delete to remove a dot", createPopupStep4Icon());
1153
+ return popup;
1154
+ }
1155
+ function createPopupStep(container, text, image) {
1156
+ const popupStep = createElement("div", "geofence-ctrl-create-polygon-mode-popup-step", container);
1157
+ const popupStepImage = createElement("div", "geofence-ctrl-create-polygon-mode-popup-step-image", popupStep);
1158
+ popupStepImage.appendChild(image);
1159
+ const popupStepText = createElement("div", "geofence-ctrl-create-polygon-mode-popup-step-text", popupStep);
1160
+ popupStepText.innerHTML = text;
1161
+ }
1162
+ function removeGeofenceCreateContainer() {
1163
+ removeElement(_createContainer);
1164
+ _createContainer = undefined;
1165
+ _circleModeContainer = undefined;
1166
+ _polygonModeContainer = undefined;
1167
+ }
1168
+ /************************************************************
1169
+ * Geofence List
1170
+ *************************************************************/
1171
+ function createGeofenceListContainer() {
1172
+ const geofenceListContainer = createElement("div", "geofence-ctrl-list-container", geofenceControlContainer);
1173
+ createGeofenceListHeader(geofenceListContainer);
1174
+ _geofenceList = createElement("div", "geofence-ctrl-list", geofenceListContainer);
1175
+ _geofenceList.addEventListener("scroll", () => {
1176
+ const { scrollHeight, scrollTop, clientHeight } = _geofenceList;
1177
+ if (scrollTop + clientHeight >= scrollHeight - 20) {
1178
+ geofenceControl.loadMoreGeofences();
1179
+ }
1180
+ });
1181
+ }
1182
+ function createGeofenceListHeader(geofenceListContainer) {
1183
+ const header = createElement("div", "geofence-ctrl-list-header", geofenceListContainer);
1184
+ _geofenceTitle = createElement("div", "geofence-ctrl-list-header-title", header);
1185
+ _geofenceTitle.innerHTML = "Geofences (0)";
1186
+ _checkBoxAllAndCreateContainer = createElement("div", "geofence-ctrl-list-header-checkbox-create-container", header);
1187
+ createCheckboxAllContainer(_checkBoxAllAndCreateContainer);
1188
+ }
1189
+ function createCheckboxAllContainer(geofenceListContainer) {
1190
+ _checkBoxAllContainer = createElement("div", "geofence-ctrl-list-checkbox-all-container", geofenceListContainer);
1191
+ _checkboxAll = createElement("input", "geofence-ctrl-list-checkbox-all", _checkBoxAllContainer);
1192
+ _checkboxAll.type = "checkbox";
1193
+ _checkboxAll.addEventListener("click", function () {
1194
+ if (_checkboxAll.checked) {
1195
+ geofenceControl.displayAllGeofences();
1196
+ checkboxAllText.innerHTML = "Deselect All";
1197
+ }
1198
+ else {
1199
+ geofenceControl.hideAllGeofences();
1200
+ checkboxAllText.innerHTML = "Select All";
1201
+ }
1202
+ });
1203
+ const checkboxAllText = createElement("div", "geofence-ctrl-list-checkbox-all-title", _checkBoxAllContainer);
1204
+ checkboxAllText.innerHTML = "Select all";
1205
+ _addGeofencebutton = createElement("div", "geofence-ctrl-list-header-add-button", _checkBoxAllContainer);
1206
+ _addGeofencebutton.innerHTML = "+ Add";
1207
+ _addGeofencebutton.addEventListener("click", () => {
1208
+ createAddGeofenceContainer();
1209
+ });
1210
+ }
1211
+ function renderListItem(geofence) {
1212
+ const container = createElement("li", "geofence-ctrl-list-item-container", _geofenceList);
1213
+ container.id = `list-item-${geofence.geofenceId}`;
1214
+ const listItem = createElement("li", "geofence-ctrl-list-item", container);
1215
+ const leftContainer = createElement("div", "geofence-ctrl-list-item-left-container", listItem);
1216
+ const checkbox = createElement("input", "geofence-ctrl-list-item-checkbox", leftContainer);
1217
+ checkbox.id = `list-item-checkbox-${geofence.geofenceId}`;
1218
+ checkbox.type = "checkbox";
1219
+ checkbox.addEventListener("click", function () {
1220
+ if (checkbox.checked) {
1221
+ geofenceControl.displayGeofence(geofence.geofenceId);
1222
+ }
1223
+ else {
1224
+ geofenceControl.hideGeofence(geofence.geofenceId);
1225
+ }
1226
+ });
1227
+ const rightContainer = createElement("div", "geofence-ctrl-list-item-right-container", listItem);
1228
+ const geofenceTitleContainer = createElement("div", "geofence-ctrl-list-item-title-container", rightContainer);
1229
+ geofenceTitleContainer.addEventListener("mouseover", function () {
1230
+ geofenceControl.displayHighlightedGeofence(geofence.geofenceId);
1231
+ });
1232
+ geofenceTitleContainer.addEventListener("mouseout", function () {
1233
+ geofenceControl.hideHighlightedGeofence();
1234
+ });
1235
+ const geofenceTitle = createElement("div", "geofence-ctrl-list-item-title", geofenceTitleContainer);
1236
+ geofenceTitle.innerHTML = geofence.geofenceId;
1237
+ const editButton = createElement("div", "geofence-ctrl-edit-button", geofenceTitleContainer);
1238
+ editButton.addEventListener("click", function () {
1239
+ geofenceControl.editGeofence(geofence.geofenceId);
1240
+ createEditControls(listItem, rightContainer, leftContainer, geofence.geofenceId);
1241
+ listItem.classList.remove("geofence-ctrl-list-item");
1242
+ listItem.classList.add("geofence-ctrl-list-selected-item");
1243
+ });
1244
+ editButton.appendChild(createEditIcon());
1245
+ }
1246
+ function createEditControls(item, rightContainer, leftContainer, id) {
1247
+ const editContainer = createElement("div", "geofence-ctrl-list-item-controls", rightContainer);
1248
+ const deleteButton = renderDeleteButton(leftContainer, id);
1249
+ const removeEditContainer = () => {
1250
+ item.classList.remove("geofence-ctrl-list-selected-item");
1251
+ item.classList.add("geofence-ctrl-list-item");
1252
+ removeElement(editContainer);
1253
+ removeElement(deleteButton);
750
1254
  };
1255
+ const cancelButton = createElement("div", "geofence-ctrl-cancel-button", editContainer);
1256
+ cancelButton.classList.add("geofence-ctrl-button");
1257
+ cancelButton.innerHTML = "Cancel";
1258
+ cancelButton.addEventListener("click", () => {
1259
+ geofenceControl.setEditingModeEnabled(false);
1260
+ removeEditContainer();
1261
+ });
1262
+ const saveGeofenceButton = createElement("div", "geofence-ctrl-save-button geofence-ctrl-button", editContainer);
1263
+ saveGeofenceButton.addEventListener("click", () => __awaiter$1(this, void 0, void 0, function* () {
1264
+ yield geofenceControl.saveGeofence();
1265
+ removeEditContainer();
1266
+ }));
1267
+ saveGeofenceButton.title = "Save";
1268
+ saveGeofenceButton.innerHTML = "Save";
1269
+ }
1270
+ /************************************************************
1271
+ * Add Geofence Controls
1272
+ *************************************************************/
1273
+ function removeAddGeofenceContainer() {
1274
+ removeElement(_addGeofenceContainer);
1275
+ clearAddGeofenceError();
1276
+ showCheckboxAllContainer();
1277
+ }
1278
+ function clearAddGeofenceError() {
1279
+ if (_errorDiv) {
1280
+ removeElement(_errorDiv);
1281
+ _errorDiv = undefined;
1282
+ }
1283
+ }
1284
+ function createAddGeofenceContainer() {
1285
+ hideCheckboxAllContainer();
1286
+ _addGeofenceContainer = createElement("div", "geofence-ctrl-add-geofence-container", _checkBoxAllAndCreateContainer);
1287
+ const addGeofencePrompt = createElement("div", "geofence-ctrl-add-geofence", _addGeofenceContainer);
1288
+ const nameInput = createElement("input", "geofence-ctrl-add-geofence-input", addGeofencePrompt);
1289
+ nameInput.placeholder = "Enter name";
1290
+ const buttonContainer = createElement("div", "geofence-ctrl-add-geofence-buttons", addGeofencePrompt);
1291
+ const cancelButton = createElement("div", "geofence-ctrl-add-geofence-cancel-button geofence-ctrl-button ", buttonContainer);
1292
+ cancelButton.innerHTML = "Cancel";
1293
+ cancelButton.addEventListener("click", () => {
1294
+ removeAddGeofenceContainer();
1295
+ geofenceControl.setEditingModeEnabled(false);
1296
+ });
1297
+ const saveButton = createElement("div", "geofence-ctrl-button geofence-ctrl-save-button", buttonContainer);
1298
+ saveButton.innerHTML = "Save";
1299
+ saveButton.addEventListener("click", function () {
1300
+ return __awaiter$1(this, void 0, void 0, function* () {
1301
+ clearAddGeofenceError();
1302
+ const output = yield geofenceControl.saveGeofence(escape(nameInput.value));
1303
+ if (output)
1304
+ removeAddGeofenceContainer();
1305
+ });
1306
+ });
1307
+ geofenceControl.addEditableGeofence();
1308
+ }
1309
+ function createAddGeofencePromptError(error) {
1310
+ if (_errorDiv) {
1311
+ return;
1312
+ }
1313
+ _errorDiv = createElement("div", "geofence-ctrl-add-geofence-error", _addGeofenceContainer);
1314
+ const errorIconContainer = createElement("div", "geofence-ctrl-add-geofence-error-icon", _errorDiv);
1315
+ errorIconContainer.appendChild(createErrorIcon());
1316
+ const errorText = createElement("div", "geofence-ctrl-add-geofence-error-text", _errorDiv);
1317
+ errorText.innerHTML = error;
1318
+ }
1319
+ /************************************************************
1320
+ * Delete Controls
1321
+ *************************************************************/
1322
+ function renderDeleteButton(container, id) {
1323
+ const deleteButton = createElement("div", "geofence-ctrl-delete-button", container);
1324
+ deleteButton.classList.add("geofence-ctrl-button");
1325
+ deleteButton.addEventListener("click", function () {
1326
+ createConfirmDeleteContainer(id);
1327
+ });
1328
+ deleteButton.appendChild(createTrashIcon());
1329
+ return deleteButton;
1330
+ }
1331
+ function createConfirmDeleteContainer(geofenceId) {
1332
+ _deleteGeofenceContainer = createElement("div", "geofence-ctrl-delete-prompt-container", geofenceControlContainer);
1333
+ const deleteGeofencePrompt = createElement("div", "geofence-ctrl-delete-prompt", _deleteGeofenceContainer);
1334
+ const title = createElement("div", "geofence-ctrl-delete-geofence-title", deleteGeofencePrompt);
1335
+ title.innerHTML = `Are you sure you want to delete <strong>${geofenceId}</strong>?`;
1336
+ createDeleteButtonsContainer(deleteGeofencePrompt, geofenceId);
1337
+ }
1338
+ function createDeleteButtonsContainer(container, geofenceId) {
1339
+ const deleteButtonsContainer = createElement("div", "geofence-ctrl-delete-geofence-buttons", container);
1340
+ const cancelButton = createElement("div", "geofence-ctrl-delete-geofence-cancel-button", deleteButtonsContainer);
1341
+ cancelButton.innerHTML = "Cancel";
1342
+ cancelButton.addEventListener("click", () => {
1343
+ removeElement(_deleteGeofenceContainer);
1344
+ });
1345
+ const confirmDeleteButton = createElement("div", "geofence-ctrl-delete-geofence-confirm-button", deleteButtonsContainer);
1346
+ confirmDeleteButton.innerHTML = "Delete";
1347
+ confirmDeleteButton.addEventListener("click", function () {
1348
+ return __awaiter$1(this, void 0, void 0, function* () {
1349
+ const id = yield geofenceControl.deleteGeofence(geofenceId);
1350
+ if (id) {
1351
+ createDeleteResultContainer(true);
1352
+ removeElement(_deleteGeofenceContainer);
1353
+ geofenceControl.setEditingModeEnabled(false);
1354
+ }
1355
+ });
1356
+ });
1357
+ }
1358
+ function createDeleteResultContainer(success) {
1359
+ _deletePopdownContainer = createElement("div", "geofence-ctrl-delete-popdown-container", geofenceControlContainer);
1360
+ const deletePopdown = createElement("div", "geofence-ctrl-delete-popdown", _deletePopdownContainer);
1361
+ const deletePopdownCloseButton = createElement("div", "geofence-ctrl-delete-popdown-close-button", _deletePopdownContainer);
1362
+ deletePopdownCloseButton.appendChild(createCloseIcon());
1363
+ deletePopdownCloseButton.addEventListener("click", () => {
1364
+ removeElement(_deletePopdownContainer);
1365
+ });
1366
+ const deleteSuccessIcon = createElement("div", "geofence-ctrl-delete-popdown-icon", deletePopdown);
1367
+ deleteSuccessIcon.appendChild(createDeleteSuccessIcon());
1368
+ const deletePopdownText = createElement("div", "geofence-ctrl-delete-popdown-text", deletePopdown);
1369
+ deletePopdownText.innerHTML = success
1370
+ ? "Geofence was deleted successfully"
1371
+ : "Geofence failed to delete";
1372
+ }
1373
+ /************************************************************
1374
+ * Utility Methods
1375
+ *************************************************************/
1376
+ function updateCheckbox(geofenceId, checked) {
1377
+ const checkbox = document.getElementById(`list-item-checkbox-${geofenceId}`);
1378
+ if (checkbox)
1379
+ checkbox.checked = checked;
1380
+ }
1381
+ function removeGeofenceListItem(geofenceId) {
1382
+ const listItem = document.getElementById(`list-item-${geofenceId}`);
1383
+ removeElement(listItem);
1384
+ }
1385
+ function setGeofenceListEnabled(enabled) {
1386
+ _checkboxAll.disabled = !enabled;
1387
+ enabled
1388
+ ? _addGeofencebutton.classList.remove("geofence-ctrl-noHover")
1389
+ : _addGeofencebutton.classList.add("geofence-ctrl-noHover");
1390
+ const inputs = document.getElementsByClassName("geofence-ctrl-list-item-checkbox");
1391
+ for (let i = 0; i < inputs.length; i++) {
1392
+ inputs.item(i).disabled = !enabled;
1393
+ }
1394
+ const items = document.getElementsByClassName("geofence-ctrl-list-item-container");
1395
+ for (let i = 0; i < items.length; i++) {
1396
+ enabled
1397
+ ? items.item(i).classList.remove("geofence-ctrl-noHover")
1398
+ : items.item(i).classList.add("geofence-ctrl-noHover");
1399
+ }
1400
+ }
1401
+ function getCheckboxAllValue() {
1402
+ return _checkboxAll.checked;
1403
+ }
1404
+ function updateGeofenceCount(count) {
1405
+ _geofenceTitle.innerHTML = `Geofences (${count})`;
1406
+ }
1407
+ function hideCheckboxAllContainer() {
1408
+ _checkBoxAllContainer.style.display = "none";
1409
+ }
1410
+ function showCheckboxAllContainer() {
1411
+ _checkBoxAllContainer.style.display = "flex";
1412
+ }
1413
+ return {
1414
+ registerControlPosition,
1415
+ createElement,
1416
+ removeElement,
1417
+ createGeofenceCreateContainer,
1418
+ createGeofenceListContainer,
1419
+ removeAddGeofenceContainer,
1420
+ createAddGeofencePromptError,
1421
+ renderListItem,
1422
+ updateCheckbox,
1423
+ removeGeofenceListItem,
1424
+ setGeofenceListEnabled,
1425
+ getCheckboxAllValue,
1426
+ removeGeofenceCreateContainer,
1427
+ updateGeofenceCount,
1428
+ };
1429
+ }
1430
+
1431
+ class AmplifyMapDraw {
1432
+ constructor(map, ui) {
1433
+ this._mapBoxDraw = new MapboxDraw__default["default"]({
1434
+ displayControlsDefault: false,
1435
+ defaultMode: "simple_select",
1436
+ userProperties: true,
1437
+ controls: {
1438
+ trash: true,
1439
+ },
1440
+ modes: Object.assign(Object.assign({}, MapboxDraw__default["default"].modes), { draw_circle: maplibreGlDrawCircle.CircleMode, direct_select: maplibreGlDrawCircle.DirectMode, simple_select: maplibreGlDrawCircle.SimpleSelectMode }),
1441
+ styles: [
1442
+ // ACTIVE (being drawn)
1443
+ // polygon fill
1444
+ {
1445
+ id: "gl-draw-polygon-fill",
1446
+ type: "fill",
1447
+ filter: ["all", ["==", "$type", "Polygon"], ["!=", "mode", "static"]],
1448
+ paint: {
1449
+ "fill-color": GEOFENCE_COLOR,
1450
+ "fill-outline-color": GEOFENCE_COLOR,
1451
+ "fill-opacity": 0.3,
1452
+ },
1453
+ },
1454
+ // polygon mid points
1455
+ {
1456
+ id: "gl-draw-polygon-midpoint",
1457
+ type: "circle",
1458
+ filter: ["all", ["==", "$type", "Point"], ["==", "meta", "midpoint"]],
1459
+ paint: {
1460
+ "circle-radius": 5,
1461
+ "circle-color": GEOFENCE_VERTEX_COLOR,
1462
+ },
1463
+ },
1464
+ // polygon border
1465
+ {
1466
+ id: "gl-draw-polygon-stroke-active",
1467
+ type: "line",
1468
+ filter: ["all", ["==", "$type", "Polygon"], ["!=", "mode", "static"]],
1469
+ layout: {
1470
+ "line-cap": "round",
1471
+ "line-join": "round",
1472
+ },
1473
+ paint: {
1474
+ "line-color": GEOFENCE_BORDER_COLOR,
1475
+ "line-width": 4,
1476
+ },
1477
+ },
1478
+ // vertex circle
1479
+ {
1480
+ id: "gl-draw-polygon-and-line-vertex-active",
1481
+ type: "circle",
1482
+ filter: [
1483
+ "all",
1484
+ ["==", "meta", "vertex"],
1485
+ ["==", "$type", "Point"],
1486
+ ["!=", "mode", "static"],
1487
+ ],
1488
+ paint: {
1489
+ "circle-radius": 8,
1490
+ "circle-color": GEOFENCE_VERTEX_COLOR,
1491
+ "circle-stroke-color": GEOFENCE_BORDER_COLOR,
1492
+ "circle-stroke-width": 1,
1493
+ },
1494
+ },
1495
+ ],
1496
+ });
1497
+ this._map = map;
1498
+ this._ui = ui;
1499
+ this.enable = this.enable.bind(this);
1500
+ this.disable = this.disable.bind(this);
1501
+ this.drawPolygonGeofence = this.drawPolygonGeofence.bind(this);
1502
+ }
1503
+ get(id) {
1504
+ return this._mapBoxDraw.get(id);
1505
+ }
1506
+ add(data) {
1507
+ const isCircle = data.properties.isCircle;
1508
+ this.enable(isCircle);
1509
+ this._mapBoxDraw.add(data);
1510
+ this._mapBoxDraw.changeMode("direct_select", {
1511
+ featureId: data.id,
1512
+ });
1513
+ }
1514
+ delete(id) {
1515
+ this._mapBoxDraw.delete(id);
1516
+ }
1517
+ disable() {
1518
+ if (this._map.hasControl(this._mapBoxDraw)) {
1519
+ this._map.removeControl(this._mapBoxDraw);
1520
+ }
1521
+ this._ui.removeGeofenceCreateContainer();
1522
+ }
1523
+ enable(isCircle) {
1524
+ if (this._map.hasControl(this._mapBoxDraw)) {
1525
+ return;
1526
+ }
1527
+ this._map.addControl(this._mapBoxDraw, "bottom-right");
1528
+ this._ui.createGeofenceCreateContainer(isCircle);
1529
+ }
1530
+ /**
1531
+ * Draws a polygonal geofence around the center of the current map view. The polygon defaults to 3/4 the size of the current map bounds
1532
+ * @param id the geofence geojson id
1533
+ */
1534
+ drawPolygonGeofence(id) {
1535
+ const mapBounds = this._map.getBounds();
1536
+ const feature = getPolygonFeatureFromBounds(id, mapBounds);
1537
+ this.add(feature);
1538
+ }
1539
+ /**
1540
+ * Draws a cicular geofence around the center of the current map view
1541
+ * @param id the geofence geojson id
1542
+ * @param radius optional parameter for setting the radius of the cicular geofence, default to 1/8th of the current map bounds length
1543
+ */
1544
+ drawCircularGeofence(id, radius) {
1545
+ const mapBounds = this._map.getBounds();
1546
+ const circleFeature = getCircleFeatureFromCoords(id, this._map.getCenter().toArray(), { bounds: mapBounds, radius });
1547
+ this.add(circleFeature);
1548
+ }
1549
+ }
1550
+
1551
+ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
1552
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1553
+ return new (P || (P = Promise))(function (resolve, reject) {
1554
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1555
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1556
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1557
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
751
1558
  });
752
- return features;
753
1559
  };
1560
+ class AmplifyGeofenceControl {
1561
+ constructor(options) {
1562
+ this._geofenceCollectionId = options === null || options === void 0 ? void 0 : options.geofenceCollectionId;
1563
+ this._loadedGeofences = {};
1564
+ this._displayedGeofences = [];
1565
+ this.changeMode = this.changeMode.bind(this);
1566
+ this.loadInitialGeofences = this.loadInitialGeofences.bind(this);
1567
+ this.loadMoreGeofences = this.loadMoreGeofences.bind(this);
1568
+ this._loadGeofence = this._loadGeofence.bind(this);
1569
+ this.updateInputRadius = this.updateInputRadius.bind(this);
1570
+ this.saveGeofence = this.saveGeofence.bind(this);
1571
+ this.editGeofence = this.editGeofence.bind(this);
1572
+ this.deleteGeofence = this.deleteGeofence.bind(this);
1573
+ this.displayAllGeofences = this.displayAllGeofences.bind(this);
1574
+ this.hideAllGeofences = this.hideAllGeofences.bind(this);
1575
+ this.addEditableGeofence = this.addEditableGeofence.bind(this);
1576
+ this.setEditingModeEnabled = this.setEditingModeEnabled.bind(this);
1577
+ this.displayHighlightedGeofence =
1578
+ this.displayHighlightedGeofence.bind(this);
1579
+ this.hideHighlightedGeofence = this.hideHighlightedGeofence.bind(this);
1580
+ this.displayGeofence = this.displayGeofence.bind(this);
1581
+ this.hideGeofence = this.hideGeofence.bind(this);
1582
+ }
1583
+ /**********************************************************************
1584
+ Public Methods for AmplifyGeofenceControl
1585
+ **********************************************************************/
1586
+ getDefaultPosition() {
1587
+ return "full-screen";
1588
+ }
1589
+ onRemove() {
1590
+ this._ui.removeElement(this._container);
1591
+ }
1592
+ // Reorders MapLibre canvas class names to fix a mapbox draw bug - https://github.com/mapbox/mapbox-gl-draw/pull/1079
1593
+ reorderMapLibreClassNames() {
1594
+ const mapCanvas = document
1595
+ .getElementsByClassName("maplibregl-canvas")
1596
+ .item(0);
1597
+ if (mapCanvas) {
1598
+ mapCanvas.className = "mapboxgl-canvas maplibregl-canvas";
1599
+ }
1600
+ }
1601
+ onAdd(map) {
1602
+ this._map = map;
1603
+ this.reorderMapLibreClassNames();
1604
+ this._container = createElement("div", "geofence-ctrl maplibregl-ctrl");
1605
+ this._ui = AmplifyGeofenceControlUI(this, this._container);
1606
+ this._amplifyDraw = new AmplifyMapDraw(map, this._ui);
1607
+ this._ui.registerControlPosition(map, "full-screen");
1608
+ this._ui.createGeofenceListContainer();
1609
+ // Draw the geofences source to the map so we can update it on geofences load/creation
1610
+ this._map.on("load", function () {
1611
+ this._drawGeofencesOutput = drawGeofences("displayedGeofences", [], this._map, {
1612
+ fillColor: GEOFENCE_COLOR,
1613
+ borderColor: GEOFENCE_BORDER_COLOR,
1614
+ borderOpacity: 1,
1615
+ });
1616
+ this._highlightedGeofenceOutput = drawGeofences("highlightedGeofence", [], this._map, {
1617
+ fillColor: GEOFENCE_COLOR,
1618
+ borderColor: GEOFENCE_BORDER_COLOR,
1619
+ borderOpacity: 1,
1620
+ borderWidth: 6,
1621
+ });
1622
+ this.loadInitialGeofences();
1623
+ map.addControl(new maplibregl__default["default"].NavigationControl({ showCompass: false }), "bottom-right");
1624
+ }.bind(this));
1625
+ return this._container;
1626
+ }
1627
+ saveGeofence(geofenceId) {
1628
+ return __awaiter(this, void 0, void 0, function* () {
1629
+ if (!geofenceId || geofenceId.length === 0) {
1630
+ this._ui.createAddGeofencePromptError("Geofence ID is empty.");
1631
+ return;
1632
+ }
1633
+ if (!isValidGeofenceId(geofenceId)) {
1634
+ this._ui.createAddGeofencePromptError("Geofence ID contains special characters.");
1635
+ return;
1636
+ }
1637
+ if (!isExistingGeofenceId(geofenceId, this._loadedGeofences)) {
1638
+ this._ui.createAddGeofencePromptError("Geofence ID already exists.");
1639
+ return;
1640
+ }
1641
+ const feature = this._amplifyDraw.get(this._editingGeofenceId);
1642
+ const idToSave = geofenceId;
1643
+ const response = yield geo.Geo.saveGeofences({
1644
+ geofenceId: idToSave,
1645
+ geometry: { polygon: feature.geometry["coordinates"] },
1646
+ });
1647
+ if (response.errors[0]) {
1648
+ const err = response.errors[0];
1649
+ throw new Error(`There was an error saving geofence with id ${idToSave}: ${err.error.code} - ${err.error.message}`);
1650
+ }
1651
+ const success = response.successes[0];
1652
+ const savedGeofence = {
1653
+ geofenceId: success.geofenceId,
1654
+ geometry: { polygon: feature.geometry["coordinates"] },
1655
+ };
1656
+ // render geofence to the map and add it to the list
1657
+ this._loadGeofence(savedGeofence);
1658
+ this.displayGeofence(savedGeofence.geofenceId);
1659
+ this.setEditingModeEnabled(false);
1660
+ return savedGeofence.geofenceId;
1661
+ });
1662
+ }
1663
+ // Each page loads 100 geofences
1664
+ loadInitialGeofences() {
1665
+ return __awaiter(this, void 0, void 0, function* () {
1666
+ try {
1667
+ const { entries, nextToken } = yield geo.Geo.listGeofences();
1668
+ this._listGeofencesNextToken = nextToken;
1669
+ const loadGeofence = this._loadGeofence;
1670
+ entries.forEach((geofence) => loadGeofence(geofence));
1671
+ this._ui.updateGeofenceCount(Object.keys(this._loadedGeofences).length);
1672
+ }
1673
+ catch (e) {
1674
+ throw new Error(`Error calling listGeofences: ${e}`);
1675
+ }
1676
+ });
1677
+ }
1678
+ loadMoreGeofences() {
1679
+ return __awaiter(this, void 0, void 0, function* () {
1680
+ if (this._listGeofencesNextToken) {
1681
+ try {
1682
+ const { entries, nextToken } = yield geo.Geo.listGeofences({
1683
+ nextToken: this._listGeofencesNextToken,
1684
+ });
1685
+ this._listGeofencesNextToken = nextToken;
1686
+ const loadGeofence = this._loadGeofence;
1687
+ entries.forEach((geofence) => loadGeofence(geofence));
1688
+ this._ui.updateGeofenceCount(Object.keys(this._loadedGeofences).length);
1689
+ }
1690
+ catch (e) {
1691
+ throw new Error(`Error calling listGeofences: ${e}`);
1692
+ }
1693
+ }
1694
+ });
1695
+ }
1696
+ editGeofence(geofenceId) {
1697
+ this.setEditingModeEnabled(true);
1698
+ const geofence = this._loadedGeofences[geofenceId];
1699
+ if (!geofence) {
1700
+ throw new Error(`Geofence with id ${geofenceId} does not exist`);
1701
+ }
1702
+ // render in mapboxdraw
1703
+ const feature = getGeofenceFeatureFromPolygon(geofence.geometry.polygon);
1704
+ const data = Object.assign({ id: geofence.geofenceId }, feature);
1705
+ this._amplifyDraw.add(data);
1706
+ this._editingGeofenceId = geofence.geofenceId;
1707
+ }
1708
+ deleteGeofence(geofenceId) {
1709
+ return __awaiter(this, void 0, void 0, function* () {
1710
+ const response = yield geo.Geo.deleteGeofences(geofenceId);
1711
+ if (response.errors[0]) {
1712
+ const err = response.errors[0].error;
1713
+ throw new Error(`There was an error deleting geofence with id ${geofenceId}: ${err.code} - ${err.message}`);
1714
+ }
1715
+ this._ui.removeGeofenceListItem(geofenceId);
1716
+ delete this._loadedGeofences[geofenceId];
1717
+ this._ui.updateGeofenceCount(Object.keys(this._loadedGeofences).length);
1718
+ this._displayedGeofences = this._displayedGeofences.filter((geofence) => geofence.geofenceId !== geofenceId);
1719
+ this._updateDisplayedGeofences();
1720
+ return geofenceId;
1721
+ });
1722
+ }
1723
+ deleteSelectedGeofences() {
1724
+ const idsToDelete = this._displayedGeofences.map((fence) => fence.geofenceId);
1725
+ // FIXME: delete geofence api call here
1726
+ idsToDelete.forEach((id) => {
1727
+ this._ui.removeGeofenceListItem(id);
1728
+ delete this._loadedGeofences[id];
1729
+ });
1730
+ this._displayedGeofences = [];
1731
+ this._updateDisplayedGeofences();
1732
+ }
1733
+ /**********************************************************************
1734
+ Private methods for CRUD Geofences
1735
+ **********************************************************************/
1736
+ _loadGeofence(geofence) {
1737
+ // If geofence exists remove it from displayed geofences
1738
+ if (this._loadedGeofences[geofence.geofenceId]) {
1739
+ this._displayedGeofences = this._displayedGeofences.filter((fence) => fence.geofenceId !== geofence.geofenceId);
1740
+ }
1741
+ else {
1742
+ // If geofence doesn't exist render a new list item for it
1743
+ this._ui.renderListItem(geofence);
1744
+ }
1745
+ this._loadedGeofences[geofence.geofenceId] = geofence;
1746
+ this._ui.updateGeofenceCount(Object.keys(this._loadedGeofences).length);
1747
+ }
1748
+ displayGeofence(geofenceId) {
1749
+ this._displayedGeofences.push(this._loadedGeofences[geofenceId]);
1750
+ this._updateDisplayedGeofences();
1751
+ this._ui.updateCheckbox(geofenceId, true);
1752
+ }
1753
+ displayAllGeofences() {
1754
+ this._displayedGeofences.push(...Object.values(this._loadedGeofences));
1755
+ this._updateDisplayedGeofences();
1756
+ const checkboxes = document.getElementsByClassName("geofence-ctrl-list-item-checkbox");
1757
+ Array.from(checkboxes).forEach((checkbox) => (checkbox.checked = this._ui.getCheckboxAllValue()));
1758
+ }
1759
+ hideGeofence(geofenceId) {
1760
+ this._displayedGeofences = this._displayedGeofences.filter((geofence) => geofence.geofenceId !== geofenceId);
1761
+ this._updateDisplayedGeofences();
1762
+ this._ui.updateCheckbox(geofenceId, false);
1763
+ }
1764
+ hideAllGeofences() {
1765
+ this._displayedGeofences = [];
1766
+ this._updateDisplayedGeofences();
1767
+ const checkboxes = document.getElementsByClassName("geofence-ctrl-list-item-checkbox");
1768
+ Array.from(checkboxes).forEach((checkbox) => (checkbox.checked = this._ui.getCheckboxAllValue()));
1769
+ }
1770
+ _updateDisplayedGeofences() {
1771
+ const feature = getGeofenceFeatureArray(this._displayedGeofences);
1772
+ this._drawGeofencesOutput.setData(feature);
1773
+ }
1774
+ displayHighlightedGeofence(geofenceId) {
1775
+ const geofence = this._loadedGeofences[geofenceId];
1776
+ if (!geofence) {
1777
+ console.warn(`Geofence with id ${geofenceId} does not exist`);
1778
+ return;
1779
+ }
1780
+ const feature = getGeofenceFeatureFromPolygon(geofence.geometry.polygon);
1781
+ this._highlightedGeofenceOutput.setData(feature);
1782
+ this._highlightedGeofenceOutput.show();
1783
+ }
1784
+ hideHighlightedGeofence() {
1785
+ this._highlightedGeofenceOutput.hide();
1786
+ }
1787
+ /**********************************************************************
1788
+ Methods for controlling amplify mapbox draw
1789
+ **********************************************************************/
1790
+ changeMode(mode) {
1791
+ // erase existing mapbox draw content
1792
+ this._amplifyDraw.delete(this._editingGeofenceId);
1793
+ if (mode === "draw_circle") {
1794
+ this._amplifyDraw.drawCircularGeofence(this._editingGeofenceId);
1795
+ }
1796
+ else {
1797
+ this._amplifyDraw.drawPolygonGeofence(this._editingGeofenceId);
1798
+ }
1799
+ }
1800
+ // Disables add button and selecting items from geofence list
1801
+ setEditingModeEnabled(enabled) {
1802
+ enabled ? this._amplifyDraw.enable() : this._amplifyDraw.disable();
1803
+ enabled
1804
+ ? this._drawGeofencesOutput.hide()
1805
+ : this._drawGeofencesOutput.show();
1806
+ this._ui.setGeofenceListEnabled(!enabled);
1807
+ }
1808
+ updateInputRadius(event) {
1809
+ const radiusString = event.target.value;
1810
+ const radius = parseInt(radiusString);
1811
+ if (isNaN(radius)) {
1812
+ return;
1813
+ }
1814
+ this._amplifyDraw.drawCircularGeofence(this._editingGeofenceId, radius);
1815
+ }
1816
+ addEditableGeofence() {
1817
+ this._editingGeofenceId = "tempGeofence";
1818
+ this._amplifyDraw.drawCircularGeofence("tempGeofence");
1819
+ this.setEditingModeEnabled(true);
1820
+ }
1821
+ }
754
1822
 
755
1823
  exports.AmplifyGeocoderAPI = AmplifyGeocoderAPI;
1824
+ exports.AmplifyGeofenceControl = AmplifyGeofenceControl;
756
1825
  exports.AmplifyMapLibreRequest = AmplifyMapLibreRequest;
757
1826
  exports.createAmplifyGeocoder = createAmplifyGeocoder;
758
1827
  exports.createDefaultIcon = createDefaultIcon;