From 0476519e996059223ff50e58e8ffeb0774e9c98f Mon Sep 17 00:00:00 2001 From: Thomas Rupprecht Date: Sat, 24 Dec 2022 13:50:28 +0100 Subject: [PATCH] add JsDoc and small code improvements --- src/background.js | 22 ++++++++++++++++-- src/popup.js | 57 ++++++++++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/background.js b/src/background.js index 00a4f6f..3547167 100644 --- a/src/background.js +++ b/src/background.js @@ -1,9 +1,18 @@ import Config from "./config.js"; -function fetchJson(url) { - return fetch(url).then((response) => (response.json())); +/** + * @param {string} url + * @returns {Promise} + */ +async function fetchJson(url) { + const response = await fetch(url); + return response.json(); } +/** + * @param {number} days + * @returns {Promise} + */ function fetchCalendar(days = 28) { let url = `${Config.calenderUrl}?o=json`; if (days) { @@ -12,10 +21,16 @@ function fetchCalendar(days = 28) { return fetchJson(url); } +/** + * @returns {Promise} + */ function fetchSpaceApi() { return fetchJson(Config.spaceApiUrl); } +/** + * @param {boolean} open + */ async function updateBadge(open) { let badgeText, badgeColor; if (open) { @@ -32,6 +47,9 @@ async function updateBadge(open) { } } +/** + * @param {boolean} open + */ function sendNotification(open) { browser.notifications.create('status-change', { type: 'basic', diff --git a/src/popup.js b/src/popup.js index 7bf1b98..ff369a9 100644 --- a/src/popup.js +++ b/src/popup.js @@ -19,6 +19,9 @@ const doorOpenSVG = ` const dateTimeFormat = Intl.DateTimeFormat([], {dateStyle: "medium", timeStyle: "short"}); +/** + * @param {Event} event + */ async function linkElementClickListener(event) { try { await browser.tabs.create({url: event.currentTarget.dataset.url}); @@ -45,6 +48,9 @@ async function init() { } init(); +/** + * @param {object} nextEvents + */ function updateNextEvent(nextEvents) { const calendarElement = document.getElementById('calendar'); calendarElement.innerText = ''; @@ -54,32 +60,34 @@ function updateNextEvent(nextEvents) { const strongElement = document.createElement('strong'); strongElement.append(hintNode); calendarElement.append(strongElement); - return; + } else { + 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(dateTimeFormat.format(beginDate)); + // divElement.innerText = ''; + divElement.append(strongElement, ' ', timeElement); + timeElement.append(dateNode); + if (nextEventDateEvent.location) { + const locationNode = document.createTextNode(` (${nextEventDateEvent.location})`); + divElement.append(locationNode); + } + calendarElement.append(divElement); + }); } - - 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(dateTimeFormat.format(beginDate)); - // divElement.innerText = ''; - divElement.append(strongElement, ' ', timeElement); - timeElement.append(dateNode); - if (nextEventDateEvent.location) { - const locationNode = document.createTextNode(` (${nextEventDateEvent.location})`); - divElement.append(locationNode); - } - calendarElement.append(divElement); - }); } +/** + * @param {object} spaceApi + */ function updateSpaceApiJson(spaceApi) { const spaceApiElement = document.querySelector('#space-api code'); const json = JSON.stringify(spaceApi, null, 2).replace(/ /g, ' ').replace(/\n/g, '
'); @@ -89,6 +97,9 @@ function updateSpaceApiJson(spaceApi) { // spaceApiElement.append(jsonNode); } +/** + * @param {object} spaceApi + */ function updateState(spaceApi) { const stateElement = document.getElementById('state'); const since = new Date(spaceApi.state.lastchange * 1000);