BlueGit

timfa bluegit

Files

bluegit / internal / server / static / repo.js

repo.js

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