website/static/dbkcalendar.js
kleines Filmröllchen fffc6a912b
All checks were successful
/ Build and deploy website (push) Successful in 4m35s
redesign calendar
2025-10-12 23:30:32 +02:00

86 lines
3.1 KiB
JavaScript

/*
Include calendar | Chaostreff Backnang | @paddy
Modernization by @kleinesfilmroellchen
*/
const uriRegex =
/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/\-_\.\:]*(\?\S+)?)?)?)/gi;
const timeFormat = new Intl.DateTimeFormat("de-DE", {
dateStyle: "full",
timeStyle: "short",
hourCycle: "h23",
});
export default function hackcal(e, p = 1) {
if (!document.getElementById("dbkcalendar")) return;
const calendarDate = document.getElementById("calendar-date");
const calendarEntry = document.getElementById("calendar-entry");
const calendarParent = document.getElementById("dbkcalendar");
calendarParent.innerHTML = "";
const calUri = `https://hackcal.ctbk.de/?period=${p}`;
fetch(calUri)
.then((res) => res.json())
.then((data) => {
Object.keys(data).forEach(function (date) {
Object.keys(data[date]).forEach(function (uid) {
const event = data[date][uid];
const startTime = event.dtstart;
const day = new Date(startTime);
console.log(day);
const dateItem = calendarDate.content.cloneNode(true);
const timeElement = dateItem.querySelector("time");
timeElement.innerText = timeFormat.format(day);
timeElement.dateTime = date;
calendarParent.appendChild(dateItem);
const entryItem = calendarEntry.content.cloneNode(true);
entryItem.querySelector(".calendar-entry").dataset.uid = uid;
// use description as link on summary if it's a valid URL
const summaryItem = entryItem.querySelector(".summary");
const possibleUri = uriRegex.exec(event.description)?.[0];
const cleanedDescription = event.description.replaceAll(uriRegex, "");
try {
const descriptionUri = new URL(possibleUri);
const descriptionLink = document.createElement("a");
descriptionLink.href = descriptionUri;
descriptionLink.target = "_blank";
descriptionLink.innerText = event.summary;
summaryItem.appendChild(descriptionLink);
} catch (e) {
summaryItem.innerText = event.summary;
}
entryItem.querySelector(".description").innerText =
cleanedDescription;
// self-link location if it's a valid URL
const locationItem = entryItem.querySelector(".location");
try {
const locationUri = new URL(event.location);
const locationLink = document.createElement("a");
locationLink.href = locationUri;
locationLink.target = "_blank";
locationLink.innerText = event.location;
locationItem.appendChild(locationLink);
} catch (e) {
locationItem.innerText = event.location;
}
const categoriesParent = entryItem.querySelector(".categories");
for (const category of event.categories?.split(",") || []) {
const categoryItem = document.createElement("em");
categoryItem.innerText = category;
categoriesParent.appendChild(categoryItem);
}
calendarParent.appendChild(entryItem);
});
});
});
}