1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-12-25 11:12:30 +01:00
ntfy/docs/static/js/extra.js

104 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-12-02 05:08:12 +01:00
// Link tabs, as per https://facelessuser.github.io/pymdown-extensions/extensions/tabbed/#linked-tabs
2024-03-24 19:21:28 +01:00
const savedCodeTab = localStorage.getItem("savedTab");
const codeTabs = document.querySelectorAll(".tabbed-set > input");
for (const tab of codeTabs) {
2024-03-24 19:21:28 +01:00
tab.addEventListener("click", () => {
const current = document.querySelector(`label[for=${tab.id}]`);
const pos = current.getBoundingClientRect().top;
const labelContent = current.innerHTML;
const labels = document.querySelectorAll(".tabbed-set > label, .tabbed-alternate > .tabbed-labels > label");
for (const label of labels) {
if (label.innerHTML === labelContent) {
document.querySelector(`input[id=${label.getAttribute("for")}]`).checked = true;
}
}
// Preserve scroll position
const delta = (current.getBoundingClientRect().top) - pos;
window.scrollBy(0, delta);
2021-12-02 05:08:12 +01:00
2024-03-24 19:21:28 +01:00
// Save
localStorage.setItem("savedTab", labelContent);
});
2021-12-02 05:08:12 +01:00
2024-03-24 19:21:28 +01:00
// Select saved tab
const current = document.querySelector(`label[for=${tab.id}]`);
const labelContent = current.innerHTML;
if (savedCodeTab === labelContent) {
tab.checked = true;
}
2021-12-02 05:08:12 +01:00
}
2021-12-05 05:37:50 +01:00
// Lightbox for screenshot
2024-03-24 19:21:28 +01:00
const lightbox = document.createElement("div");
lightbox.classList.add("lightbox");
2021-12-05 05:37:50 +01:00
document.body.appendChild(lightbox);
const showScreenshotOverlay = (e, el, group, index) => {
2024-03-24 19:21:28 +01:00
lightbox.classList.add("show");
document.addEventListener("keydown", nextScreenshotKeyboardListener);
return showScreenshot(e, group, index);
2021-12-05 05:37:50 +01:00
};
const showScreenshot = (e, group, index) => {
2024-03-24 19:21:28 +01:00
const actualIndex = resolveScreenshotIndex(group, index);
lightbox.innerHTML = "<div class=\"close-lightbox\"></div>" + screenshots[group][actualIndex].innerHTML;
lightbox.querySelector("img").onclick = (e) => {
return showScreenshot(e, group, actualIndex + 1);
};
currentScreenshotGroup = group;
currentScreenshotIndex = actualIndex;
e.stopPropagation();
return false;
2021-12-05 05:37:50 +01:00
};
const nextScreenshot = (e) => {
2024-03-24 19:21:28 +01:00
return showScreenshot(e, currentScreenshotGroup, currentScreenshotIndex + 1);
2021-12-05 05:37:50 +01:00
};
const previousScreenshot = (e) => {
2024-03-24 19:21:28 +01:00
return showScreenshot(e, currentScreenshotGroup, currentScreenshotIndex - 1);
2021-12-05 05:37:50 +01:00
};
const resolveScreenshotIndex = (group, index) => {
2024-03-24 19:21:28 +01:00
if (index < 0) {
return screenshots[group].length - 1;
} else if (index > screenshots[group].length - 1) {
return 0;
}
return index;
2021-12-05 05:37:50 +01:00
};
const hideScreenshotOverlay = (e) => {
2024-03-24 19:21:28 +01:00
lightbox.classList.remove("show");
document.removeEventListener("keydown", nextScreenshotKeyboardListener);
2021-12-05 05:37:50 +01:00
};
const nextScreenshotKeyboardListener = (e) => {
2024-03-24 19:21:28 +01:00
switch (e.keyCode) {
case 37:
previousScreenshot(e);
break;
case 39:
nextScreenshot(e);
break;
}
2021-12-05 05:37:50 +01:00
};
2024-03-24 19:21:28 +01:00
let currentScreenshotGroup = "";
2021-12-05 05:37:50 +01:00
let currentScreenshotIndex = 0;
let screenshots = {};
2024-03-24 19:21:28 +01:00
Array.from(document.getElementsByClassName("screenshots")).forEach((sg) => {
const group = sg.id;
screenshots[group] = [...sg.querySelectorAll("a")];
screenshots[group].forEach((el, index) => {
el.onclick = (e) => {
return showScreenshotOverlay(e, el, group, index);
};
});
2021-12-05 05:37:50 +01:00
});
lightbox.onclick = hideScreenshotOverlay;