Semaine, poids, activités et date de séance dans l'adresse (lot 3c)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
30a1a3b56d
commit
4844c7b8e7
@@ -58,3 +58,30 @@ export function mondayOf(iso) {
|
||||
export function daysBetween(a, b) {
|
||||
return Math.round((toUTC(b) - toUTC(a)) / DAY_MS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Semaine ISO 8601 d'un jour, au format AAAA-Www (« 2026-W47 ») : elle commence
|
||||
* le lundi, et son année est celle de son jeudi (la semaine du 29 décembre 2025
|
||||
* est la 2026-W01).
|
||||
* @param {string} iso
|
||||
*/
|
||||
export function isoWeek(iso) {
|
||||
const thursday = addDays(mondayOf(iso), 3);
|
||||
const year = thursday.slice(0, 4);
|
||||
const week = Math.floor(daysBetween(`${year}-01-01`, thursday) / 7) + 1;
|
||||
return `${year}-W${String(week).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lundi d'une semaine ISO écrite AAAA-Www, ou null si elle n'existe pas
|
||||
* (forme invalide, semaine 00, semaine 53 d'une année qui n'en a que 52).
|
||||
* @param {string} week
|
||||
* @returns {string|null}
|
||||
*/
|
||||
export function mondayOfIsoWeek(week) {
|
||||
const m = /^(\d{4})-W(\d{2})$/.exec(week);
|
||||
if (!m) return null;
|
||||
// Le 4 janvier est toujours dans la semaine 1.
|
||||
const monday = addDays(mondayOf(`${m[1]}-01-04`), (Number(m[2]) - 1) * 7);
|
||||
return Number(m[2]) >= 1 && isoWeek(monday) === week ? monday : null;
|
||||
}
|
||||
|
||||
+3
-3
@@ -135,9 +135,9 @@ startRouter(root, (route, nav) => {
|
||||
case "progress": return progressScreen({ store, nav, today: today() });
|
||||
case "history": return historyScreen({ store, nav });
|
||||
case "history-edit": return historyEditScreen({ store, nav, id: route.id });
|
||||
case "week": return weekScreen({ store, nav, today });
|
||||
case "activities": return activitiesScreen({ store, nav, today });
|
||||
case "weight": return weightScreen({ store, nav, today });
|
||||
case "week": return weekScreen({ store, nav, today, query: route.query });
|
||||
case "activities": return activitiesScreen({ store, nav, today, query: route.query });
|
||||
case "weight": return weightScreen({ store, nav, today, query: route.query });
|
||||
case "data": return dataScreen({ store, nav, journal, today });
|
||||
case "profile": return profileScreen({ store, nav });
|
||||
case "analysis": return analysisScreen({ store, nav, today, exercise: route.exercise });
|
||||
|
||||
+45
-14
@@ -9,7 +9,7 @@
|
||||
// Une adresse peut porter des paramètres après « ? » : l'état de navigation
|
||||
// (filtres, recherche) y vit, ADR 0009. L'écran qui les lit ignore ceux qu'il ne
|
||||
// connaît pas ; changer un filtre remplace l'adresse sans reconstruire l'écran
|
||||
// (nav.set). La page d'autorisation d'une IA (#/connecter-ia?…) reçoit sa requête
|
||||
// (nav.set) ; ouvrir un élément sur place l'empile (nav.set avec push). La page d'autorisation d'une IA (#/connecter-ia?…) reçoit sa requête
|
||||
// brute, à renvoyer telle quelle.
|
||||
|
||||
/**
|
||||
@@ -19,9 +19,9 @@
|
||||
* | {name: "progress"}
|
||||
* | {name: "history"}
|
||||
* | {name: "history-edit", id: string}
|
||||
* | {name: "week"}
|
||||
* | {name: "activities"}
|
||||
* | {name: "weight"}
|
||||
* | {name: "week", query: string}
|
||||
* | {name: "activities", query: string}
|
||||
* | {name: "weight", query: string}
|
||||
* | {name: "data"}
|
||||
* | {name: "profile"}
|
||||
* | {name: "analysis", exercise: string|null}
|
||||
@@ -62,16 +62,36 @@ export function parseRoute(hash) {
|
||||
if (head === "connecter-ia" && parts.length === 1) return { name: "connect-ai", query };
|
||||
if (head === "bibliotheque" && parts.length === 1) return { name: "library", query };
|
||||
if (head === "bibliotheque" && parts.length === 2 && arg) return { name: "library-item", id: arg, query };
|
||||
if (parts.length === 1 && WITH_QUERY[head]) return /** @type {Route} */ ({ name: WITH_QUERY[head], query });
|
||||
if (parts.length === 1 && SIMPLE[head]) return /** @type {Route} */ ({ name: SIMPLE[head] });
|
||||
return { name: "not-found" };
|
||||
}
|
||||
|
||||
/** Écrans sans paramètre : adresse → nom. @type {Record<string, string>} */
|
||||
const SIMPLE = {
|
||||
bravo: "finish", progression: "progress", historique: "history", semaine: "week", activites: "activities",
|
||||
poids: "weight", donnees: "data", profil: "profile", synchro: "sync", relier: "pair",
|
||||
bravo: "finish", progression: "progress", historique: "history",
|
||||
donnees: "data", profil: "profile", synchro: "sync", relier: "pair",
|
||||
};
|
||||
|
||||
/** Écrans à chemin simple qui lisent leur requête : adresse → nom. @type {Record<string, string>} */
|
||||
const WITH_QUERY = { semaine: "week", activites: "activities", poids: "weight" };
|
||||
|
||||
/**
|
||||
* Une adresse et sa requête, dans l'ordre des clés. Une valeur absente (null,
|
||||
* undefined, chaîne vide) ou égale à sa valeur par défaut est omise : l'adresse
|
||||
* reste la plus courte possible, et la même pour un même état.
|
||||
* @param {string} path chemin, par exemple « #/poids »
|
||||
* @param {Record<string, string|number|null|undefined>} params
|
||||
* @param {Record<string, string|number>} [defaults]
|
||||
*/
|
||||
export function withParams(path, params, defaults = {}) {
|
||||
const p = new URLSearchParams();
|
||||
for (const [key, value] of Object.entries(params))
|
||||
if (value != null && value !== "" && !(key in defaults && String(value) === String(defaults[key]))) p.set(key, String(value));
|
||||
const query = p.toString();
|
||||
return query ? path + "?" + query : path;
|
||||
}
|
||||
|
||||
const enc = encodeURIComponent;
|
||||
|
||||
/** Adresses, pour écrire les liens sans les recopier. */
|
||||
@@ -82,9 +102,12 @@ export const href = {
|
||||
progress: () => "#/progression",
|
||||
history: () => "#/historique",
|
||||
historyEdit: (/** @type {string} */ id) => "#/historique/" + enc(id),
|
||||
week: () => "#/semaine",
|
||||
activities: () => "#/activites",
|
||||
weight: () => "#/poids",
|
||||
/** semaine : AAAA-Www, absente pour la semaine en cours ; jour : 1 (lundi) à 7, le jour déplié */
|
||||
week: (/** @type {{week?: string|null, day?: number|null}} */ { week = null, day = null } = {}) => withParams("#/semaine", { semaine: week, jour: day }),
|
||||
/** modifier : l'activité en cours de modification */
|
||||
activities: (/** @type {string|null} */ edit = null) => withParams("#/activites", { modifier: edit }),
|
||||
/** periode : 3mois (par défaut, omise), 1an ou tout */
|
||||
weight: (/** @type {string|null} */ period = null) => withParams("#/poids", { periode: period }, { periode: "3mois" }),
|
||||
data: () => "#/donnees",
|
||||
profile: () => "#/profil",
|
||||
analysis: (/** @type {string|null} */ exercise = null) => "#/analyses" + (exercise ? "/" + enc(exercise) : ""),
|
||||
@@ -106,8 +129,10 @@ export const href = {
|
||||
* @typedef {object} Navigation
|
||||
* @property {(hash: string, options?: {replace?: boolean}) => void} go
|
||||
* @property {(fallback?: string) => void} back retour dans l'app, ou vers fallback
|
||||
* @property {(hash: string) => void} set remplace l'adresse sans changer d'écran
|
||||
* (un filtre qui change : l'écran se repeint lui-même, garde focus et défilement)
|
||||
* @property {(hash: string, options?: {push?: boolean}) => void} set change l'adresse
|
||||
* sans reconstruire l'écran (un filtre qui change : l'écran se repeint lui-même,
|
||||
* garde focus et défilement). L'adresse est remplacée, ou empilée avec push : un
|
||||
* élément ouvert sur place, que le retour referme en reconstruisant l'écran.
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -130,10 +155,16 @@ export function startRouter(root, render) {
|
||||
show();
|
||||
} else location.hash = hash;
|
||||
},
|
||||
set(hash) {
|
||||
set(hash, { push = false } = {}) {
|
||||
if (hash === location.hash) return;
|
||||
history.replaceState(null, "", hash);
|
||||
visited[visited.length - 1] = hash;
|
||||
if (push) {
|
||||
// pushState ne déclenche pas hashchange ; revenir en arrière, si.
|
||||
history.pushState(null, "", hash);
|
||||
visited.push(hash);
|
||||
} else {
|
||||
history.replaceState(null, "", hash);
|
||||
visited[visited.length - 1] = hash;
|
||||
}
|
||||
},
|
||||
back(fallback = href.home()) {
|
||||
if (visited.length > 1) history.back();
|
||||
|
||||
@@ -43,10 +43,22 @@ export function withActivity(program, { id, activity }) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation, today: () => string}} ctx
|
||||
* L'activité en cours de modification, lue dans la requête de l'adresse
|
||||
* (modifier=<id>, ADR 0009). Un id inconnu est ignoré.
|
||||
* @param {string} query partie après « ? »
|
||||
* @param {Activity[]} activities
|
||||
* @returns {Activity|null}
|
||||
*/
|
||||
export function readEditedActivity(query, activities) {
|
||||
const id = new URLSearchParams(query).get("modifier");
|
||||
return activities.find((a) => a.id === id) ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation, today: () => string, query: string}} ctx
|
||||
* @returns {import("../router.js").Screen}
|
||||
*/
|
||||
export function activitiesScreen({ store, nav, today }) {
|
||||
export function activitiesScreen({ store, nav, today, query }) {
|
||||
const list = h("div");
|
||||
const back = { label: "Accueil", onClick: () => nav.back(href.home()) };
|
||||
if (!store.getState().program) {
|
||||
@@ -80,8 +92,11 @@ export function activitiesScreen({ store, nav, today }) {
|
||||
intBtns.forEach((b) => b.addEventListener("click", () => { intensity = /** @type {string} */ (b.dataset.intensity); paintChoices(); }));
|
||||
const cancel = h("button", { type: "button", class: "btn btn--ghost btn--block", hidden: true }, "Annuler la modification");
|
||||
|
||||
// L'activité modifiée est dans l'adresse (ADR 0009) : l'ouvrir empile une
|
||||
// entrée, la refermer la remplace. Les champs restent un brouillon hors adresse.
|
||||
const reset = () => {
|
||||
editing = null;
|
||||
nav.set(href.activities());
|
||||
zones.clear();
|
||||
zones.add("cardio");
|
||||
intensity = "moderate";
|
||||
@@ -95,6 +110,7 @@ export function activitiesScreen({ store, nav, today }) {
|
||||
|
||||
/** @param {Activity} a */
|
||||
const load = (a) => {
|
||||
nav.set(href.activities(a.id), { push: editing === null });
|
||||
editing = a.id;
|
||||
zones.clear();
|
||||
a.zones.forEach((z) => zones.add(z));
|
||||
@@ -169,6 +185,11 @@ export function activitiesScreen({ store, nav, today }) {
|
||||
};
|
||||
paint();
|
||||
paintChoices();
|
||||
const edited = readEditedActivity(query, store.getState().program?.activities ?? []);
|
||||
if (edited) {
|
||||
editing = edited.id; // déjà dans l'adresse : rien à empiler
|
||||
load(edited);
|
||||
}
|
||||
|
||||
const node = h("div", { class: "screen" },
|
||||
screenHeader({ back, kicker: "Hors programme", title: "Activités", sub: "Foot, escalade, vélo… Le planning en tient compte : une activité cardio remplace une sortie, une activité intense éloigne la séance qui travaille les mêmes zones." }),
|
||||
|
||||
@@ -71,6 +71,8 @@ export function sessionScreen({ store, key, date, nav, timer }) {
|
||||
const value = dateInput.value;
|
||||
draft.date = /^\d{4}-\d{2}-\d{2}$/.test(value) && value <= today ? value : today;
|
||||
if (draft.date !== value) dateInput.value = draft.date;
|
||||
// L'adresse suit la date (ADR 0009) ; le brouillon reste la source.
|
||||
nav.set(href.session(key, draft.date === today ? null : draft.date));
|
||||
paintDate();
|
||||
onChange();
|
||||
});
|
||||
|
||||
+47
-7
@@ -4,7 +4,7 @@
|
||||
// (PlanSettingsChanged) et les faits (SessionLogged, DaySkipped…) sont au journal.
|
||||
|
||||
import { h, paintWhenIdle, replace } from "../dom.js";
|
||||
import { addDays, mondayOf } from "../../domain/dates.js";
|
||||
import { addDays, isoWeek, mondayOf, mondayOfIsoWeek } from "../../domain/dates.js";
|
||||
import { settingsForWeek } from "../../domain/planning.js";
|
||||
import { buildIcs } from "../../domain/ics.js";
|
||||
import { DAY_LETTERS, DAY_NAMES, DAY_SHORT, dayName, lockOptions, loggedForSlot, sessionForSlot, slotLabel, weekRows, withDays, withLock } from "../home-model.js";
|
||||
@@ -18,13 +18,47 @@ import { plaque } from "./home.js";
|
||||
/** @typedef {import("../home-model.js").WeekRow} WeekRow */
|
||||
|
||||
/**
|
||||
* @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation, today: () => string}} ctx
|
||||
* Lit l'état de l'écran dans la requête de l'adresse (ADR 0009) : la semaine
|
||||
* affichée (semaine=AAAA-Www, la suivante ou, par défaut, celle en cours) et le
|
||||
* jour déplié (jour=1 pour lundi … 7). Une valeur invalide est ignorée ; une
|
||||
* semaine qui n'est ni l'une ni l'autre revient à la semaine en cours.
|
||||
* @param {string} query partie après « ? »
|
||||
* @param {string} today
|
||||
* @returns {{tab: 0|1, day: number|null}} day : 0 (lundi) à 6
|
||||
*/
|
||||
export function readWeekQuery(query, today) {
|
||||
const p = new URLSearchParams(query);
|
||||
const monday = mondayOfIsoWeek(p.get("semaine") ?? "");
|
||||
const tab = monday === addDays(mondayOf(today), 7) ? 1 : 0;
|
||||
const day = Number(p.get("jour"));
|
||||
return { tab, day: Number.isInteger(day) && day >= 1 && day <= 7 ? day - 1 : null };
|
||||
}
|
||||
|
||||
/**
|
||||
* L'adresse de l'écran pour cet état : la semaine en cours et aucun jour déplié
|
||||
* sont omis.
|
||||
* @param {0|1} tab @param {number|null} day 0 (lundi) à 6 @param {string} today
|
||||
*/
|
||||
export function weekHref(tab, day, today) {
|
||||
return href.week({ week: tab === 1 ? isoWeek(addDays(today, 7)) : null, day: day === null ? null : day + 1 });
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation, today: () => string, query: string}} ctx
|
||||
* @returns {import("../router.js").Screen}
|
||||
*/
|
||||
export function weekScreen({ store, nav, today }) {
|
||||
let tab = 0;
|
||||
export function weekScreen({ store, nav, today, query }) {
|
||||
const initial = readWeekQuery(query, today());
|
||||
/** @type {0|1} */
|
||||
let tab = initial.tab;
|
||||
/** @type {number|null} */
|
||||
let lockOpen = null;
|
||||
let lockOpen = initial.day;
|
||||
/**
|
||||
* Garde l'adresse à jour : l'onglet la remplace (un filtre), le jour qu'on
|
||||
* déplie l'empile (un élément ouvert, que le retour referme).
|
||||
* @param {{push?: boolean}} [options]
|
||||
*/
|
||||
const syncAddress = (options) => nav.set(weekHref(tab, lockOpen, today()), options);
|
||||
const zone = h("div", { class: "week" });
|
||||
const status = h("p", { class: "status status--ok", role: "status", "aria-live": "polite" });
|
||||
|
||||
@@ -49,7 +83,7 @@ export function weekScreen({ store, nav, today }) {
|
||||
const tabs = h("div", { class: "segmented", role: "group", "aria-label": "Semaine affichée" },
|
||||
["Cette semaine", "Semaine suivante"].map((label, i) => h("button", {
|
||||
type: "button", class: "segmented__opt", "aria-pressed": String(tab === i),
|
||||
on: { click: () => { tab = i; lockOpen = null; status.textContent = ""; paint(); } },
|
||||
on: { click: () => { tab = /** @type {0|1} */ (i); lockOpen = null; status.textContent = ""; syncAddress(); paint(); } },
|
||||
}, label)));
|
||||
|
||||
const days = h("div", { class: "segmented segmented--days", role: "group", "aria-label": "Jours où je peux m'entraîner" },
|
||||
@@ -84,7 +118,12 @@ export function weekScreen({ store, nav, today }) {
|
||||
const head = h("button", {
|
||||
type: "button", class: "week__day", "aria-expanded": String(lockOpen === r.day),
|
||||
"aria-label": `${DAY_NAMES[r.day]} ${shortDay(r.date)} : ${title}${detail ? ", " + detail : ""}${state ? ", " + state : ""}. Choisir ce que porte ce jour.`,
|
||||
on: { click: () => { lockOpen = lockOpen === r.day ? null : r.day; paint(); } },
|
||||
on: { click: () => {
|
||||
const wasClosed = lockOpen === null;
|
||||
lockOpen = lockOpen === r.day ? null : r.day;
|
||||
syncAddress({ push: wasClosed && lockOpen !== null });
|
||||
paint();
|
||||
} },
|
||||
},
|
||||
h("span", { class: "row__day" }, h("span", { class: "mark" }, DAY_SHORT[r.day]), h("span", { class: "num" }, r.date.slice(8))),
|
||||
mark,
|
||||
@@ -99,6 +138,7 @@ export function weekScreen({ store, nav, today }) {
|
||||
save(withLock(settings, r.day, o.value));
|
||||
status.textContent = `${DAY_NAMES[r.day]} : ${o.label.toLowerCase()}.`;
|
||||
lockOpen = null;
|
||||
syncAddress();
|
||||
paint();
|
||||
} },
|
||||
}, o.label))));
|
||||
|
||||
@@ -10,15 +10,25 @@ import { href } from "../router.js";
|
||||
import { lineChart } from "../chart.js";
|
||||
import { toast } from "../feedback.js";
|
||||
|
||||
/** Périodes de la courbe, en jours (Infinity : tout). */
|
||||
const PERIODS = [{ days: 90, label: "3 mois" }, { days: 365, label: "1 an" }, { days: Infinity, label: "Tout" }];
|
||||
/** Périodes de la courbe, en jours (Infinity : tout), et leur nom dans l'adresse. La première est celle par défaut. */
|
||||
const PERIODS = [{ days: 90, label: "3 mois", param: "3mois" }, { days: 365, label: "1 an", param: "1an" }, { days: Infinity, label: "Tout", param: "tout" }];
|
||||
|
||||
/**
|
||||
* @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation, today: () => string}} ctx
|
||||
* La période de la courbe lue dans la requête de l'adresse (periode=3mois|1an|tout,
|
||||
* ADR 0009), en jours. Absente ou inconnue : 3 mois.
|
||||
* @param {string} query partie après « ? »
|
||||
*/
|
||||
export function readPeriod(query) {
|
||||
const param = new URLSearchParams(query).get("periode");
|
||||
return (PERIODS.find((p) => p.param === param) ?? PERIODS[0]).days;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation, today: () => string, query: string}} ctx
|
||||
* @returns {import("../router.js").Screen}
|
||||
*/
|
||||
export function weightScreen({ store, nav, today }) {
|
||||
let period = 90;
|
||||
export function weightScreen({ store, nav, today, query }) {
|
||||
let period = readPeriod(query);
|
||||
const summary = h("div");
|
||||
const chartZone = h("div");
|
||||
const milestones = h("div");
|
||||
@@ -79,7 +89,7 @@ export function weightScreen({ store, nav, today }) {
|
||||
const pts = inPeriod.length > 1 ? inPeriod : weights;
|
||||
replace(chartZone,
|
||||
h("div", { class: "segmented", role: "group", "aria-label": "Période de la courbe" }, PERIODS.map((p) =>
|
||||
h("button", { type: "button", class: "segmented__opt", "aria-pressed": String(p.days === period), on: { click: () => { period = p.days; paint(); } } }, p.label))),
|
||||
h("button", { type: "button", class: "segmented__opt", "aria-pressed": String(p.days === period), on: { click: () => { period = p.days; nav.set(href.weight(p.param)); paint(); } } }, p.label))),
|
||||
pts.length > 1
|
||||
? h("figure", { class: "chart" },
|
||||
lineChart({
|
||||
|
||||
Reference in New Issue
Block a user