diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..af777b2 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*.{html,css,js,json}] +indent_style = tab +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/LICENSE.txt b/LICENSE.txt index 63b4b68..a888f3c 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ MIT License -Copyright (c) [year] [fullname] +Copyright (c) 2021 /usr/space Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/README.md b/README.md index ee1464a..5fc9cbe 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ $ web-ext build -n _usr_space-X.Y.zip ``` ## Credits -Used icons from FontAwesome -https://fontawesome.com/license +Used icons from Bootstrap +https://github.com/twbs/icons/blob/main/LICENSE.md ## License MIT diff --git a/background/index.html b/background/index.html index 0458901..d34a8ea 100644 --- a/background/index.html +++ b/background/index.html @@ -7,4 +7,4 @@ - \ No newline at end of file + diff --git a/background/index.js b/background/index.js index 5e654e9..7cd05c2 100644 --- a/background/index.js +++ b/background/index.js @@ -1,85 +1,87 @@ -const fetchJson = (url) => { - return fetch(url).then((response) => (response.json())); -}; +function fetchJson(url) { + return fetch(url).then((response) => (response.json())); +} -const fetchCalendar = (days = 28) => { - let url = Config.calenderUrl + '?o=json'; - if (days) { - url += '&r=' + days; - } - return fetchJson(url); -}; +function fetchCalendar(days = 28) { + let url = `${Config.calenderUrl}?o=json`; + if (days) { + url += `&r=${days}`; + } + return fetchJson(url); +} -const fetchSpaceApi = () => { - return fetchJson(Config.spaceApiUrl); -}; +function fetchSpaceApi() { + return fetchJson(Config.spaceApiUrl); +} -const updateBadge = (open) => { - let badgeText, badgeColor; - if (open) { - badgeText = browser.browserAction.setBadgeText({text: 'open'}); - badgeColor = browser.browserAction.setBadgeBackgroundColor({color: Config.openColor}); - } else { - badgeText = browser.browserAction.setBadgeText({text: ''}); - badgeColor = browser.browserAction.setBadgeBackgroundColor({color: null}); - } - Promise.all([badgeText, badgeColor]) - .then() - .catch((error) => { - console.error(error); - }); -}; +async function updateBadge(open) { + let badgeText, badgeColor; + if (open) { + badgeText = browser.browserAction.setBadgeText({text: 'open'}); + badgeColor = browser.browserAction.setBadgeBackgroundColor({color: Config.openColor}); + } else { + badgeText = browser.browserAction.setBadgeText({text: ''}); + badgeColor = browser.browserAction.setBadgeBackgroundColor({color: null}); + } + try { + await Promise.all([badgeText, badgeColor]); + } catch (error) { + console.error(error); + } +} -const sendNotification = (open) => { - browser.notifications.create('notification-id', { - type: 'basic', - iconUrl: browser.runtime.getURL('icons/logo.svg'), - title: 'Space Status', - message: 'Space ist jetzt ' + (open ? 'offen' : 'geschlossen') + '.' - }); -}; +function sendNotification(open) { + browser.notifications.create('status-change', { + type: 'basic', + title: 'Space Status', + message: `Space ist jetzt ${open ? 'offen' : 'geschlossen'}.`, + iconUrl: browser.runtime.getURL('icons/favicon.svg') + }); +} -const fetchNewData = () => { - fetchCalendar().then((json) => { - window.calendar = json; - }).catch((error) => { - console.error(error); - }); +async function fetchNewData() { + try { + const json = await fetchCalendar(); + window.calendar = json; + } catch (error) { + console.error(error); + } - fetchSpaceApi().then((json) => { - if (window.spaceApi && window.spaceApi.state.open !== json.state.open) { - sendNotification(json.state.open) - } - window.spaceApi = json; - updateBadge(window.spaceApi.state.open); - }).catch((error) => { - console.error(error); - }); -}; + try { + const json = await fetchSpaceApi(); + if (window.spaceApi && window.spaceApi.state.open !== json.state.open) { + sendNotification(json.state.open) + } + window.spaceApi = json; + updateBadge(window.spaceApi.state.open); + } catch (error) { + console.error(error); + } +} let intervalHandler = null; -const stopFetching = () => { - if (intervalHandler !== null) { - clearInterval(intervalHandler); - intervalHandler = null; - } -}; -const startFetching = () => { - fetchNewData(); - if (intervalHandler === null) { - intervalHandler = setInterval(() => { - fetchNewData(); - }, Config.refreshTimeout); - } -}; +function stopFetching() { + if (intervalHandler !== null) { + clearInterval(intervalHandler); + intervalHandler = null; + } +} +function startFetching() { + fetchNewData(); + if (intervalHandler === null) { + intervalHandler = setInterval(() => { + fetchNewData(); + }, Config.refreshTimeout); + } +} window.addEventListener('offline', () => { - stopFetching(); + stopFetching(); }); window.addEventListener('online', () => { - startFetching(); + startFetching(); }); if (window.navigator.onLine) { - startFetching(); + startFetching(); } diff --git a/config.js b/config.js index 4115d60..2202548 100644 --- a/config.js +++ b/config.js @@ -1,32 +1,24 @@ window.Config = { - refreshTimeout: 1000 * 60 * 5, // 5min, - spaceApiUrl: 'https://www.usrspace.at/spaceapi.json', - calenderUrl: 'https://www.usrspace.at/calendar.php', - openColor: '#33cc33', - quickLinks: [ - { - url: 'https://www.usrspace.at/', - img: 'icons/font-awesome/home-solid.svg', - alt: 'Home', - text: 'Homepage' - }, - { - url: 'https://wiki.usrspace.at/', - img: 'icons/font-awesome/wikipedia-w-brands.svg', - alt: 'Wiki', - text: 'Wiki' - }, - { - url: 'https://gitea.usrspace.at/', - img: 'icons/gitea-brands.svg', - alt: 'Gitea', - text: 'Gitea' - }, - { - url: 'https://cloud.usrspace.at/', - img: 'icons/font-awesome/cloud-solid.svg', - alt: 'Cloud', - text: 'Nextcloud' - } - ] + refreshTimeout: 1000 * 60 * 5, // 5min, + spaceApiUrl: 'https://www.usrspace.at/spaceapi.json', + calenderUrl: 'https://www.usrspace.at/calendar.php', + openColor: '#33cc33', + quickLinks: [ + { + url: 'https://www.usrspace.at/', + text: 'Homepage' + }, + { + url: 'https://wiki.usrspace.at/', + text: 'Wiki' + }, + { + url: 'https://gitea.usrspace.at/', + text: 'Gitea' + }, + { + url: 'https://cloud.usrspace.at/', + text: 'Nextcloud' + } + ] }; diff --git a/icons/favicon.svg b/icons/favicon.svg new file mode 100644 index 0000000..c312918 --- /dev/null +++ b/icons/favicon.svg @@ -0,0 +1,23 @@ + + /usr/space + Logo /usr/space + + + + + + + + + + + diff --git a/icons/font-awesome/clock-regular.svg b/icons/font-awesome/clock-regular.svg deleted file mode 100644 index 6e75eee..0000000 --- a/icons/font-awesome/clock-regular.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/icons/font-awesome/cloud-solid.svg b/icons/font-awesome/cloud-solid.svg deleted file mode 100644 index 4397aac..0000000 --- a/icons/font-awesome/cloud-solid.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/icons/font-awesome/home-solid.svg b/icons/font-awesome/home-solid.svg deleted file mode 100644 index 03ed5cf..0000000 --- a/icons/font-awesome/home-solid.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/icons/font-awesome/lock-open-solid.svg b/icons/font-awesome/lock-open-solid.svg deleted file mode 100644 index 90756e8..0000000 --- a/icons/font-awesome/lock-open-solid.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/icons/font-awesome/lock-solid.svg b/icons/font-awesome/lock-solid.svg deleted file mode 100644 index a30c315..0000000 --- a/icons/font-awesome/lock-solid.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/icons/font-awesome/wikipedia-w-brands.svg b/icons/font-awesome/wikipedia-w-brands.svg deleted file mode 100644 index e177eff..0000000 --- a/icons/font-awesome/wikipedia-w-brands.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/icons/gitea-brands.svg b/icons/gitea-brands.svg deleted file mode 100644 index 73fa4bd..0000000 --- a/icons/gitea-brands.svg +++ /dev/null @@ -1,94 +0,0 @@ - - - - - - - image/svg+xml - - - - - - - - - - - - - - - - - - - - - - diff --git a/manifest.json b/manifest.json index e3af4dd..560ab89 100644 --- a/manifest.json +++ b/manifest.json @@ -1,35 +1,35 @@ { - "manifest_version": 2, - "name": "/usr/space", - "version": "0.6", - "description": "An Add-on for the Hacker-/Maker-Space /usr/space.", - "icons": { - "48": "icons/logo.svg", - "96": "icons/logo.svg" - }, - "background": { - "page": "background/index.html" - }, - "browser_action": { - "browser_style": true, - //"chrome_style": true, - "default_title": "/usr/space", - //"default_icon": "icons/logo.svg", - "default_icon": { - "16": "icons/logo.svg", - "32": "icons/logo.svg" - }, - "default_area": "navbar", - "default_popup": "popup/index.html" - }, - "permissions": [ - "https://www.usrspace.at/*", - "webRequest", - "notifications" - ], - "author": "Thomas Rupprecht" - //"developer": { - // "name": "Thomas Rupprecht", - // "url": "https://blog.ximex.at/" - //} + "manifest_version": 2, + "name": "/usr/space", + "version": "0.7", + "description": "An Add-on for the Hacker-/Maker-Space /usr/space.", + "icons": { + "48": "icons/favicon.svg", + "96": "icons/favicon.svg" + }, + "background": { + "page": "background/index.html" + }, + "browser_action": { + "browser_style": true, + //"chrome_style": true, + "default_title": "/usr/space", + //"default_icon": "icons/favicon.svg", + "default_icon": { + "16": "icons/favicon.svg", + "32": "icons/favicon.svg" + }, + "default_area": "navbar", + "default_popup": "popup/index.html" + }, + "permissions": [ + "https://www.usrspace.at/*", + "webRequest", + "notifications" + ], + "author": "Thomas Rupprecht" + //"developer": { + // "name": "Thomas Rupprecht", + // "url": "https://blog.ximex.at/" + //} } diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..f9e8eac --- /dev/null +++ b/package-lock.json @@ -0,0 +1,26 @@ +{ + "name": "usrspace-browser-addon", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "devDependencies": { + "web-ext-types": "^3.2.1" + } + }, + "node_modules/web-ext-types": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/web-ext-types/-/web-ext-types-3.2.1.tgz", + "integrity": "sha512-oQZYDU3W8X867h8Jmt3129kRVKklz70db40Y6OzoTTuzOJpF/dB2KULJUf0txVPyUUXuyzV8GmT3nVvRHoG+Ew==", + "dev": true + } + }, + "dependencies": { + "web-ext-types": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/web-ext-types/-/web-ext-types-3.2.1.tgz", + "integrity": "sha512-oQZYDU3W8X867h8Jmt3129kRVKklz70db40Y6OzoTTuzOJpF/dB2KULJUf0txVPyUUXuyzV8GmT3nVvRHoG+Ew==", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..b9d9b6a --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "web-ext-types": "^3.2.1" + } +} diff --git a/popup/font/Nunito-Bold.ttf b/popup/font/Nunito-Bold.ttf new file mode 100644 index 0000000..368468c Binary files /dev/null and b/popup/font/Nunito-Bold.ttf differ diff --git a/popup/font/Nunito-Italic.ttf b/popup/font/Nunito-Italic.ttf new file mode 100644 index 0000000..871a9dd Binary files /dev/null and b/popup/font/Nunito-Italic.ttf differ diff --git a/popup/font/Nunito-Regular.ttf b/popup/font/Nunito-Regular.ttf new file mode 100644 index 0000000..c8c90b7 Binary files /dev/null and b/popup/font/Nunito-Regular.ttf differ diff --git a/popup/font/OFL.txt b/popup/font/OFL.txt new file mode 100644 index 0000000..9a43a3a --- /dev/null +++ b/popup/font/OFL.txt @@ -0,0 +1,93 @@ +Copyright 2014 The Nunito Project Authors (https://github.com/googlefonts/nunito) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/popup/index.css b/popup/index.css index 3581388..504de36 100644 --- a/popup/index.css +++ b/popup/index.css @@ -1,52 +1,94 @@ -body { - width: 360px; +@font-face { + font-family: 'Nunito Sans'; + font-style: normal; + font-weight: normal; + src: url('font/Nunito-Regular.ttf') format('truetype'); +} +@font-face { + font-family: 'Nunito Sans'; + font-style: normal; + font-weight: bold; + src: url('font/Nunito-Bold.ttf') format('truetype'); +} +@font-face { + font-family: 'Nunito Sans'; + font-style: italic; + font-weight: normal; + src: url('font/Nunito-Italic.ttf') format('truetype'); } +:root { + --light-blue: #2AA1BF; + --dark-blue: #095C81; + --light-color: #0C0C0C; + --dark-color: #FFFFFF; + --light-background: #F9F9F9; + --dark-background: #4A4A4A; + --light-code-background: #EBEBEB; + --dark-code-background: #383838; +} + +body { + width: 360px; + padding: 6px 12px; + color: var(--light-color); + background-color: var(--light-background); + font-family: 'Nunito Sans'; +} +@media (prefers-color-scheme: dark) { + body { + color: var(--dark-color); + background-color: var(--dark-background); + } +} + +section.no-padding { + margin: 0 -12px -6px; +} h2 { - margin: 12px 8px 8px; + margin: 6px 0 4px; } summary { - margin: 12px 8px 8px; + margin: 6px 12px 6px; } summary > h2 { - margin: 0; - display: inline-block; + margin: 0; + display: inline-block; +} +time { + font-style: italic; +} +svg { + vertical-align: text-bottom; } +.panel-section-list { + margin: 0 -12px; +} .panel-list-item > .icon { - text-align: center; - width: 23px; - margin-right: 4px; -} -.panel-list-item > .icon > img { - vertical-align: text-bottom; + text-align: center; + width: 23px; + margin-right: 4px; } -#state { - margin: 0 8px; -} -#state > img { - vertical-align: text-bottom; - margin-right: 4px; -} - -#calendar { - margin: 0 8px; -} -#calendar > div > img { - vertical-align: text-bottom; - margin-right: 4px; +#calendar > div > svg, #state > svg { + margin-right: 4px; } #space-api { - margin: 0; - padding: 0 8px; - height: 240px; - overflow-x: hidden; - overflow-y: scroll; - background-color: #ebebeb; + margin: 0; + padding: 0 8px; + height: 122px; + overflow-x: hidden; + overflow-y: scroll; + background-color: var(--light-code-background); +} +@media (prefers-color-scheme: dark) { + #space-api { + background-color: var(--dark-code-background); + } } #space-api code { - font-family: monospace; - font-size: 10px; + font-family: monospace; + font-size: 10px; } diff --git a/popup/index.html b/popup/index.html index 9e3a94f..84e71e4 100644 --- a/popup/index.html +++ b/popup/index.html @@ -1,57 +1,70 @@ - - /usr/space - + + /usr/space + - -
-

Aktueller Status

-
loading...
-
-
-

Links

-
- - - - -
-
-
-

Nächster Termin

-
loading...
-
-
-
-

SpaceApi JSON

-
loading...
-
-
+
+

Aktueller Status

+
loading...
+
+
+

Links

+ +
+
+

Nächster Termin

+
loading...
+
+
+
+

SpaceApi JSON

+
loading...
+
+
- - - + + + diff --git a/popup/index.js b/popup/index.js index 9b97493..82de786 100644 --- a/popup/index.js +++ b/popup/index.js @@ -1,77 +1,100 @@ -Array.from(document.getElementsByClassName('link')).forEach((element) => { - element.addEventListener('click', (event) => { - const newTab = browser.tabs.create({ - url: event.currentTarget.dataset.url - }); - newTab.then((data) => { - // console.log(data); - }).catch((error) => { - console.error(error); - }); - }); -}); +const calendarSVG = ` + + + + +`; +const doorClosedSVG = ` + + + + +`; +const doorOpenSVG = ` + + + + +`; -browser.runtime.getBackgroundPage().then((page) => { - updateNextEvent(page.calendar); - updateSpaceApiJson(page.spaceApi); - updateState(page.spaceApi); -}).catch((error) => { - console.error(error); -}); +async function linkElementClickListener(event) { + try { + const tab = await browser.tabs.create({url: event.currentTarget.dataset.url}); + // console.log(tab); + } catch (error) { + console.error(error); + } +} -const updateNextEvent = (nextEvents) => { - const calendarElement = document.getElementById('calendar'); - calendarElement.innerText = ''; +async function init() { + const linkElements = Array.from(document.getElementsByClassName('link')); + linkElements.forEach((linkElement) => { + linkElement.addEventListener('click', linkElementClickListener); + }); - if (nextEvents.length === 0) { - const hintNode = document.createTextNode('Keine Termine in den nächsten 4 Wochen!'); - const strongElement = document.createElement('strong'); - strongElement.append(hintNode); - calendarElement.append(strongElement); - return; - } + try { + const page = await browser.runtime.getBackgroundPage(); - const nextEventDate = nextEvents[0].begin.substr(0, 10); - const nextEventDateEvents = nextEvents.filter((nextEvent) => (nextEvent.begin.startsWith(nextEventDate))); + updateNextEvent(page.calendar); + updateSpaceApiJson(page.spaceApi); + updateState(page.spaceApi); + } catch (error) { + console.error(error); + } +} +init(); - nextEventDateEvents.forEach((nextEventDateEvent) => { - const divElement = document.createElement('div'); - divElement.innerHTML = 'Clock'; - const beginDate = new Date(nextEventDateEvent.begin); - const strongElement = document.createElement('strong'); - const timeElement = document.createElement('time'); - strongElement.textContent = nextEventDateEvent.name; - timeElement.datetime = beginDate.toISOString(); - const dateNode = document.createTextNode(beginDate.toLocaleString()); - // divElement.innerText = ''; - divElement.append(strongElement); - divElement.append(document.createTextNode(' ')); - divElement.append(timeElement); - timeElement.append(dateNode); - if (nextEventDateEvent.location) { - const locationNode = document.createTextNode(' (' + nextEventDateEvent.location + ')'); - divElement.append(locationNode); - } - calendarElement.append(divElement); - }); -}; +function updateNextEvent(nextEvents) { + const calendarElement = document.getElementById('calendar'); + calendarElement.innerText = ''; -const updateSpaceApiJson = (spaceApi) => { - const spaceApiElement = document.querySelector('#space-api code'); - const json = JSON.stringify(spaceApi, null, 2).replace(/ /g, ' ').replace(/\n/g, '
'); - spaceApiElement.innerHTML = json; - // const jsonNode = document.createTextNode(json); - // spaceApiElement.innerText = ''; - // spaceApiElement.append(jsonNode); -}; + if (nextEvents.length === 0) { + const hintNode = document.createTextNode('Keine Termine in den nächsten 4 Wochen!'); + const strongElement = document.createElement('strong'); + strongElement.append(hintNode); + calendarElement.append(strongElement); + return; + } -const updateState = (spaceApi) => { - const stateElement = document.getElementById('state'); - const since = new Date(spaceApi.state.lastchange * 1000); - const sinceStr = ' seit '; - if (spaceApi.state.open) { - stateElement.innerHTML = 'OffenOffen' + sinceStr; - } else { - stateElement.innerHTML = 'GeschlossenGeschlossen' + sinceStr; - } -}; + const nextEventDate = nextEvents[0].begin.substr(0, 10); + const nextEventDateEvents = nextEvents.filter((nextEvent) => (nextEvent.begin.startsWith(nextEventDate))); + + nextEventDateEvents.forEach((nextEventDateEvent) => { + const divElement = document.createElement('div'); + divElement.innerHTML = calendarSVG; + const beginDate = new Date(nextEventDateEvent.begin); + const strongElement = document.createElement('strong'); + const timeElement = document.createElement('time'); + strongElement.textContent = nextEventDateEvent.name; + timeElement.datetime = beginDate.toISOString(); + const dateNode = document.createTextNode(beginDate.toLocaleString([], {dateStyle: "medium", timeStyle: "short"})); + // divElement.innerText = ''; + divElement.append(strongElement, ' ', timeElement); + timeElement.append(dateNode); + if (nextEventDateEvent.location) { + const locationNode = document.createTextNode(` (${nextEventDateEvent.location})`); + divElement.append(locationNode); + } + calendarElement.append(divElement); + }); +} + +function updateSpaceApiJson(spaceApi) { + const spaceApiElement = document.querySelector('#space-api code'); + const json = JSON.stringify(spaceApi, null, 2).replace(/ /g, ' ').replace(/\n/g, '
'); + spaceApiElement.innerHTML = json; + // const jsonNode = document.createTextNode(json); + // spaceApiElement.innerText = ''; + // spaceApiElement.append(jsonNode); +} + +function updateState(spaceApi) { + const stateElement = document.getElementById('state'); + const since = new Date(spaceApi.state.lastchange * 1000); + const sinceStr = ` seit `; + if (spaceApi.state.open) { + stateElement.innerHTML = `${doorOpenSVG}Offen ${sinceStr}`; + } else { + stateElement.innerHTML = `${doorClosedSVG}Geschlossen ${sinceStr}`; + } +}