igneum/site/leaderboard.js

17 lines
1.4 KiB
JavaScript

/* The /live weight leaderboard's rows (7 October 2026): the top 10 by default, a toggle "Show top 20" then "Show all N" (the
count live), then back to the ten; the viewer's own key is always visible, as its row inside the ten or as a pinned extra
row below them with its true rank. A classic script (window.IgneumLeaderboard) for live.html; module.exports for the test. */
(function (root) {
var L = {};
L.rows = function (keys, show, mine) {
keys = (keys || []).slice(); var total = keys.length, n = Math.min(show || 10, total);
var rows = keys.slice(0, n).map(function (k, i) { return Object.assign({}, k, { rank: i + 1, you: !!mine && k.id === mine }); });
var pinned = null;
if (mine) { var idx = -1; for (var i = 0; i < keys.length; i++) if (keys[i].id === mine) { idx = i; break; } if (idx >= n) pinned = Object.assign({}, keys[idx], { rank: idx + 1, you: true }); }
var toggle = null;
if (total > 10) { if (n <= 10 && total > 20) toggle = { label: 'Show top 20', next: 20 }; else if (n < total) toggle = { label: 'Show all ' + total, next: total }; else toggle = { label: 'Show top 10', next: 10 }; }
return { rows: rows, pinned: pinned, toggle: toggle, total: total, shown: n };
};
root.IgneumLeaderboard = L;
if (typeof module === 'object' && module && module.exports) module.exports = L;
})(typeof window !== 'undefined' ? window : globalThis);