Skip to content

Commit

Permalink
map service update
Browse files Browse the repository at this point in the history
  • Loading branch information
abhilash-aot committed Jul 4, 2024
1 parent a13b329 commit 58fdf65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
10 changes: 6 additions & 4 deletions components/src/components/Map/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,14 @@ export default class Component extends (FieldComponent as any) {
onDrawnItemsChange: this.saveDrawnItems.bind(this),
});



// Load existing data if available
if (this.dataValue) {
this.mapService.loadDrawnItems(JSON.parse(this.dataValue));
try {
const parsedValue = JSON.parse(this.dataValue);
this.mapService.loadDrawnItems(parsedValue);
} catch (error) {
console.error('Failed to parse dataValue:', error);
}
}
}

Expand Down Expand Up @@ -120,7 +123,6 @@ export default class Component extends (FieldComponent as any) {
}
});


// Convert to JSON string
const jsonValue =
this.component.numPoints === 1
Expand Down
38 changes: 17 additions & 21 deletions components/src/components/Map/services/MapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ const DEFAULT_LAYER_ATTRIBUTION =
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
const DEFAULT_MAP_ZOOM = 13;
const DECIMALS_LATLNG = 5; // the number of decimals of latitude and longitude to be displayed in the marker popup
const COMPONENT_EDIT_CLASS = "component-edit-tabs";


const COMPONENT_EDIT_CLASS = 'component-edit-tabs';

interface MapServiceOptions {
mapContainer: HTMLElement;
Expand All @@ -20,7 +18,7 @@ interface MapServiceOptions {
form: HTMLCollectionOf<Element>;
numPoints: number;
defaultZoom?: number;
readOnlyMap?: boolean
readOnlyMap?: boolean;
onDrawnItemsChange: (items: any) => void; // Support both single and multiple items
}

Expand All @@ -42,7 +40,7 @@ class MapService {
let layer = e.layer;
if (drawnItems.getLayers().length === options.numPoints) {
L.popup()
.setLatLng(layer._latlng)
.setLatLng(layer.getLatLng())
.setContent('<p>Only one marker for submission</p>')
.openOn(map);
} else {
Expand All @@ -55,7 +53,8 @@ class MapService {
}

initializeMap(options: MapServiceOptions) {
let { mapContainer, center, drawOptions, form, defaultZoom, readOnlyMap } = options;
let { mapContainer, center, drawOptions, form, defaultZoom, readOnlyMap } =
options;
if (drawOptions.rectangle) {
drawOptions.rectangle.showArea = false;
}
Expand All @@ -72,7 +71,7 @@ class MapService {
map.addLayer(drawnItems);

// Add Drawing Controllers
if(!readOnlyMap){
if (!readOnlyMap) {
let drawControl = new L.Control.Draw({
draw: drawOptions,
edit: {
Expand All @@ -82,20 +81,20 @@ class MapService {
map.addControl(drawControl);
}

//Checking to see if the map should be interactable
const componentEditNode = document.getElementsByClassName(COMPONENT_EDIT_CLASS)
// Checking to see if the map should be interactable
const componentEditNode =
document.getElementsByClassName(COMPONENT_EDIT_CLASS);
if (form) {
if (form[0]?.classList.contains('formbuilder')) {
map.dragging.disable();
map.scrollWheelZoom.disable();
if (this.hasChildNode(componentEditNode[0], mapContainer)) {
map.dragging.enable();
map.scrollWheelZoom.enable();
}
map.dragging.disable();
map.scrollWheelZoom.disable();
if (this.hasChildNode(componentEditNode[0], mapContainer)) {
map.dragging.enable();
map.scrollWheelZoom.enable();
}
}
}
}

// Attach Controls to map
return { map, drawnItems };
}

Expand Down Expand Up @@ -132,15 +131,13 @@ class MapService {
loadDrawnItems(items: any) {
const { drawnItems } = this;

// Ensure drawnItems is defined before attempting to clear layers
if (!drawnItems) {
console.error('drawnItems is undefined');
return;
}

drawnItems.clearLayers();

// Check if items is an array
if (!Array.isArray(items)) {
items = [items];
}
Expand Down Expand Up @@ -172,12 +169,11 @@ class MapService {
}
for (let i = 0; i < parent?.childNodes?.length; i++) {
if (this.hasChildNode(parent.childNodes[i], targetNode)) {
return true
return true;
}
}
return false;
}

}

export default MapService;

0 comments on commit 58fdf65

Please sign in to comment.