BlueGit

timfa bluegit

Files

bluegit / internal / server / static / repo.js

repo.js

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