// ===============================
// PANNELLO INFO + MAPPA
// ===============================
const infoPanel = document.getElementById("infoPanel");
let infoMapInstance = null;
// -------------------------------
// Helpers UI / stato
// -------------------------------
function isPanelOpen() {
return infoPanel.classList.contains("open") ||
infoPanel.getAttribute("aria-hidden") === "false" ||
infoPanel.getAttribute("data-open") === "1" ||
infoPanel.style.display === "block";
}
function markButtonActive(active) {
const btn = document.getElementById("modalInfoBtn");
if (btn) btn.classList.toggle("active", !!active);
}
// -------------------------------
// Render contenuti + (ri)creazione mappa
// -------------------------------
function renderInfo(photo) {
if (!photo) return;
const gps = photo.gps || { lat: "-", lng: "-", alt: "-" };
const loc = photo.location || {};
const folder = photo.cartella || "-";
infoPanel.innerHTML = `
Informazioni
Nome: ${photo.name ?? "-"}
Data: ${photo.taken_at ?? "-"}
Latitudine: ${gps.lat ?? "-"}
Longitudine: ${gps.lng ?? "-"}
Altitudine: ${gps.alt ?? "-"} m
Dimensioni: ${photo.width ?? "-"} × ${photo.height ?? "-"}
Peso: ${
photo.size_bytes
? (photo.size_bytes / 1024 / 1024).toFixed(2) + " MB"
: "-"
}
Tipo: ${photo.mime_type ?? "-"}
Cartella: ${folder}
Mappa
${
gps.lat !== "-" && gps.lng !== "-"
? ''
: ""
}
Location
${loc.continent ? `Continente: ${loc.continent}
` : ""}
${loc.country ? `Nazione: ${loc.country}
` : ""}
${loc.region ? `Regione: ${loc.region}
` : ""}
${loc.city ? `Città: ${loc.city}
` : ""}
${loc.address ? `Indirizzo: ${loc.address}
` : ""}
${loc.postcode ? `CAP: ${loc.postcode}
` : ""}
${loc.county_code ? `Provincia: ${loc.county_code}
` : ""}
${loc.timezone ? `Timezone: ${loc.timezone}
` : ""}
${loc.time ? `Offset: ${loc.time}
` : ""}
`;
// Reset mappa precedente
try { infoMapInstance?.remove(); } catch {}
infoMapInstance = null;
// Crea nuova mappa se ci sono coordinate
if (gps.lat !== "-" && gps.lng !== "-") {
setTimeout(() => {
try {
infoMapInstance = L.map("infoMap", {
zoomControl: false,
attributionControl: false
}).setView([gps.lat, gps.lng], 13);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 19
}).addTo(infoMapInstance);
L.marker([gps.lat, gps.lng]).addTo(infoMapInstance);
} catch (err) {
console.warn("Errore creazione mappa info:", err);
}
}, 50);
}
}
// -------------------------------
// API pubbliche: apri / chiudi / toggle
// -------------------------------
window.openInfoPanel = function(photo) {
renderInfo(photo || window.currentPhoto);
infoPanel.classList.add("open");
infoPanel.setAttribute("aria-hidden", "false");
infoPanel.setAttribute("data-open", "1");
markButtonActive(true);
};
window.closeInfoPanel = function() {
infoPanel.classList.remove("open");
infoPanel.setAttribute("aria-hidden", "true");
infoPanel.setAttribute("data-open", "0");
markButtonActive(false);
try { infoMapInstance?.remove(); } catch {}
infoMapInstance = null;
};
window.toggleInfoPanel = function(photo) {
if (isPanelOpen()) window.closeInfoPanel();
else window.openInfoPanel(photo || window.currentPhoto);
};
// -------------------------------
// Chiudi pannello cliccando FUORI
// -------------------------------
document.addEventListener("click", (e) => {
if (!isPanelOpen()) return;
const inside = infoPanel.contains(e.target);
const isBtn = e.target.id === "modalInfoBtn";
if (!inside && !isBtn) window.closeInfoPanel();
});
// -------------------------------
// Auto-refresh su cambio media nel modal
// -------------------------------
(() => {
const mediaContainer = document.getElementById("modalMediaContainer");
if (!mediaContainer) return;
const refreshIfOpen = () => {
if (!isPanelOpen()) return;
const photo = window.currentPhoto;
if (photo) renderInfo(photo);
};
const mo = new MutationObserver(() => {
setTimeout(refreshIfOpen, 0);
});
mo.observe(mediaContainer, { childList: true });
document.getElementById("modalPrev")?.addEventListener("click", () =>
setTimeout(refreshIfOpen, 0)
);
document.getElementById("modalNext")?.addEventListener("click", () =>
setTimeout(refreshIfOpen, 0)
);
document.addEventListener("keydown", (e) => {
if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
setTimeout(refreshIfOpen, 0);
}
});
})();