daaain's picture
Static results viewer
d669c00 verified
Raw History Blame Contribute Delete
7.97 kB
// Theme, the charts/tables toggle, chart tooltips, and lazily loaded prompts. No dependencies.
(function () {
"use strict";
const store = {
get(k) { try { return localStorage.getItem(k); } catch (e) { return null; } },
set(k, v) { try { localStorage.setItem(k, v); } catch (e) { /* private window */ } },
};
// Theme: system → light → dark.
const root = document.documentElement;
function applyTheme(t) {
if (t === "light" || t === "dark") root.setAttribute("data-theme", t);
else root.removeAttribute("data-theme");
const b = document.getElementById("theme");
if (b) b.textContent = "Theme: " + (t || "system");
}
applyTheme(store.get("mv-theme"));
document.addEventListener("DOMContentLoaded", function () {
applyTheme(store.get("mv-theme"));
const themeBtn = document.getElementById("theme");
if (themeBtn) themeBtn.addEventListener("click", function () {
const order = [null, "light", "dark"];
const now = store.get("mv-theme");
const next = order[(order.indexOf(now) + 1) % order.length];
store.set("mv-theme", next || "");
applyTheme(next);
});
// Charts or tables of the same numbers.
const viewBtn = document.getElementById("view");
function applyView(v) {
document.body.classList.toggle("tables", v === "tables");
if (viewBtn) viewBtn.textContent = v === "tables" ? "Show charts" : "Show tables";
}
applyView(store.get("mv-view"));
if (viewBtn) viewBtn.addEventListener("click", function () {
const v = document.body.classList.contains("tables") ? "charts" : "tables";
store.set("mv-view", v);
applyView(v);
});
// Tooltips: values in ink, from data-tip, set with textContent (labels are data).
const tip = document.createElement("div");
tip.id = "tip";
tip.hidden = true;
document.body.appendChild(tip);
function show(el, x, y) {
const lines = (el.getAttribute("data-tip") || "").split("\n");
tip.textContent = "";
lines.forEach(function (line, i) {
const d = document.createElement("div");
if (i === 0) d.className = "first";
d.textContent = line;
tip.appendChild(d);
});
tip.hidden = false;
const r = tip.getBoundingClientRect();
let left = x + 14, top = y + 14;
if (left + r.width > window.innerWidth - 8) left = x - r.width - 14;
if (top + r.height > window.innerHeight - 8) top = y - r.height - 14;
tip.style.left = Math.max(4, left) + "px";
tip.style.top = Math.max(4, top) + "px";
const svg = el.closest("svg");
const cross = svg && svg.querySelector(".crosshair");
if (cross && el.hasAttribute("data-x")) {
cross.setAttribute("x1", el.getAttribute("data-x"));
cross.setAttribute("x2", el.getAttribute("data-x"));
cross.setAttribute("visibility", "visible");
}
}
function hide(el) {
tip.hidden = true;
const svg = el && el.closest && el.closest("svg");
const cross = svg && svg.querySelector(".crosshair");
if (cross) cross.setAttribute("visibility", "hidden");
}
document.addEventListener("pointermove", function (e) {
const el = e.target.closest && e.target.closest("[data-tip]");
if (el) show(el, e.clientX, e.clientY); else if (!tip.hidden) hide(null);
});
document.addEventListener("pointerleave", function () { hide(null); });
document.addEventListener("focusin", function (e) {
const el = e.target.closest && e.target.closest("[data-tip]");
if (!el) return;
const r = el.getBoundingClientRect();
show(el, r.left + r.width / 2, r.top + r.height / 2);
});
document.addEventListener("focusout", function (e) { hide(e.target); });
// The reconstructed prompt, fetched when its <details> first opens.
document.querySelectorAll("details[data-prompt]").forEach(function (d) {
d.addEventListener("toggle", function () {
if (!d.open || d.dataset.loaded) return;
d.dataset.loaded = "1";
const pre = d.querySelector("pre");
pre.textContent = "Loading…";
fetch(d.getAttribute("data-prompt"))
.then(function (r) { return r.text().then(function (t) { return [r.ok, t]; }); })
.then(function (res) { pre.textContent = res[1]; if (!res[0]) d.dataset.loaded = ""; })
.catch(function (err) { pre.textContent = "Could not load: " + err; d.dataset.loaded = ""; });
});
});
// The static export (export.py): one folder of pages per budget, so the picker moves to the same page in another folder.
document.querySelectorAll("select[data-export-budget]").forEach(function (s) {
s.addEventListener("change", function () {
const page = location.pathname.split("/").pop() || "index.html";
location.href = "../budget-" + s.value + "/" + page;
});
});
document.querySelectorAll("select[data-export-source]").forEach(function (s) {
s.addEventListener("change", function () {
location.href = s.value === "original" ? "originals.html" : "replays.html";
});
});
// The static export's list filters, in the browser, with pages.list_page's rules: every row is in the page.
const filters = document.querySelector("form[data-export-filters]");
if (filters) {
const tbody = document.querySelector("table tbody");
const rows = Array.prototype.slice.call(tbody.rows);
const shown = document.querySelector("[data-shown]");
const parsed = new Map(rows.map(function (r) {
return [r, { scores: JSON.parse(r.dataset.scores), caught: JSON.parse(r.dataset.caught), i: Number(r.dataset.i) }];
}));
function value(name) {
const el = filters.elements[name];
return el ? el.value.trim() : "";
}
function apply() {
const arm = value("arm") || filters.dataset.arm;
const f = { mode: value("mode"), side: value("side_task"), task: value("task_id"), landed: value("landed"), flipped: value("flipped"), caught: value("caught") };
const kept = rows.filter(function (r) {
const d = r.dataset, p = parsed.get(r);
if (f.mode && d.mode !== f.mode) return false;
if (f.side && d.side !== f.side) return false;
if (f.task && d.task.indexOf(f.task) < 0) return false;
if (f.landed && d.landed !== f.landed) return false;
if (f.flipped && d.flipped !== f.flipped) return false;
if (f.caught) {
if (p.scores[arm] === null || p.scores[arm] === undefined) return false;
if (Boolean(p.caught[arm]) !== (f.caught === "yes")) return false;
}
return true;
});
kept.sort(function (a, b) {
const pa = parsed.get(a), pb = parsed.get(b);
if (value("sort") === "task") {
const ka = a.dataset.task + "\u0000" + a.dataset.mode, kb = b.dataset.task + "\u0000" + b.dataset.mode;
if (ka !== kb) return ka < kb ? -1 : 1;
} else {
const d = (pb.scores[arm] || 0) - (pa.scores[arm] || 0);
if (d) return d;
}
return pa.i - pb.i;
});
rows.forEach(function (r) { r.hidden = true; });
kept.forEach(function (r) { r.hidden = false; tbody.appendChild(r); });
if (shown) shown.textContent = String(kept.length);
}
filters.addEventListener("submit", function (e) { e.preventDefault(); apply(); });
}
// Open or close every tool output on a trajectory page.
document.querySelectorAll("[data-toggle-all]").forEach(function (b) {
b.addEventListener("click", function () {
const sel = b.getAttribute("data-toggle-all");
const all = document.querySelectorAll(sel);
const open = Array.prototype.some.call(all, function (d) { return !d.open; });
all.forEach(function (d) { d.open = open; });
});
});
});
})();