BlueGit

timfa bluegit

Files

bluegit / internal / server / static / repo.js

repo.js

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