use html template tag

This commit is contained in:
Thomas Rupprecht 2023-01-05 04:31:19 +01:00
parent 35d2a0fafb
commit c07f82322d
5 changed files with 131 additions and 83 deletions

View File

@ -20,6 +20,9 @@
"nextEvent": {
"message": "Nächstes Event"
},
"event": {
"message": "Event"
},
"spaceApiJson": {
"message": "Space-API JSON"
},

View File

@ -20,6 +20,9 @@
"nextEvent": {
"message": "Next Event"
},
"event": {
"message": "Event"
},
"spaceApiJson": {
"message": "Space-API JSON"
},

View File

@ -7,18 +7,22 @@ export default {
eventColor: '#9933cc',
quickLinks: [
{
iconTemplateId: 'template-icon-homepage',
url: 'https://www.usrspace.at/',
text: 'Homepage',
},
{
iconTemplateId: 'template-icon-wiki',
url: 'https://wiki.usrspace.at/',
text: 'Wiki',
},
{
iconTemplateId: 'template-icon-gitea',
url: 'https://gitea.usrspace.at/',
text: 'Gitea',
},
{
iconTemplateId: 'template-icon-nextcloud',
url: 'https://cloud.usrspace.at/',
text: 'Nextcloud',
},

View File

@ -4,6 +4,69 @@
<meta charset="utf-8" />
<title>/usr/space</title>
<link rel="stylesheet" href="popup.css" />
<template id="template-icon-closed">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="">
<path d="M3 2a1 1 0 0 1 1-1h8a1 1 0 0 1 1 1v13h1.5a.5.5 0 0 1 0 1h-13a.5.5 0 0 1 0-1H3V2zm1 13h8V2H4v13z"/>
<path d="M9 9a1 1 0 1 0 2 0 1 1 0 0 0-2 0z"/>
</svg>
</template>
<template id="template-icon-open">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="">
<path d="M8.5 10c-.276 0-.5-.448-.5-1s.224-1 .5-1 .5.448.5 1-.224 1-.5 1z"/>
<path d="M10.828.122A.5.5 0 0 1 11 .5V1h.5A1.5 1.5 0 0 1 13 2.5V15h1.5a.5.5 0 0 1 0 1h-13a.5.5 0 0 1 0-1H3V1.5a.5.5 0 0 1 .43-.495l7-1a.5.5 0 0 1 .398.117zM11.5 2H11v13h1V2.5a.5.5 0 0 0-.5-.5zM4 1.934V15h6V1.077l-6 .857z"/>
</svg>
</template>
<template id="template-state">
<svg></svg>
<span></span>
<time datetime=""></time>
</template>
<template id="template-icon-homepage">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="">
<path fill-rule="evenodd" d="m8 3.293 6 6V13.5a1.5 1.5 0 0 1-1.5 1.5h-9A1.5 1.5 0 0 1 2 13.5V9.293l6-6zm5-.793V6l-2-2V2.5a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 .5.5z"/>
<path fill-rule="evenodd" d="M7.293 1.5a1 1 0 0 1 1.414 0l6.647 6.646a.5.5 0 0 1-.708.708L8 2.207 1.354 8.854a.5.5 0 1 1-.708-.708L7.293 1.5z"/>
</svg>
</template>
<template id="template-icon-wiki">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" viewBox="0 0 640 512" fill="currentColor" role="img" aria-level="">
<path d="M640 51.2l-.3 12.2c-28.1.8-45 15.8-55.8 40.3-25 57.8-103.3 240-155.3 358.6H415l-81.9-193.1c-32.5 63.6-68.3 130-99.2 193.1-.3.3-15 0-15-.3C172 352.3 122.8 243.4 75.8 133.4 64.4 106.7 26.4 63.4.2 63.7c0-3.1-.3-10-.3-14.2h161.9v13.9c-19.2 1.1-52.8 13.3-43.3 34.2 21.9 49.7 103.6 240.3 125.6 288.6 15-29.7 57.8-109.2 75.3-142.8-13.9-28.3-58.6-133.9-72.8-160-9.7-17.8-36.1-19.4-55.8-19.7V49.8l142.5.3v13.1c-19.4.6-38.1 7.8-29.4 26.1 18.9 40 30.6 68.1 48.1 104.7 5.6-10.8 34.7-69.4 48.1-100.8 8.9-20.6-3.9-28.6-38.6-29.4.3-3.6 0-10.3.3-13.6 44.4-.3 111.1-.3 123.1-.6v13.6c-22.5.8-45.8 12.8-58.1 31.7l-59.2 122.8c6.4 16.1 63.3 142.8 69.2 156.7L559.2 91.8c-8.6-23.1-36.4-28.1-47.2-28.3V49.6l127.8 1.1.2.5z"/>
</svg>
</template>
<template id="template-icon-gitea">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="">
<path d="M15.698 7.287 8.712.302a1.03 1.03 0 0 0-1.457 0l-1.45 1.45 1.84 1.84a1.223 1.223 0 0 1 1.55 1.56l1.773 1.774a1.224 1.224 0 0 1 1.267 2.025 1.226 1.226 0 0 1-2.002-1.334L8.58 5.963v4.353a1.226 1.226 0 1 1-1.008-.036V5.887a1.226 1.226 0 0 1-.666-1.608L5.093 2.465l-4.79 4.79a1.03 1.03 0 0 0 0 1.457l6.986 6.986a1.03 1.03 0 0 0 1.457 0l6.953-6.953a1.031 1.031 0 0 0 0-1.457"/>
</svg>
</template>
<template id="template-icon-nextcloud">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="">
<path d="M4.406 3.342A5.53 5.53 0 0 1 8 2c2.69 0 4.923 2 5.166 4.579C14.758 6.804 16 8.137 16 9.773 16 11.569 14.502 13 12.687 13H3.781C1.708 13 0 11.366 0 9.318c0-1.763 1.266-3.223 2.942-3.593.143-.863.698-1.723 1.464-2.383z"/>
</svg>
</template>
<template id="template-link-item">
<li class="panel-list-item link" data-url="">
<div class="icon"></div>
<div class="text"></div>
<div class="text-shortcut"></div>
</li>
</template>
<template id="template-calendar-entry">
<div class="calendar-entry">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="">
<path d="M11 6.5a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-1a.5.5 0 0 1-.5-.5v-1z"/>
<path d="M3.5 0a.5.5 0 0 1 .5.5V1h8V.5a.5.5 0 0 1 1 0V1h1a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V3a2 2 0 0 1 2-2h1V.5a.5.5 0 0 1 .5-.5zM1 4v10a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V4H1z"/>
</svg>
<div>
<div>
<strong></strong>
<time datetime=""></time>
</div>
</div>
</div>
</template>
<template id="template-calendar-entry-location">
<div>(<address></address>)</div>
</template>
</head>
<body>
<section>
@ -12,45 +75,7 @@
</section>
<section>
<h2 id="links"></h2>
<ul class="panel-section panel-section-list">
<li class="panel-list-item link" data-url="https://www.usrspace.at/">
<div class="icon">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="Homepage">
<path fill-rule="evenodd" d="m8 3.293 6 6V13.5a1.5 1.5 0 0 1-1.5 1.5h-9A1.5 1.5 0 0 1 2 13.5V9.293l6-6zm5-.793V6l-2-2V2.5a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 .5.5z"/>
<path fill-rule="evenodd" d="M7.293 1.5a1 1 0 0 1 1.414 0l6.647 6.646a.5.5 0 0 1-.708.708L8 2.207 1.354 8.854a.5.5 0 1 1-.708-.708L7.293 1.5z"/>
</svg>
</div>
<div class="text">Homepage</div>
<div class="text-shortcut"></div>
</li>
<li class="panel-list-item link" data-url="https://wiki.usrspace.at/">
<div class="icon">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" viewBox="0 0 640 512" fill="currentColor" role="img" aria-level="Wiki">
<path d="M640 51.2l-.3 12.2c-28.1.8-45 15.8-55.8 40.3-25 57.8-103.3 240-155.3 358.6H415l-81.9-193.1c-32.5 63.6-68.3 130-99.2 193.1-.3.3-15 0-15-.3C172 352.3 122.8 243.4 75.8 133.4 64.4 106.7 26.4 63.4.2 63.7c0-3.1-.3-10-.3-14.2h161.9v13.9c-19.2 1.1-52.8 13.3-43.3 34.2 21.9 49.7 103.6 240.3 125.6 288.6 15-29.7 57.8-109.2 75.3-142.8-13.9-28.3-58.6-133.9-72.8-160-9.7-17.8-36.1-19.4-55.8-19.7V49.8l142.5.3v13.1c-19.4.6-38.1 7.8-29.4 26.1 18.9 40 30.6 68.1 48.1 104.7 5.6-10.8 34.7-69.4 48.1-100.8 8.9-20.6-3.9-28.6-38.6-29.4.3-3.6 0-10.3.3-13.6 44.4-.3 111.1-.3 123.1-.6v13.6c-22.5.8-45.8 12.8-58.1 31.7l-59.2 122.8c6.4 16.1 63.3 142.8 69.2 156.7L559.2 91.8c-8.6-23.1-36.4-28.1-47.2-28.3V49.6l127.8 1.1.2.5z"/>
</svg>
</div>
<div class="text">Wiki</div>
<div class="text-shortcut"></div>
</li>
<li class="panel-list-item link" data-url="https://gitea.usrspace.at/">
<div class="icon">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="Gitea">
<path d="M15.698 7.287 8.712.302a1.03 1.03 0 0 0-1.457 0l-1.45 1.45 1.84 1.84a1.223 1.223 0 0 1 1.55 1.56l1.773 1.774a1.224 1.224 0 0 1 1.267 2.025 1.226 1.226 0 0 1-2.002-1.334L8.58 5.963v4.353a1.226 1.226 0 1 1-1.008-.036V5.887a1.226 1.226 0 0 1-.666-1.608L5.093 2.465l-4.79 4.79a1.03 1.03 0 0 0 0 1.457l6.986 6.986a1.03 1.03 0 0 0 1.457 0l6.953-6.953a1.031 1.031 0 0 0 0-1.457"/>
</svg>
</div>
<div class="text">Gitea</div>
<div class="text-shortcut"></div>
</li>
<li class="panel-list-item link" data-url="https://cloud.usrspace.at/">
<div class="icon">
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="Nextcloud">
<path d="M4.406 3.342A5.53 5.53 0 0 1 8 2c2.69 0 4.923 2 5.166 4.579C14.758 6.804 16 8.137 16 9.773 16 11.569 14.502 13 12.687 13H3.781C1.708 13 0 11.366 0 9.318c0-1.763 1.266-3.223 2.942-3.593.143-.863.698-1.723 1.464-2.383z"/>
</svg>
</div>
<div class="text">Nextcloud</div>
<div class="text-shortcut"></div>
</li>
</ul>
<ul id="link-list" class="panel-section panel-section-list"></ul>
</section>
<section>
<h2 id="nextEvent"></h2>

View File

@ -1,21 +1,4 @@
const calendarSVG =
`<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="Kalender">
<path d="M11 6.5a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-1a.5.5 0 0 1-.5-.5v-1z"/>
<path d="M3.5 0a.5.5 0 0 1 .5.5V1h8V.5a.5.5 0 0 1 1 0V1h1a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V3a2 2 0 0 1 2-2h1V.5a.5.5 0 0 1 .5-.5zM1 4v10a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V4H1z"/>
</svg>`
;
const doorClosedSVG =
`<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="Geschlossen">
<path d="M3 2a1 1 0 0 1 1-1h8a1 1 0 0 1 1 1v13h1.5a.5.5 0 0 1 0 1h-13a.5.5 0 0 1 0-1H3V2zm1 13h8V2H4v13z"/>
<path d="M9 9a1 1 0 1 0 2 0 1 1 0 0 0-2 0z"/>
</svg>`
;
const doorOpenSVG =
`<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" fill="currentColor" viewBox="0 0 16 16" role="img" aria-label="Offen">
<path d="M8.5 10c-.276 0-.5-.448-.5-1s.224-1 .5-1 .5.448.5 1-.224 1-.5 1z"/>
<path d="M10.828.122A.5.5 0 0 1 11 .5V1h.5A1.5 1.5 0 0 1 13 2.5V15h1.5a.5.5 0 0 1 0 1h-13a.5.5 0 0 1 0-1H3V1.5a.5.5 0 0 1 .43-.495l7-1a.5.5 0 0 1 .398.117zM11.5 2H11v13h1V2.5a.5.5 0 0 0-.5-.5zM4 1.934V15h6V1.077l-6 .857z"/>
</svg>`
;
import Config from './config.js';
const dateTimeFormat = Intl.DateTimeFormat([], {dateStyle: 'medium', timeStyle: 'short'});
@ -32,6 +15,24 @@ function setL10n() {
document.getElementById('spaceApiJson').textContent = browser.i18n.getMessage('spaceApiJson');
}
function setLinks() {
const listElement = document.getElementById('link-list');
Config.quickLinks.forEach((quickLink) => {
const template = getCleanTemplateById('template-link-item');
template.querySelector('.link').dataset.url = quickLink.url;
const svgIcon = getCleanTemplateById(quickLink.iconTemplateId);
svgIcon.querySelector('svg').setAttribute('aria-label', quickLink.text);
template.querySelector('.icon').append(svgIcon);
template.querySelector('.text').textContent = quickLink.text;
listElement.append(template);
});
}
/**
* @param {Event} event
*/
@ -45,6 +46,7 @@ async function linkElementClickListener(event) {
async function init() {
setL10n();
setLinks();
const linkElements = Array.from(document.getElementsByClassName('link'));
linkElements.forEach((linkElement) => {
@ -79,38 +81,26 @@ function updateNextEvent(nextEvents) {
const nextEventDateEvents = nextEvents.filter((nextEvent) => (nextEvent.begin.startsWith(nextEventDate)));
nextEventDateEvents.forEach((nextEventDateEvent) => {
const calenderEntryElement = document.createElement('div');
calenderEntryElement.classList.add('calendar-entry');
calenderEntryElement.innerHTML = calendarSVG;
const calendarEntryElement = getCleanTemplateById('template-calendar-entry');
const containerElement = document.createElement('div');
calenderEntryElement.append(containerElement);
calendarEntryElement.querySelector('svg').setAttribute('aria-label', browser.i18n.getMessage('event'));
const strongElement = document.createElement('strong');
strongElement.textContent = nextEventDateEvent.name;
calendarEntryElement.querySelector('strong').textContent = nextEventDateEvent.name;
const timeElement = calendarEntryElement.querySelector('time');
const beginDate = new Date(nextEventDateEvent.begin);
const timeElement = document.createElement('time');
timeElement.dateTime = beginDate.toISOString();
timeElement.textContent = dateTimeFormat.format(beginDate);
const titleElement = document.createElement('div');
titleElement.append(strongElement, timeElement);
containerElement.append(titleElement);
if (nextEventDateEvent.location) {
const locationElement = document.createElement('div');
const locationElement = getCleanTemplateById('template-calendar-entry-location');
const addressElement = document.createElement('address');
addressElement.textContent = nextEventDateEvent.location;
locationElement.querySelector('address').textContent = nextEventDateEvent.location;
const parenthesesOpenNode = document.createTextNode('(');
const parenthesesCloseNode = document.createTextNode(')');
locationElement.append(parenthesesOpenNode, addressElement, parenthesesCloseNode);
containerElement.append(locationElement);
calendarEntryElement.querySelector('.calendar-entry > div').append(locationElement);
}
calendarElement.append(calenderEntryElement);
calendarElement.append(calendarEntryElement);
});
}
}
@ -133,15 +123,38 @@ function updateSpaceApiJson(spaceApi) {
* @param {object} spaceApi
*/
function updateState(spaceApi) {
const stateIcon = spaceApi.state.open ? doorOpenSVG : doorClosedSVG;
const stateTextNode = document.createTextNode(browser.i18n.getMessage(spaceApi.state.open ? 'openSince' : 'closedSince'));
const state = spaceApi.state.open ? 'open' : 'closed';
const stateElements = getCleanTemplateById('template-state');
const svgIconState = getCleanTemplateById(`template-icon-${state}`);
svgIconState.querySelector('svg').setAttribute('aria-label', browser.i18n.getMessage(state));
stateElements.querySelector('svg').replaceWith(svgIconState);
stateElements.querySelector('span').textContent = browser.i18n.getMessage(spaceApi.state.open ? 'openSince' : 'closedSince');
const timeElement = stateElements.querySelector('time');
const since = new Date(spaceApi.state.lastchange * 1000);
const timeElement = document.createElement('time');
timeElement.dateTime = since.toISOString();
timeElement.textContent = dateTimeFormat.format(since);
const stateElement = document.getElementById('state');
stateElement.innerHTML = stateIcon;
stateElement.append(stateTextNode, timeElement);
stateElement.innerText = '';
stateElement.append(stateElements);
}
/**
* @param {string} templateId
* @returns {DocumentFragment}
*/
function getCleanTemplateById(templateId) {
const template = document.getElementById(templateId).content.cloneNode(true);
if (template.hasChildNodes()) {
for (const childNode of template.childNodes) {
if (childNode.nodeType !== Node.ELEMENT_NODE) {
template.removeChild(childNode);
}
}
}
return template;
}