BlueGit

timfa bluegit

Files

bluegit / internal / server / static / repo.js

repo.js

repo.js 8.2 KiB Download Raw
1(function () {
2 function branchMatch(el, name) {
3 if (!name) return false;
4 var raw = el.getAttribute("data-branch") || "";
5 return raw.split(/\s+/).indexOf(name) !== -1;
6 }
7 function applyCommitGraph() {
8 var slot = document.querySelector(".commitgraph-slot");
9 var graph = slot && slot.querySelector(".commitgraph");
10 if (!graph) return;
11 var sel = document.querySelector("select[data-rev]");
12 var name = sel ? sel.value : (graph.getAttribute("data-selected") || "");
13 graph.setAttribute("data-selected", name);
14 graph.querySelectorAll("[data-branch]").forEach(function (el) {
15 el.classList.toggle("is-glow", branchMatch(el, name));
16 });
17 slot.classList.remove("is-right", "is-hidden");
18 var rect = graph.getBoundingClientRect();
19 if (rect.width === 0) return;
20 var side = document.querySelector(".sidenav");
21 var sideRight = 0;
22 if (side && getComputedStyle(side).display !== "none") {
23 sideRight = side.getBoundingClientRect().right;
24 }
25 // In de flow met gereserveerde breedte: vertrouw de CSS, tenzij
26 // de grafiek duidelijk onder de zijbalk schuift. Niet verplaatsen
27 // om een rand van een paar pixels.
28 var reserved = parseFloat(getComputedStyle(slot).width) || 0;
29 if (reserved > 0 && rect.left >= sideRight - 1) {
30 return;
31 }
32 if (rect.left < sideRight - 1) {
33 slot.classList.add("is-right");
34 rect = graph.getBoundingClientRect();
35 if (rect.right > window.innerWidth - 8 || rect.width === 0 || rect.left < sideRight - 1) {
36 slot.classList.remove("is-right");
37 slot.classList.add("is-hidden");
38 }
39 }
40 }
41 applyCommitGraph();
42 window.addEventListener("resize", applyCommitGraph);
43 document.querySelectorAll("select[data-rev]").forEach(function (sel) {
44 sel.addEventListener("change", function () {
45 applyCommitGraph();
46 var url = new URL(window.location.href);
47 url.searchParams.set("rev", sel.value);
48 url.searchParams.delete("page");
49 window.location = url.toString();
50 });
51 });
52 var filter = document.querySelector("[data-tree-filter]");
53 var list = document.querySelector(".treelist");
54 if (list) {
55 var repo = list.getAttribute("data-repo") || "";
56 var key = "bluegit.tree." + repo;
57 var saved = {};
58 try {
59 var raw = JSON.parse(localStorage.getItem(key) || "{}");
60 if (raw && typeof raw === "object") {
61 saved = raw;
62 }
63 } catch (e) {
64 saved = {};
65 }
66 function parentOf(path) {
67 var i = path.lastIndexOf("/");
68 return i < 0 ? "" : path.slice(0, i);
69 }
70 var force = {};
71 var active = null;
72 list.querySelectorAll("li.active").forEach(function (li) {
73 var path = li.getAttribute("data-path") || "";
74 if (!active || path.length > (active.getAttribute("data-path") || "").length) {
75 active = li;
76 }
77 });
78 if (active) {
79 var activePath = active.getAttribute("data-path") || "";
80 if (active.getAttribute("data-dir") === "1") {
81 force[activePath] = true;
82 }
83 var anc = parentOf(activePath);
84 while (anc) {
85 force[anc] = true;
86 anc = parentOf(anc);
87 }
88 }
89 function isOpen(path, li) {
90 if (force[path]) {
91 return true;
92 }
93 if (Object.prototype.hasOwnProperty.call(saved, path)) {
94 return !!saved[path];
95 }
96 return li.classList.contains("open");
97 }
98 function paint() {
99 var items = list.querySelectorAll("li");
100 var byPath = {};
101 var paths = [];
102 items.forEach(function (li) {
103 var path = li.getAttribute("data-path") || "";
104 byPath[path] = li;
105 paths.push(path.toLowerCase());
106 if (li.getAttribute("data-dir") === "1") {
107 var open = isOpen(path, li);
108 li.classList.toggle("open", open);
109 var btn = li.querySelector("button.twist");
110 if (btn) {
111 btn.setAttribute("aria-expanded", open ? "true" : "false");
112 }
113 }
114 });
115 var q = filter ? filter.value.trim().toLowerCase() : "";
116 items.forEach(function (li, i) {
117 var path = li.getAttribute("data-path") || "";
118 var low = paths[i];
119 if (q !== "") {
120 if (low.indexOf(q) !== -1) {
121 li.hidden = false;
122 return;
123 }
124 var prefix = low + "/";
125 var child = false;
126 for (var j = 0; j < paths.length; j++) {
127 if (paths[j].indexOf(prefix) === 0 && paths[j].indexOf(q) !== -1) {
128 child = true;
129 break;
130 }
131 }
132 li.hidden = !child;
133 return;
134 }
135 var hidden = false;
136 var up = parentOf(path);
137 while (up) {
138 var node = byPath[up];
139 if (node && node.getAttribute("data-dir") === "1" && !node.classList.contains("open")) {
140 hidden = true;
141 break;
142 }
143 up = parentOf(up);
144 }
145 li.hidden = hidden;
146 });
147 }
148 function remember(path, open) {
149 delete force[path];
150 saved[path] = open;
151 try {
152 localStorage.setItem(key, JSON.stringify(saved));
153 } catch (e) {}
154 paint();
155 }
156 list.addEventListener("click", function (ev) {
157 var li = ev.target.closest("li");
158 if (!li || !list.contains(li) || li.getAttribute("data-dir") !== "1") {
159 return;
160 }
161 var link = ev.target.closest("a");
162 if (link && li.contains(link) && (ev.metaKey || ev.ctrlKey || ev.shiftKey || ev.altKey)) {
163 return;
164 }
165 ev.preventDefault();
166 var path = li.getAttribute("data-path") || "";
167 remember(path, !li.classList.contains("open"));
168 });
169 if (filter) {
170 filter.addEventListener("input", paint);
171 if (new URLSearchParams(window.location.search).get("focus") === "1") {
172 filter.focus();
173 }
174 }
175 paint();
176 var shown = null;
177 list.querySelectorAll("li.active").forEach(function (li) {
178 if (li.hidden) {
179 return;
180 }
181 if (!shown || (li.getAttribute("data-path") || "").length > (shown.getAttribute("data-path") || "").length) {
182 shown = li;
183 }
184 });
185 if (shown) {
186 var top = shown.offsetTop;
187 var bottom = top + shown.offsetHeight;
188 if (top < list.scrollTop) {
189 list.scrollTop = top;
190 } else if (bottom > list.scrollTop + list.clientHeight) {
191 list.scrollTop = bottom - list.clientHeight;
192 }
193 }
194 }
195})();
196
197// Bestandskaart: Copy zet de inhoud in het klembord; fouten in een eigen
198// modal. SVG-voorbeeld: lege/ongeldige SVG toont een lege staat.
199(function () {
200 document.querySelectorAll("[data-copy-file]").forEach(function (btn) {
201 btn.addEventListener("click", function () {
202 if (btn.disabled) { return; }
203 var fail = function () {
204 var m = window.BGModal;
205 if (m && m.alert) { m.alert(btn.getAttribute("data-copy-fail"), { title: btn.getAttribute("data-copy-title"), ok: btn.getAttribute("data-copy-close") }); }
206 };
207 fetch(btn.getAttribute("data-copy-file"), { credentials: "same-origin" }).then(function (res) {
208 if (!res.ok || res.headers.get("X-BlueGit-Binary") === "1") { throw new Error("raw"); }
209 return res.text();
210 }).then(function (text) {
211 if (!navigator.clipboard || !window.isSecureContext) { throw new Error("clipboard"); }
212 return navigator.clipboard.writeText(text);
213 }).then(function () {
214 var old = btn.textContent;
215 btn.textContent = btn.getAttribute("data-copy-ok");
216 setTimeout(function () { btn.textContent = old; }, 2000);
217 }).catch(fail);
218 });
219 });
220 document.querySelectorAll("[data-img-preview]").forEach(function (img) {
221 var fit = function () {
222 if (img.naturalWidth && img.naturalWidth < 128 && img.naturalHeight < 128) {
223 img.classList.add("pixel");
224 img.style.width = (img.naturalWidth * Math.max(1, Math.floor(128 / img.naturalWidth))) + "px";
225 }
226 };
227 if (img.complete) { fit(); } else { img.addEventListener("load", fit); }
228 });
229 document.querySelectorAll("[data-svg-preview]").forEach(function (img) {
230 var empty = function () {
231 img.hidden = true;
232 var c = img.parentNode.querySelector("[data-svg-empty]");
233 if (c) { c.hidden = false; }
234 };
235 img.addEventListener("error", empty);
236 });
237})();