blob: c4fc9ee43bc58c31b259aa25c93ddb9e086029c0 [file] [log] [blame]
async function getCalendar(icalUrl) {
const response = await fetch(icalUrl);
const data = await response.text();
return data;
}
function getDateFromjCal(jCal, event, name) {
const date = event.getFirstPropertyValue(name);
const vtimezone = jCal.getFirstSubcomponent('vtimezone');
if (vtimezone && moment(date.toJSDate()).utcOffset()) {
date.zone = new ICAL.Timezone(vtimezone);
}
var locale = window.navigator.userLanguage || window.navigator.language;
moment.locale(locale);
return moment.parseZone(date.toJSDate()).utcOffset(date.utcOffset() / 60)
};
function insertCalendarEvents(dlElement, icalUrl, maxAgeDays = 1) {
if (!icalUrl) {
return;
}
getCalendar(icalUrl).then(data => {
// console.log(data);
var jcalData = ICAL.parse(data);
var vcalendar = new ICAL.Component(jcalData);
ICAL.helpers.updateTimezones(vcalendar, ICAL.TimezoneService);
const element = dlElement;
var events = vcalendar.getAllSubcomponents('vevent');
var eventArray = [];
var eventKeys = [];
var now = moment();
events.forEach(event => {
var start = getDateFromjCal(vcalendar, event, 'dtstart');
if (now.diff(start, 'days') <= maxAgeDays) {
var key = event.getFirstPropertyValue("dtstart") + "-" + event.getFirstPropertyValue("dtend");
eventArray[key] = event;
eventKeys.push(key);
}
});
dlElement.replaceChildren();
if (eventKeys.length == 0) {
const dd = document.createElement('dd');
dd.appendChild(document.createTextNode("–"));
element.appendChild(dd);
return;
}
eventKeys.sort().reverse().forEach(key => {
var event = eventArray[key];
var summary = event.getFirstPropertyValue('summary');
var start = getDateFromjCal(vcalendar, event, 'dtstart');
var end = getDateFromjCal(vcalendar, event, 'dtend');
const dt = document.createElement('dt');
const time = document.createElement('time');
dt.setAttribute('class', 'maintenance');
const node = document.createTextNode(start.format('dddd, LL'));
time.appendChild(node);
time.setAttribute('datetime', start.format('YYYY-MM-DD'));
dt.appendChild(time);
element.appendChild(dt);
const dd = document.createElement('dd');
const description = document.createTextNode(start.format("HH:mm") + "-" + end.format("HH:mm") + ` ${summary}`);
dd.appendChild(description);
element.appendChild(dd);
});
}
).catch(error => {
console.log(error);
});
}