status/gui/dashboard/app.svelte

239 lines
5.2 KiB
Svelte
Raw Permalink Normal View History

<script>
import { onMount } from 'svelte';
import Tile from './tile.svelte';
import Settings from './settings.svelte';
import { flip } from 'svelte/animate';
2023-02-24 09:07:08 +00:00
import { ringBell, settings, shuffle } from './lib';
2022-08-06 16:52:37 +00:00
import { connect } from './apiclient';
const [ send, receive ] = shuffle;
let maxNumberOfTilesOnPage = ($settings.cols || 4) * ($settings.rows || 3);
let pageNum = -1;
2022-07-15 15:10:52 +00:00
let pageCount = 1;
let allTiles = [];
let tilesOnPage = [];
let time = '';
2022-07-13 10:23:54 +00:00
let hasData = false;
function previousPage() {
pageNum--;
if (pageNum < 0) {
pageNum = pageCount - 1;
}
}
function nextPage() {
pageNum++;
if (pageNum >= pageCount) {
pageNum = 0;
}
}
function organiseGrid(goToNextPage = true) {
const newTiles = allTiles.sort((a, b) => b.prio - a.prio);
const pinnedTilesCount = allTiles.filter(t => t.prio > 0).length;
const placesLeft = maxNumberOfTilesOnPage - pinnedTilesCount;
pageCount = Math.ceil(newTiles.length / placesLeft);
if (((newTiles.length >= placesLeft) && goToNextPage) || (pageNum === -1)) {
nextPage();
}
tilesOnPage = [
...newTiles.slice(0, pinnedTilesCount),
...newTiles.slice(pinnedTilesCount + (pageNum * placesLeft)),
].slice(0, maxNumberOfTilesOnPage);
}
function keydown(event) {
switch (event.code) {
case 'ArrowLeft':
case 'PageUp':
event.preventDefault();
previousPage();
organiseGrid(false);
break;
case 'ArrowRight':
case 'PageDown':
event.preventDefault();
nextPage();
organiseGrid(false);
break;
case 'Home':
event.preventDefault();
pageNum = 0;
organiseGrid(false);
break;
case 'End':
event.preventDefault();
pageNum = pageCount - 1;
organiseGrid(false);
break;
default:
if (event.code.startsWith('Digit')) {
event.preventDefault();
let num = parseInt(event.code.slice(5));
if (!isNaN(num)) {
if (num > pageCount) {
num = pageCount;
}
pageNum = num - 1;
organiseGrid(false);
}
}
break;
}
}
2022-08-06 16:52:37 +00:00
onMount(async () => {
await connect({
2023-02-24 09:07:08 +00:00
onData: ({ tiles, newOutage }) => {
allTiles = tiles?.map(tile => {
if (tile?.service?.checked) {
tile.service.checked = new Date(tile.service.checked);
}
return tile;
});
2023-02-24 09:07:08 +00:00
if (newOutage) {
ringBell();
}
2022-08-06 16:52:37 +00:00
organiseGrid();
hasData = true;
},
});
const clockInterval = setInterval(() => {
time = new Date().toLocaleTimeString('en-GB', { timeStyle: 'medium' });
}, 100);
settings.subscribe(s => {
maxNumberOfTilesOnPage = (s.cols || 4) * (s.rows || 3);
2022-08-29 10:22:17 +00:00
if (hasData) {
organiseGrid();
}
});
return () => clearInterval(clockInterval);
});
</script>
<svelte:window on:keydown={keydown} />
<div
class="center theme-{$settings.theme}"
2022-07-18 09:40:44 +00:00
style="
--cols: {$settings.cols || 4};
--rows: {$settings.rows || 3};
font-size: {$settings.fontSize}rem;
"
>
<div class="ratio">
2022-07-18 09:40:44 +00:00
<div class="tiles">
{#each tilesOnPage || [] as tile (tile.serviceId)}
2022-07-18 09:40:44 +00:00
<div
in:receive={{ key: tile.serviceId }}
out:send={{ key: tile.serviceId }}
2022-10-28 13:16:04 +00:00
animate:flip={{ duration: $settings.animate ? (d => Math.sqrt(d) * 120) : 0 }}
2022-07-18 09:40:44 +00:00
>
<Tile {tile} />
2022-07-18 09:40:44 +00:00
</div>
{/each}
</div>
<div class="footer">
<div class="time">{time}</div>
2022-07-18 09:51:14 +00:00
{#if pageCount > 1}
<div>
2022-07-18 10:11:45 +00:00
<span class="pagecount">{pageNum + 1}/{pageCount}</span>
<span class="pagination">
{#each Array(pageCount).fill('') as _, i}
<em class:active={pageNum === i}></em>
{/each}
</span>
2022-07-18 09:40:44 +00:00
</div>
{/if}
<Settings />
</div>
</div>
</div>
<style>
.center {
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
background-color: var(--body-bg);
color: var(--body-fg);
}
.ratio {
2022-07-18 09:40:44 +00:00
height: calc(56.25vw - 2rem);
left: 50%;
2022-07-18 09:40:44 +00:00
max-height: calc(100vh - 2rem);
max-width: calc(177.77778vh - 2rem);
position: absolute;
top: 50%;
transform: translate(-50%,-50%);
2022-07-18 09:40:44 +00:00
width: calc(100vw - 2rem);
2022-07-15 15:10:52 +00:00
}
.tiles {
display: grid;
grid-template-columns: repeat(var(--cols), 1fr);
grid-template-rows: repeat(var(--rows), 1fr);
gap: 1rem;
2022-07-18 09:40:44 +00:00
align-items: stretch;
justify-content: stretch;
height: calc(100% - 50px);
}
.tiles > * {
display: flex;
align-items: stretch;
}
.tiles > * > :global(.tile) {
width: 100%;
}
2022-07-15 15:10:52 +00:00
.footer {
display: flex;
justify-content: space-between;
2022-07-18 09:40:44 +00:00
height: 50px;
font-size: 1.2em;
2022-07-18 09:51:14 +00:00
align-items: flex-end;
2022-07-15 15:10:52 +00:00
}
2022-07-18 10:11:45 +00:00
.footer > .time {
width: 5em;
}
2022-07-15 15:10:52 +00:00
.pagination em {
display: inline-block;
margin-left: 0.5rem;
2022-07-18 09:40:44 +00:00
height: 10px;
width: 10px;
2022-07-15 15:10:52 +00:00
border-radius: 5px;
background-color: #fff;
opacity: 0.4;
2022-07-18 10:11:45 +00:00
vertical-align: middle;
2022-07-15 15:10:52 +00:00
}
.pagination em.active {
opacity: 1;
}
2022-07-18 10:11:45 +00:00
.pagecount {
width: 2em;
display: inline-block;
}
</style>