Files
ia-fit/app/ui/timer.js
T

121 lines
4.2 KiB
JavaScript

// Le minuteur, fixé en bas de l'écran : repos entre les séries, ou chrono d'une
// série au temps. Il se cale sur l'horloge (Date.now) et non sur le nombre de
// tics : il reste juste si le téléphone met l'onglet en veille.
// Ce n'est pas un événement (ADR 0006, point 3).
import { h, vibrate } from "./dom.js";
import { clock } from "./format.js";
/**
* @typedef {object} Timer
* @property {(seconds: number, label: string, endText?: string) => void} startRest
* repos, ou toute durée simple (échauffement) ; endText s'affiche à la fin
* @property {(seconds: number, label: string, onDone: () => void) => void} startWork
* chrono de série : onDone est appelé à la fin, ou si l'on touche « Terminer »
* @property {(seconds: number) => void} add
* @property {() => void} stop
* @property {HTMLElement} node
*/
/** @returns {Timer} */
export function createTimer() {
const bar = h("div", { class: "timer__bar", "aria-hidden": "true" });
const label = h("div", { class: "timer__label" }, "Repos");
const time = h("div", { class: "timer__time" }, "0:00");
const live = h("div", { class: "visually-hidden", role: "status", "aria-live": "polite" });
const close = h("button", { type: "button", class: "btn btn--primary timer__btn" }, "Terminer");
const more = h("button", { type: "button", class: "btn timer__btn", "aria-label": "Ajouter 30 secondes" }, "+30 s");
const node = h("section", { class: "timer", "aria-label": "Minuteur", hidden: true },
bar,
h("div", { class: "timer__in" },
h("div", { class: "timer__read" }, label, time),
h("div", { class: "timer__btns" }, more, close)),
live);
let end = 0, total = 0, labelText = "";
/** @type {ReturnType<typeof setInterval>|null} */
let tick = null;
/** @type {(() => void)|null} */
let onDone = null;
let work = false;
let endText = "Repos terminé";
const paint = () => {
const left = Math.max(0, Math.round((end - Date.now()) / 1000));
time.textContent = clock(left);
node.classList.toggle("timer--low", left > 0 && left <= 10);
node.classList.toggle("timer--zero", left === 0);
bar.style.setProperty("--progress", String(total ? left / total : 0));
if (left === 0 && tick) finish();
};
const finish = () => {
if (tick) clearInterval(tick);
tick = null;
vibrate(work ? [300, 120, 300, 120, 300] : [180, 90, 180]);
label.textContent = work ? `${labelText} · série terminée` : endText;
live.textContent = work ? "Série terminée" : endText;
close.textContent = "Fermer";
const done = onDone;
onDone = null;
done?.();
};
/** @param {number} seconds @param {string} text @param {boolean} isWork */
const start = (seconds, text, isWork) => {
// Un chrono de série interrompu par un nouveau minuteur ne coche rien.
onDone = null;
work = isWork;
labelText = text;
total = seconds;
end = Date.now() + seconds * 1000;
label.textContent = text;
live.textContent = "";
close.textContent = "Terminer";
node.classList.toggle("timer--work", isWork);
node.hidden = false;
document.body.classList.add("has-timer");
if (!tick) tick = setInterval(paint, 250);
paint();
};
const stop = () => {
if (tick) clearInterval(tick);
tick = null;
end = 0;
onDone = null;
node.hidden = true;
document.body.classList.remove("has-timer");
};
more.addEventListener("click", () => {
if (!end) return;
const left = Math.max(0, end - Date.now());
end = Date.now() + left + 30_000;
total = Math.max(total, Math.round((end - Date.now()) / 1000));
label.textContent = labelText;
close.textContent = "Terminer";
if (!tick) tick = setInterval(paint, 250);
paint();
});
close.addEventListener("click", () => {
// « Terminer » pendant un chrono de série : la série est faite.
if (tick && onDone) {
const done = onDone;
onDone = null;
stop();
done();
return;
}
stop();
});
return {
node,
startRest: (seconds, text, end = "Repos terminé") => { start(seconds, text, false); endText = end; },
startWork: (seconds, text, done) => { start(seconds, text, true); onDone = done; },
add: (seconds) => { end += seconds * 1000; total += seconds; paint(); },
stop,
};
}