Files
Febe/src/Febe.Web/wwwroot/js/pages/day-strip.js
T

92 lines
3.7 KiB
JavaScript

// The strip of the day (spec §10.1, §7.9; mockups 06 and 11): a 24-hour bar with the sessions as irregular strokes,
// the current session highlighted, the planned ones dashed (indicative), night and outside-window hatched, throttles
// in amber, stops as a red hatch, the «adesso» line, hour ticks and a legend. Every stretch keeps its Italian label
// from the server as a tooltip; a hidden list repeats them for screen readers.
import { h } from '../core/dom.js';
import { t } from '../core/text.js';
const HOURS = [0, 3, 6, 9, 12, 15, 18, 21, 24];
/** Milliseconds of a time from the API (ISO text or number). */
export function ms(value) {
return typeof value === 'number' ? value : Date.parse(value);
}
/** A small deterministic wobble (0..1) from a number: strokes look drawn by hand but stay put between updates. */
function wobble(n) {
const x = Math.sin(n * 12.9898) * 43758.5453;
return x - Math.floor(x);
}
/**
* Draws the strip of a DayStrip. Options: stopOpen (an open stop runs to the end of the day, mockup 11).
* Returns { strip, hours, legend } so the page can place the legend in its header row.
*/
export function dayStrip(day, { stopOpen = false } = {}) {
const start = ms(day.start);
const end = ms(day.end);
const now = ms(day.now);
const span = Math.max(1, end - start);
const pos = (value) => Math.min(100, Math.max(0, ((ms(value) - start) / span) * 100));
const bar = h('div.striscia', { role: 'img', 'aria-label': `${t('attivita.striscia')}: ${day.summary}` });
for (const hour of HOURS.slice(1, -1)) bar.append(h('span.tacca', { style: { left: `${(hour / 24) * 100}%` } }));
const kinds = new Set();
const spoken = [];
for (const s of day.spans) {
const left = pos(s.start);
let right = pos(s.end);
if (s.kind === 'Stop' && stopOpen && ms(s.end) >= now - 60000) right = 100;
const width = Math.max(0, right - left);
const style = { left: `${left}%`, width: `${width}%` };
let el = null;
switch (s.kind) {
case 'OutsideWindow':
el = h('span.notte', { style });
kinds.add('riposo');
break;
case 'Session':
case 'CurrentSession':
case 'PlannedSession': {
const w = wobble(ms(s.start) / 60000);
const inset = 6 + Math.round(w * 4);
el = h(`span.sess${s.kind === 'CurrentSession' ? '.ora' : ''}${s.kind === 'PlannedSession' ? '.prev' : ''}`,
{ style: { ...style, top: `${inset}px`, bottom: `${6 + Math.round((1 - w) * 4)}px` } });
kinds.add(s.kind === 'PlannedSession' ? 'prossime' : 'sessione');
break;
}
case 'Throttle':
el = h('span.freno', { style });
kinds.add('rallentamento');
break;
case 'Stop':
el = h('span.fermo', { style });
kinds.add('fermo');
break;
default:
break; // rest between sessions is the plain background
}
if (el) {
el.title = s.label;
bar.append(el);
if (s.kind !== 'OutsideWindow') spoken.push(s.label);
}
}
if (now >= start && now <= end) {
bar.append(h('span.adesso', { style: { left: `${pos(now)}%` }, title: t('attivita.adesso') }));
}
bar.append(h('ul.vh', spoken.map((label) => h('li', label))));
const hours = h('div.ore', { 'aria-hidden': 'true' }, HOURS.map((hour) => h('span', { style: { left: `${(hour / 24) * 100}%` } }, String(hour))));
const items = [
['sessione', h('i.l-sess')],
['prossime', h('i.l-prev')],
['rallentamento', h('i.l-freno')],
['fermo', h('i.l-fermo')],
['riposo', h('i.l-notte')],
].filter(([key]) => key === 'sessione' || kinds.has(key));
const legend = h('div.legenda', items.map(([key, swatch]) => h('span', swatch, t(`attivita.legenda.${key}`))));
return { strip: bar, hours, legend };
}