BlueGit

timfa bluegit

Files

bluegit / internal / server / static / repo.js

repo.js

feat: klap mappen in de bestandsboom in
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed f3820e63 History
repo.js 4.6 KiB Download Raw
1(function () {
2 document.querySelectorAll("select[data-rev]").forEach(function (sel) {
3 sel.addEventListener("change", function () {
4 var url = new URL(window.location.href);
5 url.searchParams.set("rev", sel.value);
6 window.location = url.toString();
7 });
8 });
9 var filter = document.querySelector("[data-tree-filter]");
10 var list = document.querySelector(".treelist");
11 if (list) {
12 var repo = list.getAttribute("data-repo") || "";
13 var key = "bluegit.tree." + repo;
14 var saved = {};
15 try {
16 var raw = JSON.parse(localStorage.getItem(key) || "{}");
17 if (raw && typeof raw === "object") {
18 saved = raw;
19 }
20 } catch (e) {
21 saved = {};
22 }
23 function parentOf(path) {
24 var i = path.lastIndexOf("/");
25 return i < 0 ? "" : path.slice(0, i);
26 }
27 var force = {};
28 var active = null;
29 list.querySelectorAll("li.active").forEach(function (li) {
30 var path = li.getAttribute("data-path") || "";
31 if (!active || path.length > (active.getAttribute("data-path") || "").length) {
32 active = li;
33 }
34 });
35 if (active) {
36 var activePath = active.getAttribute("data-path") || "";
37 if (active.getAttribute("data-dir") === "1") {
38 force[activePath] = true;
39 }
40 var anc = parentOf(activePath);
41 while (anc) {
42 force[anc] = true;
43 anc = parentOf(anc);
44 }
45 }
46 function isOpen(path, li) {
47 if (force[path]) {
48 return true;
49 }
50 if (Object.prototype.hasOwnProperty.call(saved, path)) {
51 return !!saved[path];
52 }
53 return li.classList.contains("open");
54 }
55 function paint() {
56 var items = list.querySelectorAll("li");
57 var byPath = {};
58 var paths = [];
59 items.forEach(function (li) {
60 var path = li.getAttribute("data-path") || "";
61 byPath[path] = li;
62 paths.push(path.toLowerCase());
63 if (li.getAttribute("data-dir") === "1") {
64 var open = isOpen(path, li);
65 li.classList.toggle("open", open);
66 var btn = li.querySelector("button.twist");
67 if (btn) {
68 btn.setAttribute("aria-expanded", open ? "true" : "false");
69 }
70 }
71 });
72 var q = filter ? filter.value.trim().toLowerCase() : "";
73 items.forEach(function (li, i) {
74 var path = li.getAttribute("data-path") || "";
75 var low = paths[i];
76 if (q !== "") {
77 if (low.indexOf(q) !== -1) {
78 li.hidden = false;
79 return;
80 }
81 var prefix = low + "/";
82 var child = false;
83 for (var j = 0; j < paths.length; j++) {
84 if (paths[j].indexOf(prefix) === 0 && paths[j].indexOf(q) !== -1) {
85 child = true;
86 break;
87 }
88 }
89 li.hidden = !child;
90 return;
91 }
92 var hidden = false;
93 var up = parentOf(path);
94 while (up) {
95 var node = byPath[up];
96 if (node && node.getAttribute("data-dir") === "1" && !node.classList.contains("open")) {
97 hidden = true;
98 break;
99 }
100 up = parentOf(up);
101 }
102 li.hidden = hidden;
103 });
104 }
105 function remember(path, open) {
106 delete force[path];
107 saved[path] = open;
108 try {
109 localStorage.setItem(key, JSON.stringify(saved));
110 } catch (e) {}
111 paint();
112 }
113 list.addEventListener("click", function (ev) {
114 var li = ev.target.closest("li");
115 if (!li || !list.contains(li) || li.getAttribute("data-dir") !== "1") {
116 return;
117 }
118 var link = ev.target.closest("a");
119 if (link && li.contains(link) && (ev.metaKey || ev.ctrlKey || ev.shiftKey || ev.altKey)) {
120 return;
121 }
122 ev.preventDefault();
123 var path = li.getAttribute("data-path") || "";
124 remember(path, !li.classList.contains("open"));
125 });
126 if (filter) {
127 filter.addEventListener("input", paint);
128 if (new URLSearchParams(window.location.search).get("focus") === "1") {
129 filter.focus();
130 }
131 }
132 paint();
133 var shown = null;
134 list.querySelectorAll("li.active").forEach(function (li) {
135 if (li.hidden) {
136 return;
137 }
138 if (!shown || (li.getAttribute("data-path") || "").length > (shown.getAttribute("data-path") || "").length) {
139 shown = li;
140 }
141 });
142 if (shown) {
143 var top = shown.offsetTop;
144 var bottom = top + shown.offsetHeight;
145 if (top < list.scrollTop) {
146 list.scrollTop = top;
147 } else if (bottom > list.scrollTop + list.clientHeight) {
148 list.scrollTop = bottom - list.clientHeight;
149 }
150 }
151 }
152})();