my_gallery/js/gallery.js
2026-02-22 01:17:13 +01:00

149 lines
3.9 KiB
JavaScript

// ===============================
// ORDINAMENTO
// ===============================
function sortByDate(photos, direction = "desc") {
return photos.slice().sort((a, b) => {
const da = new Date(a.taken_at);
const db = new Date(b.taken_at);
return direction === "asc" ? da - db : db - da;
});
}
// ===============================
// FILTRI (base, estendibili)
// ===============================
function applyFilters(photos) {
if (!currentFilter) return photos;
switch (currentFilter) {
case "folder":
return photos.filter(p => p.folder); // da migliorare
case "location":
return photos.filter(p => p.gps && p.gps.lat);
case "type":
return photos.filter(p => p.mime_type.startsWith("image/"));
default:
return photos;
}
}
// ===============================
// RAGGRUPPAMENTO STILE GOOGLE PHOTOS
// ===============================
function groupByDate(photos, mode = "auto") {
const sections = [];
const now = new Date();
function getLabel(photo) {
const date = new Date(photo.taken_at);
const diffDays = Math.floor((now - date) / (1000 * 60 * 60 * 24));
if (mode === "day") return formatDay(date);
if (mode === "month") return formatMonth(date);
if (mode === "year") return date.getFullYear().toString();
// modalità AUTO (Google Photos)
if (diffDays === 0) return "Oggi";
if (diffDays === 1) return "Ieri";
if (diffDays <= 7) return "Questa settimana";
if (diffDays <= 14) return "La settimana scorsa";
if (diffDays <= 30) return "Questo mese";
if (diffDays <= 60) return "Mese scorso";
// stesso anno → raggruppa per mese
if (date.getFullYear() === now.getFullYear()) {
return formatMonth(date);
}
// anni precedenti → raggruppa per anno
return date.getFullYear().toString();
}
photos.forEach(photo => {
const label = getLabel(photo);
let section = sections.find(s => s.label === label);
if (!section) {
section = { label, photos: [] };
sections.push(section);
}
section.photos.push(photo);
});
return sections;
}
// ===============================
// FORMATTATORI
// ===============================
function formatDay(date) {
return date.toLocaleDateString("it-IT", {
weekday: "long",
day: "numeric",
month: "long"
});
}
function formatMonth(date) {
return date.toLocaleDateString("it-IT", {
month: "long",
year: "numeric"
});
}
// ===============================
// RENDER GALLERIA A SEZIONI
// ===============================
function renderGallery(sections) {
const gallery = document.getElementById("gallery");
gallery.innerHTML = "";
sections.forEach(section => {
// HEADER DELLA SEZIONE
const h = document.createElement("h2");
h.className = "gallery-section-title";
h.textContent = section.label;
gallery.appendChild(h);
// CONTENITORE DELLE FOTO
const container = document.createElement("div");
container.className = "gallery-section";
section.photos.forEach(photo => {
const thumbDiv = document.createElement("div");
thumbDiv.className = "thumb";
const img = document.createElement("img");
img.src = `https://prova.patachina.it/${photo.thub1}`;
img.alt = photo.name || "";
img.loading = "lazy";
thumbDiv.appendChild(img);
if (photo.mime_type.startsWith("video/")) {
const play = document.createElement("div");
play.className = "play-icon";
play.textContent = "▶";
thumbDiv.appendChild(play);
}
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}`,
preview,
photo
);
});
container.appendChild(thumbDiv);
});
gallery.appendChild(container);
});
}