BlueGit

timfa bluegit

Files

bluegit / internal / server / static / repo.js

repo.js

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