server_photo_s85js/public/js/config.js
2026-08-11 08:45:04 +02:00

129 lines
2.5 KiB
JavaScript

// public/js/config.js
// Configurazione globale caricata dal backend.
(() => {
"use strict";
const DEFAULT_CONFIG = {
ready: false,
baseUrl: "",
pathFull: "",
galleryRefreshSeconds: 60,
mapboxToken: null
};
// Mantiene eventuali valori già inizializzati nell'HTML.
window.AppConfig = {
...DEFAULT_CONFIG,
...(window.AppConfig || {})
};
window.BASE_URL =
window.BASE_URL ?? "";
window.PATH_FULL =
window.PATH_FULL ?? "";
window.PHOTOS_URL =
window.PHOTOS_URL ?? "";
window.MEDIA_BASE_ORIGIN =
window.MEDIA_BASE_ORIGIN ?? "";
// Mantiene invariati i percorsi restituiti dal backend.
function toAbsoluteUrl(pathOrUrl) {
return pathOrUrl
? String(pathOrUrl)
: "";
}
window.toAbsoluteUrl = toAbsoluteUrl;
async function loadConfig() {
try {
const response = await fetch("/config", {
method: "GET",
cache: "no-store",
headers: {
Accept: "application/json"
}
});
if (!response.ok) {
throw new Error(
`Errore configurazione: HTTP ${response.status}`
);
}
const data = await response.json();
window.AppConfig = {
...DEFAULT_CONFIG,
...data,
ready: true
};
window.BASE_URL =
window.AppConfig.baseUrl || "";
window.PATH_FULL =
window.AppConfig.pathFull || "";
console.log(
"[config] Configurazione caricata",
{
ready:
window.AppConfig.ready,
baseUrl:
window.AppConfig.baseUrl,
pathFullConfigured:
Boolean(
window.AppConfig.pathFull
),
mapboxConfigured:
Boolean(
window.AppConfig.mapboxToken
)
}
);
window.dispatchEvent(
new CustomEvent("config:ready", {
detail: window.AppConfig
})
);
return window.AppConfig;
} catch (error) {
console.error(
"[config] Errore caricamento:",
error
);
window.AppConfig = {
...DEFAULT_CONFIG,
...(window.AppConfig || {}),
ready: true,
error: error.message
};
window.dispatchEvent(
new CustomEvent("config:error", {
detail: {
error,
config: window.AppConfig
}
})
);
return window.AppConfig;
}
}
// Promessa globale usata da mapBaseLayers.js.
window.AppConfigPromise = loadConfig();
})();