@blueshed/joined 0.2.2 → 0.3.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.
Files changed (209) hide show
  1. package/.claude/skills/joined/SKILL.md +9 -1
  2. package/.claude/skills/joined/reference.md +267 -53
  3. package/.claude/skills/joined-door/SKILL.md +57 -0
  4. package/README.md +19 -50
  5. package/docs/design.md +76 -10
  6. package/docs/introduction.md +18 -18
  7. package/docs/light-and-shadow.svg +1 -1
  8. package/examples/README.md +20 -0
  9. package/examples/account/account.json +1 -0
  10. package/examples/account/derive.ts +41 -0
  11. package/examples/account/generated/elements.ts +52 -0
  12. package/examples/account/generated/rows.ts +22 -0
  13. package/examples/account/generated/rules.ts +32 -0
  14. package/examples/account/routes.ts +5 -0
  15. package/examples/app.css +2 -1
  16. package/examples/cafe/books.html +2 -2
  17. package/examples/cafe/cafe.css +9 -16
  18. package/examples/cafe/generated/books.rows.ts +22 -0
  19. package/examples/cafe/generated/books.rules.ts +3 -2
  20. package/examples/cafe/generated/kitchen.rows.ts +27 -0
  21. package/examples/cafe/generated/kitchen.rules.ts +11 -1
  22. package/examples/cafe/generated/menu.rows.ts +22 -0
  23. package/examples/cafe/generated/menu.rules.ts +3 -2
  24. package/examples/cafe/generated/order.rows.ts +27 -0
  25. package/examples/cafe/generated/order.rules.ts +11 -1
  26. package/examples/cafe/generated/staff.rows.ts +22 -0
  27. package/examples/cafe/generated/staff.rules.ts +6 -1
  28. package/examples/cafe/generated/supplies.queries.ts +4 -2
  29. package/examples/cafe/generated/supplies.rows.ts +22 -0
  30. package/examples/cafe/generated/supplies.rules.ts +12 -1
  31. package/examples/cafe/generated/till.queries.ts +2 -0
  32. package/examples/cafe/generated/till.rows.ts +27 -0
  33. package/examples/cafe/generated/till.rules.ts +11 -1
  34. package/examples/cafe/index.html +14 -15
  35. package/examples/cafe/kitchen.ts +1 -1
  36. package/examples/cafe/look.ts +30 -0
  37. package/examples/cafe/menu.html +10 -26
  38. package/examples/cafe/menu.ts +8 -80
  39. package/examples/cafe/order.html +6 -6
  40. package/examples/cafe/order.ts +5 -1
  41. package/examples/cafe/routes.ts +0 -1
  42. package/examples/cafe/stories.jsonl +3 -1
  43. package/examples/cafe/supplies.html +34 -34
  44. package/examples/cafe/supplies.ts +14 -11
  45. package/examples/cafe/till.ts +5 -1
  46. package/examples/chart/arches.ts +148 -0
  47. package/examples/chart/chart.css +6 -0
  48. package/examples/chart/chart.json +87 -0
  49. package/examples/chart/generated/elements.ts +105 -0
  50. package/examples/chart/generated/queries.ts +36 -0
  51. package/examples/chart/generated/rows.ts +27 -0
  52. package/examples/chart/generated/rules.ts +111 -0
  53. package/examples/chart/grid.ts +118 -0
  54. package/examples/chart/index.html +78 -0
  55. package/examples/chart/migrations/001-chart.ts +6 -0
  56. package/examples/chart/migrations/002-create-conditions.ts +6 -0
  57. package/examples/chart/page.ts +20 -0
  58. package/examples/chart/routes.ts +7 -0
  59. package/examples/chart/stories.jsonl +14 -0
  60. package/examples/chart/teeth.ts +57 -0
  61. package/examples/desk/generated/rows.ts +22 -0
  62. package/examples/desk/generated/rules.ts +34 -1
  63. package/examples/desk/index.html +9 -2
  64. package/examples/desk/routes.ts +0 -2
  65. package/examples/desk/stories.jsonl +3 -2
  66. package/examples/desk/thumbs.ts +2 -2
  67. package/examples/hello/generated/elements.ts +2 -2
  68. package/examples/hello/generated/rules.ts +3 -2
  69. package/examples/hello/hello.css +2 -4
  70. package/examples/hello/index.html +10 -3
  71. package/examples/hello/page.ts +4 -4
  72. package/examples/index.html +4 -5
  73. package/examples/notes/generated/elements.ts +112 -0
  74. package/examples/notes/generated/queries.ts +36 -0
  75. package/examples/notes/generated/rows.ts +27 -0
  76. package/examples/notes/generated/rules.ts +133 -0
  77. package/examples/notes/index.html +32 -0
  78. package/examples/notes/look.ts +41 -0
  79. package/examples/notes/migrations/001-notes.ts +7 -0
  80. package/examples/notes/notes.json +78 -0
  81. package/examples/notes/page.ts +21 -0
  82. package/examples/notes/routes.ts +6 -0
  83. package/examples/notes/stories.jsonl +21 -0
  84. package/examples/people/generated/rows.ts +22 -0
  85. package/examples/people/generated/rules.ts +11 -1
  86. package/examples/people/links.ts +2 -1
  87. package/examples/people/routes.ts +1 -3
  88. package/examples/people/stories.jsonl +1 -0
  89. package/examples/people/view.ts +5 -2
  90. package/examples/trip/generated/elements.ts +4 -2
  91. package/examples/trip/generated/queries.ts +12 -1
  92. package/examples/trip/generated/rows.ts +27 -0
  93. package/examples/trip/generated/rules.ts +56 -2
  94. package/examples/trip/index.html +58 -12
  95. package/examples/trip/line.css +186 -0
  96. package/examples/trip/line.ts +51 -40
  97. package/examples/trip/page.ts +65 -71
  98. package/examples/trip/routes.ts +9 -5
  99. package/examples/trip/stays.css +12 -12
  100. package/examples/trip/stays.ts +15 -10
  101. package/examples/trip/stories.jsonl +2 -0
  102. package/examples/trip/told.ts +3 -26
  103. package/examples/trip/trip.css +16 -0
  104. package/examples/trips/generated/rows.ts +22 -0
  105. package/examples/trips/generated/rules.ts +9 -1
  106. package/examples/trips/index.html +10 -10
  107. package/examples/trips/map.ts +3 -14
  108. package/examples/trips/new-trip.ts +14 -16
  109. package/examples/weeks/generated/elements.ts +1 -1
  110. package/examples/weeks/generated/queries.ts +11 -0
  111. package/examples/weeks/generated/rows.ts +22 -0
  112. package/examples/weeks/generated/rules.ts +11 -5
  113. package/examples/weeks/routes.ts +3 -2
  114. package/examples/weeks/stories.jsonl +1 -0
  115. package/examples/welcome.css +7 -5
  116. package/package.json +8 -2
  117. package/src/awesome.ts +103 -0
  118. package/src/board.css +75 -0
  119. package/src/board.ts +173 -0
  120. package/src/cli.ts +15 -2
  121. package/src/compile.ts +569 -35
  122. package/src/definitions.ts +53 -15
  123. package/src/draft.ts +38 -23
  124. package/src/drawer.ts +222 -0
  125. package/src/engine.ts +240 -166
  126. package/src/fields.ts +268 -0
  127. package/src/files.ts +6 -4
  128. package/src/ground.ts +110 -0
  129. package/src/icon-names.ts +15 -2
  130. package/src/kit.ts +51 -0
  131. package/src/link.ts +3 -3
  132. package/src/map.css +133 -0
  133. package/src/map.ts +200 -0
  134. package/src/mcp.ts +106 -22
  135. package/src/members/demo.ts +13 -12
  136. package/src/members/generated/rows.ts +22 -0
  137. package/src/members/generated/rules.ts +3 -2
  138. package/src/members/group.ts +65 -39
  139. package/src/members/keys.ts +93 -77
  140. package/src/members/panel.css +23 -17
  141. package/src/members/panel.ts +49 -35
  142. package/src/migrate.ts +120 -104
  143. package/src/month.css +125 -0
  144. package/src/month.ts +179 -0
  145. package/src/page.css +8 -0
  146. package/src/page.ts +7 -4
  147. package/src/parts.css +462 -170
  148. package/src/parts.ts +209 -26
  149. package/src/plane.css +56 -0
  150. package/src/plane.ts +151 -0
  151. package/src/postgres-server.ts +60 -0
  152. package/src/reading.css +77 -0
  153. package/src/reading.ts +238 -0
  154. package/src/rows.css +308 -0
  155. package/src/rows.ts +780 -0
  156. package/src/search.css +80 -0
  157. package/src/search.ts +130 -0
  158. package/src/server.ts +205 -114
  159. package/src/settings.ts +9 -4
  160. package/src/sheet.ts +114 -0
  161. package/src/skill.ts +40 -0
  162. package/src/slots.css +94 -0
  163. package/src/slots.ts +213 -0
  164. package/src/specs.ts +38 -0
  165. package/src/starter.ts +5 -2
  166. package/src/store.ts +184 -143
  167. package/src/stories.ts +43 -11
  168. package/src/surface.ts +159 -0
  169. package/src/timeline.css +111 -0
  170. package/src/timeline.ts +226 -0
  171. package/src/toast.ts +21 -48
  172. package/src/together.ts +0 -7
  173. package/src/tokens.css +78 -1
  174. package/src/tokens.json +249 -0
  175. package/src/tool.ts +211 -0
  176. package/src/upkeep.ts +18 -20
  177. package/src/views.css +44 -0
  178. package/src/views.ts +94 -0
  179. package/src/week.css +151 -0
  180. package/src/week.ts +250 -0
  181. package/src/write.ts +202 -69
  182. package/starter/CLAUDE.md +3 -3
  183. package/starter/bunfig +4 -0
  184. package/starter/prettierignore +4 -0
  185. package/starter/tests/hello.test.ts +2 -0
  186. package/docs/learning.md +0 -76
  187. package/examples/cafe/trust.ts +0 -20
  188. package/examples/desk/trust.ts +0 -24
  189. package/examples/people/trust.ts +0 -14
  190. package/examples/trip/answer-rows.ts +0 -70
  191. package/examples/trip/bar.ts +0 -89
  192. package/examples/trip/calendar-map.css +0 -711
  193. package/examples/trip/calendar-map.ts +0 -249
  194. package/examples/trip/drags.ts +0 -143
  195. package/examples/trip/drawer.ts +0 -250
  196. package/examples/trip/event-form.ts +0 -200
  197. package/examples/trip/holding.ts +0 -61
  198. package/examples/trip/map.ts +0 -234
  199. package/examples/trip/month.ts +0 -87
  200. package/examples/trip/other-weeks.ts +0 -67
  201. package/examples/trip/peek.ts +0 -71
  202. package/examples/trip/people-chips.ts +0 -74
  203. package/examples/trip/shell.ts +0 -39
  204. package/examples/trip/trust.ts +0 -51
  205. package/examples/trip/week.ts +0 -237
  206. package/examples/trip/writes.ts +0 -91
  207. package/examples/trip/zones.ts +0 -16
  208. package/examples/weeks/derive.ts +0 -23
  209. package/src/scrim.ts +0 -81
package/src/rows.ts ADDED
@@ -0,0 +1,780 @@
1
+ // A collection drawn as rows (joined/rows): the display the compiler gives every collection, as its app's
2
+ // generated/rows.ts says it, until the app draws one of its own. Nothing is guessed as it runs: which field names an
3
+ // item, how each is held and which collection is under which are the compiler's, read off the stories and their
4
+ // definitions.
5
+ //
6
+ // A row is its item's title, where it's got to beside it (a field that moves), and under it what else it holds, said
7
+ // as each reads: a number, a time, what a link is to. A yes or no is a tick at its start, kept at once. At its end, what its person may do there: a list
8
+ // that's each member's own, pressed to be in it, with the faces of those who are; a field that moves, its next
9
+ // steps. Tapped, its sheet (<j-sheet>, ./sheet.ts). It's a list: read as a board or a month, it's
10
+ // held in <j-views> beside <j-board> and <j-month>, as the kit's readings. A collection whose items are each of one of this one's (an option's
11
+ // question) is drawn under it, where the page puts its element in too: each item with its own beneath, and a way to
12
+ // add one there.
13
+ //
14
+ // It's an app's to take as it is, to change or to build on (the skill's §5): a page defines the class the compiler
15
+ // exports, or one extending it, by define(); each part of a row is a method to override (label, named, says, facts,
16
+ // ticks, deeds, row, step, asks), and each part of what's drawn is a part to style (j-options-rows::part(title)).
17
+
18
+ import { follow, held } from "./follow.ts";
19
+ import { icon, iconButton } from "./icons.ts";
20
+ import "./kit.ts";
21
+ import { keep, type Row } from "./keep.ts";
22
+ import { signedIn } from "./members/panel.ts";
23
+ import type { Type } from "./ops.ts";
24
+ import type { Answer } from "./page.ts";
25
+ import { reads, type Asked } from "./fields.ts";
26
+ import { mapsLink } from "./geo.ts";
27
+ import { adopt, chip, face, mention, peek, place, said, tick } from "./parts.ts";
28
+ import { el, type Drawer } from "./drawer.ts";
29
+ import { found, wordsOf } from "./search.ts";
30
+ import { askable, titled } from "./sheet.ts";
31
+ import { kindOf, use } from "./specs.ts";
32
+ import { dated, dayOf, lasts, local } from "./time.ts";
33
+ import STYLE from "./rows.css" with { type: "text" };
34
+
35
+ /** A field as the compiler says it: its type, whether an add needs it, and how it's held: each member's own list
36
+ * (`with` the lists none is in twice), one that moves by its steps, the server's to fill, a link to one item (`to`:
37
+ * its element, and the attribute that names it), or a list of ids (`to`, where it's of people). */
38
+ export type Field = {
39
+ name: string;
40
+ type: Type;
41
+ needed?: true;
42
+ how?: "mine" | "moves" | "filled" | "one" | "ids";
43
+ /** A link that's a reference (never structure): it pops what it points at. */
44
+ ref?: true;
45
+ /** Where a field that moves may be, in the order the stories take it there. */
46
+ steps?: string[];
47
+ with?: string[];
48
+ to?: { tag: string; title?: string; people?: true };
49
+ };
50
+ /** A collection as rows: its document, its element and its items', what an item is called, the field that names
51
+ * one, and the field saying which item of another collection it's under (`under`, that collection's name). */
52
+ export type Spec = {
53
+ doc: string;
54
+ name: string;
55
+ place: string;
56
+ tag: string;
57
+ one: string;
58
+ title?: string;
59
+ under?: { field: string; in: string };
60
+ /** Its items are each one member's own value for the item they're under (definitions' mine): never rows. */
61
+ mine?: { of: string; by: string };
62
+ fields: Field[];
63
+ };
64
+ /** An item's element, as the compiler makes it. */
65
+ export type Item = HTMLElement & {
66
+ key: number;
67
+ path: string;
68
+ data: Record<string, any>;
69
+ replace?(fields: object): Promise<Answer>;
70
+ removeField?(...fields: string[]): Promise<Answer>;
71
+ removeItem?(): Promise<Answer>;
72
+ };
73
+
74
+ const SHEET = new CSSStyleSheet();
75
+ SHEET.replaceSync(STYLE);
76
+ const TIME: Intl.DateTimeFormatOptions = {
77
+ weekday: "short",
78
+ day: "numeric",
79
+ month: "short",
80
+ hour: "2-digit",
81
+ minute: "2-digit",
82
+ };
83
+
84
+ /** The rows of a collection as its spec says it, with those under it (`nested`), for a page to define (define()), as
85
+ * it is or extended. */
86
+ export function rows(spec: Spec, items: Record<string, CustomElementConstructor>, ...nested: Spec[]) {
87
+ const specs = [spec, ...nested],
88
+ linked = specs.some((s) => s.fields.some((f) => f.to));
89
+ return class Rows extends HTMLElement {
90
+ static tag = `${spec.place}-rows`;
91
+ /** Each item's element, as it is: defined by define() unless the page has given it a view. */
92
+ static items = items;
93
+ /** Sheets of an app's own, adopted after joined's: a class extending this says them. */
94
+ static sheets: CSSStyleSheet[] = [];
95
+ /** The collections it draws, given to the kit (joined/kit) by define(): a sheet or a search finds them by name. */
96
+ static specs = specs;
97
+ root = this.attachShadow({ mode: "open" });
98
+ /** Who the page is signed in as, once the group has said. */
99
+ me?: number;
100
+ /** Whose order it's in: mine (0), another member's, or everyone's. Said in the address. */
101
+ whose: number | "all" = 0;
102
+ #whose = el("div", { className: "whose", part: "whose" });
103
+ #list = el("ul", { part: "list" });
104
+ #none = el("p", { className: "none", part: "none" }, `No ${spec.name} yet.`);
105
+ #add = this.adder(spec);
106
+ #said = el("p", { className: "said", role: "status" });
107
+ #kept = new Map<string, { shows: string; el: HTMLElement }>();
108
+ /** What's under each item, by its key: its list, kept as the item's row is made again. */
109
+ #under = new Map<string, { list: HTMLElement; kept: Map<string, { shows: string; el: HTMLElement }> }[]>();
110
+ #watch = new MutationObserver(() => this.draw());
111
+ #away = new AbortController();
112
+ /** The words a search of its collection (<j-search>) looks for: its rows without every one aren't shown. */
113
+ #words: string[] = [];
114
+
115
+ connectedCallback() {
116
+ if (!this.root.childElementCount) {
117
+ adopt(this.root, SHEET, ...(this.constructor as typeof Rows).sheets);
118
+ this.root.append(this.#whose, this.#list, this.#none, this.#add, this.#said);
119
+ this.#grips();
120
+ // in whose order, as its address says
121
+ const order = new URLSearchParams(location.search).get("order");
122
+ this.whose = order === "all" ? "all" : Number(order) || 0;
123
+ }
124
+ this.#away = new AbortController();
125
+ // its own items; and, where one links to another, the page's, wherever that collection is
126
+ this.#watch.observe(linked ? document.body : this, { childList: true, subtree: true, attributes: true });
127
+ signedIn((me) => ((this.me = me), this.draw()), this.#away.signal);
128
+ document.addEventListener(
129
+ "j-search",
130
+ (e) => {
131
+ const d = (e as CustomEvent).detail;
132
+ if (d.for === spec.name) ((this.#words = d.words), this.draw());
133
+ },
134
+ { signal: this.#away.signal },
135
+ );
136
+ document.addEventListener(
137
+ "j-open",
138
+ (e) => specs.some((s) => s.name === (e as CustomEvent).detail.for) && this.draw(),
139
+ { signal: this.#away.signal },
140
+ );
141
+ // a search that was there first (its words from the address) has said them already: they're read from it
142
+ const search = document.querySelector<HTMLElement & { words?: string[] }>(`j-search[for="${spec.name}"]`);
143
+ if (search)
144
+ this.#words =
145
+ search.words ?? wordsOf(new URLSearchParams(location.search).get(search.getAttribute("param") ?? "q") ?? "");
146
+ this.draw();
147
+ }
148
+ disconnectedCallback() {
149
+ this.#watch.disconnect();
150
+ this.#away.abort();
151
+ }
152
+
153
+ // what an app may override: each is asked of an item of this collection, or of one under it
154
+
155
+ /** The collection an item (or a field's own) is of. */
156
+ specOf(it: Element) {
157
+ return specs.find((s) => s.tag === it.localName)!;
158
+ }
159
+ /** What the page says of a field (its <j-field> in its collection's sheet, joined/kit). */
160
+ kind(f: Field): Asked {
161
+ const s = specs.find((s) => s.fields.includes(f));
162
+ return { ...f, ...(s ? kindOf(s.name, f.name) : {}) };
163
+ }
164
+ /** What a field is called: what its <j-field> says, else its name, as a label is written. A class extending this
165
+ * says its own words. */
166
+ label(f: Field) {
167
+ return this.kind(f).label ?? (f.name[0]!.toUpperCase() + f.name.slice(1)).replaceAll("-", " ");
168
+ }
169
+ /** What an item is called. */
170
+ named(it: Element) {
171
+ return titled(it, this.specOf(it)?.title);
172
+ }
173
+ /** How one field of an item reads in its row, or nothing where it has none. */
174
+ says(it: Item, f: Field): string | Node | null {
175
+ const v = it.data[f.name],
176
+ label = this.label(f);
177
+ if (v === undefined || v === "" || v === false || (Array.isArray(v) && !v.length)) return null;
178
+ // a yes or no this person may change is a tick at the row's start (ticks), never a word; the server's own, a word
179
+ if (f.type === "boolean") return f.how ? label : null;
180
+ const read = reads(this.kind(f), v);
181
+ if (read) return `${label} ${read}`;
182
+ if (f.type === "integer" && !f.to) return `${label} ${v.toLocaleString()}`;
183
+ if (f.type === "decimal") return `${label} ${v.toLocaleString()}`;
184
+ // a time is said under the words, where it pops its month (pops): never among the facts
185
+ if (f.type === "date-time" || f.type === "instant") return null;
186
+ if (f.type === "duration") return `${label} ${lasts(v)}`;
187
+ if (f.type === "place" || f.type === "file") return null;
188
+ // a reference to one thing, or to several that aren't each member's own to be in: said where it pops (pops)
189
+ if (f.ref && f.how !== "mine" && f.how !== "filled") return null;
190
+ // words say themselves; anything else, what it's of
191
+ if (!f.to) return f.type === "string" ? String(v) : `${label} ${v}`;
192
+ // what it links to: by its title; people, by their faces
193
+ const of = (key: number) => document.querySelector(`${f.to!.tag}[key="${key}"]`);
194
+ if (f.how !== "ids") return `${label} ${titled(of(v), f.to.title)}`;
195
+ if (!f.to.people) return `${label} ${v.length}`;
196
+ return el("span", { className: "faces" }, `${label} `, ...v.map((k: number) => face(k, titled(of(k)))));
197
+ }
198
+ /** What a row says of an item under its title: each field it has, but its title, what's said at its end, and the
199
+ * item it's drawn under. */
200
+ facts(it: Item): (string | Node)[] {
201
+ const s = this.specOf(it),
202
+ under = nested.includes(s) ? s.under?.field : undefined;
203
+ return s.fields
204
+ .filter((f) => f.name !== s.title && f.name !== under && f.how !== "mine" && f.how !== "moves")
205
+ .flatMap((f) => this.says(it, f) ?? []);
206
+ }
207
+ /** An item's ticks, at its row's start: each yes or no it holds, pressed where it's yes. A pick, so kept at once
208
+ * (docs/design.md, "When a change is kept"): pressed, it's ticked; again, the tick is taken off (the field
209
+ * removed, where its person may, else said no). One this person may not change is shown where it's yes, and
210
+ * can't be pressed. */
211
+ ticks(it: Item): Node[] {
212
+ const may = (it.getAttribute("may") ?? "").split(" ");
213
+ return this.specOf(it)
214
+ .fields.filter((f) => f.type === "boolean" && !f.how)
215
+ .flatMap((f) => {
216
+ const on = it.data[f.name] === true,
217
+ off = may.includes(`remove:${f.name}`),
218
+ can = may.includes(`replace:${f.name}`) || (on && off);
219
+ if (!can && !on) return [];
220
+ const t = tick(() => this.said(this.yes(it, f, !on)));
221
+ t.set(on, `${this.named(it)}: ${this.label(f).toLowerCase()}`);
222
+ t.el.disabled = !can;
223
+ t.el.setAttribute("part", "tick");
224
+ return [t.el];
225
+ });
226
+ }
227
+ /** How a time reads where it's said: its day, and its hour unless it's the day alone (nine, a day's own). */
228
+ timed(v: string, f: Field) {
229
+ const at = f.type === "instant" ? local(Date.parse(v)) : v,
230
+ day = dated(dayOf(at), { weekday: "short", day: "numeric", month: "short" });
231
+ return at.slice(11, 16) === "09:00" ? day : `${day}, ${at.slice(11, 16)}`;
232
+ }
233
+ /** What of an item pops (docs/design.md, "A peek"): each value with a view of its own, said under its words as a
234
+ * mention. A time pops its month, small, that day marked; the pop's button opens the page's month there. */
235
+ pops(it: Item): Node[] {
236
+ const s = this.specOf(it),
237
+ under = nested.includes(s) ? s.under?.field : undefined,
238
+ out: Node[] = [];
239
+ const made = (words: string | (string | Node)[], pop: (by: HTMLElement) => void) => {
240
+ const m = mention(pop);
241
+ if (typeof words === "string") m.say(words);
242
+ else m.el.append(...words);
243
+ m.el.setAttribute("part", "pop");
244
+ out.push(m.el);
245
+ };
246
+ for (const f of s.fields) {
247
+ const v = it.data[f.name];
248
+ if (v === undefined || v === "" || f.name === under || f.how === "mine") continue;
249
+ if (f.type === "date-time" || f.type === "instant") {
250
+ const at = f.type === "instant" ? local(Date.parse(v)) : (v as string);
251
+ made(f.how === "filled" ? `${this.label(f)} ${this.timed(v, f)}` : this.timed(v, f), (by) =>
252
+ this.pop(it, f, at.slice(0, 10), by),
253
+ );
254
+ }
255
+ // a place: a small map of it; whole, it's the maps app's
256
+ else if (f.type === "place")
257
+ made(this.label(f), (by) => {
258
+ const link = mapsLink(v);
259
+ this.popped(
260
+ by,
261
+ `${this.named(it)}: ${this.label(f).toLowerCase()}`,
262
+ [place(v)],
263
+ link ? "Open in Maps" : null,
264
+ () => open(link!, "_blank", "noopener"),
265
+ );
266
+ });
267
+ // a reference: what it points at, each a mention; popped, that thing as its own row says it
268
+ else if (f.ref && f.how !== "filled")
269
+ for (const key of [v].flat() as number[]) {
270
+ const to = document.querySelector<Item>(`${f.to!.tag}[key="${key}"]`),
271
+ name = titled(to, f.to!.title);
272
+ made(f.to!.people ? [face(key, name), name] : f.how === "ids" ? name : `${this.label(f)} ${name}`, (by) => {
273
+ if (to) this.refer(to, f, by);
274
+ });
275
+ }
276
+ }
277
+ return out;
278
+ }
279
+ /** A reference popped: the thing it points at, as its own row says it (its name, and what else it holds); its
280
+ * button opens it whole, where it's one of these rows' and this person may. */
281
+ refer(to: Item, f: Field, by: HTMLElement) {
282
+ const s = specs.find((x) => x.tag === to.localName),
283
+ may = (to.getAttribute("may") ?? "").split(" "),
284
+ facts = s ? this.facts(to).map((x) => el("p", {}, x)) : [];
285
+ const opens = !!s && (may.includes("remove") || this.asks(s).some((a) => may.includes(`replace:${a.name}`)));
286
+ this.popped(
287
+ by,
288
+ titled(to, f.to!.title),
289
+ facts.length
290
+ ? facts
291
+ : [el("p", {}, f.to!.people ? "One of the group." : `A ${s?.one ?? this.label(f).toLowerCase()}.`)],
292
+ opens ? "Open" : null,
293
+ () => this.drawer(s!).show(to),
294
+ );
295
+ }
296
+ /** A step a field may take from where it is, as its button says it. */
297
+ step(_f: Field, to: string) {
298
+ return `Mark ${to}`;
299
+ }
300
+ /** What a person may do to an item from its row: each list that's theirs to be in, each step it may take. */
301
+ deeds(it: Item): Node[] {
302
+ const may = (it.getAttribute("may") ?? "").split(" "),
303
+ out: Node[] = [];
304
+ for (const f of this.specOf(it).fields) {
305
+ // a list each member's own: who's in it, how many, yours pressed; pressed, given (the others taken back) or taken back
306
+ if (f.how === "mine") {
307
+ const ids: number[] = it.data[f.name] ?? [],
308
+ mine = this.me !== undefined && ids.includes(this.me),
309
+ who = ids.map((k) => titled(document.querySelector(`${f.to?.tag}[key="${k}"]`)));
310
+ const b = el("button", { type: "button", className: "act small mine", part: "mine" }, this.label(f));
311
+ b.append(...ids.map((k, i) => face(k, who[i]!)), el("b", {}, String(ids.length)));
312
+ b.ariaPressed = String(mine);
313
+ b.title = `${this.label(f)}: ${who.join(", ") || "nobody yet"}`;
314
+ b.disabled = this.me === undefined || !may.includes(`add:${f.name}[]`);
315
+ b.addEventListener("click", () => this.said(this.send(it, this.mine(it, f, mine))));
316
+ out.push(b);
317
+ }
318
+ // a field that moves: each step this person may make from where it is
319
+ if (f.how === "moves")
320
+ for (const m of may.filter((m) => m.startsWith(`replace:${f.name}>`))) {
321
+ const to = decodeURIComponent(m.slice(m.indexOf(">") + 1));
322
+ const b = el("button", { type: "button", className: "act small", part: "step" }, this.step(f, to));
323
+ b.addEventListener("click", () => this.said(it.replace!({ [f.name]: to })));
324
+ out.push(b);
325
+ }
326
+ }
327
+ return out;
328
+ }
329
+ /** An item's row: its words, which open its drawer where there's something to do there, then its deeds. */
330
+ row(it: Item): HTMLElement {
331
+ const s = this.specOf(it),
332
+ may = (it.getAttribute("may") ?? "").split(" "),
333
+ opens = may.includes("remove") || this.asks(s).some((f) => may.includes(`replace:${f.name}`));
334
+ const name = el("span", { className: "name", part: "title" }, this.named(it));
335
+ for (const f of s.fields.filter((f) => f.how === "moves" && it.data[f.name] !== undefined))
336
+ name.append(el("span", { className: "state", part: "state" }, String(it.data[f.name])));
337
+ const facts = this.facts(it).flatMap((f, i) => (i ? [" · ", f] : [f]));
338
+ const said = el("span", { className: "said-of" }, name);
339
+ if (facts.length) said.append(el("small", { part: "facts" }, ...facts));
340
+ // its words open its drawer, and say so (a chevron); the one whose drawer is open is marked (design.md, "A row")
341
+ const words = el(opens ? "button" : "div", { className: "open", part: "open" }, said);
342
+ if (opens) (((words as HTMLButtonElement).type = "button"), words.append(icon("on")));
343
+ if (this.opened(it)) words.ariaCurrent = "true";
344
+ words.addEventListener("click", () => opens && this.drawer(s).show(it));
345
+ // under its words: what of it pops, each a mention; and what else is said there (under())
346
+ const pops = [...this.pops(it), ...this.below(it)],
347
+ col = el(
348
+ "div",
349
+ { className: "col" },
350
+ words,
351
+ ...(pops.length ? [el("div", { className: "pops" }, ...pops)] : []),
352
+ );
353
+ // its ticks stand before its words, in the words' column: the row's deeds keep theirs
354
+ const ticks = this.ticks(it);
355
+ return el(
356
+ "div",
357
+ { className: "row", part: "row" },
358
+ ticks.length ? el("div", { className: "ticked" }, ...ticks, col) : col,
359
+ el("span", { className: "deeds", part: "deeds" }, ...this.deeds(it), ...this.grip(it)),
360
+ );
361
+ }
362
+ /** Whether an item is the one its drawer holds. */
363
+ opened(it: Item) {
364
+ const s = this.specOf(it);
365
+ return this.sheet(s).drawer?.open === it;
366
+ }
367
+ /** An item's grip, where the list is in my own order: one to drag it up or down that. */
368
+ grip(it: Item): Node[] {
369
+ if (this.specOf(it) !== spec) return [];
370
+ const made = (kind: string, words: string) => {
371
+ const g = iconButton("grip", words);
372
+ g.classList.add("grip", kind);
373
+ g.setAttribute("part", "grip");
374
+ return g;
375
+ };
376
+ return this.orderBy() && this.me !== undefined && this.shown() === this.me
377
+ ? [made("order", `Move ${this.named(it)} in my order`)]
378
+ : [];
379
+ }
380
+ /** What else is said under an item's words, after what pops: nothing, unless a reading says so. */
381
+ below(it: Item): Node[] {
382
+ if (this.specOf(it) !== spec || !this.orderBy() || this.shown() !== "all") return [];
383
+ const at = [...(this.places().get(it.key) ?? [])];
384
+ if (!at.length) return [];
385
+ return [
386
+ el(
387
+ "span",
388
+ { className: "places j-chips", part: "places" },
389
+ ...at.map(([by, i]) => chip(by, this.who(by), `${by === this.me ? "Me" : this.who(by)} ${i}`)),
390
+ ),
391
+ ];
392
+ }
393
+ /** The fields a collection's drawer asks for, in their order: the one that names an item first; never what
394
+ * the server fills, a list, or a field that moves by its steps. */
395
+ asks(s: Spec): Field[] {
396
+ return askable(s);
397
+ }
398
+
399
+ /** Its items, as the document has them. */
400
+ items() {
401
+ return [...(this.getElementsByTagName(spec.tag) as HTMLCollectionOf<Item>)];
402
+ }
403
+ /** A yes or no given, or taken off (the field removed, where its person may, else said no). */
404
+ yes(it: Item, f: Field, on: boolean) {
405
+ const may = (it.getAttribute("may") ?? "").split(" ");
406
+ return on
407
+ ? it.replace!({ [f.name]: true })
408
+ : may.includes(`remove:${f.name}`)
409
+ ? this.send(it, [{ op: "remove", path: `${it.path}/${f.name}` }])
410
+ : it.replace!({ [f.name]: false });
411
+ }
412
+ #address(name: string, value: string | null) {
413
+ const url = new URL(location.href);
414
+ if (value === null) url.searchParams.delete(name);
415
+ else url.searchParams.set(name, value);
416
+ history.replaceState(history.state, "", url);
417
+ }
418
+
419
+ // whose order it's in: where a collection under it is each member's own number for an item
420
+
421
+ /** The collection that's each member's own place for an item of this one (definitions' mine, its value a number),
422
+ * where the page put its element in: its spec, and the fields that say which item, whose, and where. */
423
+ orderBy() {
424
+ for (const s of nested) {
425
+ const value = s.mine && s.fields.find((f) => (f.type === "decimal" || f.type === "integer") && !f.how);
426
+ if (s.mine && value && this.querySelector(s.place))
427
+ return { s, of: s.mine.of, by: s.mine.by, value: value.name };
428
+ }
429
+ return undefined;
430
+ }
431
+ /** A member's place for an item, if they've said one; and their order: the items by their places, the rest after,
432
+ * as they were added. */
433
+ placeOf(by: number, key: number) {
434
+ const o = this.orderBy()!;
435
+ return [...(this.getElementsByTagName(o.s.tag) as HTMLCollectionOf<Item>)].find(
436
+ (p) => p.data[o.by] === by && p.data[o.of] === key,
437
+ );
438
+ }
439
+ orderOf(by: number) {
440
+ const o = this.orderBy()!,
441
+ at = (it: Item) => (this.placeOf(by, it.key)?.data[o.value] as number | undefined) ?? 1e6 + it.key;
442
+ return this.items().sort((a, b) => at(a) - at(b));
443
+ }
444
+ /** Everyone who has an order of their own, me first; and what a member is called. */
445
+ people(): number[] {
446
+ const o = this.orderBy()!,
447
+ theirs = [...(this.getElementsByTagName(o.s.tag) as HTMLCollectionOf<Item>)].map((p) => p.data[o.by] as number);
448
+ return [...new Set([this.me ?? 0, ...theirs])].filter(Boolean);
449
+ }
450
+ who(by: number) {
451
+ const tag = this.orderBy()!.s.fields.find((f) => f.name === this.orderBy()!.by)?.to?.tag;
452
+ return titled(tag ? document.querySelector(`${tag}[key="${by}"]`) : null);
453
+ }
454
+ /** Whose order is shown: mine, a member's, or everyone's. */
455
+ shown(): number | "all" {
456
+ return this.whose === "all" ? "all" : this.whose || (this.me ?? 0);
457
+ }
458
+ /** Where each person puts each item, first 1: by item, then by person. */
459
+ places() {
460
+ const at = new Map<number, Map<number, number>>();
461
+ for (const by of this.people())
462
+ this.orderOf(by).forEach((it, i) => (at.get(it.key) ?? at.set(it.key, new Map()).get(it.key)!).set(by, i + 1));
463
+ return at;
464
+ }
465
+ /** The items in the order shown: a member's, or everyone's (each by where people put it, on average). */
466
+ ordered(its: Item[]) {
467
+ if (!this.orderBy()) return its;
468
+ const shown = this.shown();
469
+ if (shown !== "all") return this.orderOf(shown);
470
+ const at = this.places(),
471
+ mean = (it: Item) => {
472
+ const all = [...(at.get(it.key)?.values() ?? [])];
473
+ return all.reduce((a, b) => a + b, 0) / (all.length || 1);
474
+ };
475
+ return [...its].sort((a, b) => mean(a) - mean(b));
476
+ }
477
+ /** Whose order is shown, said in the address. */
478
+ order(by: number | "all") {
479
+ this.whose = by === this.me ? 0 : by;
480
+ this.#address("order", this.whose ? String(this.whose) : null);
481
+ this.draw();
482
+ }
483
+ /** An item given a place in my order: `at` among the others, between those it then sits between. A drag, so
484
+ * kept at once. */
485
+ place(it: Item, at: number) {
486
+ const o = this.orderBy()!,
487
+ me = this.me!,
488
+ others = this.orderOf(me).filter((i) => i !== it),
489
+ rank = (i: Item) => (this.placeOf(me, i.key)?.data[o.value] as number | undefined) ?? 1e6 + i.key;
490
+ const [before, after] = [others[at - 1], others[at]];
491
+ if (!before && !after) return;
492
+ const value = before && after ? (rank(before) + rank(after)) / 2 : before ? rank(before) + 1 : rank(after!) - 1;
493
+ const mine = this.placeOf(me, it.key);
494
+ this.said(
495
+ mine
496
+ ? mine.replace!({ [o.value]: value })
497
+ : this.send(this.querySelector(o.s.place)!, [
498
+ { op: "add", path: `/${o.s.name}/-`, value: { [o.of]: it.key, [o.value]: value } },
499
+ ]),
500
+ );
501
+ }
502
+ /** A row's grip: pressed (held, by a finger), its row follows the pointer and, let go, is placed where it is; and
503
+ * by the arrow keys, a place at a time. */
504
+ #grips() {
505
+ const rowsNow = () =>
506
+ [...this.#list.children].filter((li): li is HTMLElement => !!(li as HTMLElement).dataset.key),
507
+ itemOf = (li: HTMLElement) => this.items().find((i) => String(i.key) === li.dataset.key)!;
508
+ this.#list.addEventListener("pointerdown", (down) => {
509
+ const grip = (down.target as Element).closest?.(".grip"),
510
+ li = grip?.closest("li") as HTMLElement | null;
511
+ if (!grip || !li?.dataset.key) return;
512
+ held(down, () => {
513
+ const all = rowsNow(),
514
+ tops = all.map((r) => r.getBoundingClientRect().top);
515
+ let at = all.indexOf(li);
516
+ li.toggleAttribute("dragged", true);
517
+ follow(
518
+ down,
519
+ (e) => {
520
+ li.style.translate = `0 ${e.clientY - down.clientY}px`;
521
+ at = Math.max(
522
+ 0,
523
+ tops.findLastIndex((t) => t <= e.clientY),
524
+ );
525
+ },
526
+ (e) => {
527
+ li.style.translate = "";
528
+ li.toggleAttribute("dragged", false);
529
+ if (e && at !== all.indexOf(li)) this.place(itemOf(li), at);
530
+ },
531
+ );
532
+ });
533
+ });
534
+ this.#list.addEventListener("keydown", (e) => {
535
+ const grip = (e.target as Element).closest?.(".grip"),
536
+ li = grip?.closest("li") as HTMLElement | null;
537
+ if (!grip || !li?.dataset.key) return;
538
+ const by = e.key === "ArrowUp" ? -1 : e.key === "ArrowDown" ? 1 : 0;
539
+ if (!by) return;
540
+ e.preventDefault();
541
+ const all = rowsNow(),
542
+ at = all.indexOf(li) + by;
543
+ if (at >= 0 && at < all.length) this.place(itemOf(li), at);
544
+ });
545
+ }
546
+
547
+ // what it's made of
548
+
549
+ /** The pop (Peek): one for the rows. What it shows, and what its button opens whole, are each pop's own. */
550
+ #month = el("j-month" as "div", { ariaLabel: "Its month" });
551
+ #whole = () => {};
552
+ #peek = peek(() => this.#whole());
553
+ /** Something popped by what was tapped: its name, what's said of it, and its button (none, where there's no
554
+ * whole to open). */
555
+ popped(by: HTMLElement, name: string, body: Node[], deed: string | null, whole: () => void) {
556
+ if (!this.#peek.el.isConnected) this.root.append(this.#peek.el);
557
+ this.#peek.body.replaceChildren(...body);
558
+ this.#peek.deed(deed);
559
+ this.#whole = whole;
560
+ this.#peek.show(by, name);
561
+ }
562
+ /** A time of an item popped: its month, small, that day marked; whole, the page's month of it (<j-month> in
563
+ * <j-views>), where it has one. */
564
+ pop(it: Item, f: Field, day: string, by: HTMLElement) {
565
+ const s = this.specOf(it),
566
+ whole = document.querySelector<HTMLElement>(`j-views > j-month[for="${s.name}"]`);
567
+ for (const [a, v] of Object.entries({ for: s.name, when: f.name, day, small: "" }))
568
+ this.#month.setAttribute(a, v);
569
+ this.popped(
570
+ by,
571
+ dated(dayOf(day), { weekday: "long", day: "numeric", month: "long" }),
572
+ [this.#month],
573
+ whole ? "Open the month" : null,
574
+ () => {
575
+ const url = new URL(location.href);
576
+ url.searchParams.set(whole!.getAttribute("param") ?? "day", day);
577
+ history.replaceState(history.state, "", url);
578
+ (whole!.parentElement as HTMLElement & { choose(r: Element): void }).choose(whole!);
579
+ },
580
+ );
581
+ }
582
+ /** The button that adds to a collection, opening its drawer empty: under an item, given it. */
583
+ adder(s: Spec, given: Record<string, number> = {}): HTMLElement {
584
+ // a thing is added with the least it needs (docs/design.md, "Adding"): its name alone, a box on the page
585
+ const asked = this.asks(s).filter((f) => !(f.name in given)),
586
+ needed = asked.filter((f) => f.needed);
587
+ if (s === spec && needed.length === 1 && needed[0]!.name === s.title && needed[0]!.type === "string") {
588
+ const box = el("input", {
589
+ name: `new-${s.one}`,
590
+ required: true,
591
+ autocomplete: "off",
592
+ placeholder: `A new ${s.one}`,
593
+ }),
594
+ line = el("p", { className: "said", role: "status" });
595
+ box.ariaLabel = `A new ${s.one}`;
596
+ const form = el(
597
+ "form",
598
+ { className: "adding box", part: "add" },
599
+ box,
600
+ el("button", { className: "act ink" }, icon("add"), "Add"),
601
+ line,
602
+ );
603
+ form.addEventListener("submit", async (e) => {
604
+ e.preventDefault();
605
+ if (!box.value.trim()) return;
606
+ const value = { ...given, [s.title!]: box.value.trim() };
607
+ const why = await said(
608
+ line,
609
+ this.send(this.querySelector(s.place)!, [{ op: "add", path: `/${s.name}/-`, value }]),
610
+ );
611
+ if (!why) form.reset();
612
+ });
613
+ return form;
614
+ }
615
+ const b = el(
616
+ "button",
617
+ { type: "button", className: "act small adding", part: "add" },
618
+ icon("add"),
619
+ `Add ${s.one}`,
620
+ );
621
+ b.addEventListener("click", () => this.drawer(s).show("new", given));
622
+ return b;
623
+ }
624
+ /** A collection's sheet: the page's (<j-sheet for>, joined/kit), else one put on the page as it's first wanted. */
625
+ sheet(s: Spec) {
626
+ let sheet = document.querySelector<HTMLElement & { drawer?: Drawer }>(`j-sheet[for="${s.name}"]`);
627
+ if (!sheet) {
628
+ sheet = Object.assign(document.createElement("j-sheet"), { hidden: false });
629
+ sheet.setAttribute("for", s.name);
630
+ document.body.append(sheet);
631
+ }
632
+ return sheet;
633
+ }
634
+ /** A collection's drawer: its sheet's. */
635
+ drawer(s: Spec) {
636
+ return this.sheet(s).drawer!;
637
+ }
638
+ /** Ops sent from an element of the document (its collection's doc attribute, an instance of a kind), as the
639
+ * compiled elements send theirs. */
640
+ send(from: Element, ops: object[]) {
641
+ return new Promise<Answer>((answer) => {
642
+ const doc = from.closest("[doc]")?.getAttribute("doc") ?? spec.doc;
643
+ from.dispatchEvent(new CustomEvent("ops", { bubbles: true, composed: true, detail: { doc, ops, answer } }));
644
+ });
645
+ }
646
+ /** A write's refusal, said under the rows (Said). */
647
+ said(request: Promise<Answer>) {
648
+ void said(this.#said, request);
649
+ }
650
+ /** The ops that give this person's id to a list of an item's (taken back from the lists none is in twice), or
651
+ * take it back. */
652
+ mine(it: Item, f: Field, mine: boolean) {
653
+ const at = (list: string) => `${it.path}/${list}`;
654
+ if (mine) return [{ op: "remove", path: `${at(f.name)}/${this.me}` }];
655
+ return [
656
+ ...(f.with ?? [])
657
+ .filter((o) => (it.data[o] ?? []).includes(this.me))
658
+ .map((o) => ({ op: "remove", path: `${at(o)}/${this.me}` })),
659
+ { op: "add", path: `${at(f.name)}/-`, value: this.me },
660
+ ];
661
+ }
662
+ /** A list's rows kept: each made again only as what it shows changes, the rest left as they are. */
663
+ #rows(list: HTMLElement, kept: Map<string, { shows: string; el: HTMLElement }>, its: Item[], inner: Spec[]) {
664
+ const wanted = new Set(its.map((it) => String(it.key)));
665
+ for (const key of this.#under.keys()) if (inner.length && !wanted.has(key)) this.#under.delete(key);
666
+ const top = list === this.#list;
667
+ keep(
668
+ list,
669
+ kept,
670
+ its.map((it): Row => {
671
+ const key = String(it.key),
672
+ row = () => this.row(it);
673
+ // what's under it: a list for each collection whose element the page put in, kept through its row's remaking
674
+ const under = inner.map((s, i) => {
675
+ const its = [...(this.getElementsByTagName(s.tag) as HTMLCollectionOf<Item>)].filter(
676
+ (c) => c.data[s.under!.field] === it.key,
677
+ );
678
+ if (!this.#under.has(key)) this.#under.set(key, []);
679
+ const u = (this.#under.get(key)![i] ??= {
680
+ list: el("ul", { className: "under", part: "under" }),
681
+ kept: new Map(),
682
+ });
683
+ u.list.dataset.none = `No ${s.name} yet.`;
684
+ this.#rows(u.list, u.kept, its, []);
685
+ return { s, list: u.list, adds: !!this.querySelector(`${s.place}[may~="add"]`) };
686
+ });
687
+ const text = (f: string | Node) => (typeof f === "string" ? f : f.textContent);
688
+ return {
689
+ key,
690
+ shows: JSON.stringify([
691
+ it.data,
692
+ it.getAttribute("may"),
693
+ this.me,
694
+ this.named(it),
695
+ this.facts(it).map(text),
696
+ this.opened(it),
697
+ under.map((u) => u.adds),
698
+ // how it's read, whose order it's in and what's said of it there, and whether its drawer is open
699
+ top ? [this.shown(), this.below(it).map(text)] : [],
700
+ ]),
701
+ make: () => {
702
+ const li = el("li", {}, this.row(it));
703
+ if (top) li.dataset.key = key;
704
+ for (const u of under)
705
+ li.append(u.list, ...(u.adds ? [this.adder(u.s, { [u.s.under!.field]: it.key })] : []));
706
+ return li;
707
+ },
708
+ };
709
+ }),
710
+ );
711
+ }
712
+ /** Everything drawn again from the document as it is: only what changed is made again. */
713
+ draw() {
714
+ // (a collection of each member's own values for these isn't rows under them)
715
+ const inner = nested.filter((s) => !s.mine && this.querySelector(s.place)),
716
+ its = this.ordered(this.items());
717
+ this.#rows(this.#list, this.#kept, its, inner);
718
+ this.#none.hidden = its.length > 0;
719
+ // what a search looks for: the rows with every word in what they say shown, and how many told to it
720
+ let shown = 0;
721
+ for (const it of its) {
722
+ const li = this.#kept.get(String(it.key))?.el,
723
+ is = found(
724
+ [this.named(it), ...this.facts(it).map((f) => (typeof f === "string" ? f : f.textContent))].join(" "),
725
+ this.#words,
726
+ );
727
+ if (li) li.hidden = !is;
728
+ shown += Number(is);
729
+ }
730
+ document.dispatchEvent(new CustomEvent("j-found", { detail: { for: spec.name, shown, of: its.length } }));
731
+ this.#add.hidden = !this.querySelector(`${spec.place}[may~="add"]`);
732
+ this.#heads();
733
+ // a sheet named in the address (a link to it, or Back) is put on the page to be opened
734
+ for (const s of specs) if (location.hash.startsWith(`#${s.one}=`)) this.sheet(s);
735
+ }
736
+ /** Whose order it's in, where each has their own. */
737
+ #heads() {
738
+ // (nothing to put in order, nobody's order to choose)
739
+ const ordered = !!this.orderBy() && this.me !== undefined && this.items().length > 0;
740
+ this.#whose.hidden = !ordered;
741
+ if (!ordered) return;
742
+ const shown = this.shown(),
743
+ want = [
744
+ ...this.people().map((by) => ({ key: String(by), words: by === this.me ? "Mine" : `${this.who(by)}’s`, by })),
745
+ { key: "all", words: "Everyone’s", by: 0 },
746
+ ];
747
+ if (this.#whose.dataset.shows !== JSON.stringify(want)) {
748
+ this.#whose.dataset.shows = JSON.stringify(want);
749
+ const chips = el("div", { className: "j-chips", role: "group", ariaLabel: "In the order of" });
750
+ chips.append(
751
+ ...want.map(({ key, words, by }) => {
752
+ const b = el("button", { type: "button", name: key }, ...(by ? [face(by, this.who(by))] : []), words);
753
+ b.addEventListener("click", () => this.order(key === "all" ? "all" : by));
754
+ return b;
755
+ }),
756
+ );
757
+ this.#whose.replaceChildren(el("span", {}, "In the order of"), chips);
758
+ }
759
+ for (const b of this.#whose.querySelectorAll("button")) b.ariaPressed = String(b.name === String(shown));
760
+ const whose = shown === "all" ? "all" : shown === this.me ? "mine" : "theirs";
761
+ if (this.dataset.order !== whose) this.dataset.order = whose;
762
+ }
763
+ };
764
+ }
765
+
766
+ /** Each display defined as the page has it (the compiler's class, or one extending it), and each item's element
767
+ * as it is, unless the page gave it a view first. */
768
+ export function define(
769
+ ...displays: (CustomElementConstructor & {
770
+ tag: string;
771
+ items: Record<string, CustomElementConstructor>;
772
+ specs: Spec[];
773
+ })[]
774
+ ) {
775
+ for (const d of displays) {
776
+ use(...d.specs);
777
+ for (const [tag, item] of Object.entries(d.items)) if (!customElements.get(tag)) customElements.define(tag, item);
778
+ customElements.define(d.tag, d);
779
+ }
780
+ }