index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff
gitrs / templates / base.html
-rw-r--r-- blob 5315b50e05de160b027e19aa197c5ed4ec135c1e 3903 bytes (plain)
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 applySort = () => {
57
              // 1st click -> ascending (top-down), 2nd click -> descending (bottom-up)
58
              const nextDir = th.classList.contains("sort-asc") ? "desc" : "asc";
59
              const factor = nextDir === "asc" ? 1 : -1;
60
61
              headers.forEach((h) => {
62
                h.classList.remove("sort-asc", "sort-desc");
63
                h.setAttribute("aria-sort", "none");
64
              });
65
              th.classList.add(nextDir === "asc" ? "sort-asc" : "sort-desc");
66
              th.setAttribute(
67
                "aria-sort",
68
                nextDir === "asc" ? "ascending" : "descending"
69
              );
70
71
              const allRows = Array.from(tbody.querySelectorAll(":scope > tr"));
72
              const topPinned = allRows.filter((r) =>
73
                r.classList.contains("tree-parent")
74
              );
75
              const bottomPinned = allRows.filter((r) =>
76
                r.classList.contains("no-background")
77
              );
78
              const sortableRows = allRows.filter(
79
                (r) =>
80
                  !r.classList.contains("tree-parent") &&
81
                  !r.classList.contains("no-background")
82
              );
83
84
              sortableRows.sort((rowA, rowB) => {
85
                const a = cellValue(rowA, colIdx);
86
                const b = cellValue(rowB, colIdx);
87
                if (!Number.isNaN(a.num) && !Number.isNaN(b.num)) {
88
                  return (a.num - b.num) * factor;
89
                }
90
                return (
91
                  a.text.localeCompare(b.text, undefined, {
92
                    numeric: true,
93
                    sensitivity: "base",
94
                  }) * factor
95
                );
96
              });
97
98
              tbody.append(...topPinned, ...sortableRows, ...bottomPinned);
99
            };
100
101
            th.addEventListener("click", applySort);
102
            th.addEventListener("keydown", (e) => {
103
              if (e.key === "Enter" || e.key === " ") {
104
                e.preventDefault();
105
                applySort();
106
              }
107
            });
108
          });
109
        });
110
      </script>
111
  </body>
112
</html>