diff --git a/app.js b/app.js index 6e4288b..8f8399c 100644 --- a/app.js +++ b/app.js @@ -1,16 +1,54 @@ +// =============================== +// CONFIG +// =============================== const API_URL = 'https://prova.patachina.it/photos'; +let photosData = []; +let currentPhoto = null; + +// =============================== +// DEBUG INIZIALE +// =============================== +console.log("app.js caricato correttamente"); + + +// =============================== +// FETCH DELLE FOTO +// =============================== async function loadPhotos() { - const res = await fetch(API_URL); - if (!res.ok) { - console.error('Errore nel fetch', res.status); + console.log("Inizio fetch:", API_URL); + + let res; + try { + res = await fetch(API_URL); + } catch (e) { + console.error("Errore fetch:", e); return; } - const data = await res.json(); - renderGallery(data); + + console.log("Status fetch:", res.status); + + const text = await res.text(); + console.log("Risposta grezza (prime 200 chars):", text.substring(0, 200)); + + try { + photosData = JSON.parse(text); + console.log("JSON parse OK, numero foto:", photosData.length); + } catch (e) { + console.error("Errore nel parse JSON:", e); + return; + } + + renderGallery(photosData); } + +// =============================== +// RENDER GALLERIA +// =============================== function renderGallery(photos) { + console.log("Render gallery, foto:", photos.length); + const gallery = document.getElementById('gallery'); gallery.innerHTML = ''; @@ -19,29 +57,48 @@ function renderGallery(photos) { thumbDiv.className = 'thumb'; const img = document.createElement('img'); - // anteprima img.src = `https://prova.patachina.it/${photo.thub1}`; img.alt = photo.name || ''; + img.loading = "lazy"; thumbDiv.appendChild(img); - // click: apri immagine grande + // Fallback se thub2 manca + const preview = photo.thub2 + ? `https://prova.patachina.it/${photo.thub2}` + : `https://prova.patachina.it/${photo.thub1}`; + thumbDiv.addEventListener('click', () => { - openModal(`https://prova.patachina.it/${photo.path}`); + openModal( + `https://prova.patachina.it/${photo.path}`, + preview, + photo + ); }); gallery.appendChild(thumbDiv); }); } -// Modal + +// =============================== +// MODALE IMMAGINE +// =============================== const modal = document.getElementById('modal'); const modalImg = document.getElementById('modalImage'); const modalClose = document.getElementById('modalClose'); -function openModal(src) { - modalImg.src = src; +function openModal(srcOriginal, srcPreview, photo) { + currentPhoto = photo; + + modalImg.src = srcPreview; modal.classList.add('open'); + + const img = new Image(); + img.src = srcOriginal; + img.onload = () => { + modalImg.src = srcOriginal; + }; } function closeModal() { @@ -54,5 +111,56 @@ modal.addEventListener('click', (e) => { if (e.target === modal) closeModal(); }); -// avvio + +// =============================== +// PANNELLO INFO +// =============================== +const infoPanel = document.getElementById('infoPanel'); +const infoBtn = document.getElementById('modalInfoBtn'); + +infoBtn.addEventListener('click', () => { + if (!currentPhoto) return; + + // Gestione sicura GPS null + const gps = currentPhoto.gps || { lat: '-', lng: '-', alt: '-' }; + + // Cartella estratta dal path + const folder = currentPhoto.path.split('/').slice(2, -1).join('/'); + + infoPanel.innerHTML = ` +