Interface : magasin, routeur, brouillon de séance et modules purs testés
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
74db3014f0
commit
ba08daaa89
@@ -0,0 +1,112 @@
|
||||
// Un écran par adresse, dans le fragment (#/…) : l'app reste un site statique
|
||||
// et le bouton retour du navigateur fonctionne.
|
||||
//
|
||||
// Les fragments #import= (lien d'une IA, ADR 0005 point 7) et #pair=
|
||||
// (appariement, lot 2) sont réservés : le routeur ne les interprète pas et ne
|
||||
// les réécrit pas. À brancher : link.js (#import=), appariement (#pair=).
|
||||
|
||||
/**
|
||||
* @typedef {{name: "home"}
|
||||
* | {name: "session", key: string}
|
||||
* | {name: "history"}
|
||||
* | {name: "history-edit", id: string}
|
||||
* | {name: "reserved"}
|
||||
* | {name: "not-found"}} Route
|
||||
*/
|
||||
|
||||
/** Fragments réservés à d'autres modules. */
|
||||
export const RESERVED = ["#import=", "#pair="];
|
||||
|
||||
/**
|
||||
* Lit une adresse. Fonction pure, testée.
|
||||
* @param {string} hash location.hash, avec son #
|
||||
* @returns {Route}
|
||||
*/
|
||||
export function parseRoute(hash) {
|
||||
if (RESERVED.some((p) => hash.startsWith(p))) return { name: "reserved" };
|
||||
const path = hash.replace(/^#\/?/, "").replace(/\/+$/, "");
|
||||
if (path === "") return { name: "home" };
|
||||
const parts = path.split("/").map((p) => {
|
||||
try { return decodeURIComponent(p); } catch { return p; }
|
||||
});
|
||||
if (parts[0] === "seance" && parts.length === 2 && parts[1]) return { name: "session", key: parts[1] };
|
||||
if (parts[0] === "historique" && parts.length === 1) return { name: "history" };
|
||||
if (parts[0] === "historique" && parts.length === 2 && parts[1]) return { name: "history-edit", id: parts[1] };
|
||||
return { name: "not-found" };
|
||||
}
|
||||
|
||||
/** Adresses, pour écrire les liens sans les recopier. */
|
||||
export const href = {
|
||||
home: () => "#/",
|
||||
session: (/** @type {string} */ key) => "#/seance/" + encodeURIComponent(key),
|
||||
history: () => "#/historique",
|
||||
historyEdit: (/** @type {string} */ id) => "#/historique/" + encodeURIComponent(id),
|
||||
};
|
||||
|
||||
/**
|
||||
* @typedef {object} Screen
|
||||
* @property {HTMLElement} node
|
||||
* @property {string} title titre de l'onglet
|
||||
* @property {() => void} [destroy] appelé quand on quitte l'écran
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} Navigation
|
||||
* @property {(hash: string, options?: {replace?: boolean}) => void} go
|
||||
* @property {(fallback?: string) => void} back retour dans l'app, ou vers fallback
|
||||
*/
|
||||
|
||||
/**
|
||||
* Branche le routeur sur la fenêtre.
|
||||
* @param {HTMLElement} root
|
||||
* @param {(route: Route, nav: Navigation) => Screen} render
|
||||
*/
|
||||
export function startRouter(root, render) {
|
||||
/** Adresses vues pendant la visite, pour savoir si « retour » reste dans l'app. */
|
||||
const visited = [location.hash];
|
||||
/** @type {Screen|null} */
|
||||
let current = null;
|
||||
|
||||
/** @type {Navigation} */
|
||||
const nav = {
|
||||
go(hash, { replace = false } = {}) {
|
||||
if (replace) {
|
||||
history.replaceState(null, "", hash);
|
||||
visited[visited.length - 1] = hash;
|
||||
show();
|
||||
} else location.hash = hash;
|
||||
},
|
||||
back(fallback = href.home()) {
|
||||
if (visited.length > 1) history.back();
|
||||
else nav.go(fallback, { replace: true });
|
||||
},
|
||||
};
|
||||
|
||||
const show = () => {
|
||||
let route = parseRoute(location.hash);
|
||||
// Un fragment réservé ne change pas l'écran ; au démarrage, on montre l'accueil.
|
||||
if (route.name === "reserved") {
|
||||
if (current) return;
|
||||
route = { name: "home" };
|
||||
}
|
||||
current?.destroy?.();
|
||||
current = render(route, nav);
|
||||
root.replaceChildren(current.node);
|
||||
document.title = current.title ? `${current.title} · IA-Fit` : "IA-Fit";
|
||||
window.scrollTo(0, 0);
|
||||
// Le focus suit l'écran (lecteurs d'écran, clavier).
|
||||
const heading = current.node.querySelector("h1");
|
||||
if (heading) {
|
||||
heading.setAttribute("tabindex", "-1");
|
||||
heading.focus({ preventScroll: true });
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("hashchange", () => {
|
||||
if (visited.length > 1 && visited[visited.length - 2] === location.hash) visited.pop();
|
||||
else visited.push(location.hash);
|
||||
show();
|
||||
});
|
||||
show();
|
||||
return nav;
|
||||
}
|
||||
Reference in New Issue
Block a user