Auto-resize dashboard tile grid

Signed-off-by: Romein van Buren <romein@vburen.nl>
This commit is contained in:
Romein van Buren 2022-07-12 13:32:19 +02:00
parent b43f50bf24
commit 36d26ee5c8
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
2 changed files with 53 additions and 25 deletions

View File

@ -3,13 +3,14 @@
import TileRawValue from './tile-rawvalue.svelte'; import TileRawValue from './tile-rawvalue.svelte';
import Settings from './settings.svelte'; import Settings from './settings.svelte';
import { flip } from 'svelte/animate'; import { flip } from 'svelte/animate';
import { settings, shuffle, ringBell } from './lib'; import { proportionalGrid, settings, shuffle, ringBell } from './lib';
const [ send, receive ] = shuffle; const [ send, receive ] = shuffle;
const size = ($settings.cols || 4) * ($settings.rows || 3) - 1; let size = ($settings.cols || 4) * ($settings.rows || 3) - 1;
let pageNum = -1; let pageNum = -1;
let tiles = []; let tiles = [];
let time = ''; let time = '';
let globalData = {};
function tileProps(service) { function tileProps(service) {
let props = { let props = {
@ -38,6 +39,31 @@
return props; return props;
} }
function organiseGrid() {
let servicesTemp = [];
const { servicesUp, servicesDown, servicesUnknown, total } = globalData;
const upOrUnknown = [ ...servicesUp, ...servicesUnknown ];
servicesTemp = servicesDown.slice(0, size);
if (pageNum === -1 || total >= size) {
pageNum++;
if (pageNum > Math.ceil(upOrUnknown.length / size)) {
pageNum = 0;
}
}
const placesLeft = size - servicesTemp.length;
const offset = placesLeft * pageNum;
if (placesLeft > 0) {
servicesTemp.push(
...upOrUnknown.slice(offset, placesLeft + offset)
);
}
tiles = servicesTemp;
}
onMount(() => { onMount(() => {
const ws = new WebSocket('ws://__SERVER__/statusdashboard/socket'); const ws = new WebSocket('ws://__SERVER__/statusdashboard/socket');
@ -46,28 +72,8 @@
switch (data.cmd) { switch (data.cmd) {
case 'data': case 'data':
let servicesTemp = []; globalData = data;
const { servicesUp, servicesDown, servicesUnknown, total } = data; organiseGrid();
const upOrUnknown = [ ...servicesUp, ...servicesUnknown ];
servicesTemp = servicesDown.slice(0, size);
if (pageNum === -1 || total >= size) {
pageNum++;
if (pageNum > Math.ceil(upOrUnknown.length / size)) {
pageNum = 0;
}
}
const placesLeft = size - servicesTemp.length;
const offset = placesLeft * pageNum;
if (placesLeft > 0) {
servicesTemp.push(
...upOrUnknown.slice(offset, placesLeft + offset)
);
}
tiles = servicesTemp;
break; break;
case 'bell': case 'bell':
@ -85,10 +91,16 @@
}); });
}, 100); }, 100);
settings.subscribe(s => {
size = (s.cols || 4) * (s.rows || 3) - 1;
organiseGrid();
});
return () => clearInterval(clockInterval); return () => clearInterval(clockInterval);
}); });
</script> </script>
<svelte:window on:resize={proportionalGrid} />
<Settings /> <Settings />
<div <div

View File

@ -19,7 +19,7 @@ function createSettingsStore() {
return { return {
subscribe: s.subscribe, subscribe: s.subscribe,
set: val => updateStorage(val), set: val => updateStorage(val),
update: val => updateStorage({ ...get(s), val }), update: val => updateStorage({ ...get(s), ...val }),
}; };
} }
@ -65,3 +65,19 @@ export function formatDuration(ms) {
.map(([ key, val ]) => `${val} ${key}`) .map(([ key, val ]) => `${val} ${key}`)
.join(' '); .join(' ');
} }
export function proportionalGrid() {
const container = document.querySelector('.center');
const w = container.clientWidth;
const h = container.clientHeight;
const tileW = 400;
const tileH = 300;
const availableCols = Math.floor(w / tileW);
const availableRows = Math.floor(h / tileH);
console.log(w, h, availableCols, availableRows);
settings.update({
cols: availableCols,
rows: availableRows,
});
}