2017-12-01 16:29:50 +01:00
|
|
|
import dateformat from 'dateformat';
|
2017-12-01 17:32:06 +01:00
|
|
|
import '../elements/time';
|
2017-12-01 16:29:50 +01:00
|
|
|
|
2017-12-08 18:43:24 +01:00
|
|
|
function getEvents(number, callback) {
|
2017-12-01 16:29:50 +01:00
|
|
|
const now = new Date();
|
2019-01-28 22:31:39 +01:00
|
|
|
fetch('/../calendar')
|
2017-12-08 18:48:55 +01:00
|
|
|
.then(res => res.json())
|
2019-01-28 22:31:39 +01:00
|
|
|
.then((calendars) => {
|
|
|
|
const allEvents = calendars.c_base_events.concat(
|
|
|
|
calendars.c_base_regulars, calendars.c_base_seminars,
|
|
|
|
);
|
|
|
|
const current = allEvents.filter((event) => {
|
|
|
|
if (event.id === 45) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const start = new Date(event.start);
|
|
|
|
if ((!event.data || event.allDay) && start.toDateString() !== now.toDateString()) {
|
|
|
|
return false;
|
2017-12-08 18:48:55 +01:00
|
|
|
}
|
2019-01-28 22:31:39 +01:00
|
|
|
const end = new Date(event.end);
|
|
|
|
if (start > now) {
|
|
|
|
return false;
|
2017-12-08 18:48:55 +01:00
|
|
|
}
|
2019-01-28 22:31:39 +01:00
|
|
|
if (end < now) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2017-12-08 18:48:55 +01:00
|
|
|
});
|
2019-01-28 22:31:39 +01:00
|
|
|
const upcoming = allEvents.filter((event) => {
|
|
|
|
if (event.id === 45) {
|
|
|
|
return false;
|
2017-12-08 18:48:55 +01:00
|
|
|
}
|
2019-01-28 22:31:39 +01:00
|
|
|
const start = new Date(event.start);
|
|
|
|
if (start > now) {
|
|
|
|
return true;
|
2017-12-08 18:48:55 +01:00
|
|
|
}
|
2019-01-28 22:31:39 +01:00
|
|
|
return false;
|
2017-12-08 18:48:55 +01:00
|
|
|
});
|
2019-01-28 22:31:39 +01:00
|
|
|
|
|
|
|
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',
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const events = current.concat(upcoming, launches);
|
|
|
|
events.sort((a, b) => {
|
|
|
|
if (a.start < b.start) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.start > b.start) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
return callback(events.slice(0, number));
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
const events = current.concat(upcoming);
|
|
|
|
events.sort((a, b) => {
|
|
|
|
if (a.start < b.start) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.start > b.start) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
return callback(events.slice(0, number));
|
|
|
|
});
|
2017-12-08 18:43:24 +01:00
|
|
|
});
|
2017-12-01 16:29:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
2017-12-05 13:59:47 +01:00
|
|
|
const cleanTitle = (title) => {
|
|
|
|
let cleaned = title.replace(/\sBerlin$/, '');
|
|
|
|
cleaned = cleaned.replace(/\sUser Group/, ' UG');
|
|
|
|
return cleaned;
|
|
|
|
};
|
2017-12-01 16:29:50 +01:00
|
|
|
const timeFormat = 'HH:MM';
|
|
|
|
if (event.allDay) {
|
|
|
|
time.innerHTML = '--:--';
|
|
|
|
} else {
|
|
|
|
time.innerHTML = dateformat(startDate, timeFormat);
|
|
|
|
}
|
|
|
|
|
2017-12-08 18:43:24 +01:00
|
|
|
if (typeof event.id === 'number') {
|
2019-01-04 18:37:43 +01:00
|
|
|
code.innerHTML = `c${pad(event.id)}`;
|
2017-12-08 18:43:24 +01:00
|
|
|
} else {
|
|
|
|
code.innerHTML = event.id;
|
|
|
|
}
|
2017-12-05 13:59:47 +01:00
|
|
|
destination.innerHTML = cleanTitle(event.title);
|
2017-12-08 18:43:24 +01:00
|
|
|
if (event.type === 'launch') {
|
|
|
|
destination.classList.add('launch');
|
|
|
|
}
|
2017-12-01 16:29:50 +01:00
|
|
|
|
|
|
|
if (startDate.toDateString() !== prevDate.toDateString()) {
|
2017-12-01 18:00:31 +01:00
|
|
|
status.innerHTML = dateformat(startDate, 'dd.mm.');
|
2017-12-01 16:29:50 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-01 16:40:04 +01:00
|
|
|
function render() {
|
2017-12-01 16:29:50 +01:00
|
|
|
const table = document.getElementById('events');
|
2017-12-08 18:43:24 +01:00
|
|
|
getEvents(17, (events) => {
|
|
|
|
while (table.firstChild) {
|
|
|
|
table.removeChild(table.firstChild);
|
|
|
|
}
|
|
|
|
events.forEach(event => renderEvent(event, table));
|
|
|
|
});
|
2017-12-01 16:29:50 +01:00
|
|
|
}
|
2017-12-01 16:40:04 +01:00
|
|
|
|
|
|
|
function onPageReady() {
|
|
|
|
window.removeEventListener('load', onPageReady, false);
|
|
|
|
render();
|
|
|
|
setInterval(render, 30000);
|
|
|
|
}
|
2017-12-01 16:29:50 +01:00
|
|
|
window.addEventListener('load', onPageReady, false);
|