ctbk.de/js/vanilla.dbkcalendar.js
Patrick Schwarz b212fc11fa fix js
2024-02-01 20:21:07 +01:00

26 lines
1.5 KiB
JavaScript

/*
Include calendar | Chaostreff Backnang | @paddy
*/
document.addEventListener("DOMContentLoaded", hackcal);
function hackcal(e,p=1) {
var cal_uri = "/hackcal/?period="+p;
var uri_regex = /(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/\-_\.\:]*(\?\S+)?)?)?)/ig
fetch(cal_uri).then(res => res.json()).then(data => {
var items = [];
Object.keys(data).forEach(function (date) {
var day = new Date(date);
items.push("<div data-date='" + date + "'><span>" + day.toLocaleDateString(true, {weekday:'short', day:'numeric', month:'long', year: 'numeric'}) + "</span></div>");
Object.keys(data[date]).forEach(function (uid) {
var event = data[date][uid];
var location = (event.location)?event.location.replace(/\n/g,"<br>").replace(uri_regex, "<a href='$1' target='_blank'>$1</a>"):'';
var description = (event.description)?event.description.replace(/\n/g,"<br>").replace(uri_regex, (url) => {event.summary = `<a href='${url}' target='_blank'>${event.summary}</a>`; return "";}):'';
var categories = (event.categories)?"<i>" + event.categories.replace(",", "</i> <i>") + "</i>":'';
items.push("<div data-uid='" + uid + "'><span>" + event.datestr + "</span><span><b>" + event.summary + "</b> | " + location + "<br/>" + description + categories + "</span></div>");
});
});
document.querySelector('#dbkcalendar').innerHTML = items.join( "" );
});
}