(function () {
"use strict";
function cellValue(row, key, type) {
const raw = row.dataset[key];
return type === "number" ? Number(raw) : raw;
}
function compareRows(a, b, key, type, ascending) {
const av = cellValue(a, key, type);
const bv = cellValue(b, key, type);
const cmp = type === "number" ? av - bv : String(av).localeCompare(String(bv));
return ascending ? cmp : -cmp;
}
if (typeof module !== "undefined") {
module.exports = { cellValue, compareRows };
}
if (typeof document === "undefined") return;
const table = document.getElementById("fixture-table");
if (!table) return;
const tbody = table.querySelector("tbody");
const headers = Array.from(table.querySelectorAll("th[data-sort]"));
let currentKey = "name";
let ascending = true;
function sortBy(key, type) {
if (key === currentKey) {
ascending = !ascending;
} else {
currentKey = key;
ascending = true;
}
const rows = Array.from(tbody.querySelectorAll("tr"));
rows.sort((a, b) => compareRows(a, b, key, type, ascending));
for (const row of rows) tbody.appendChild(row);
for (const th of headers) {
th.setAttribute("aria-sort", th.dataset.sort === key ? (ascending ? "ascending" : "descending") : "none");
}
}
for (const th of headers) {
th.addEventListener("click", () => sortBy(th.dataset.sort, th.dataset.type));
th.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
sortBy(th.dataset.sort, th.dataset.type);
}
});
}
})();