Added functions to delete custom markers

This commit is contained in:
Alexis
2024-01-13 14:53:37 +01:00
parent 244bd06f37
commit 3abf35ee58
2 changed files with 49 additions and 2 deletions

View File

@@ -296,6 +296,10 @@ map.setMaxBounds(
// List to be persisted in localStorage // List to be persisted in localStorage
let customMarkersList = [] let customMarkersList = []
/**
* Function that saves the customMarkersList object into the localStorage
* It also sends an event to refresh data on other components that use localStorage
*/
function saveCustomMarkers() { function saveCustomMarkers() {
localStorage.setItem('custom-markers', JSON.stringify(customMarkersList)) localStorage.setItem('custom-markers', JSON.stringify(customMarkersList))
@@ -315,7 +319,7 @@ function storeLastHeldPositions(e) {
/** /**
* Function that creates custom markers and places them on the map * Function that creates custom markers and places them on the map
* @param {string?} markerTitle * @param {string} markerTitle
*/ */
function addCustomMarker(markerTitle) { function addCustomMarker(markerTitle) {
const lon = Number(localStorage.getItem('lastHeldXPosition')) const lon = Number(localStorage.getItem('lastHeldXPosition'))
@@ -345,6 +349,7 @@ function addCustomMarker(markerTitle) {
/** /**
* Register event listener to fly to this custom marker * Register event listener to fly to this custom marker
* TODO : Refactor these functions with the loadCustomMarkersFromStorage implementation
*/ */
document.addEventListener(`fly-to-${markerTitle}`, (e) => { document.addEventListener(`fly-to-${markerTitle}`, (e) => {
map.flyTo(customMarkerCoords, flyToZoomLevel, { duration: flyToDuration }) map.flyTo(customMarkerCoords, flyToZoomLevel, { duration: flyToDuration })
@@ -353,6 +358,14 @@ function addCustomMarker(markerTitle) {
}, flyToDuration * 1000) }, flyToDuration * 1000)
}) })
/**
* Register event listener to remove this custom marker
* TODO : Refactor these functions with the loadCustomMarkersFromStorage implementation
*/
document.addEventListener(`delete-${markerTitle}`, () => {
removeCustomMarker(customMarker, markerTitle)
})
customMarkersList.push({ customMarkersList.push({
lat, lat,
lon, lon,
@@ -395,6 +408,7 @@ function loadCustomMarkersFromStorage() {
/** /**
* Register event listener to fly to this custom marker * Register event listener to fly to this custom marker
* TODO : Refactor these functions with the addCustomMarker implementation
*/ */
document.addEventListener(`fly-to-${m.title}`, (e) => { document.addEventListener(`fly-to-${m.title}`, (e) => {
map.flyTo(savedMarkerCoords, flyToZoomLevel, { duration: flyToDuration }) map.flyTo(savedMarkerCoords, flyToZoomLevel, { duration: flyToDuration })
@@ -402,10 +416,33 @@ function loadCustomMarkersFromStorage() {
savedMarker.openPopup() savedMarker.openPopup()
}, flyToDuration * 1000) }, flyToDuration * 1000)
}) })
/**
* Register event listener to remove this custom marker
* TODO : Refactor these functions with the addCustomMarker implementation
*/
document.addEventListener(`delete-${m.title}`, () => {
removeCustomMarker(savedMarker, m.title)
})
} }
} }
loadCustomMarkersFromStorage() loadCustomMarkersFromStorage()
/**
* Function that deletes a specific marker from storage and removes it from the map
* @param {Marker} marker Leaflet marker object
* @param {string} markerTitle Key for custom marker
*/
function removeCustomMarker(marker, markerTitle) {
marker.remove()
customMarkersList = customMarkersList.filter(m => {
return m.title !== markerTitle
})
saveCustomMarkers()
}
/** /**
* UTILITIES * UTILITIES
*/ */

View File

@@ -18,6 +18,7 @@ const allMarkers = computed(() => {
}) })
// Search functions // Search functions
const searchBar = ref<HTMLDivElement>()
const qInput = ref() const qInput = ref()
const { focused: isFocused } = useFocus(qInput) const { focused: isFocused } = useFocus(qInput)
@@ -179,6 +180,15 @@ onMounted(() => {
customMarkersData.value = useLocalStorage('custom-markers', []).value customMarkersData.value = useLocalStorage('custom-markers', []).value
}) })
}) })
/**
* Emit event to delete custom marker
*/
function emitDeleteCustomMarker(markerTitle: string) {
const deleteCMarkerEvent = new CustomEvent(`delete-${markerTitle}`, { bubbles: true })
searchBar.value?.dispatchEvent(deleteCMarkerEvent)
}
</script> </script>
<template> <template>
@@ -222,7 +232,7 @@ onMounted(() => {
<i class="ph-light ph-pencil-simple-line"></i> <i class="ph-light ph-pencil-simple-line"></i>
</button> --> </button> -->
<button class="btn btn-danger btn-icon btn-sm"> <button class="btn btn-danger btn-icon btn-sm" @click="emitDeleteCustomMarker(m.title)">
<i class="ph-light ph-trash"></i> <i class="ph-light ph-trash"></i>
</button> </button>
</div> </div>