BlueGit

timfa bluegit

Files

bluegit / internal / server / static / repo.js

repo.js

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