2024-08-19 03:15:22 +02:00
|
|
|
import { setupPopovers } from './popover.js';
|
|
|
|
|
2024-04-27 21:10:24 +02:00
|
|
|
function throttledDebounce(callback, maxDebounceTimes, debounceDelay) {
|
|
|
|
let debounceTimeout;
|
|
|
|
let timesDebounced = 0;
|
|
|
|
|
|
|
|
return function () {
|
|
|
|
if (timesDebounced == maxDebounceTimes) {
|
|
|
|
clearTimeout(debounceTimeout);
|
|
|
|
timesDebounced = 0;
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeout(debounceTimeout);
|
|
|
|
timesDebounced++;
|
|
|
|
|
|
|
|
debounceTimeout = setTimeout(() => {
|
|
|
|
timesDebounced = 0;
|
|
|
|
callback();
|
|
|
|
}, debounceDelay);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-07-10 15:22:58 +02:00
|
|
|
async function fetchPageContent(pageData) {
|
2024-04-27 21:10:24 +02:00
|
|
|
// TODO: handle non 200 status codes/time outs
|
|
|
|
// TODO: add retries
|
2024-08-05 15:08:16 +02:00
|
|
|
const response = await fetch(`${pageData.baseURL}/api/pages/${pageData.slug}/content/`);
|
2024-04-27 21:10:24 +02:00
|
|
|
const content = await response.text();
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupCarousels() {
|
|
|
|
const carouselElements = document.getElementsByClassName("carousel-container");
|
|
|
|
|
2024-05-16 21:41:50 +02:00
|
|
|
if (carouselElements.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-27 21:10:24 +02:00
|
|
|
for (let i = 0; i < carouselElements.length; i++) {
|
|
|
|
const carousel = carouselElements[i];
|
2024-05-14 15:32:51 +02:00
|
|
|
carousel.classList.add("show-right-cutoff");
|
2024-04-27 21:10:24 +02:00
|
|
|
const itemsContainer = carousel.getElementsByClassName("carousel-items-container")[0];
|
|
|
|
|
|
|
|
const determineSideCutoffs = () => {
|
|
|
|
if (itemsContainer.scrollLeft != 0) {
|
|
|
|
carousel.classList.add("show-left-cutoff");
|
|
|
|
} else {
|
|
|
|
carousel.classList.remove("show-left-cutoff");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Math.ceil(itemsContainer.scrollLeft) + itemsContainer.clientWidth < itemsContainer.scrollWidth) {
|
|
|
|
carousel.classList.add("show-right-cutoff");
|
|
|
|
} else {
|
|
|
|
carousel.classList.remove("show-right-cutoff");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const determineSideCutoffsRateLimited = throttledDebounce(determineSideCutoffs, 20, 100);
|
|
|
|
|
|
|
|
itemsContainer.addEventListener("scroll", determineSideCutoffsRateLimited);
|
2024-05-23 01:05:21 +02:00
|
|
|
window.addEventListener("resize", determineSideCutoffsRateLimited);
|
2024-04-27 21:10:24 +02:00
|
|
|
|
2024-05-18 19:42:23 +02:00
|
|
|
afterContentReady(determineSideCutoffs);
|
2024-04-27 21:10:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const minuteInSeconds = 60;
|
|
|
|
const hourInSeconds = minuteInSeconds * 60;
|
|
|
|
const dayInSeconds = hourInSeconds * 24;
|
|
|
|
const monthInSeconds = dayInSeconds * 30;
|
|
|
|
const yearInSeconds = monthInSeconds * 12;
|
|
|
|
|
|
|
|
function relativeTimeSince(timestamp) {
|
|
|
|
const delta = Math.round((Date.now() / 1000) - timestamp);
|
|
|
|
|
|
|
|
if (delta < minuteInSeconds) {
|
|
|
|
return "1m";
|
|
|
|
}
|
|
|
|
if (delta < hourInSeconds) {
|
|
|
|
return Math.floor(delta / minuteInSeconds) + "m";
|
|
|
|
}
|
|
|
|
if (delta < dayInSeconds) {
|
|
|
|
return Math.floor(delta / hourInSeconds) + "h";
|
|
|
|
}
|
|
|
|
if (delta < monthInSeconds) {
|
|
|
|
return Math.floor(delta / dayInSeconds) + "d";
|
|
|
|
}
|
|
|
|
if (delta < yearInSeconds) {
|
|
|
|
return Math.floor(delta / monthInSeconds) + "mo";
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math.floor(delta / yearInSeconds) + "y";
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateRelativeTimeForElements(elements)
|
|
|
|
{
|
|
|
|
for (let i = 0; i < elements.length; i++)
|
|
|
|
{
|
|
|
|
const element = elements[i];
|
|
|
|
const timestamp = element.dataset.dynamicRelativeTime;
|
|
|
|
|
|
|
|
if (timestamp === undefined)
|
|
|
|
continue
|
|
|
|
|
2024-05-25 03:28:45 +02:00
|
|
|
element.textContent = relativeTimeSince(timestamp);
|
2024-04-27 21:10:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-11 21:30:10 +02:00
|
|
|
function setupSearchBoxes() {
|
2024-05-25 04:58:11 +02:00
|
|
|
const searchWidgets = document.getElementsByClassName("search");
|
|
|
|
|
|
|
|
if (searchWidgets.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < searchWidgets.length; i++) {
|
|
|
|
const widget = searchWidgets[i];
|
|
|
|
const defaultSearchUrl = widget.dataset.defaultSearchUrl;
|
2024-06-11 21:30:10 +02:00
|
|
|
const newTab = widget.dataset.newTab === "true";
|
2024-05-25 04:58:11 +02:00
|
|
|
const inputElement = widget.getElementsByClassName("search-input")[0];
|
|
|
|
const bangElement = widget.getElementsByClassName("search-bang")[0];
|
|
|
|
const bangs = widget.querySelectorAll(".search-bangs > input");
|
|
|
|
const bangsMap = {};
|
|
|
|
const kbdElement = widget.getElementsByTagName("kbd")[0];
|
|
|
|
let currentBang = null;
|
|
|
|
|
|
|
|
for (let j = 0; j < bangs.length; j++) {
|
|
|
|
const bang = bangs[j];
|
|
|
|
bangsMap[bang.dataset.shortcut] = bang;
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleKeyDown = (event) => {
|
|
|
|
if (event.key == "Escape") {
|
|
|
|
inputElement.blur();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key == "Enter") {
|
|
|
|
const input = inputElement.value.trim();
|
|
|
|
let query;
|
|
|
|
let searchUrlTemplate;
|
|
|
|
|
|
|
|
if (currentBang != null) {
|
|
|
|
query = input.slice(currentBang.dataset.shortcut.length + 1);
|
|
|
|
searchUrlTemplate = currentBang.dataset.url;
|
|
|
|
} else {
|
|
|
|
query = input;
|
|
|
|
searchUrlTemplate = defaultSearchUrl;
|
|
|
|
}
|
2024-07-02 23:16:16 +02:00
|
|
|
if (query.length == 0 && currentBang == null) {
|
2024-05-25 04:58:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = searchUrlTemplate.replace("!QUERY!", encodeURIComponent(query));
|
|
|
|
|
2024-06-09 15:18:56 +02:00
|
|
|
if (newTab && !event.ctrlKey || !newTab && event.ctrlKey) {
|
2024-05-25 04:58:11 +02:00
|
|
|
window.open(url, '_blank').focus();
|
|
|
|
} else {
|
|
|
|
window.location.href = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const changeCurrentBang = (bang) => {
|
|
|
|
currentBang = bang;
|
|
|
|
bangElement.textContent = bang != null ? bang.dataset.title : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleInput = (event) => {
|
2024-07-02 23:16:16 +02:00
|
|
|
const value = event.target.value.trim();
|
|
|
|
if (value in bangsMap) {
|
|
|
|
changeCurrentBang(bangsMap[value]);
|
|
|
|
return;
|
|
|
|
}
|
2024-05-25 04:58:11 +02:00
|
|
|
|
2024-07-02 23:16:16 +02:00
|
|
|
const words = value.split(" ");
|
2024-05-25 04:58:11 +02:00
|
|
|
if (words.length >= 2 && words[0] in bangsMap) {
|
|
|
|
changeCurrentBang(bangsMap[words[0]]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
changeCurrentBang(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
inputElement.addEventListener("focus", () => {
|
|
|
|
document.addEventListener("keydown", handleKeyDown);
|
|
|
|
document.addEventListener("input", handleInput);
|
|
|
|
});
|
|
|
|
inputElement.addEventListener("blur", () => {
|
|
|
|
document.removeEventListener("keydown", handleKeyDown);
|
|
|
|
document.removeEventListener("input", handleInput);
|
|
|
|
});
|
|
|
|
|
|
|
|
document.addEventListener("keydown", (event) => {
|
|
|
|
if (['INPUT', 'TEXTAREA'].includes(document.activeElement.tagName)) return;
|
|
|
|
if (event.key != "s") return;
|
|
|
|
|
|
|
|
inputElement.focus();
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
kbdElement.addEventListener("mousedown", () => {
|
|
|
|
requestAnimationFrame(() => inputElement.focus());
|
|
|
|
});
|
2024-04-27 21:10:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupDynamicRelativeTime() {
|
|
|
|
const elements = document.querySelectorAll("[data-dynamic-relative-time]");
|
|
|
|
const updateInterval = 60 * 1000;
|
|
|
|
let lastUpdateTime = Date.now();
|
|
|
|
|
2024-05-16 21:41:50 +02:00
|
|
|
updateRelativeTimeForElements(elements);
|
|
|
|
|
2024-04-27 21:10:24 +02:00
|
|
|
const updateElementsAndTimestamp = () => {
|
|
|
|
updateRelativeTimeForElements(elements);
|
|
|
|
lastUpdateTime = Date.now();
|
|
|
|
};
|
|
|
|
|
|
|
|
const scheduleRepeatingUpdate = () => setInterval(updateElementsAndTimestamp, updateInterval);
|
|
|
|
|
|
|
|
if (document.hidden === undefined) {
|
|
|
|
scheduleRepeatingUpdate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeout = scheduleRepeatingUpdate();
|
|
|
|
|
|
|
|
document.addEventListener("visibilitychange", () => {
|
|
|
|
if (document.hidden) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const delta = Date.now() - lastUpdateTime;
|
|
|
|
|
|
|
|
if (delta >= updateInterval) {
|
|
|
|
updateElementsAndTimestamp();
|
|
|
|
timeout = scheduleRepeatingUpdate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
updateElementsAndTimestamp();
|
|
|
|
timeout = scheduleRepeatingUpdate();
|
|
|
|
}, updateInterval - delta);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:34:07 +02:00
|
|
|
function setupGroups() {
|
|
|
|
const groups = document.getElementsByClassName("widget-type-group");
|
|
|
|
|
|
|
|
if (groups.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let g = 0; g < groups.length; g++) {
|
|
|
|
const group = groups[g];
|
|
|
|
const titles = group.getElementsByClassName("widget-header")[0].children;
|
|
|
|
const tabs = group.getElementsByClassName("widget-group-contents")[0].children;
|
|
|
|
let current = 0;
|
|
|
|
|
|
|
|
for (let t = 0; t < titles.length; t++) {
|
|
|
|
const title = titles[t];
|
|
|
|
title.addEventListener("click", () => {
|
|
|
|
if (t == current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < titles.length; i++) {
|
|
|
|
titles[i].classList.remove("widget-group-title-current");
|
|
|
|
tabs[i].classList.remove("widget-group-content-current");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current < t) {
|
|
|
|
tabs[t].dataset.direction = "right";
|
|
|
|
} else {
|
|
|
|
tabs[t].dataset.direction = "left";
|
|
|
|
}
|
|
|
|
|
|
|
|
current = t;
|
|
|
|
|
|
|
|
title.classList.add("widget-group-title-current");
|
|
|
|
tabs[t].classList.add("widget-group-content-current");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 04:18:55 +02:00
|
|
|
function setupLazyImages() {
|
|
|
|
const images = document.querySelectorAll("img[loading=lazy]");
|
|
|
|
|
|
|
|
if (images.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function imageFinishedTransition(image) {
|
|
|
|
image.classList.add("finished-transition");
|
|
|
|
}
|
|
|
|
|
2024-05-18 19:42:23 +02:00
|
|
|
afterContentReady(() => {
|
2024-05-18 19:52:54 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
for (let i = 0; i < images.length; i++) {
|
|
|
|
const image = images[i];
|
2024-05-16 21:41:50 +02:00
|
|
|
|
2024-05-18 19:52:54 +02:00
|
|
|
if (image.complete) {
|
|
|
|
image.classList.add("cached");
|
|
|
|
setTimeout(() => imageFinishedTransition(image), 1);
|
|
|
|
} else {
|
|
|
|
// TODO: also handle error event
|
|
|
|
image.addEventListener("load", () => {
|
|
|
|
image.classList.add("loaded");
|
|
|
|
setTimeout(() => imageFinishedTransition(image), 400);
|
|
|
|
});
|
|
|
|
}
|
2024-05-16 21:41:50 +02:00
|
|
|
}
|
2024-05-18 19:52:54 +02:00
|
|
|
}, 1);
|
2024-05-18 19:42:23 +02:00
|
|
|
});
|
2024-05-16 21:41:50 +02:00
|
|
|
}
|
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
function attachExpandToggleButton(collapsibleContainer) {
|
2024-05-16 21:41:50 +02:00
|
|
|
const showMoreText = "Show more";
|
|
|
|
const showLessText = "Show less";
|
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
let expanded = false;
|
|
|
|
const button = document.createElement("button");
|
|
|
|
const icon = document.createElement("span");
|
|
|
|
icon.classList.add("expand-toggle-button-icon");
|
|
|
|
const textNode = document.createTextNode(showMoreText);
|
|
|
|
button.classList.add("expand-toggle-button");
|
|
|
|
button.append(textNode, icon);
|
|
|
|
button.addEventListener("click", () => {
|
|
|
|
expanded = !expanded;
|
|
|
|
|
|
|
|
if (expanded) {
|
|
|
|
collapsibleContainer.classList.add("container-expanded");
|
|
|
|
button.classList.add("container-expanded");
|
|
|
|
textNode.nodeValue = showLessText;
|
|
|
|
return;
|
|
|
|
}
|
2024-05-16 21:41:50 +02:00
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
const topBefore = button.getClientRects()[0].top;
|
2024-05-16 22:17:10 +02:00
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
collapsibleContainer.classList.remove("container-expanded");
|
|
|
|
button.classList.remove("container-expanded");
|
|
|
|
textNode.nodeValue = showMoreText;
|
2024-05-16 22:17:10 +02:00
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
const topAfter = button.getClientRects()[0].top;
|
2024-05-16 22:17:10 +02:00
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
if (topAfter > 0)
|
|
|
|
return;
|
2024-05-16 22:17:10 +02:00
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
window.scrollBy({
|
|
|
|
top: topAfter - topBefore,
|
|
|
|
behavior: "instant"
|
2024-05-16 21:41:50 +02:00
|
|
|
});
|
2024-05-17 13:15:44 +02:00
|
|
|
});
|
2024-05-16 21:41:50 +02:00
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
collapsibleContainer.after(button);
|
|
|
|
|
|
|
|
return button;
|
|
|
|
};
|
2024-05-16 21:41:50 +02:00
|
|
|
|
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
function setupCollapsibleLists() {
|
|
|
|
const collapsibleLists = document.querySelectorAll(".list.collapsible-container");
|
|
|
|
|
|
|
|
if (collapsibleLists.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < collapsibleLists.length; i++) {
|
|
|
|
const list = collapsibleLists[i];
|
|
|
|
|
|
|
|
if (list.dataset.collapseAfter === undefined) {
|
2024-05-16 21:41:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
const collapseAfter = parseInt(list.dataset.collapseAfter);
|
|
|
|
|
|
|
|
if (collapseAfter == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-05-16 21:41:50 +02:00
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
if (list.children.length <= collapseAfter) {
|
2024-05-16 21:41:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
attachExpandToggleButton(list);
|
2024-05-16 21:41:50 +02:00
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
for (let c = collapseAfter; c < list.children.length; c++) {
|
|
|
|
const child = list.children[c];
|
|
|
|
child.classList.add("collapsible-item");
|
2024-05-16 21:41:50 +02:00
|
|
|
child.style.animationDelay = ((c - collapseAfter) * 20).toString() + "ms";
|
2024-05-12 04:18:55 +02:00
|
|
|
}
|
2024-05-17 13:15:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupCollapsibleGrids() {
|
|
|
|
const collapsibleGridElements = document.querySelectorAll(".cards-grid.collapsible-container");
|
|
|
|
|
|
|
|
if (collapsibleGridElements.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < collapsibleGridElements.length; i++) {
|
|
|
|
const gridElement = collapsibleGridElements[i];
|
|
|
|
|
|
|
|
if (gridElement.dataset.collapseAfterRows === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const collapseAfterRows = parseInt(gridElement.dataset.collapseAfterRows);
|
|
|
|
|
|
|
|
if (collapseAfterRows == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const getCardsPerRow = () => {
|
|
|
|
return parseInt(getComputedStyle(gridElement).getPropertyValue('--cards-per-row'));
|
|
|
|
};
|
|
|
|
|
|
|
|
const button = attachExpandToggleButton(gridElement);
|
|
|
|
|
|
|
|
let cardsPerRow = 2;
|
|
|
|
|
|
|
|
const resolveCollapsibleItems = () => {
|
|
|
|
const hideItemsAfterIndex = cardsPerRow * collapseAfterRows;
|
|
|
|
|
|
|
|
if (hideItemsAfterIndex >= gridElement.children.length) {
|
|
|
|
button.style.display = "none";
|
|
|
|
} else {
|
|
|
|
button.style.removeProperty("display");
|
|
|
|
}
|
|
|
|
|
|
|
|
let row = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < gridElement.children.length; i++) {
|
|
|
|
const child = gridElement.children[i];
|
|
|
|
|
|
|
|
if (i >= hideItemsAfterIndex) {
|
|
|
|
child.classList.add("collapsible-item");
|
|
|
|
child.style.animationDelay = (row * 40).toString() + "ms";
|
|
|
|
|
|
|
|
if (i % cardsPerRow + 1 == cardsPerRow) {
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
child.classList.remove("collapsible-item");
|
|
|
|
child.style.removeProperty("animation-delay");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-18 19:42:23 +02:00
|
|
|
afterContentReady(() => {
|
2024-05-17 13:15:44 +02:00
|
|
|
cardsPerRow = getCardsPerRow();
|
|
|
|
resolveCollapsibleItems();
|
2024-05-18 19:42:23 +02:00
|
|
|
});
|
2024-05-17 13:15:44 +02:00
|
|
|
|
|
|
|
window.addEventListener("resize", () => {
|
|
|
|
const newCardsPerRow = getCardsPerRow();
|
|
|
|
|
|
|
|
if (cardsPerRow == newCardsPerRow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cardsPerRow = newCardsPerRow;
|
|
|
|
resolveCollapsibleItems();
|
|
|
|
});
|
2024-05-12 04:18:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-18 19:42:23 +02:00
|
|
|
const contentReadyCallbacks = [];
|
|
|
|
|
|
|
|
function afterContentReady(callback) {
|
|
|
|
contentReadyCallbacks.push(callback);
|
|
|
|
}
|
|
|
|
|
2024-05-25 03:28:45 +02:00
|
|
|
const weekDayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
|
|
|
|
const monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
|
|
|
|
|
|
|
|
function makeSettableTimeElement(element, hourFormat) {
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
const hour = document.createElement('span');
|
|
|
|
const minute = document.createElement('span');
|
|
|
|
const amPm = document.createElement('span');
|
|
|
|
fragment.append(hour, document.createTextNode(':'), minute);
|
|
|
|
|
|
|
|
if (hourFormat == '12h') {
|
|
|
|
fragment.append(document.createTextNode(' '), amPm);
|
2024-05-18 21:40:33 +02:00
|
|
|
}
|
2024-05-25 03:28:45 +02:00
|
|
|
|
|
|
|
element.append(fragment);
|
|
|
|
|
|
|
|
return (date) => {
|
|
|
|
const hours = date.getHours();
|
|
|
|
|
|
|
|
if (hourFormat == '12h') {
|
|
|
|
amPm.textContent = hours < 12 ? 'AM' : 'PM';
|
|
|
|
hour.textContent = hours % 12 || 12;
|
|
|
|
} else {
|
|
|
|
hour.textContent = hours < 10 ? '0' + hours : hours;
|
|
|
|
}
|
|
|
|
|
|
|
|
const minutes = date.getMinutes();
|
|
|
|
minute.textContent = minutes < 10 ? '0' + minutes : minutes;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function timeInZone(now, zone) {
|
|
|
|
let timeInZone;
|
|
|
|
|
|
|
|
try {
|
|
|
|
timeInZone = new Date(now.toLocaleString('en-US', { timeZone: zone }));
|
|
|
|
} catch (e) {
|
|
|
|
// TODO: indicate to the user that this is an invalid timezone
|
|
|
|
console.error(e);
|
|
|
|
timeInZone = now
|
|
|
|
}
|
|
|
|
|
|
|
|
const diffInHours = Math.round((timeInZone.getTime() - now.getTime()) / 1000 / 60 / 60);
|
|
|
|
|
|
|
|
return { time: timeInZone, diffInHours: diffInHours };
|
2024-05-18 21:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setupClocks() {
|
2024-05-25 03:28:45 +02:00
|
|
|
const clocks = document.getElementsByClassName('clock');
|
|
|
|
|
|
|
|
if (clocks.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateCallbacks = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < clocks.length; i++) {
|
|
|
|
const clock = clocks[i];
|
|
|
|
const hourFormat = clock.dataset.hourFormat;
|
|
|
|
const localTimeContainer = clock.querySelector('[data-local-time]');
|
|
|
|
const localDateElement = localTimeContainer.querySelector('[data-date]');
|
|
|
|
const localWeekdayElement = localTimeContainer.querySelector('[data-weekday]');
|
|
|
|
const localYearElement = localTimeContainer.querySelector('[data-year]');
|
|
|
|
const timeZoneContainers = clock.querySelectorAll('[data-time-in-zone]');
|
|
|
|
|
|
|
|
const setLocalTime = makeSettableTimeElement(
|
|
|
|
localTimeContainer.querySelector('[data-time]'),
|
|
|
|
hourFormat
|
|
|
|
);
|
|
|
|
|
|
|
|
updateCallbacks.push((now) => {
|
|
|
|
setLocalTime(now);
|
|
|
|
localDateElement.textContent = now.getDate() + ' ' + monthNames[now.getMonth()];
|
|
|
|
localWeekdayElement.textContent = weekDayNames[now.getDay()];
|
|
|
|
localYearElement.textContent = now.getFullYear();
|
|
|
|
});
|
|
|
|
|
|
|
|
for (var z = 0; z < timeZoneContainers.length; z++) {
|
|
|
|
const timeZoneContainer = timeZoneContainers[z];
|
|
|
|
const diffElement = timeZoneContainer.querySelector('[data-time-diff]');
|
|
|
|
|
|
|
|
const setZoneTime = makeSettableTimeElement(
|
|
|
|
timeZoneContainer.querySelector('[data-time]'),
|
|
|
|
hourFormat
|
|
|
|
);
|
|
|
|
|
|
|
|
updateCallbacks.push((now) => {
|
|
|
|
const { time, diffInHours } = timeInZone(now, timeZoneContainer.dataset.timeInZone);
|
|
|
|
setZoneTime(time);
|
|
|
|
diffElement.textContent = (diffInHours <= 0 ? diffInHours : '+' + diffInHours) + 'h';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateClocks = () => {
|
|
|
|
const now = new Date();
|
|
|
|
|
|
|
|
for (var i = 0; i < updateCallbacks.length; i++)
|
|
|
|
updateCallbacks[i](now);
|
|
|
|
|
|
|
|
setTimeout(updateClocks, (60 - now.getSeconds()) * 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
updateClocks();
|
2024-05-18 21:40:33 +02:00
|
|
|
}
|
|
|
|
|
2024-04-27 21:10:24 +02:00
|
|
|
async function setupPage() {
|
|
|
|
const pageElement = document.getElementById("page");
|
2024-05-16 21:41:50 +02:00
|
|
|
const pageContentElement = document.getElementById("page-content");
|
2024-07-10 15:22:58 +02:00
|
|
|
const pageContent = await fetchPageContent(pageData);
|
2024-04-27 21:10:24 +02:00
|
|
|
|
2024-05-16 21:41:50 +02:00
|
|
|
pageContentElement.innerHTML = pageContent;
|
2024-04-27 21:10:24 +02:00
|
|
|
|
2024-05-16 21:41:50 +02:00
|
|
|
try {
|
2024-08-19 03:15:22 +02:00
|
|
|
setupPopovers();
|
2024-05-18 21:40:33 +02:00
|
|
|
setupClocks()
|
2024-05-16 21:41:50 +02:00
|
|
|
setupCarousels();
|
2024-06-11 21:30:10 +02:00
|
|
|
setupSearchBoxes();
|
2024-05-16 21:41:50 +02:00
|
|
|
setupCollapsibleLists();
|
2024-05-17 13:15:44 +02:00
|
|
|
setupCollapsibleGrids();
|
2024-08-01 22:34:07 +02:00
|
|
|
setupGroups();
|
2024-05-16 21:41:50 +02:00
|
|
|
setupDynamicRelativeTime();
|
2024-05-18 19:52:54 +02:00
|
|
|
setupLazyImages();
|
2024-05-16 21:41:50 +02:00
|
|
|
} finally {
|
|
|
|
pageElement.classList.add("content-ready");
|
2024-05-18 19:42:23 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < contentReadyCallbacks.length; i++) {
|
|
|
|
contentReadyCallbacks[i]();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
document.body.classList.add("page-columns-transitioned");
|
|
|
|
}, 300);
|
2024-05-16 21:41:50 +02:00
|
|
|
}
|
2024-04-27 21:10:24 +02:00
|
|
|
}
|
|
|
|
|
2024-06-11 13:11:49 +02:00
|
|
|
setupPage();
|