Debian Codegraph @

docs/static/js/extra.js

raw
1 // Link tabs, as per https://facelessuser.github.io/pymdown-extensions/extensions/tabbed/#linked-tabs
2
3 const savedCodeTab = localStorage.getItem("savedTab");
4 const codeTabs = document.querySelectorAll(".tabbed-set > input");
5 for (const tab of codeTabs) {
6 tab.addEventListener("click", () => {
7 const current = document.querySelector(`label[for=${tab.id}]`);
8 const pos = current.getBoundingClientRect().top;
9 const labelContent = current.innerHTML;
10 const labels = document.querySelectorAll(".tabbed-set > label, .tabbed-alternate > .tabbed-labels > label");
11 for (const label of labels) {
12 if (label.innerHTML === labelContent) {
13 document.querySelector(`input[id=${label.getAttribute("for")}]`).checked = true;
14 }
15 }
16
17 // Preserve scroll position
18 const delta = (current.getBoundingClientRect().top) - pos;
19 window.scrollBy(0, delta);
20
21 // Save
22 localStorage.setItem("savedTab", labelContent);
23 });
24
25 // Select saved tab
26 const current = document.querySelector(`label[for=${tab.id}]`);
27 const labelContent = current.innerHTML;
28 if (savedCodeTab === labelContent) {
29 tab.checked = true;
30 }
31 }
32
33 // Lightbox for screenshot
34
35 const lightbox = document.createElement("div");
36 lightbox.classList.add("lightbox");
37 document.body.appendChild(lightbox);
38
39 const showScreenshotOverlay = (e, el, group, index) => {
40 lightbox.classList.add("show");
41 document.addEventListener("keydown", nextScreenshotKeyboardListener);
42 return showScreenshot(e, group, index);
43 };
44
45 const showScreenshot = (e, group, index) => {
46 const actualIndex = resolveScreenshotIndex(group, index);
47 lightbox.innerHTML = "<div class=\"close-lightbox\"></div>" + screenshots[group][actualIndex].innerHTML;
48 lightbox.querySelector("img").onclick = (e) => {
49 return showScreenshot(e, group, actualIndex + 1);
50 };
51 currentScreenshotGroup = group;
52 currentScreenshotIndex = actualIndex;
53 e.stopPropagation();
54 return false;
55 };
56
57 const nextScreenshot = (e) => {
58 return showScreenshot(e, currentScreenshotGroup, currentScreenshotIndex + 1);
59 };
60
61 const previousScreenshot = (e) => {
62 return showScreenshot(e, currentScreenshotGroup, currentScreenshotIndex - 1);
63 };
64
65 const resolveScreenshotIndex = (group, index) => {
66 if (index < 0) {
67 return screenshots[group].length - 1;
68 } else if (index > screenshots[group].length - 1) {
69 return 0;
70 }
71 return index;
72 };
73
74 const hideScreenshotOverlay = (e) => {
75 lightbox.classList.remove("show");
76 document.removeEventListener("keydown", nextScreenshotKeyboardListener);
77 };
78
79 const nextScreenshotKeyboardListener = (e) => {
80 switch (e.keyCode) {
81 case 37:
82 previousScreenshot(e);
83 break;
84 case 39:
85 nextScreenshot(e);
86 break;
87 }
88 };
89
90 let currentScreenshotGroup = "";
91 let currentScreenshotIndex = 0;
92 let screenshots = {};
93 Array.from(document.getElementsByClassName("screenshots")).forEach((sg) => {
94 const group = sg.id;
95 screenshots[group] = [...sg.querySelectorAll("a")];
96 screenshots[group].forEach((el, index) => {
97 el.onclick = (e) => {
98 return showScreenshotOverlay(e, el, group, index);
99 };
100 });
101 });
102
103 lightbox.onclick = hideScreenshotOverlay;