add JsDoc and small code improvements
This commit is contained in:
parent
e3d6cce769
commit
0476519e99
|
@ -1,9 +1,18 @@
|
|||
import Config from "./config.js";
|
||||
|
||||
function fetchJson(url) {
|
||||
return fetch(url).then((response) => (response.json()));
|
||||
/**
|
||||
* @param {string} url
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
async function fetchJson(url) {
|
||||
const response = await fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} days
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
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<any>}
|
||||
*/
|
||||
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',
|
||||
|
|
57
src/popup.js
57
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, '<br/>');
|
||||
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue