Code4Scene / assets /paper.js
KoeYe's picture
Leaderboard: one tabbed section; score-against-cost chart coloured by provider
0665cbb verified
Raw History Blame Contribute Delete
3.3 kB
// Code4Scene page behaviour: chart tooltips, the Pareto view toggle, sortable leaderboards, copying the BibTeX.
(function () {
// tooltips: any element with data-tip='{"t": title, "r": [[label, value], ...]}' or plain text
var tip = document.createElement("div"); tip.className = "tip"; document.body.appendChild(tip);
function show(el, e) {
var raw = el.getAttribute("data-tip"), html;
try { var d = JSON.parse(raw); html = "<b>" + d.t + "</b>" + (d.r || []).map(function (r) { return r[0] + ": " + r[1]; }).join("<br>"); }
catch (err) { html = raw; }
tip.innerHTML = html; tip.classList.add("on"); move(e);
}
function move(e) { tip.style.left = Math.min(innerWidth - 160, Math.max(160, e.clientX)) + "px"; tip.style.top = e.clientY + "px"; }
document.addEventListener("mouseover", function (e) { var el = e.target.closest("[data-tip]"); if (el) show(el, e); });
document.addEventListener("mousemove", function (e) { if (tip.classList.contains("on")) move(e); });
document.addEventListener("mouseout", function (e) { var el = e.target.closest("[data-tip]"); if (el && !el.contains(e.relatedTarget)) tip.classList.remove("on"); });
// chart view toggles: <div class="seg" data-for="id"><button data-view="x">; the chart's groups carry data-view="x"
document.querySelectorAll(".seg[data-for]").forEach(function (seg) {
var chart = document.getElementById(seg.getAttribute("data-for"));
seg.querySelectorAll("button").forEach(function (b) {
b.addEventListener("click", function () {
seg.querySelectorAll("button").forEach(function (x) { x.classList.toggle("on", x === b); });
chart.querySelectorAll("[data-view]").forEach(function (g) {
if (seg.contains(g)) return; // the tab buttons carry data-view too
g.style.display = g.getAttribute("data-view") === b.getAttribute("data-view") ? "" : "none"; });
});
});
});
// sortable leaderboards: th[data-k] sorts rows by td[data-v] in that column; the rank column follows the order
document.querySelectorAll("table.board").forEach(function (tb) {
var ths = [].slice.call(tb.querySelectorAll("th"));
ths.forEach(function (th, ci) {
if (!th.hasAttribute("data-k")) { th.classList.add("nosort"); return; }
th.addEventListener("click", function () {
var asc = th.classList.contains("sorted") ? !th.classList.contains("asc") : th.getAttribute("data-best") === "low";
ths.forEach(function (x) { x.classList.remove("sorted", "asc"); }); th.classList.add("sorted"); if (asc) th.classList.add("asc");
var body = tb.tBodies[0], rows = [].slice.call(body.rows);
rows.sort(function (a, b) { var x = +a.cells[ci].getAttribute("data-v"), y = +b.cells[ci].getAttribute("data-v"); return asc ? x - y : y - x; });
rows.forEach(function (r, i) { r.cells[0].textContent = i + 1; body.appendChild(r); });
});
});
});
var cb = document.getElementById("copy-btn");
if (cb) cb.addEventListener("click", function () {
navigator.clipboard.writeText(document.getElementById("bibtex-text").textContent).then(function () {
cb.textContent = "Copied"; cb.classList.add("copied"); setTimeout(function () { cb.textContent = "Copy"; cb.classList.remove("copied"); }, 1600);
});
});
})();