From cd5aef5c23f4e60a37cc827b97a1fe9ce1834dbe Mon Sep 17 00:00:00 2001 From: Henri Bergius Date: Fri, 8 Dec 2017 18:43:24 +0100 Subject: [PATCH] Inject next space launch to events calendar --- events/calendar.js | 70 +++++++++++++++++++++++++++++++++++----------- events/index.html | 6 ++++ 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/events/calendar.js b/events/calendar.js index f79e26f..fa147aa 100644 --- a/events/calendar.js +++ b/events/calendar.js @@ -1,7 +1,7 @@ import dateformat from 'dateformat'; import '../elements/time'; -function getEvents(number) { +function getEvents(number, callback) { const now = new Date(); const allEvents = window.c_base_events.concat(window.c_base_regulars, window.c_base_seminars); const current = allEvents.filter((event) => { @@ -32,17 +32,45 @@ function getEvents(number) { return false; }); - 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; + fetch('https://launchlibrary.net/1.3/launch/next/1') + .then(res => res.json()) + .then((res) => { + const launches = res.launches.map((launch) => { + return { + allDay: false, + start: new Date(launch.windowstart).toISOString(), + end: new Date(launch.windowend).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; + }); + console.log(events.slice(0, number)); + return callback(events.slice(0, number)); + }) + .catch((e) => { + 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)); }); - return events.slice(0, number); } let prevDate = new Date(); @@ -80,8 +108,15 @@ function renderEvent(event, container) { time.innerHTML = dateformat(startDate, timeFormat); } - code.innerHTML = `C${pad(event.id)}`; + 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.'); @@ -106,11 +141,12 @@ function renderEvent(event, container) { function render() { const table = document.getElementById('events'); - while (table.firstChild) { - table.removeChild(table.firstChild); - } - const events = getEvents(17); - events.forEach(event => renderEvent(event, table)); + getEvents(17, (events) => { + while (table.firstChild) { + table.removeChild(table.firstChild); + } + events.forEach(event => renderEvent(event, table)); + }); } function onPageReady() { diff --git a/events/index.html b/events/index.html index ad506e0..f72921f 100644 --- a/events/index.html +++ b/events/index.html @@ -38,9 +38,15 @@ overflow: hidden; text-overflow: ellipsis; } + #events td.destination.launch { + color: hsl(210, 98%, 36%); + } #events tr.today td.destination { color: hsl(35, 98%, 46%); } + #events tr.today td.destination.launch { + color: hsl(210, 98%, 46%); + } #events td.code { font-family: 'ceva', sans-serif; }