198 lines
No EOL
3.8 KiB
JavaScript
198 lines
No EOL
3.8 KiB
JavaScript
// public/js/gallery.js
|
|
// Coordinatore della galleria.
|
|
|
|
(() => {
|
|
"use strict";
|
|
|
|
const ACTIVITY_EXTENSIONS = [
|
|
"fit",
|
|
"tcx",
|
|
"gpx",
|
|
];
|
|
|
|
function isAuthenticated() {
|
|
return (
|
|
document.body.classList
|
|
.contains("authenticated") &&
|
|
window.AppAuth &&
|
|
typeof window.AppAuth
|
|
.isLoggedIn === "function" &&
|
|
window.AppAuth.isLoggedIn()
|
|
);
|
|
}
|
|
|
|
function filterByMode(items) {
|
|
if (
|
|
window.state?.mode ===
|
|
"photos"
|
|
) {
|
|
return items.filter(
|
|
(item) =>
|
|
item.mime_type
|
|
?.startsWith("image/") ||
|
|
item.mime_type
|
|
?.startsWith("video/"),
|
|
);
|
|
}
|
|
|
|
return items.filter(
|
|
(item) => {
|
|
const format =
|
|
String(
|
|
item.format ||
|
|
item.file_extension ||
|
|
item.mime_type ||
|
|
"",
|
|
).toLowerCase();
|
|
|
|
const name =
|
|
String(
|
|
item.name ||
|
|
item.file_name ||
|
|
item.path ||
|
|
"",
|
|
).toLowerCase();
|
|
|
|
return ACTIVITY_EXTENSIONS.some(
|
|
(extension) =>
|
|
format === extension ||
|
|
format.includes(
|
|
extension,
|
|
) ||
|
|
name.endsWith(
|
|
`.${extension}`,
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
function refreshGallery() {
|
|
if (!isAuthenticated()) {
|
|
console.warn(
|
|
"[refreshGallery] rendering bloccato: autenticazione non completata",
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
const startedAt =
|
|
performance.now();
|
|
|
|
let items =
|
|
window.getLocalItems?.() ||
|
|
[];
|
|
|
|
console.log(
|
|
`[refreshGallery] mode=${window.state?.mode} items=${items.length}`,
|
|
);
|
|
|
|
items =
|
|
filterByMode(items);
|
|
|
|
const showDeleted =
|
|
document.getElementById(
|
|
"showDeleted",
|
|
)?.checked;
|
|
|
|
if (!showDeleted) {
|
|
items =
|
|
items.filter(
|
|
(item) =>
|
|
!item.deleted_at,
|
|
);
|
|
}
|
|
|
|
const filtered =
|
|
window.GalleryUtils
|
|
.applyFilters(items);
|
|
|
|
const sorted =
|
|
window.GalleryUtils
|
|
.sortByDate(
|
|
filtered,
|
|
window.currentSort ||
|
|
"desc",
|
|
);
|
|
|
|
const sections =
|
|
window.GalleryUtils
|
|
.groupByDate(
|
|
sorted,
|
|
window.currentGroup ||
|
|
"auto",
|
|
);
|
|
|
|
window.GalleryRenderer
|
|
.renderGallery(sections);
|
|
|
|
console.log(
|
|
`[refreshGallery] render completato in ${Math.round(
|
|
performance.now() -
|
|
startedAt,
|
|
)} ms`,
|
|
);
|
|
}
|
|
|
|
function initializeGallery() {
|
|
if (!isAuthenticated()) {
|
|
console.warn(
|
|
"[gallery] inizializzazione bloccata: autenticazione non completata",
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
const checkbox =
|
|
document.getElementById(
|
|
"showDeleted",
|
|
);
|
|
|
|
if (checkbox) {
|
|
checkbox.checked =
|
|
localStorage.getItem(
|
|
"showDeleted",
|
|
) === "1";
|
|
|
|
checkbox.addEventListener(
|
|
"change",
|
|
() => {
|
|
if (!isAuthenticated()) {
|
|
return;
|
|
}
|
|
|
|
localStorage.setItem(
|
|
"showDeleted",
|
|
checkbox.checked
|
|
? "1"
|
|
: "0",
|
|
);
|
|
|
|
refreshGallery();
|
|
},
|
|
);
|
|
}
|
|
|
|
window.loadLocalState?.();
|
|
|
|
refreshGallery();
|
|
}
|
|
|
|
window.refreshGallery =
|
|
refreshGallery;
|
|
|
|
if (
|
|
document.readyState ===
|
|
"loading"
|
|
) {
|
|
document.addEventListener(
|
|
"DOMContentLoaded",
|
|
initializeGallery,
|
|
{
|
|
once: true,
|
|
},
|
|
);
|
|
} else {
|
|
initializeGallery();
|
|
}
|
|
})(); |