status/gui/dashboard/lib.js

81 lines
1.7 KiB
JavaScript
Raw Normal View History

import { get, writable } from 'svelte/store';
import { quintOut } from 'svelte/easing';
import { crossfade } from 'svelte/transition';
function createSettingsStore() {
2022-07-13 09:21:59 +00:00
const defaults = {
theme: 'dark',
cols: 4,
rows: 3,
2022-07-13 09:21:59 +00:00
};
const s = writable(defaults);
function updateStorage(val) {
2022-07-13 10:06:48 +00:00
window.localStorage.setItem('statusdash', JSON.stringify({
2022-07-13 09:21:59 +00:00
...defaults,
2022-07-13 10:23:54 +00:00
...val,
2022-07-13 10:06:48 +00:00
}));
s.set(val);
}
2022-07-13 10:10:20 +00:00
const localStorageString = window.localStorage.getItem('statusdash');
let localStorage = {};
try {
localStorage = JSON.parse(localStorageString);
}
catch {
localStorage = {};
}
updateStorage(localStorage);
return {
subscribe: s.subscribe,
set: val => updateStorage(val),
update: val => updateStorage({ ...get(s), ...val }),
};
}
export const settings = createSettingsStore();
export const shuffle = crossfade({
fallback(node) {
const style = getComputedStyle(node);
const transform = style.transform === 'none' ? '' : style.transform;
return {
duration: 400,
easing: quintOut,
css: t => `
transform: ${transform} scale(${t});
opacity: ${t}
`,
};
},
});
export function ringBell() {
2022-07-13 09:19:48 +00:00
const bell = new Audio(window.location.href + '/sound');
bell.addEventListener('canplaythrough', () => bell.play());
}
export function formatDuration(ms) {
// modified from https://www.30secondsofcode.org/js/s/format-duration
if (ms < 0) {
ms = -ms;
}
const time = {
d: Math.floor(ms / 86400000),
h: Math.floor(ms / 3600000) % 24,
m: Math.floor(ms / 60000) % 60,
s: Math.floor(ms / 1000) % 60,
};
return Object.entries(time)
.filter(val => val[1] !== 0)
.map(([ key, val ]) => `${val} ${key}`)
.join(' ');
}