ruscomap/public/js/eventHandler.js

115 lines
4.6 KiB
JavaScript
Raw Normal View History

2024-06-30 22:56:19 +02:00
import { basicElements } from "./getHtmlElements.js";
2024-08-23 11:06:43 +02:00
import { createMarkerTemp } from "./markerHandler.js";
2024-08-25 15:09:27 +02:00
import { mapInit } from "./mapInit.js";
2024-06-30 22:56:19 +02:00
2024-08-22 00:35:33 +02:00
export function initEvents(map) {
2024-06-30 22:56:19 +02:00
2024-08-22 00:35:33 +02:00
let editMode = false
let isTmpMarkerSet = false
let tmpMarkerSet = {}
const [addMarkerBtn, markerFormContainer, markerForm, noticeCoordsPicker, remainingForm] = basicElements()
2024-06-30 22:56:19 +02:00
markerForm.addEventListener("submit", processForm);
2024-08-18 19:14:27 +02:00
2024-08-22 00:35:33 +02:00
function setStyleForEditing(){
markerFormContainer.style.display = 'flex';
noticeCoordsPicker.style.display = 'block'; //messaggio inserimento
2024-08-22 00:35:33 +02:00
remainingForm.style.display = 'none'; // form dati
// imposta una X rossa come icona pulsante, come icona cursore un marker e entra nella modalità inserimento.
document.getElementById("addMarkerBtn").innerHTML = '<img src="../img/xred20.png" alt="Button Image" />';
document.getElementById('map').style = 'cursor: url("js/dist/images/marker-icon.png") 13 41, auto;';
2024-08-22 00:35:33 +02:00
}
2024-08-18 19:14:27 +02:00
2024-08-22 00:35:33 +02:00
function setStyleNotEditing() {
markerForm.reset();
document.getElementById("addMarkerBtn").innerHTML = '<img src="../img/marker-icon18.png" alt="Button Image" />';
document.getElementById('map').style = 'cursor: auto;';
noticeCoordsPicker.style.display = 'none'; //messaggio inserimento
remainingForm.style.display = 'none'; // form dati
markerFormContainer.style.display = 'none';
}
2024-08-18 19:14:27 +02:00
2024-08-22 00:35:33 +02:00
addMarkerBtn.onclick = function (event) {
event.stopPropagation();
if (!editMode) {
setStyleForEditing()
2024-08-18 20:01:48 +02:00
editMode = true;
2024-08-22 00:35:33 +02:00
isTmpMarkerSet = false;
tmpMarkerSet = {};
2024-08-23 17:05:21 +02:00
document.querySelector('.file-chosen').textContent = '';
2024-08-22 00:35:33 +02:00
} else {
setStyleNotEditing();
editMode = false;
map.removeLayer(tmpMarkerSet)
isTmpMarkerSet = false;
tmpMarkerSet = {};
2024-08-18 19:14:27 +02:00
}
2024-06-30 22:56:19 +02:00
};
2024-08-16 23:55:03 +02:00
2024-08-22 17:29:00 +02:00
//event listener onclick on map, inserisce marker il temporaneo e mostra il remainingForm
2024-06-30 22:56:19 +02:00
map.on("click", function (e) {
2024-08-22 00:35:33 +02:00
if (editMode && !isTmpMarkerSet) {
2024-06-30 22:56:19 +02:00
let formInputCoordY = document.getElementById('formInputCoordY')
let formInputCoordX = document.getElementById('formInputCoordX')
formInputCoordY.value = e.latlng.lat
formInputCoordX.value = e.latlng.lng
noticeCoordsPicker.style.display = 'none';
remainingForm.style.display = 'block';
2024-08-17 01:03:14 +02:00
map.getContainer().style.cursor = 'auto';
2024-08-22 18:26:33 +02:00
const formattedDate = new Date().toISOString();
const tmpMarker = {
2024-08-22 00:35:33 +02:00
coordinate: { x: formInputCoordX.value, y: formInputCoordY.value },
2024-08-22 17:49:30 +02:00
name: "",
2024-08-22 17:29:00 +02:00
description: "Compila il form per aggiungere un marker qui. Oppure annulla l'inserimento del marker cliccando sulla X in alto a destra",
2024-08-22 17:49:30 +02:00
filename: "../img/trashw.png",
2024-08-22 18:26:33 +02:00
ts: formattedDate,
}
2024-08-23 11:06:43 +02:00
tmpMarkerSet = createMarkerTemp(tmpMarker, L, map, true)
2024-08-22 00:35:33 +02:00
isTmpMarkerSet = true
2024-06-30 22:56:19 +02:00
}
})
2024-08-22 00:35:33 +02:00
function handleSentFormStyle() {
document.getElementById("addMarkerBtn").innerHTML = '<img src="../img/marker-icon18.png" alt="Button Image" />';
document.getElementById('map').style = 'cursor: auto;';
markerFormContainer.style.display = 'none'; //tutto
const remainingForm = document.getElementById("remainingForm")
remainingForm.style.display = 'none';
document.getElementById("addMarkerBtn").innerHTML = '<img src="../img/marker-icon18.png" alt="Button Image" />';
document.getElementById("markerFormContainer").style.display = 'none';
}
async function processForm(e) {
e.preventDefault();
handleSentFormStyle()
// imposta un marker come icona pulsante, icona cursore normale e esce dalla modalità inserimento.
editMode = false;
isTmpMarkerSet = false;
const markerForm = document.getElementById("markerForm")
const formData = new FormData(markerForm)
markerForm.reset();
await fetch('/uploadMarker', {
method: 'POST',
body: formData,
}).then(async () => {
await map.fetchNewMarkers().then( () =>
map.removeLayer(tmpMarkerSet)
);
}
)
};
}
document.getElementById('file-input').addEventListener('change', function () {
const fileName = this.files[0] ? this.files[0].name : 'No file chosen';
const truncatedFileName = fileName.length > 21 ? fileName.slice(0, 21) + '...' : fileName;
document.querySelector('.file-chosen').textContent = truncatedFileName;
2024-08-22 17:29:00 +02:00
});
2024-08-25 15:09:27 +02:00