infoscreens/35c3-events/calendar.js

170 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2018-12-24 15:18:35 +01:00
import dateformat from 'dateformat';
2018-12-25 01:18:53 +01:00
import cbaseevents from './c-base';
2018-12-24 15:18:35 +01:00
import '../elements/time';
function sortEvents(a, b) {
if (a.start < b.start) {
return -1;
}
if (a.start > b.start) {
return 1;
}
return 0;
}
function getEvents(number) {
const now = new Date();
2018-12-25 01:18:53 +01:00
return Promise.resolve(cbaseevents)
.then(events => events.filter((e) => {
const endDate = new Date(e.end);
if (endDate < now) {
return false;
}
return true;
}))
.then(baseevents => fetch('http://c-flo.cbrp3.c-base.org/35c3/fahrplan')
.then(res => res.json())
.then((res) => {
// res.schedule.conference.days[0].rooms.Adams[0]
const talks = [];
res.schedule.conference.days.forEach((day) => {
const dayEnd = new Date(day.day_end);
if (dayEnd < now) {
// Already done with this day
return;
}
Object.keys(day.rooms).forEach((room) => {
day.rooms[room].forEach((slot) => {
2018-12-26 12:24:09 +01:00
if (!slot.date) {
return;
}
2018-12-25 01:18:53 +01:00
const start = new Date(slot.date);
const [durationH, durationM] = slot.duration.split(':').map(val => parseInt(val, 10));
const end = new Date(slot.date);
end.setHours(end.getHours() + durationH);
end.setMinutes(end.getMinutes() + durationM);
if (end < now) {
// This event is already over
return;
}
talks.push({
allDay: false,
start: start.toISOString(),
end: end.toISOString(),
title: slot.title,
id: room,
});
2018-12-24 15:18:35 +01:00
});
});
});
2018-12-26 12:24:09 +01:00
return talks;
2018-12-25 01:18:53 +01:00
})
.then(talks => baseevents.concat(talks)))
2018-12-24 15:18:35 +01:00
.then(talks => fetch('https://launchlibrary.net/1.3/launch/next/2')
.then(res => res.json())
.then((res) => {
const launches = res.launches.map((launch) => {
const start = new Date(launch.windowstart);
const end = new Date(launch.windowend);
return {
allDay: false,
start: start.toISOString(),
end: end.toISOString(),
title: launch.name,
id: launch.lsp.abbrev,
type: 'launch',
};
});
return launches;
})
.then((launches) => {
const allEvents = talks.concat(launches);
allEvents.sort(sortEvents);
return allEvents.slice(0, number);
}));
}
let prevDate = new Date();
function renderEvent(event, container) {
const now = new Date();
const row = document.createElement('tr');
const time = document.createElement('td');
time.className = 'time';
const destination = document.createElement('td');
destination.className = 'destination';
const code = document.createElement('td');
code.className = 'code';
const status = document.createElement('td');
status.className = 'status';
const startDate = new Date(event.start);
const pad = (t) => {
let val = `${t}`;
if (val.length === 1) {
val = `0${val}`;
}
if (val.length === 2) {
val = `0${val}`;
}
return val;
};
const cleanTitle = (title) => {
let cleaned = title.replace(/\sBerlin$/, '');
cleaned = cleaned.replace(/\sUser Group/, ' UG');
return cleaned;
};
const timeFormat = 'HH:MM';
if (event.allDay) {
time.innerHTML = '--:--';
} else {
time.innerHTML = dateformat(startDate, timeFormat);
}
if (typeof event.id === 'number') {
code.innerHTML = `C${pad(event.id)}`;
} else {
code.innerHTML = event.id;
}
destination.innerHTML = cleanTitle(event.title);
if (event.type === 'launch') {
destination.classList.add('launch');
}
if (startDate.toDateString() !== prevDate.toDateString()) {
status.innerHTML = dateformat(startDate, 'dd.mm.');
}
if (startDate.toDateString() === now.toDateString()) {
row.classList.add('today');
if (startDate < now) {
status.innerHTML = 'Ongoing';
status.classList.add('ongoing');
} else {
status.innerHTML = 'Today';
}
}
row.appendChild(status);
row.appendChild(time);
row.appendChild(destination);
row.appendChild(code);
container.appendChild(row);
prevDate = startDate;
}
function render() {
const table = document.getElementById('events');
getEvents(17)
.then((events) => {
while (table.firstChild) {
table.removeChild(table.firstChild);
}
events.forEach(event => renderEvent(event, table));
});
}
function onPageReady() {
window.removeEventListener('load', onPageReady, false);
render();
setInterval(render, 30000);
}
window.addEventListener('load', onPageReady, false);