| 1 |
{%- import "macros.html" as macros -%} |
| 2 |
|
| 3 |
<!DOCTYPE html> |
| 4 |
<html lang="en"> |
| 5 |
<head> |
| 6 |
<meta charset="utf-8"> |
| 7 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 8 |
<title>{% block title %}rsgit{% endblock %}</title> |
| 9 |
<link rel="stylesheet" href="/static/style.css"> |
| 10 |
<link rel="shortcut icon" href="/static/favicon.ico"> |
| 11 |
</head> |
| 12 |
<body> |
| 13 |
<table class="header"> |
| 14 |
<tbody> |
| 15 |
{% block header %}{% endblock %} |
| 16 |
</tbody> |
| 17 |
</table> |
| 18 |
<div id="body"> |
| 19 |
{% block body %} |
| 20 |
<table class="tabs"> |
| 21 |
<thead> |
| 22 |
{% block tabs %}{% endblock %} |
| 23 |
</thead> |
| 24 |
</table> |
| 25 |
{% block breadcrumb %}{% endblock %} |
| 26 |
|
| 27 |
<main> |
| 28 |
{% block content %}{% endblock %} |
| 29 |
</main> |
| 30 |
{% endblock %} |
| 31 |
</div> |
| 32 |
<script> |
| 33 |
document.querySelectorAll("main table").forEach((table) => { |
| 34 |
const headers = table.querySelectorAll("thead th"); |
| 35 |
const tbody = table.querySelector("tbody"); |
| 36 |
if (!headers.length || !tbody) return; |
| 37 |
|
| 38 |
const cellValue = (row, idx) => { |
| 39 |
const cell = row.children[idx]; |
| 40 |
if (!cell) return { num: NaN, text: "" }; |
| 41 |
const rawSort = cell.getAttribute("data-sort"); |
| 42 |
if (rawSort !== null) { |
| 43 |
return { num: Number(rawSort), text: rawSort }; |
| 44 |
} |
| 45 |
const text = cell.textContent.trim(); |
| 46 |
return { num: NaN, text }; |
| 47 |
}; |
| 48 |
|
| 49 |
headers.forEach((th, colIdx) => { |
| 50 |
th.setAttribute("tabindex", "0"); |
| 51 |
th.setAttribute("role", "columnheader"); |
| 52 |
if (!th.hasAttribute("aria-sort")) { |
| 53 |
th.setAttribute("aria-sort", "none"); |
| 54 |
} |
| 55 |
|
| 56 |
const sortTableBy = (dir) => { |
| 57 |
const factor = dir === "asc" ? 1 : -1; |
| 58 |
|
| 59 |
headers.forEach((h) => { |
| 60 |
h.classList.remove("sort-asc", "sort-desc"); |
| 61 |
h.setAttribute("aria-sort", "none"); |
| 62 |
}); |
| 63 |
th.classList.add(dir === "asc" ? "sort-asc" : "sort-desc"); |
| 64 |
th.setAttribute( |
| 65 |
"aria-sort", |
| 66 |
dir === "asc" ? "ascending" : "descending" |
| 67 |
); |
| 68 |
|
| 69 |
const allRows = Array.from(tbody.querySelectorAll(":scope > tr")); |
| 70 |
const topPinned = allRows.filter((r) => |
| 71 |
r.classList.contains("tree-parent") |
| 72 |
); |
| 73 |
const bottomPinned = allRows.filter((r) => |
| 74 |
r.classList.contains("no-background") |
| 75 |
); |
| 76 |
const sortableRows = allRows.filter( |
| 77 |
(r) => |
| 78 |
!r.classList.contains("tree-parent") && |
| 79 |
!r.classList.contains("no-background") |
| 80 |
); |
| 81 |
|
| 82 |
sortableRows.sort((rowA, rowB) => { |
| 83 |
const a = cellValue(rowA, colIdx); |
| 84 |
const b = cellValue(rowB, colIdx); |
| 85 |
if (!Number.isNaN(a.num) && !Number.isNaN(b.num)) { |
| 86 |
return (a.num - b.num) * factor; |
| 87 |
} |
| 88 |
return ( |
| 89 |
a.text.localeCompare(b.text, undefined, { |
| 90 |
numeric: true, |
| 91 |
sensitivity: "base", |
| 92 |
}) * factor |
| 93 |
); |
| 94 |
}); |
| 95 |
|
| 96 |
tbody.append(...topPinned, ...sortableRows, ...bottomPinned); |
| 97 |
}; |
| 98 |
|
| 99 |
// Apply initial sort immediately on page load if header has sort-asc or sort-desc |
| 100 |
if (th.classList.contains("sort-desc")) { |
| 101 |
sortTableBy("desc"); |
| 102 |
} else if (th.classList.contains("sort-asc")) { |
| 103 |
sortTableBy("asc"); |
| 104 |
} |
| 105 |
|
| 106 |
const toggleSort = () => { |
| 107 |
const nextDir = th.classList.contains("sort-asc") ? "desc" : "asc"; |
| 108 |
sortTableBy(nextDir); |
| 109 |
}; |
| 110 |
|
| 111 |
th.addEventListener("click", toggleSort); |
| 112 |
th.addEventListener("keydown", (e) => { |
| 113 |
if (e.key === "Enter" || e.key === " ") { |
| 114 |
e.preventDefault(); |
| 115 |
toggleSort(); |
| 116 |
} |
| 117 |
}); |
| 118 |
}); |
| 119 |
}); |
| 120 |
</script> |
| 121 |
</body> |
| 122 |
</html> |