trebol 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -1
- package/dist/cli.js +17097 -12238
- package/dist/fonts/OFL.txt +93 -0
- package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/fonts/inter-greek.woff2 +0 -0
- package/dist/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/fonts/inter-latin.woff2 +0 -0
- package/dist/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/core/boot.js +12 -8
- package/dist/lib/core/eval.js +2 -2
- package/dist/lib/core/func.d.ts +4 -0
- package/dist/lib/core/machine/checks.d.ts +1 -0
- package/dist/lib/core/machine/checks.js +250 -0
- package/dist/lib/core/machine/events.d.ts +1 -0
- package/dist/lib/core/machine/events.js +365 -0
- package/dist/lib/core/machine/running.d.ts +1 -0
- package/dist/lib/core/machine/running.js +180 -0
- package/dist/lib/core/machine/shared.d.ts +1 -0
- package/dist/lib/core/machine/shared.js +123 -0
- package/dist/lib/core/machine/snapshot.d.ts +1 -0
- package/dist/lib/core/machine/snapshot.js +244 -0
- package/dist/lib/core/machine/spec.d.ts +1 -0
- package/dist/lib/core/machine/spec.js +274 -0
- package/dist/lib/core/machine/words.d.ts +1 -0
- package/dist/lib/core/machine/words.js +98 -0
- package/dist/lib/core/machine.d.ts +1 -0
- package/dist/lib/core/machine.js +17 -0
- package/dist/lib/core/mezz.js +31 -0
- package/dist/lib/core/natives/control.js +2 -2
- package/dist/lib/core/natives/data.js +6 -1
- package/dist/lib/core/natives/series.js +59 -9
- package/dist/lib/core/natives/text.js +3 -1
- package/dist/lib/host/browser/calendar.d.ts +34 -0
- package/dist/lib/host/browser/calendar.js +268 -0
- package/dist/lib/host/browser/count.d.ts +15 -0
- package/dist/lib/host/browser/count.js +48 -0
- package/dist/lib/host/browser/dialogs.d.ts +39 -0
- package/dist/lib/host/browser/dialogs.js +443 -0
- package/dist/lib/host/browser/drag.d.ts +70 -0
- package/dist/lib/host/browser/drag.js +268 -0
- package/dist/lib/host/browser/drawings.d.ts +30 -0
- package/dist/lib/host/browser/drawings.js +176 -0
- package/dist/lib/host/browser/events.d.ts +16 -0
- package/dist/lib/host/browser/events.js +125 -0
- package/dist/lib/host/browser/faces.d.ts +35 -0
- package/dist/lib/host/browser/faces.js +131 -0
- package/dist/lib/host/browser/fields.d.ts +16 -0
- package/dist/lib/host/browser/fields.js +322 -0
- package/dist/lib/host/browser/keys.d.ts +29 -0
- package/dist/lib/host/browser/keys.js +151 -0
- package/dist/lib/host/browser/lookup.d.ts +25 -0
- package/dist/lib/host/browser/lookup.js +275 -0
- package/dist/lib/host/browser/menus.d.ts +20 -0
- package/dist/lib/host/browser/menus.js +149 -0
- package/dist/lib/host/browser/mount.d.ts +92 -0
- package/dist/lib/host/browser/mount.js +448 -0
- package/dist/lib/host/browser/orbits.d.ts +90 -0
- package/dist/lib/host/browser/orbits.js +379 -0
- package/dist/lib/host/browser/page.d.ts +13 -0
- package/dist/lib/host/browser/page.js +35 -0
- package/dist/lib/host/browser/richtext.d.ts +19 -0
- package/dist/lib/host/browser/richtext.js +379 -0
- package/dist/lib/host/browser/row-faces.d.ts +5 -0
- package/dist/lib/host/browser/row-faces.js +74 -0
- package/dist/lib/host/browser/screens.d.ts +37 -0
- package/dist/lib/host/browser/screens.js +140 -0
- package/dist/lib/host/browser/swipe.d.ts +31 -0
- package/dist/lib/host/browser/swipe.js +178 -0
- package/dist/lib/host/browser/sync.d.ts +24 -0
- package/dist/lib/host/browser/sync.js +450 -0
- package/dist/lib/host/browser/tables.d.ts +65 -0
- package/dist/lib/host/browser/tables.js +505 -0
- package/dist/lib/host/browser/times.d.ts +14 -0
- package/dist/lib/host/browser/times.js +114 -0
- package/dist/lib/host/browser/toolbars.d.ts +17 -0
- package/dist/lib/host/browser/toolbars.js +129 -0
- package/dist/lib/host/browser/trees.d.ts +20 -0
- package/dist/lib/host/browser/trees.js +235 -0
- package/dist/lib/host/browser/uploads.d.ts +29 -0
- package/dist/lib/host/browser/uploads.js +370 -0
- package/dist/lib/host/browser/widget.d.ts +13 -0
- package/dist/lib/host/browser/widget.js +105 -0
- package/dist/lib/host/browser.d.ts +25 -0
- package/dist/lib/host/browser.js +129 -3964
- package/dist/lib/host/devlog.d.ts +11 -0
- package/dist/lib/host/devlog.js +13 -0
- package/dist/lib/host/devtools/console.d.ts +9 -0
- package/dist/lib/host/devtools/console.js +104 -0
- package/dist/lib/host/devtools/faces.d.ts +25 -0
- package/dist/lib/host/devtools/faces.js +169 -0
- package/dist/lib/host/devtools/log.d.ts +3 -0
- package/dist/lib/host/devtools/log.js +32 -0
- package/dist/lib/host/devtools/machines.d.ts +34 -0
- package/dist/lib/host/devtools/machines.js +212 -0
- package/dist/lib/host/devtools/read.d.ts +13 -0
- package/dist/lib/host/devtools/read.js +52 -0
- package/dist/lib/host/devtools/state.d.ts +90 -0
- package/dist/lib/host/devtools/state.js +123 -0
- package/dist/lib/host/devtools.d.ts +19 -0
- package/dist/lib/host/devtools.js +238 -0
- package/dist/lib/host/fonts/OFL.txt +93 -0
- package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
- package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/host/fonts.d.ts +11 -0
- package/dist/lib/host/fonts.js +30 -0
- package/dist/lib/host/icons.js +4 -0
- package/dist/lib/host/index.d.ts +5 -1
- package/dist/lib/host/index.js +110 -5
- package/dist/lib/host/js.d.ts +5 -0
- package/dist/lib/host/js.js +130 -0
- package/dist/lib/host/locale.d.ts +18 -0
- package/dist/lib/host/locale.js +75 -5
- package/dist/lib/host/markdown.d.ts +6 -0
- package/dist/lib/host/markdown.js +18 -0
- package/dist/lib/host/modules.d.ts +4 -0
- package/dist/lib/host/modules.js +157 -20
- package/dist/lib/host/motion.d.ts +32 -0
- package/dist/lib/host/motion.js +190 -0
- package/dist/lib/host/net.js +14 -3
- package/dist/lib/host/orbit.d.ts +121 -0
- package/dist/lib/host/orbit.js +374 -0
- package/dist/lib/host/palette.d.ts +17 -0
- package/dist/lib/host/palette.js +131 -0
- package/dist/lib/host/rows.d.ts +14 -0
- package/dist/lib/host/rows.js +29 -0
- package/dist/lib/host/scheduler.d.ts +8 -0
- package/dist/lib/host/scheduler.js +64 -36
- package/dist/lib/host/share.d.ts +31 -0
- package/dist/lib/host/share.js +134 -0
- package/dist/lib/host/theme/classic.d.ts +1 -0
- package/dist/lib/host/theme/classic.js +120 -0
- package/dist/lib/host/theme/cupertino.d.ts +1 -0
- package/dist/lib/host/theme/cupertino.js +65 -0
- package/dist/lib/host/theme/data.d.ts +1 -0
- package/dist/lib/host/theme/data.js +109 -0
- package/dist/lib/host/theme/devtools.d.ts +1 -0
- package/dist/lib/host/theme/devtools.js +166 -0
- package/dist/lib/host/theme/dialogs.d.ts +1 -0
- package/dist/lib/host/theme/dialogs.js +63 -0
- package/dist/lib/host/theme/fluent.d.ts +1 -0
- package/dist/lib/host/theme/fluent.js +64 -0
- package/dist/lib/host/theme/forms.d.ts +1 -0
- package/dist/lib/host/theme/forms.js +239 -0
- package/dist/lib/host/theme/fusion.d.ts +1 -0
- package/dist/lib/host/theme/fusion.js +66 -0
- package/dist/lib/host/theme/layout.d.ts +1 -0
- package/dist/lib/host/theme/layout.js +166 -0
- package/dist/lib/host/theme/media.d.ts +1 -0
- package/dist/lib/host/theme/media.js +106 -0
- package/dist/lib/host/theme/navigation.d.ts +1 -0
- package/dist/lib/host/theme/navigation.js +36 -0
- package/dist/lib/host/theme/panes.d.ts +1 -0
- package/dist/lib/host/theme/panes.js +99 -0
- package/dist/lib/host/theme/popups.d.ts +1 -0
- package/dist/lib/host/theme/popups.js +149 -0
- package/dist/lib/host/theme/responsive.d.ts +1 -0
- package/dist/lib/host/theme/responsive.js +103 -0
- package/dist/lib/host/theme/toasts.d.ts +1 -0
- package/dist/lib/host/theme/toasts.js +62 -0
- package/dist/lib/host/theme.js +19 -1179
- package/dist/lib/ui/dialect/events.trb +276 -0
- package/dist/lib/ui/dialect/faces.trb +135 -0
- package/dist/lib/ui/dialect/layout.trb +283 -0
- package/dist/lib/ui/dialect/palette.trb +87 -0
- package/dist/lib/ui/dialect/problems.trb +174 -0
- package/dist/lib/ui/dialect/reactions.trb +198 -0
- package/dist/lib/ui/dialect/rows.trb +208 -0
- package/dist/lib/ui/dialect/screens.trb +293 -0
- package/dist/lib/ui/dialect/steps.trb +203 -0
- package/dist/lib/ui/dialect/validation.trb +137 -0
- package/dist/lib/ui/dialect/view.trb +110 -0
- package/dist/lib/ui/dialect/walking.trb +98 -0
- package/dist/lib/ui/dialect/words.trb +70 -0
- package/dist/lib/ui/i18n.trb +23 -0
- package/dist/lib/ui/test.trb +12 -0
- package/dist/lib/ui/testing/actions.trb +421 -0
- package/dist/lib/ui/testing/faces.trb +148 -0
- package/dist/lib/ui/testing/server.trb +73 -0
- package/dist/lib/ui/ui.trb +25 -1234
- package/dist/trebol.js +4882 -1088
- package/editors/vscode/README.md +15 -0
- package/editors/vscode/language-configuration.json +28 -0
- package/editors/vscode/package.json +42 -0
- package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
- package/package.json +7 -3
- package/src/core/boot.ts +10 -7
- package/src/core/eval.ts +2 -2
- package/src/core/func.ts +4 -0
- package/src/core/machine/checks.ts +251 -0
- package/src/core/machine/events.ts +366 -0
- package/src/core/machine/running.ts +181 -0
- package/src/core/machine/shared.ts +124 -0
- package/src/core/machine/snapshot.ts +245 -0
- package/src/core/machine/spec.ts +275 -0
- package/src/core/machine/words.ts +99 -0
- package/src/core/machine.ts +20 -0
- package/src/core/mezz.ts +31 -0
- package/src/core/natives/control.ts +2 -2
- package/src/core/natives/data.ts +6 -1
- package/src/core/natives/parse.ts +1 -1
- package/src/core/natives/series.ts +55 -11
- package/src/core/natives/text.ts +2 -1
- package/src/host/browser/calendar.ts +264 -0
- package/src/host/browser/count.ts +48 -0
- package/src/host/browser/dialogs.ts +439 -0
- package/src/host/browser/drag.ts +278 -0
- package/src/host/browser/drawings.ts +178 -0
- package/src/host/browser/events.ts +117 -0
- package/src/host/browser/faces.ts +143 -0
- package/src/host/browser/fields.ts +315 -0
- package/src/host/browser/keys.ts +145 -0
- package/src/host/browser/lookup.ts +262 -0
- package/src/host/browser/menus.ts +146 -0
- package/src/host/browser/mount.ts +420 -0
- package/src/host/browser/orbits.ts +418 -0
- package/src/host/browser/page.ts +43 -0
- package/src/host/browser/richtext.ts +346 -0
- package/src/host/browser/row-faces.ts +75 -0
- package/src/host/browser/screens.ts +142 -0
- package/src/host/browser/swipe.ts +168 -0
- package/src/host/browser/sync.ts +354 -0
- package/src/host/browser/tables.ts +461 -0
- package/src/host/browser/times.ts +115 -0
- package/src/host/browser/toolbars.ts +126 -0
- package/src/host/browser/trees.ts +205 -0
- package/src/host/browser/uploads.ts +367 -0
- package/src/host/browser/widget.ts +103 -0
- package/src/host/browser.ts +128 -3747
- package/src/host/devlog.ts +16 -0
- package/src/host/devtools/console.ts +102 -0
- package/src/host/devtools/faces.ts +160 -0
- package/src/host/devtools/log.ts +34 -0
- package/src/host/devtools/machines.ts +208 -0
- package/src/host/devtools/read.ts +58 -0
- package/src/host/devtools/state.ts +148 -0
- package/src/host/devtools.ts +238 -0
- package/src/host/fonts/OFL.txt +93 -0
- package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/src/host/fonts/inter-cyrillic.woff2 +0 -0
- package/src/host/fonts/inter-greek-ext.woff2 +0 -0
- package/src/host/fonts/inter-greek.woff2 +0 -0
- package/src/host/fonts/inter-latin-ext.woff2 +0 -0
- package/src/host/fonts/inter-latin.woff2 +0 -0
- package/src/host/fonts/inter-vietnamese.woff2 +0 -0
- package/src/host/fonts.ts +40 -0
- package/src/host/icons.ts +4 -0
- package/src/host/index.ts +110 -5
- package/src/host/js.ts +127 -0
- package/src/host/locale.ts +71 -5
- package/src/host/markdown.ts +18 -0
- package/src/host/modules.ts +150 -22
- package/src/host/motion.ts +204 -0
- package/src/host/net.ts +13 -3
- package/src/host/orbit.ts +432 -0
- package/src/host/palette.ts +139 -0
- package/src/host/rows.ts +30 -0
- package/src/host/scheduler.ts +67 -36
- package/src/host/share.ts +168 -0
- package/src/host/theme/classic.ts +121 -0
- package/src/host/theme/cupertino.ts +66 -0
- package/src/host/theme/data.ts +110 -0
- package/src/host/theme/devtools.ts +167 -0
- package/src/host/theme/dialogs.ts +64 -0
- package/src/host/theme/fluent.ts +65 -0
- package/src/host/theme/forms.ts +240 -0
- package/src/host/theme/fusion.ts +67 -0
- package/src/host/theme/layout.ts +167 -0
- package/src/host/theme/media.ts +107 -0
- package/src/host/theme/navigation.ts +37 -0
- package/src/host/theme/panes.ts +100 -0
- package/src/host/theme/popups.ts +150 -0
- package/src/host/theme/responsive.ts +104 -0
- package/src/host/theme/toasts.ts +63 -0
- package/src/host/theme.ts +19 -1179
- package/src/ui/dialect/events.trb +276 -0
- package/src/ui/dialect/faces.trb +135 -0
- package/src/ui/dialect/layout.trb +283 -0
- package/src/ui/dialect/palette.trb +87 -0
- package/src/ui/dialect/problems.trb +174 -0
- package/src/ui/dialect/reactions.trb +198 -0
- package/src/ui/dialect/rows.trb +208 -0
- package/src/ui/dialect/screens.trb +293 -0
- package/src/ui/dialect/steps.trb +203 -0
- package/src/ui/dialect/validation.trb +137 -0
- package/src/ui/dialect/view.trb +110 -0
- package/src/ui/dialect/walking.trb +98 -0
- package/src/ui/dialect/words.trb +70 -0
- package/src/ui/i18n.trb +23 -0
- package/src/ui/test.trb +12 -0
- package/src/ui/testing/actions.trb +421 -0
- package/src/ui/testing/faces.trb +148 -0
- package/src/ui/testing/server.trb +73 -0
- package/src/ui/ui.trb +25 -1234
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
// --- tables and lists ---------------------------------------------------------------
|
|
2
|
+
|
|
3
|
+
import { K, NONE, form, intern, spell, canon, type Cell, type ObjectCell, type BlockCell, type WordCell, type Sym } from '../../core/index.ts';
|
|
4
|
+
import { translate, activeLanguage } from '../index.ts';
|
|
5
|
+
import { rowField, groupRow, sortGroups, rowWindow, DRAW_ALL_UP_TO } from '../rows.ts';
|
|
6
|
+
import { mayMove, rowPlaces, moveRows, leaveRow } from '../motion.ts';
|
|
7
|
+
import { get, str, own, display, flag, icon } from './faces.ts';
|
|
8
|
+
import { type Mounted, host, rowOf, fire, el } from './mount.ts';
|
|
9
|
+
import { handlerFor } from './events.ts';
|
|
10
|
+
|
|
11
|
+
/** A column; `image` when it shows each row's picture, `round` when cut to a circle. */
|
|
12
|
+
export interface Column { sym: Sym; word: Cell; label: string; image: boolean; round: boolean }
|
|
13
|
+
|
|
14
|
+
export function columns(face: ObjectCell): Column[] {
|
|
15
|
+
const c = get(face, 'columns');
|
|
16
|
+
if (c.t !== K.Block) return [];
|
|
17
|
+
const out: Column[] = [];
|
|
18
|
+
const d = c.s.data;
|
|
19
|
+
for (let i = c.i; i + 1 < d.length; i += 2) {
|
|
20
|
+
const w = d[i]!;
|
|
21
|
+
if (w.t !== K.Word) continue;
|
|
22
|
+
// a column of images keeps its label in a block with what it is: ["Photo" image round]
|
|
23
|
+
const spec = d[i + 1]!;
|
|
24
|
+
const parts = spec.t === K.Block ? (spec as BlockCell).s.data.slice((spec as BlockCell).i) : [spec];
|
|
25
|
+
const has = (name: string): boolean => parts.some(p => p.t === K.Word && canon((p as WordCell).sym) === canon(intern(name)));
|
|
26
|
+
out.push({ sym: (w as WordCell).sym, word: w, label: translate(parts[0] ?? NONE, host.rt), image: has('image'), round: has('round') });
|
|
27
|
+
}
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The picture of a row in a column of images, from an address or a file; without one, an empty place for it. */
|
|
32
|
+
export function thumb(box: Element, v: Cell, round: boolean): void {
|
|
33
|
+
const src = v.t === K.String || v.t === K.Url || v.t === K.File ? form(v) : '';
|
|
34
|
+
const old = box.querySelector<HTMLElement>(':scope > .tb-thumb');
|
|
35
|
+
let pic = old;
|
|
36
|
+
if (src === '' ? pic?.tagName !== 'SPAN' : pic?.tagName !== 'IMG') {
|
|
37
|
+
// decoration: the row's text says who or what it is
|
|
38
|
+
pic = el(src === '' ? 'span' : 'img', 'tb-thumb');
|
|
39
|
+
if (src !== '') (pic as HTMLImageElement).alt = '';
|
|
40
|
+
if (old !== null) old.replaceWith(pic);
|
|
41
|
+
else box.prepend(pic);
|
|
42
|
+
}
|
|
43
|
+
if (src !== '' && pic!.getAttribute('src') !== src) (pic as HTMLImageElement).src = src;
|
|
44
|
+
pic!.classList.toggle('tb-round', round);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function sameRow(a: Cell, b: Cell): boolean {
|
|
48
|
+
if (a.t === K.Map && b.t === K.Map) return a.s === b.s;
|
|
49
|
+
if (a.t === K.Object && b.t === K.Object) return a.ctx === b.ctx;
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function cellText(v: Cell): string {
|
|
54
|
+
if (v.t === K.None) return '';
|
|
55
|
+
if (v.t === K.Logic) return own(v.v ? 'yes' : 'no');
|
|
56
|
+
return display(v);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const NUMERIC: ReadonlySet<number> = new Set([K.Integer, K.Decimal, K.Money, K.Percent]);
|
|
60
|
+
|
|
61
|
+
export function buildRows(face: ObjectCell, style: string, m: Mounted): HTMLElement {
|
|
62
|
+
const wrap = el('div', style === 'table' ? 'tb-table-wrap' : 'tb-list-wrap');
|
|
63
|
+
let box: HTMLElement;
|
|
64
|
+
if (style === 'table') {
|
|
65
|
+
// explicit roles keep the table semantics when narrow screens restyle it as cards
|
|
66
|
+
const table = el('table', 'tb-table');
|
|
67
|
+
table.setAttribute('role', 'table');
|
|
68
|
+
const head = el('thead');
|
|
69
|
+
const tr = el('tr');
|
|
70
|
+
tr.setAttribute('role', 'row');
|
|
71
|
+
const cols = columns(face);
|
|
72
|
+
for (const _ of cols) {
|
|
73
|
+
const th = el('th');
|
|
74
|
+
th.scope = 'col';
|
|
75
|
+
th.setAttribute('role', 'columnheader');
|
|
76
|
+
tr.append(th);
|
|
77
|
+
}
|
|
78
|
+
head.append(tr);
|
|
79
|
+
head.addEventListener('click', ev => {
|
|
80
|
+
const th = (ev.target as Element).closest('th');
|
|
81
|
+
const c = th !== null && th.querySelector('.tb-sort') !== null ? cols[th.cellIndex] : undefined;
|
|
82
|
+
if (c !== undefined) void fire('ui-sort', face, c.word);
|
|
83
|
+
});
|
|
84
|
+
box = el('tbody');
|
|
85
|
+
// the totals row sits outside tbody, so it is never chosen, sorted or moved
|
|
86
|
+
const foot = el('tfoot');
|
|
87
|
+
const totals = newTableRow(cols);
|
|
88
|
+
totals.className = 'tb-totals';
|
|
89
|
+
foot.append(totals);
|
|
90
|
+
// the room of the rows not drawn, above and below the ones drawn
|
|
91
|
+
const room = (): HTMLElement => {
|
|
92
|
+
const b = el('tbody', 'tb-room');
|
|
93
|
+
b.setAttribute('aria-hidden', 'true');
|
|
94
|
+
const r = el('tr');
|
|
95
|
+
r.append(el('td'));
|
|
96
|
+
b.append(r);
|
|
97
|
+
return b;
|
|
98
|
+
};
|
|
99
|
+
table.append(head, room(), box, room(), foot);
|
|
100
|
+
wrap.append(table);
|
|
101
|
+
} else {
|
|
102
|
+
box = el('ul', 'tb-list');
|
|
103
|
+
wrap.append(box);
|
|
104
|
+
}
|
|
105
|
+
const empty = el('p', 'tb-empty');
|
|
106
|
+
wrap.append(empty);
|
|
107
|
+
const choose = (target: EventTarget | null): void => {
|
|
108
|
+
const item = (target as Element | null)?.closest(style === 'table' ? 'tr' : 'li');
|
|
109
|
+
const row = item ? rowOf.get(item) : undefined;
|
|
110
|
+
if (row !== undefined) void fire('ui-select', face, row);
|
|
111
|
+
};
|
|
112
|
+
box.addEventListener('click', ev => choose(ev.target));
|
|
113
|
+
box.addEventListener('keydown', ev => {
|
|
114
|
+
if (ev.key === 'Enter' || ev.key === ' ') {
|
|
115
|
+
ev.preventDefault();
|
|
116
|
+
choose(ev.target);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
m.box = box;
|
|
120
|
+
m.rows = new Map();
|
|
121
|
+
m.empty = empty;
|
|
122
|
+
return wrap;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The rows of a table or list in the order shown, with their keys; then draws them. */
|
|
126
|
+
export function syncRows(m: Mounted): void {
|
|
127
|
+
const f = m.face;
|
|
128
|
+
const data = get(f, 'data');
|
|
129
|
+
let rows = data.t === K.Block ? data.s.data.slice(data.i) : [];
|
|
130
|
+
if (m.box!.tagName === 'TBODY') {
|
|
131
|
+
syncHead(f, m.box!.parentElement as HTMLTableElement, columns(f));
|
|
132
|
+
const order = hostSorted(f);
|
|
133
|
+
if (order !== null) rows = sortGroups(rows, order.sym, order.desc, activeLanguage(host.rt), cellText);
|
|
134
|
+
}
|
|
135
|
+
const keyWord = get(f, 'key');
|
|
136
|
+
const key = keyWord.t === K.Word || keyWord.t === K.LitWord ? (keyWord as WordCell).sym : intern('id');
|
|
137
|
+
const seen = new Set<string>();
|
|
138
|
+
m.items = rows.map((row, i) => {
|
|
139
|
+
const group = groupRow(row);
|
|
140
|
+
let k: string;
|
|
141
|
+
if (group !== null) k = `${group.kind}#${i}`;
|
|
142
|
+
else {
|
|
143
|
+
const id = rowField(row, key);
|
|
144
|
+
k = id.t === K.None ? `#${i}` : `${id.t}:${form(id)}`;
|
|
145
|
+
if (seen.has(k)) k = `${k}#${i}`;
|
|
146
|
+
}
|
|
147
|
+
seen.add(k);
|
|
148
|
+
return { row, k };
|
|
149
|
+
});
|
|
150
|
+
drawRows(m);
|
|
151
|
+
m.empty!.textContent = str(f, 'text') ?? own('empty');
|
|
152
|
+
m.empty!.hidden = rows.length > 0;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Tables and lists with too many rows to draw them all. */
|
|
156
|
+
export const longRows = new Set<Mounted>();
|
|
157
|
+
/** While printing, every row is drawn. */
|
|
158
|
+
export let printing = false;
|
|
159
|
+
|
|
160
|
+
/** The rows to draw: all of them, or those near the screen. */
|
|
161
|
+
export function rowsToDraw(m: Mounted): { first: number; end: number } {
|
|
162
|
+
const count = m.items.length;
|
|
163
|
+
if (count <= DRAW_ALL_UP_TO || printing) return { first: 0, end: count };
|
|
164
|
+
// where the first row would be: the rows drawn start below the room of those that are not
|
|
165
|
+
const top = m.box!.getBoundingClientRect().top - m.first * m.rowHeight;
|
|
166
|
+
const now = m.end > m.first ? { first: m.first, end: m.end } : undefined;
|
|
167
|
+
return rowWindow(count, m.rowHeight || 48, -top, innerHeight, now);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Creates, updates, moves or removes only the rows drawn whose key changed. */
|
|
171
|
+
export function drawRows(m: Mounted, again = true): void {
|
|
172
|
+
const f = m.face;
|
|
173
|
+
const box = m.box!;
|
|
174
|
+
const table = box.tagName === 'TBODY';
|
|
175
|
+
const cols = columns(f);
|
|
176
|
+
const selected = get(f, 'selected');
|
|
177
|
+
const selectable = handlerFor(f, 'on-select');
|
|
178
|
+
box.parentElement!.classList.toggle('tb-selectable', selectable);
|
|
179
|
+
const { first, end } = rowsToDraw(m);
|
|
180
|
+
const count = m.items.length;
|
|
181
|
+
const old = m.rows!;
|
|
182
|
+
const next = new Map<string, HTMLElement>();
|
|
183
|
+
const morph = morphName(get(f, 'morph'));
|
|
184
|
+
const keyWord = get(f, 'key');
|
|
185
|
+
const keySym = keyWord.t === K.Word || keyWord.t === K.LitWord ? (keyWord as WordCell).sym : intern('id');
|
|
186
|
+
// rows added, taken away or in another order move to their places, when all of them are drawn
|
|
187
|
+
const moved = old.size > 0 && count <= DRAW_ALL_UP_TO && !printing && box.isConnected && mayMove()
|
|
188
|
+
&& !sameKeys(old, m.items) && m.items.some(it => old.has(it.k));
|
|
189
|
+
const was = moved ? rowPlaces(old.values()) : null;
|
|
190
|
+
let cursor = box.firstChild;
|
|
191
|
+
for (let i = first; i < end; i++) {
|
|
192
|
+
const { row, k } = m.items[i]!;
|
|
193
|
+
const group = groupRow(row);
|
|
194
|
+
let item = old.get(k);
|
|
195
|
+
if (group !== null) {
|
|
196
|
+
// a group's head or foot: kept by its place, never chosen, dragged or sorted
|
|
197
|
+
if (item === undefined) item = group.kind === 'head' ? newGroupHead(cols, table) : table ? newTableRow(cols) : newListItem();
|
|
198
|
+
item.className = `tb-group-${group.kind} tb-level-${group.level}`;
|
|
199
|
+
if (group.kind === 'head') {
|
|
200
|
+
const t = translate(group.value, host.rt);
|
|
201
|
+
const cell = item.firstElementChild!;
|
|
202
|
+
if (cell.textContent !== t) cell.textContent = t;
|
|
203
|
+
if (table) (cell as HTMLTableCellElement).colSpan = Math.max(1, cols.length);
|
|
204
|
+
} else fillRow(item, group.value, cols, table);
|
|
205
|
+
} else {
|
|
206
|
+
if (item === undefined) item = table ? newTableRow(cols) : newListItem();
|
|
207
|
+
rowOf.set(item, row);
|
|
208
|
+
// morph 'client: the row is "client <its key>", for a face of the next screen to become
|
|
209
|
+
if (morph !== null) item.dataset['tbMorph'] = `${morph} ${form(rowField(row, keySym))}`;
|
|
210
|
+
else delete item.dataset['tbMorph'];
|
|
211
|
+
fillRow(item, row, cols, table);
|
|
212
|
+
if (selectable) item.tabIndex = 0;
|
|
213
|
+
else item.removeAttribute('tabindex');
|
|
214
|
+
const on = selected.t !== K.None && sameRow(selected, row);
|
|
215
|
+
item.classList.toggle('tb-selected', on);
|
|
216
|
+
if (selectable) item.setAttribute('aria-selected', on ? 'true' : 'false');
|
|
217
|
+
else item.removeAttribute('aria-selected');
|
|
218
|
+
}
|
|
219
|
+
next.set(k, item);
|
|
220
|
+
// with rows left undrawn, each says where it is among all of them
|
|
221
|
+
if (count > end - first) {
|
|
222
|
+
item.setAttribute(table ? 'aria-rowindex' : 'aria-posinset', String(table ? i + 2 : i + 1));
|
|
223
|
+
if (!table) item.setAttribute('aria-setsize', String(count));
|
|
224
|
+
} else {
|
|
225
|
+
item.removeAttribute(table ? 'aria-rowindex' : 'aria-posinset');
|
|
226
|
+
item.removeAttribute('aria-setsize');
|
|
227
|
+
}
|
|
228
|
+
if (item !== cursor) box.insertBefore(item, cursor);
|
|
229
|
+
else cursor = cursor.nextSibling;
|
|
230
|
+
}
|
|
231
|
+
for (const [k, item] of old) {
|
|
232
|
+
if (next.get(k) === item) continue;
|
|
233
|
+
if (was !== null) leaveRow(item, was.get(item)!);
|
|
234
|
+
else item.remove();
|
|
235
|
+
}
|
|
236
|
+
m.rows = next;
|
|
237
|
+
if (was !== null) moveRows(next.values(), was);
|
|
238
|
+
m.first = first;
|
|
239
|
+
m.end = end;
|
|
240
|
+
if (table) {
|
|
241
|
+
if (count > end - first) box.parentElement!.setAttribute('aria-rowcount', String(count + 1));
|
|
242
|
+
else box.parentElement!.removeAttribute('aria-rowcount');
|
|
243
|
+
}
|
|
244
|
+
if (count <= DRAW_ALL_UP_TO) {
|
|
245
|
+
longRows.delete(m);
|
|
246
|
+
room(m, 0, 0);
|
|
247
|
+
if (table) keepColumns(box.parentElement as HTMLTableElement, false);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
longRows.add(m);
|
|
251
|
+
if (table) keepColumns(box.parentElement as HTMLTableElement, true);
|
|
252
|
+
// how tall a row is, from those drawn; the room of the others follows it
|
|
253
|
+
const drawn = end - first;
|
|
254
|
+
const h = box.getBoundingClientRect().height - (table ? 0 : parseFloat(box.style.paddingTop || '0') + parseFloat(box.style.paddingBottom || '0'));
|
|
255
|
+
if (drawn > 0 && h > 0) m.rowHeight = h / drawn;
|
|
256
|
+
room(m, first * m.rowHeight, (count - end) * m.rowHeight);
|
|
257
|
+
// a first guess of the height may have drawn the wrong rows
|
|
258
|
+
if (again) {
|
|
259
|
+
const want = rowsToDraw(m);
|
|
260
|
+
if (want.first !== first || want.end !== end) drawRows(m, false);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** A morph name, 'logo or [client 7], as text: "logo", "client 7"; null for none. */
|
|
265
|
+
export function morphName(v: Cell): string | null {
|
|
266
|
+
if (v.t === K.Word || v.t === K.LitWord) return spell((v as WordCell).sym);
|
|
267
|
+
if (v.t === K.Block) return v.s.data.slice(v.i).map(c => form(c)).join(' ');
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Whether the rows drawn are the rows to draw, in the same order. */
|
|
272
|
+
export function sameKeys(old: Map<string, HTMLElement>, items: { k: string }[]): boolean {
|
|
273
|
+
if (old.size !== items.length) return false;
|
|
274
|
+
let i = 0;
|
|
275
|
+
for (const k of old.keys()) if (items[i++]!.k !== k) return false;
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* A long table keeps the widths its columns first took, as shares of its width,
|
|
281
|
+
* so they do not change with the rows drawn while it scrolls.
|
|
282
|
+
*/
|
|
283
|
+
export function keepColumns(table: HTMLTableElement, keep: boolean): void {
|
|
284
|
+
const ths = Array.from(table.tHead!.rows[0]!.cells);
|
|
285
|
+
if (!keep) {
|
|
286
|
+
table.classList.remove('tb-fixed');
|
|
287
|
+
for (const th of ths) th.style.width = '';
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (table.classList.contains('tb-fixed')) return;
|
|
291
|
+
const widths = ths.map(th => th.getBoundingClientRect().width);
|
|
292
|
+
const all = widths.reduce((a, b) => a + b, 0);
|
|
293
|
+
// on a narrow screen the rows are cards and the headers take no room: later
|
|
294
|
+
if (all === 0) return;
|
|
295
|
+
ths.forEach((th, i) => { th.style.width = `${(widths[i]! / all) * 100}%`; });
|
|
296
|
+
table.classList.add('tb-fixed');
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Sets the room of the rows not drawn above and below those drawn. */
|
|
300
|
+
export function room(m: Mounted, above: number, below: number): void {
|
|
301
|
+
const box = m.box!;
|
|
302
|
+
if (box.tagName === 'TBODY') {
|
|
303
|
+
for (const [b, px] of [[box.previousElementSibling, above], [box.nextElementSibling, below]] as const) {
|
|
304
|
+
const td = (b as HTMLElement).querySelector('td')!;
|
|
305
|
+
td.style.height = `${px}px`;
|
|
306
|
+
(b as HTMLElement).hidden = px === 0;
|
|
307
|
+
}
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
box.style.paddingTop = above > 0 ? `${above}px` : '';
|
|
311
|
+
box.style.paddingBottom = below > 0 ? `${below}px` : '';
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export let rowsFrame = 0;
|
|
315
|
+
|
|
316
|
+
/** On a scroll or a resize, the long tables and lists draw the rows now near the screen. */
|
|
317
|
+
export function onRowsMoved(): void {
|
|
318
|
+
if (rowsFrame !== 0 || longRows.size === 0) return;
|
|
319
|
+
rowsFrame = requestAnimationFrame(() => {
|
|
320
|
+
rowsFrame = 0;
|
|
321
|
+
for (const m of longRows) {
|
|
322
|
+
if (!m.box!.isConnected) { longRows.delete(m); continue; }
|
|
323
|
+
const want = rowsToDraw(m);
|
|
324
|
+
if (want.first !== m.first || want.end !== m.end) drawRows(m);
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Printing draws every row, and the screen goes back to drawing some after. */
|
|
330
|
+
export function onPrint(on: boolean): void {
|
|
331
|
+
printing = on;
|
|
332
|
+
for (const m of longRows) {
|
|
333
|
+
if (!m.box!.isConnected) continue;
|
|
334
|
+
// all the rows cover the screen, but are not to be kept
|
|
335
|
+
if (!on) m.end = m.first;
|
|
336
|
+
drawRows(m);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** Headers in the active language; in a sortable table each is a button, with an arrow on the sorted one. */
|
|
341
|
+
export function syncHead(f: ObjectCell, table: HTMLTableElement, cols: Column[]): void {
|
|
342
|
+
const sortable = flag(f, 'sortable') || handlerFor(f, 'on-sort');
|
|
343
|
+
const order = sortedBy(f);
|
|
344
|
+
const ths = table.tHead!.rows[0]!.cells;
|
|
345
|
+
cols.forEach((c, i) => {
|
|
346
|
+
const th = ths[i];
|
|
347
|
+
if (th === undefined) return;
|
|
348
|
+
const on = order !== null && canon(order.sym) === canon(c.sym);
|
|
349
|
+
let button = th.querySelector<HTMLButtonElement>('.tb-sort');
|
|
350
|
+
if (sortable && button === null) {
|
|
351
|
+
button = el('button', 'tb-sort');
|
|
352
|
+
button.type = 'button';
|
|
353
|
+
button.append(el('span'));
|
|
354
|
+
th.replaceChildren(button);
|
|
355
|
+
} else if (!sortable && button !== null) {
|
|
356
|
+
th.replaceChildren();
|
|
357
|
+
button = null;
|
|
358
|
+
}
|
|
359
|
+
const label = button !== null ? button.firstChild! : th;
|
|
360
|
+
if (label.textContent !== c.label) label.textContent = c.label;
|
|
361
|
+
if (button !== null) {
|
|
362
|
+
// the button stays, so a header chosen from the keyboard keeps the focus
|
|
363
|
+
const arrow = on && order.desc ? 'arrow-down' : 'arrow-up';
|
|
364
|
+
const svg = button.querySelector('svg');
|
|
365
|
+
if (svg?.dataset['icon'] !== arrow) {
|
|
366
|
+
const next = icon(arrow);
|
|
367
|
+
if (next !== null) {
|
|
368
|
+
next.dataset['icon'] = arrow;
|
|
369
|
+
if (svg) svg.replaceWith(next);
|
|
370
|
+
else button.append(next);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
button.querySelector('svg')?.classList.toggle('tb-sort-idle', !on);
|
|
374
|
+
}
|
|
375
|
+
if (on) th.setAttribute('aria-sort', order.desc ? 'descending' : 'ascending');
|
|
376
|
+
else th.removeAttribute('aria-sort');
|
|
377
|
+
});
|
|
378
|
+
const totals = get(f, 'totals');
|
|
379
|
+
const foot = table.tFoot!;
|
|
380
|
+
foot.hidden = totals.t === K.None;
|
|
381
|
+
if (totals.t !== K.None) fillRow(foot.rows[0]!, totals, cols, true);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** A table's sorted facet: [column asc] or [column desc]. */
|
|
385
|
+
export function sortedBy(f: ObjectCell): { sym: Sym; desc: boolean } | null {
|
|
386
|
+
const v = get(f, 'sorted');
|
|
387
|
+
if (v.t !== K.Block || v.s.data.length - v.i < 2) return null;
|
|
388
|
+
const c = v.s.data[v.i]!;
|
|
389
|
+
const o = v.s.data[v.i + 1]!;
|
|
390
|
+
if ((c.t !== K.Word && c.t !== K.LitWord) || (o.t !== K.Word && o.t !== K.LitWord)) return null;
|
|
391
|
+
return { sym: (c as WordCell).sym, desc: canon((o as WordCell).sym) === canon(intern('desc')) };
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** The order the host applies itself: a sortable table without on-sort, which sorts for itself. */
|
|
395
|
+
export function hostSorted(f: ObjectCell): { sym: Sym; desc: boolean } | null {
|
|
396
|
+
return flag(f, 'sortable') && !handlerFor(f, 'on-sort') ? sortedBy(f) : null;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** A group's title, the width of the whole row. */
|
|
400
|
+
export function newGroupHead(cols: Column[], table: boolean): HTMLElement {
|
|
401
|
+
if (!table) {
|
|
402
|
+
const li = el('li');
|
|
403
|
+
li.append(el('span'));
|
|
404
|
+
return li;
|
|
405
|
+
}
|
|
406
|
+
const tr = el('tr');
|
|
407
|
+
tr.setAttribute('role', 'row');
|
|
408
|
+
const th = el('th');
|
|
409
|
+
th.scope = 'rowgroup';
|
|
410
|
+
th.setAttribute('role', 'rowheader');
|
|
411
|
+
th.colSpan = Math.max(1, cols.length);
|
|
412
|
+
tr.append(th);
|
|
413
|
+
return tr;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
export function newTableRow(cols: Column[]): HTMLElement {
|
|
417
|
+
const tr = el('tr');
|
|
418
|
+
tr.setAttribute('role', 'row');
|
|
419
|
+
for (const c of cols) {
|
|
420
|
+
const td = el('td');
|
|
421
|
+
td.setAttribute('role', 'cell');
|
|
422
|
+
td.dataset['label'] = c.label;
|
|
423
|
+
tr.append(td);
|
|
424
|
+
}
|
|
425
|
+
return tr;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
export function newListItem(): HTMLElement {
|
|
429
|
+
const li = el('li');
|
|
430
|
+
const texts = el('span', 'tb-list-texts');
|
|
431
|
+
texts.append(el('span', 'tb-list-primary'), el('span', 'tb-list-secondary'));
|
|
432
|
+
li.append(texts);
|
|
433
|
+
return li;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export function fillRow(item: HTMLElement, row: Cell, cols: Column[], table: boolean): void {
|
|
437
|
+
const set = (e: Element, t: string): void => {
|
|
438
|
+
if (e.textContent !== t) e.textContent = t;
|
|
439
|
+
};
|
|
440
|
+
if (table) {
|
|
441
|
+
cols.forEach((c, i) => {
|
|
442
|
+
const v = rowField(row, c.sym);
|
|
443
|
+
const td = item.children[i]!;
|
|
444
|
+
if (c.image) {
|
|
445
|
+
td.classList.add('tb-thumb-cell');
|
|
446
|
+
return thumb(td, v, c.round);
|
|
447
|
+
}
|
|
448
|
+
set(td, cellText(v));
|
|
449
|
+
td.classList.toggle('tb-num', NUMERIC.has(v.t));
|
|
450
|
+
});
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
// a list shows its picture first, then its first column, and the others on a quieter line
|
|
454
|
+
const pic = cols.find(c => c.image);
|
|
455
|
+
if (pic !== undefined) thumb(item, rowField(row, pic.sym), pic.round);
|
|
456
|
+
else item.querySelector(':scope > .tb-thumb')?.remove();
|
|
457
|
+
const [first, ...rest] = cols.filter(c => !c.image).map(c => cellText(rowField(row, c.sym)));
|
|
458
|
+
const texts = item.querySelector(':scope > .tb-list-texts')!;
|
|
459
|
+
set(texts.children[0]!, first ?? '');
|
|
460
|
+
set(texts.children[1]!, rest.filter(Boolean).join(' · '));
|
|
461
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// --- times ---------------------------------------------------------------------------
|
|
2
|
+
//
|
|
3
|
+
// The list of a time field: the times from its min to its max, step minutes apart (30 unless told), as a
|
|
4
|
+
// listbox. It opens on the time it holds, or the next one from now. Arrows, Home, End and the Page keys move,
|
|
5
|
+
// Enter or Space picks, Esc closes.
|
|
6
|
+
|
|
7
|
+
import { K, string, type Cell, type ObjectCell } from '../../core/index.ts';
|
|
8
|
+
import { activeLanguage, amountOf } from '../index.ts';
|
|
9
|
+
import { get, own } from './faces.ts';
|
|
10
|
+
import { host, fire, el } from './mount.ts';
|
|
11
|
+
import { seenKeys } from './events.ts';
|
|
12
|
+
import { ruleArg, closeCalendar } from './calendar.ts';
|
|
13
|
+
|
|
14
|
+
export interface Times { face: ObjectCell; opener: HTMLButtonElement; list: HTMLElement }
|
|
15
|
+
export let times: Times | null = null;
|
|
16
|
+
|
|
17
|
+
/** The minutes of a time! cell since midnight, or null. */
|
|
18
|
+
export function minutesOf(v: Cell): number | null {
|
|
19
|
+
return v.t === K.Time ? Math.floor(Number(v.v) / 6e10) : null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function openTimes(face: ObjectCell, opener: HTMLButtonElement): void {
|
|
23
|
+
closeTimes(false);
|
|
24
|
+
closeCalendar(false);
|
|
25
|
+
const step = Math.max(1, Math.round(amountOf(get(face, 'step')) ?? 30));
|
|
26
|
+
const low = minutesOf(ruleArg(face, 'min')) ?? 0;
|
|
27
|
+
const high = minutesOf(ruleArg(face, 'max')) ?? 24 * 60 - 1;
|
|
28
|
+
const chosen = minutesOf(get(face, 'value'));
|
|
29
|
+
const now = new Date();
|
|
30
|
+
const aim = chosen ?? now.getHours() * 60 + now.getMinutes();
|
|
31
|
+
const fmt = new Intl.DateTimeFormat(activeLanguage(host.rt), { hour: 'numeric', minute: '2-digit', timeZone: 'UTC' });
|
|
32
|
+
const list = el('ul', 'tb-lookup-list tb-times');
|
|
33
|
+
list.setAttribute('role', 'listbox');
|
|
34
|
+
list.setAttribute('aria-label', opener.getAttribute('aria-label') ?? own('choose-time'));
|
|
35
|
+
list.setAttribute('popover', 'manual');
|
|
36
|
+
let first: HTMLElement | null = null;
|
|
37
|
+
for (let t = low; t <= high; t += step) {
|
|
38
|
+
const li = el('li', 'tb-lookup-option');
|
|
39
|
+
li.setAttribute('role', 'option');
|
|
40
|
+
li.setAttribute('aria-selected', t === chosen ? 'true' : 'false');
|
|
41
|
+
li.tabIndex = -1;
|
|
42
|
+
const at = `${String(Math.floor(t / 60)).padStart(2, '0')}:${String(t % 60).padStart(2, '0')}`;
|
|
43
|
+
li.textContent = fmt.format(t * 60000);
|
|
44
|
+
li.addEventListener('click', () => pickTime(at));
|
|
45
|
+
list.append(li);
|
|
46
|
+
if (first === null && t >= aim) first = li;
|
|
47
|
+
}
|
|
48
|
+
first ??= list.lastElementChild as HTMLElement | null;
|
|
49
|
+
if (first !== null) first.tabIndex = 0;
|
|
50
|
+
times = { face, opener, list };
|
|
51
|
+
opener.setAttribute('aria-expanded', 'true');
|
|
52
|
+
list.addEventListener('keydown', timesKey);
|
|
53
|
+
list.addEventListener('focusout', ev => {
|
|
54
|
+
if (times?.list === list && !list.contains(ev.relatedTarget as Node | null) && ev.relatedTarget !== null) closeTimes(false);
|
|
55
|
+
});
|
|
56
|
+
document.addEventListener('pointerdown', outsideTimes, true);
|
|
57
|
+
(opener.closest('dialog') ?? document.body).append(list);
|
|
58
|
+
list.showPopover?.();
|
|
59
|
+
const r = opener.closest('.tb-date')!.getBoundingClientRect();
|
|
60
|
+
list.style.width = `${Math.min(r.width, 240)}px`;
|
|
61
|
+
list.style.left = `${r.right - list.getBoundingClientRect().width}px`;
|
|
62
|
+
const height = list.getBoundingClientRect().height;
|
|
63
|
+
list.style.top = `${r.bottom + 4 + height > innerHeight - 8 ? Math.max(8, r.top - 4 - height) : r.bottom + 4}px`;
|
|
64
|
+
first?.scrollIntoView({ block: 'center' });
|
|
65
|
+
first?.focus();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function closeTimes(refocus = true): void {
|
|
69
|
+
if (times === null) return;
|
|
70
|
+
const { list, opener } = times;
|
|
71
|
+
times = null;
|
|
72
|
+
list.hidePopover?.();
|
|
73
|
+
list.remove();
|
|
74
|
+
opener.setAttribute('aria-expanded', 'false');
|
|
75
|
+
document.removeEventListener('pointerdown', outsideTimes, true);
|
|
76
|
+
if (refocus) opener.focus();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function outsideTimes(ev: PointerEvent): void {
|
|
80
|
+
if (times !== null && !times.list.contains(ev.target as Node) && !times.opener.contains(ev.target as Node)) closeTimes(false);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function pickTime(at: string): void {
|
|
84
|
+
const face = times?.face;
|
|
85
|
+
if (face === undefined) return;
|
|
86
|
+
closeTimes();
|
|
87
|
+
void fire('ui-input', face, string(at)).then(() => fire('ui-blur', face));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function timesKey(ev: KeyboardEvent): void {
|
|
91
|
+
if (times === null) return;
|
|
92
|
+
seenKeys.add(ev);
|
|
93
|
+
const items = [...times.list.children] as HTMLElement[];
|
|
94
|
+
const at = items.indexOf(ev.target as HTMLElement);
|
|
95
|
+
if (ev.key === 'Escape') {
|
|
96
|
+
ev.preventDefault();
|
|
97
|
+
ev.stopPropagation();
|
|
98
|
+
closeTimes();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (ev.key === 'Enter' || ev.key === ' ') {
|
|
102
|
+
ev.preventDefault();
|
|
103
|
+
(ev.target as HTMLElement).click();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const moves: Record<string, number> = { ArrowDown: at + 1, ArrowUp: at - 1, Home: 0, End: items.length - 1, PageDown: at + 8, PageUp: at - 8 };
|
|
107
|
+
if (!(ev.key in moves) || at < 0) return;
|
|
108
|
+
ev.preventDefault();
|
|
109
|
+
const to = items[Math.max(0, Math.min(items.length - 1, moves[ev.key]!))]!;
|
|
110
|
+
items[at]!.tabIndex = -1;
|
|
111
|
+
to.tabIndex = 0;
|
|
112
|
+
to.focus();
|
|
113
|
+
to.scrollIntoView({ block: 'nearest' });
|
|
114
|
+
}
|
|
115
|
+
|