const COLLAPSE_STORAGE_KEY = "yunohost-portal-collapsed-groups"; // Page affichee dans l'apercu de l'editeur (editor.html) : la config arrive // par postMessage et les liens ne doivent pas quitter l'apercu. const IS_PREVIEW = window.parent !== window && new URLSearchParams(window.location.search).has("preview"); function loadCollapsedState() { try { return JSON.parse(localStorage.getItem(COLLAPSE_STORAGE_KEY)) || {}; } catch (e) { return {}; } } function saveCollapsedState(state) { try { localStorage.setItem(COLLAPSE_STORAGE_KEY, JSON.stringify(state)); } catch (e) { /* ignore */ } } function guessFaviconUrl(appUrl) { try { return new URL("favicon.ico", new URL(appUrl, window.location.href)).href; } catch (e) { return null; } } function showInitialsFallback(icon, app) { icon.innerHTML = ""; icon.style.background = app.color || "#8e8e8e"; icon.textContent = app.initials || app.name.slice(0, 2).toUpperCase(); } function tileNode(app) { const a = document.createElement("a"); a.className = "tile"; a.href = app.url; const icon = document.createElement("div"); icon.className = "tile-icon"; const iconSrc = app.icon || guessFaviconUrl(app.url); if (iconSrc) { const img = document.createElement("img"); img.src = iconSrc; img.alt = ""; img.addEventListener("error", () => showInitialsFallback(icon, app), { once: true }); icon.appendChild(img); } else { showInitialsFallback(icon, app); } const name = document.createElement("div"); name.className = "tile-name"; name.textContent = app.name; a.appendChild(icon); a.appendChild(name); return a; } function groupNode(group, collapsedState) { const wrapper = document.createElement("section"); wrapper.className = "group"; const isCollapsed = collapsedState[group.id] ?? group.collapsed ?? false; if (isCollapsed) wrapper.classList.add("collapsed"); const header = document.createElement("div"); header.className = "group-header"; const toggle = document.createElement("span"); toggle.className = "group-toggle"; toggle.textContent = "▾"; const title = document.createElement("span"); title.className = "group-title"; title.textContent = group.name; const count = document.createElement("span"); count.className = "group-count"; count.textContent = `(${group.apps.length})`; header.appendChild(toggle); header.appendChild(title); header.appendChild(count); header.addEventListener("click", () => { const nowCollapsed = wrapper.classList.toggle("collapsed"); collapsedState[group.id] = nowCollapsed; saveCollapsedState(collapsedState); }); const grid = document.createElement("div"); grid.className = "tile-grid"; group.apps.forEach((app) => grid.appendChild(tileNode(app))); wrapper.appendChild(header); wrapper.appendChild(grid); return wrapper; } // Compte connecte, lu dans le portail YunoHost (/yunohost/portalapi/me) : // chaque utilisateur voit son propre nom. Le bloc "user" de config.json ne // sert plus que de valeur de secours (portail injoignable). let portalUser = null; let fallbackUser = {}; function renderUser() { const u = portalUser || {}; const f = fallbackUser || {}; const username = u.username || f.username || ""; const fullname = u.fullname || f.fullname || ""; const address = u.mail || (f.username && f.domain ? `${f.username}@${f.domain}` : ""); document.getElementById("user-fullname").textContent = username; document.getElementById("user-sub").textContent = fullname; document.getElementById("user-domain").textContent = address; } function loadPortalUser() { fetch("/yunohost/portalapi/me", { credentials: "same-origin", cache: "no-store" }) .then((r) => (r.ok ? r.json() : null)) .then((me) => { if (!me || !me.username) return; portalUser = { username: me.username, fullname: me.fullname, mail: me.mail }; renderUser(); }) .catch(() => { /* portail injoignable : on garde config.json */ }); } function render(config) { fallbackUser = config.user || {}; renderUser(); const adminUrl = config.portal.adminUrl || "/yunohost/admin/"; document.getElementById("admin-btn").href = adminUrl; document.getElementById("footer-admin").href = adminUrl; document.getElementById("footer-edit-profile").href = config.portal.editProfileUrl || "#"; document.getElementById("edit-profile").href = config.portal.editProfileUrl || "#"; document.getElementById("footer-doc").href = config.portal.documentationUrl || "#"; document.getElementById("footer-support").href = config.portal.supportUrl || "#"; // Icone d'onglet forcee dans config.json ; sinon favicon.js reprend le // logo de l'app defini dans YunoHost. if (config.portal.favicon && window.homepageSetFavicon) { window.homepageFaviconOverride = true; window.homepageSetFavicon(config.portal.favicon); } const logoutUrl = config.portal.logoutUrl || "/yunohost/portalapi/logout"; document.getElementById("logout-btn").onclick = (e) => { e.preventDefault(); fetch(logoutUrl, { credentials: "same-origin" }).finally(() => { window.location.href = "/yunohost/sso/"; }); }; const app = document.getElementById("app"); app.innerHTML = ""; const collapsedState = loadCollapsedState(); if (config.pinned && config.pinned.length) { const section = document.createElement("section"); section.className = "pinned-section"; const grid = document.createElement("div"); grid.className = "tile-grid"; config.pinned.forEach((appItem) => grid.appendChild(tileNode(appItem))); section.appendChild(grid); app.appendChild(section); } const groups = [...(config.groups || [])]; groups.forEach((group) => { app.appendChild(groupNode(group, collapsedState)); }); if (config.ungrouped && config.ungrouped.length) { const section = document.createElement("section"); section.className = "ungrouped-section"; const grid = document.createElement("div"); grid.className = "tile-grid"; config.ungrouped.forEach((appItem) => grid.appendChild(tileNode(appItem))); section.appendChild(grid); app.appendChild(section); } } // Le lien "Editer la page d'accueil" n'apparait que pour les comptes ayant // la permission YunoHost .editor. C'est SSOwat qui protege /edit/ : un // compte sans la permission recoit un refus ou une redirection vers le // portail (redirect "manual" -> reponse opaque, r.ok faux), et le lien reste // cache. function showEditLinkIfAllowed() { if (IS_PREVIEW) return; fetch("edit/api.php?action=whoami", { credentials: "same-origin", cache: "no-store", redirect: "manual" }) .then((r) => (r.ok ? r.json() : null)) .then((me) => { if (me && me.canEdit) document.getElementById("footer-edit-home").hidden = false; }) .catch(() => {}); } if (IS_PREVIEW) { // Dans l'apercu : liens ouverts dans un nouvel onglet, deconnexion inactive. document.addEventListener("click", (e) => { const a = e.target.closest("a"); if (!a) return; if (a.id === "logout-btn") { e.preventDefault(); e.stopImmediatePropagation(); return; } a.target = "_blank"; a.rel = "noopener"; }, true); window.addEventListener("message", (e) => { if (e.origin !== window.location.origin || e.source !== window.parent) return; if (e.data && e.data.type === "homepage-config") render(e.data.config); }); } fetch("config.json", { cache: "no-cache" }) .then((r) => r.json()) .then(render) .catch((err) => { document.getElementById("app").textContent = "Erreur de chargement de la configuration (config.json)."; console.error(err); }) .finally(() => { if (IS_PREVIEW) window.parent.postMessage({ type: "homepage-preview-ready" }, window.location.origin); }); loadPortalUser(); showEditLinkIfAllowed();