Files
2026-11-06 11:42:34 -05:00

156 lines
6.3 KiB
JavaScript

// Small progressive enhancements. Every page works without this file.
(() => {
"use strict";
// ---- live "2h 05m" counters ------------------------------------------
const fmtDuration = (secs) => {
const mins = Math.max(0, Math.floor(secs / 60));
const d = Math.floor(mins / 1440), h = Math.floor((mins % 1440) / 60), m = mins % 60;
if (d) return `${d}d ${h}h`;
if (h) return `${h}h ${String(m).padStart(2, "0")}m`;
return `${m}m`;
};
const tick = () => {
const now = Date.now() / 1000;
document.querySelectorAll("[data-since]").forEach((el) => {
el.textContent = fmtDuration(now - Number(el.dataset.since));
});
document.querySelectorAll("[data-ago]").forEach((el) => {
const s = now - Number(el.dataset.ago);
el.textContent = s < 60 ? "just now" : `${fmtDuration(s)} ago`;
});
};
tick();
setInterval(tick, 30_000);
// ---- keep the Today page fresh -----------------------------------------
// Reload when the parent comes back to the tab, or every N s while open,
// unless they're in the middle of typing something.
const auto = document.querySelector("[data-autorefresh]");
if (auto) {
const every = Number(auto.dataset.autorefresh) * 1000;
let loaded = Date.now();
const busy = () => document.activeElement && /INPUT|SELECT|TEXTAREA/.test(document.activeElement.tagName);
const maybeReload = () => {
if (document.visibilityState === "visible" && !busy() && Date.now() - loaded > every) {
loaded = Date.now();
location.reload();
}
};
setInterval(maybeReload, 15_000);
document.addEventListener("visibilitychange", maybeReload);
}
// ---- confirm before destructive buttons --------------------------------
document.addEventListener("submit", (e) => {
const msg = e.target.dataset && e.target.dataset.confirm;
if (msg && !window.confirm(msg)) e.preventDefault();
});
// ---- copy buttons ------------------------------------------------------
document.querySelectorAll("[data-copy]").forEach((btn) => {
btn.addEventListener("click", async () => {
const src = document.querySelector(btn.dataset.copy);
try {
await navigator.clipboard.writeText(src.textContent.trim());
btn.textContent = "Copied";
} catch {
const r = document.createRange();
r.selectNodeContents(src);
getSelection().removeAllRanges();
getSelection().addRange(r);
}
});
});
// ---- show "End" only for start/stop event types ------------------------
document.querySelectorAll("[data-kind-form]").forEach((form) => {
const select = form.querySelector("select[name=kind]");
const end = form.querySelector("[data-end-field]");
if (!select || !end) return;
const sync = () => {
end.hidden = select.selectedOptions[0]?.dataset.duration !== "1";
};
select.addEventListener("change", sync);
sync();
});
// ---- timezone mismatch hint --------------------------------------------
const hint = document.getElementById("tz-hint");
try {
const browserTz = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (hint && browserTz && browserTz !== document.body.dataset.tz) {
hint.querySelector("[data-browser-tz]").textContent = browserTz;
hint.querySelector("input[name=timezone]").value = browserTz;
hint.hidden = false;
}
} catch { /* old browser: no hint */ }
const tzSelect = document.querySelector("[data-tz-select]");
if (tzSelect && document.body.dataset.tz === "UTC") {
const browserTz = Intl.DateTimeFormat().resolvedOptions().timeZone;
const opt = [...tzSelect.options].find((o) => o.value === browserTz);
if (opt) opt.selected = true;
}
// ---- tooltips ------------------------------------------------------------
const tip = document.getElementById("tip");
const showTip = (text, x, y) => {
const [head, rest] = String(text).split("|");
tip.replaceChildren();
const strong = document.createElement("strong");
strong.textContent = head;
tip.append(strong);
if (rest) {
const span = document.createElement("span");
span.textContent = rest;
tip.append(span);
}
tip.hidden = false;
const w = tip.offsetWidth, h = tip.offsetHeight;
const left = Math.min(Math.max(8, x - w / 2), innerWidth - w - 8);
const top = y - h - 14 < 8 ? y + 18 : y - h - 14;
tip.style.left = `${left}px`;
tip.style.top = `${top}px`;
};
const hideTip = () => { tip.hidden = true; };
let hideTimer;
document.addEventListener("pointerover", (e) => {
const el = e.target.closest("[data-tip]");
if (!el) return;
clearTimeout(hideTimer);
showTip(el.dataset.tip, e.clientX, e.clientY);
if (e.pointerType === "touch") hideTimer = setTimeout(hideTip, 2500);
});
document.addEventListener("pointermove", (e) => {
const el = e.target.closest("[data-tip]");
if (el && !tip.hidden && e.pointerType !== "touch") showTip(el.dataset.tip, e.clientX, e.clientY);
});
document.addEventListener("pointerout", (e) => {
if (e.target.closest("[data-tip]") && e.pointerType !== "touch") hideTip();
});
addEventListener("scroll", hideTip, { passive: true });
// ---- line chart crosshair ---------------------------------------------------
document.querySelectorAll("[data-line]").forEach((chart) => {
const plot = chart.querySelector(".line-plot");
const data = JSON.parse(plot.querySelector("script[type='application/json']").textContent);
const cross = plot.querySelector(".line-cross");
const dot = plot.querySelector(".line-dot");
if (!data.length) return;
const move = (e) => {
const r = plot.getBoundingClientRect();
const xPct = ((e.clientX - r.left) / r.width) * 100;
let best = data[0];
for (const p of data) if (Math.abs(p[0] - xPct) < Math.abs(best[0] - xPct)) best = p;
cross.style.left = `${best[0]}%`;
dot.style.left = `${best[0]}%`;
dot.style.top = `${100 - best[1]}%`;
cross.hidden = dot.hidden = false;
showTip(best[2], r.left + (best[0] / 100) * r.width, r.top + (1 - best[1] / 100) * r.height);
};
plot.addEventListener("pointermove", move);
plot.addEventListener("pointerdown", move);
plot.addEventListener("pointerleave", () => { cross.hidden = dot.hidden = true; hideTip(); });
});
})();