@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
@@ -1,10 +1,12 @@
1
1
  // An app's definitions (its stories.jsonl's header lines): what its stories can't show, read by the compiler alone and
2
- // written into its generated/rules.ts as plain code, so nothing reads them as it runs (docs/plan.md, "The
3
- // definitions"). Each key is optional, a line of its own.
2
+ // written into its generated/rules.ts as plain code, so nothing reads them as it runs (the skill's §15). Each key is optional, a line of its own.
4
3
  //
5
4
  // links a field that links though it isn't named for its collection: { organisers: "members" }, a list;
6
5
  // { account: { to: "accounts", one: true } }, one id. Its own document's collection, or another's.
7
6
  // { post: { to: "posts", one: true, gone: "with" } }: removing the post removes what links to it, as ops
7
+ // { order: { to: "orders", one: true, part: true } }: structure, not a reference (is a, has a, composition):
8
+ // what the item is part of, one id. A page places it by that and never pops it (docs/design.md, "A peek");
9
+ // a link that goes with what it points at is part of it too. Any other link is a reference, and pops
8
10
  // roles who someone is to the document, beyond a member, each a who-path to them.
9
11
  // owns who sees what: a who-path for the whole document, or one per collection (a collection not named
10
12
  // is everyone's). A page is sent, and told of, only the items its person sees.
@@ -19,6 +21,16 @@
19
21
  // ["room"] } }, among those whose same fields are equal (none: all of them). A range is half
20
22
  // open, from its start until its end, a time or a number, or a duration on from a time; an
21
23
  // item without a start or an end is open on that side.
24
+ // mine a list of members that's each member's own: { tasks: ["ups"] }, a member gives and takes only their own
25
+ // id in it, whoever the grants allow; several together ({ tasks: [["ups", "downs"]] }), and nobody is in two
26
+ // of them on one item (a thumb up or down; a vote).
27
+ // Or a collection of values, each one member's for one item of another: { ranks: { of: "note", by: "by" } },
28
+ // my place for a note, my rating, my word on it. `of` is the link to the item (one id), `by` the link to
29
+ // its member (one id), which the server fills with its writer; a member has one for an item at most, and
30
+ // only they change or remove theirs, whoever the grants allow. Everyone sees them (the skill's reference, §15).
31
+ // Or a link to one member, each member's own to say ({ people: ["member"] }: which person you are, as
32
+ // yourself), set only to its writer, and taken back only by them. A field with its own words, refused in them:
33
+ // { people: [{ member: "you say which person you are, as yourself" }] }.
22
34
  // window a part of a collection a page may hold alone, by a parameter of its query and its topic
23
35
  // (trip?week=2026-11-02): { events: { week: { from: "start", for: "P7D" } } }, the items whose start is
24
36
  // in the days from it; with a length too ({ ..., length: "duration" }), those whose time overlaps them; { products: { category: "category" } }, those whose field is it.
@@ -29,7 +41,15 @@
29
41
  // timed a field whose time, an instant, comes: { cards: "due" }. Then the server clears it, as its own
30
42
  // write, told and reacted to as any is (a clock's next tick is a reaction setting it again).
31
43
  // frozen { by: "frozen", refused: "…" }: in a kind, while the root item's field is true, its instance is kept as it
32
- // is: every page's write refused in those words (src/settings.ts).
44
+ // is: every page's write refused in those words (src/settings.ts). Or an item kept as it is once it's
45
+ // settled, by its own yes-or-no field: { supplies: { by: "delivered", fields: ["price", "terms"], refused:
46
+ // "…" } }, those fields (none named: all of them) changed by no write while it's true (a trigger).
47
+ // addedWith an item added only in the write adding what a link of it points at, never after: { lines: { order:
48
+ // "an order's lines are set when it's placed" } } (a trigger, in those words).
49
+ // within a list's ids each in another list of the same item: { events: [{ lists: ["keen", "asked"], of: "people",
50
+ // refused: "…" }] }, held as the write leaves it (SQLite's by code, Postgres's by a trigger at its end).
51
+ // apart lists of an item no id is in two of: { events: [{ lists: ["keen", "asked", "declined"], refused: "…" }] },
52
+ // held as within is.
33
53
  // waits { by: "moderated", posts: "waiting", refused: "…" }: in a kind, while the root item's field is true, an
34
54
  // add to posts gives its field that moves (moves) as that first step, or it's refused in those words.
35
55
  //
@@ -45,7 +65,7 @@ import type { Op } from "./ops.ts";
45
65
 
46
66
  /** An app's definitions, as its stories' header lines say them, one a key. */
47
67
  export type Definitions = {
48
- links?: Record<string, string | { to: string; one?: true; gone?: "with" }>;
68
+ links?: Record<string, string | { to: string; one?: true; gone?: "with"; part?: true }>;
49
69
  roles?: Record<string, string>;
50
70
  owns?: string | Record<string, string>;
51
71
  kind?: { root: string };
@@ -53,6 +73,7 @@ export type Definitions = {
53
73
  timed?: Record<string, string>;
54
74
  unique?: Record<string, (string | string[])[]>;
55
75
  noOverlap?: Record<string, { range: [string, string]; same?: string[] }>;
76
+ mine?: Record<string, (string | string[] | Record<string, string>)[] | { of: string; by: string }>;
56
77
  window?: Record<string, Record<string, string | { from: string; for: string; length?: string }>>;
57
78
  order?: Record<string, string>;
58
79
  copies?: Record<string, { from: string; fields: string[] }>;
@@ -72,12 +93,25 @@ export type Definitions = {
72
93
  where?: Record<string, Record<string, Where | Where[] | string>>;
73
94
  /** In a kind, a yes-or-no field of the root item: while it's true, every page's write to its instance is refused, in
74
95
  * these words (a frozen board). src/settings.ts. */
75
- frozen?: { by: string; refused: string };
96
+ frozen?: { by: string; refused: string } | Record<string, Frozen>;
97
+ /** An item added only in the write adding what a link of it points at, never after: by collection, each link and
98
+ * the words a later add is refused in (an order's lines). */
99
+ addedWith?: Record<string, Record<string, string>>;
100
+ /** A list's ids each in another list of the same item (an answer from someone on the event): by collection. */
101
+ within?: Record<string, { lists: string[]; of: string; refused: string }[]>;
102
+ /** Lists of an item no id is in two of (one answer each): by collection. */
103
+ apart?: Record<string, { lists: string[]; refused: string }[]>;
76
104
  /** In a kind, a yes-or-no field of the root item: while it's true, an add to each collection named gives its field
77
105
  * that moves as this first step, or it's refused in these words (a board held for approval, a post waiting).
78
106
  * src/settings.ts. */
79
107
  waits?: { by: string; refused: string; [coll: string]: string };
80
108
  };
109
+ /** An item kept as it is once it's settled (definitions' frozen, by collection): while its own yes-or-no field (`by`)
110
+ * is true, its fields (`fields`; none named, all of them) are changed by no write, refused in these words. */
111
+ export type Frozen = { by: string; fields?: string[]; refused: string };
112
+ /** A kind's frozen (its root item's field), or none: the other is an item's own, by collection. */
113
+ export const kindFrozen = (f: Definitions["frozen"]) =>
114
+ f && typeof f.by === "string" ? (f as { by: string; refused: string }) : undefined;
81
115
  /** Which rows of a collection a document holds (definitions' where): those whose fields are each the value given,
82
116
  * or one of the values; with `last`, the newest so many by a field (`by`). Several: any of them. A collection's
83
117
  * name in their place: the rows linking to those its document holds of that one (an order's lines). `holds`, a
@@ -86,16 +120,20 @@ export type Where = { [field: string]: unknown; last?: number; by?: string };
86
120
  /** A read-only document over an app's entities, by member (each side's books): a collection each, its items the rows
87
121
  * of a table one of whose fields names the member (`sides`, each said as a word), those with `when` true; each with
88
122
  * some of its fields, and its total: the sum of a field of the rows naming it, each times another of theirs. */
89
- export type Query = Record<
90
- string,
91
- {
92
- from: string;
93
- when?: string;
94
- sides: Record<string, string>;
95
- fields: string[];
96
- total: { sum: string; times?: string };
97
- }
98
- >;
123
+ export type Query = Record<string, Sided | Counted>;
124
+ /** A collection of a read-only document: the rows naming the member, each with some of its fields and its total. */
125
+ export type Sided = {
126
+ from: string;
127
+ when?: string;
128
+ sides: Record<string, string>;
129
+ fields: string[];
130
+ total: { sum: string; times?: string };
131
+ };
132
+ /** A collection of a read-only document counting rows by what they link to and the week they're in (each trip's
133
+ * weeks): the rows of `from`, those whose item a link of theirs points at (`by`: the link, and the table it's to) names
134
+ * the member in one of its lists (`seen`), by that item and the Monday of the week a date and time of theirs is in
135
+ * (`week`), how many each (`count`); numbered in that order. Its items: id, the link, week and the count. */
136
+ export type Counted = { from: string; by: Record<string, string>; seen: string[]; week: string; count: string };
99
137
  /** A rule over values: a collection's field taken from a field of what a link of it points at, while a field of
100
138
  * what another of its links points at is true (an item's quantity from its product's stock, while its order is
101
139
  * placed): `{ items: { quantity: "product.stock", while: "order.placed", refused: "…" } }`. Taken or given back as
package/src/draft.ts CHANGED
@@ -2,13 +2,11 @@
2
2
  // Never mind (`never`). Each named control is held against the value the document has, so a change
3
3
  // made elsewhere fills the controls not being changed here, and keeps the ones that are. The form
4
4
  // says whether it's kept in its note, and why, if a write was refused; Save is off until there's something to keep
5
- // and it's ready to be kept. In its drawer (or modal) it has its ×, made at the end of its head where the form has
6
- // none, which Escape, and on a phone a tap on the dimmed page (./scrim.ts), press too. Every drawer that changes
5
+ // and it's ready to be kept. Its drawer (./surface.ts) closes by its ×, Escape and, on a phone, a tap on the
6
+ // dimmed page. Every drawer that changes
7
7
  // something is one: a page that wants it imports this; what its changes are as element ops is the page's own, and a
8
8
  // drawer that holds no fields against the document says its state by show() alone.
9
9
 
10
- import { iconButton } from "./icons.ts";
11
-
12
10
  type Value = string | boolean;
13
11
  type Control = HTMLInputElement;
14
12
 
@@ -32,19 +30,22 @@ export class Draft {
32
30
  /** The form as the document has it: afresh, every control set; as it changes elsewhere, only those
33
31
  * not being changed here. */
34
32
  fill(saved: Record<string, Value>, afresh = false) {
33
+ const written: (Promise<unknown> | undefined)[] = [];
35
34
  for (const [name, value] of Object.entries(saved)) {
36
35
  const control = this.#control(name),
37
36
  was = control.dataset.saved;
38
37
  control.dataset.saved = String(value);
39
- if (afresh || read(control) === was) write(control, value);
38
+ if (afresh || read(control) === was) written.push(write(control, value));
40
39
  }
40
+ /** Settled once each control written has drawn it and said again whether it's valid. */
41
+ return Promise.all(written);
41
42
  }
42
43
  /** What the form has changed, by name: a tick as a boolean, the rest as strings. */
43
44
  edits() {
44
45
  const changed: Record<string, Value> = {};
45
46
  for (const control of this.form.querySelectorAll<Control>("[data-saved]"))
46
47
  if (read(control) !== control.dataset.saved)
47
- changed[control.name] = control.type === "checkbox" ? control.checked : control.value;
48
+ changed[control.name] = ticks(control) ? control.checked : read(control);
48
49
  return changed;
49
50
  }
50
51
  /** What the form says: "kept", nothing to save or put back; "changed", not kept yet; "new", not
@@ -56,29 +57,43 @@ export class Draft {
56
57
  (this.form.elements.namedItem(name) as HTMLButtonElement).disabled =
57
58
  state === "kept" || (name === "go" && !ready);
58
59
  if (!this.note.classList.contains("refused"))
59
- this.note.textContent = state === "changed" ? "Not kept yet: Save keeps it, Never mind puts it back." : "";
60
+ this.#say(state === "changed" ? "Not kept yet: Save keeps it, Never mind puts it back." : "");
61
+ }
62
+ /** Its note's words, said in place (a wa-callout: a refusal its danger), and hidden where there are none. */
63
+ #say(words: string) {
64
+ this.note.textContent = words;
65
+ this.note.hidden = !words;
66
+ this.note.setAttribute("variant", this.note.classList.contains("refused") ? "danger" : "neutral");
60
67
  }
61
68
  /** Why a write was refused, said in the note; nothing takes it back. */
62
69
  refuse(why: string) {
63
70
  this.note.classList.toggle("refused", !!why);
64
- this.note.textContent = why;
71
+ this.#say(why);
65
72
  }
66
- /** The drawer (or modal) it's in: its × (`close`, made at the end of its head where the form has none) and Escape
67
- * close it; Never mind (`never`) puts back what it holds. */
68
- inside(box: HTMLElement, close: () => void, never: () => void) {
69
- let x = this.form.elements.namedItem("close") as HTMLButtonElement | null;
70
- if (!x)
71
- ((x = Object.assign(iconButton("close", "Close"), { name: "close" })),
72
- this.form.querySelector("header")!.append(x));
73
- x.addEventListener("click", close);
74
- (this.form.elements.namedItem("never") as HTMLButtonElement).addEventListener("click", never);
75
- // taken as its own, so the page's Escape (./scrim.ts) doesn't close another
76
- box.addEventListener("keydown", (e) => {
77
- if ((e as KeyboardEvent).key === "Escape" && !e.defaultPrevented) (e.preventDefault(), close());
78
- });
73
+ /** Never mind (`never`) puts back what it holds. (Its surface closes itself: src/surface.ts.) */
74
+ never(then: () => void) {
75
+ (this.form.elements.namedItem("never") as HTMLButtonElement).addEventListener("click", then);
79
76
  }
80
77
  #control = (name: string) => this.form.elements.namedItem(name) as Control;
81
78
  }
82
79
 
83
- const read = (c: Control) => (c.type === "checkbox" ? String(c.checked) : c.value);
84
- const write = (c: Control, v: Value) => (c.type === "checkbox" ? (c.checked = v === true) : (c.value = String(v)));
80
+ /** Whether a control is a tick (a wa-checkbox, a wa-switch, or the browser's own). */
81
+ const ticks = (c: Control) => c.localName === "wa-checkbox" || c.localName === "wa-switch" || c.type === "checkbox";
82
+ const read = (c: Control) => (ticks(c) ? String(c.checked) : c.localName === "wa-select" ? chosen(c) : c.value);
83
+ /** A control set to a value, settled once it's said again whether it's valid: Web Awesome's control checks the box it
84
+ * draws, which holds a value set from outside only once it's drawn. */
85
+ function write(c: Control, v: Value) {
86
+ if (ticks(c)) c.checked = v === true;
87
+ else if (c.localName === "wa-select") choose(c, String(v));
88
+ else c.value = String(v);
89
+ const wa = c as Control & { updateComplete?: Promise<unknown>; updateValidity?: () => void };
90
+ return wa.updateComplete?.then(() => wa.updateValidity?.());
91
+ }
92
+ type Option = Element & { value: string; selected: boolean };
93
+ /** A choice of Web Awesome's (a wa-select) set to a value: its option chosen too, as it keeps only what's chosen. */
94
+ export function choose(c: { value: string | null } & Element, v: string | null) {
95
+ for (const o of c.querySelectorAll<Option>("wa-option")) o.selected = o.value === v;
96
+ c.value = v;
97
+ }
98
+ /** What a wa-select has chosen, by its option ("" for none): its own value is read from options it learns of later. */
99
+ export const chosen = (c: Element) => [...c.querySelectorAll<Option>("wa-option")].find((o) => o.selected)?.value ?? "";
package/src/drawer.ts ADDED
@@ -0,0 +1,222 @@
1
+ // The drawer of a sheet (<j-sheet>, ./sheet.ts): one item's fields as this person may change them, held as a
2
+ // Draft until Save, one write; Remove at its foot, apart, asked first in a modal. A new item is a modal's, never the
3
+ // drawer's (design.md: new and delete are modal, edit is the drawer): asked only what it can't be without (what it
4
+ // needs, its title, what's offered filled in), Add off until it has it; the rest is filled in afterwards, in the
5
+ // drawer. A link to one is chosen as design.md's Choosing says: six or
6
+ // fewer, a select; more, a typeahead. What's open is in the address's hash (#option=2), so Back closes it.
7
+
8
+ import { Draft } from "./draft.ts";
9
+ import type { Answer } from "./page.ts";
10
+ import { control, el, type Asked, type Control } from "./fields.ts";
11
+ import { askFirst, inHash, surface } from "./parts.ts";
12
+ import { Surface } from "./surface.ts";
13
+ import { icon } from "./icons.ts";
14
+ import type { Field, Item, Spec } from "./rows.ts";
15
+
16
+ export { el };
17
+
18
+ let forms = 0;
19
+
20
+ /** What a drawer asks of the display it's in: where it's put, what things are called, and how a write is sent. */
21
+ export type Host = {
22
+ root: ShadowRoot;
23
+ label(f: Field): string;
24
+ title(it: Element): string;
25
+ /** The items a link may be to, as they're on the page: each its key and its title. */
26
+ choices(f: Field): [key: number, words: string][];
27
+ add(spec: Spec, value: object): Promise<Answer>;
28
+ /** What's said of a write refused once the drawer has closed (a removal). */
29
+ said(request: Promise<Answer>): void;
30
+ /** Told as the drawer opens on an item or closes: what's open is marked on its row. */
31
+ shown(): void;
32
+ };
33
+
34
+ export class Drawer {
35
+ /** The item in it: one of the document's, "new" for one being added, or none. */
36
+ open: Item | "new" | null = null;
37
+ #form = el("form", { id: `j-form-${++forms}` });
38
+ #box: Surface;
39
+ #ask = surface("wa-dialog", { className: "ask" });
40
+ /** Where a new one is asked: the drawer's form and foot, borrowed. */
41
+ #new = surface("wa-dialog", { className: "modal" });
42
+ #go = el("button", { className: "act ink", name: "go" });
43
+ #gone = el("button", { type: "button", className: "act danger", name: "gone" }, icon("remove"), "Remove");
44
+ #draft: Draft;
45
+ #hash: ReturnType<typeof inHash>;
46
+ /** What a new item is given without being asked (the item it's added under). */
47
+ #given: Record<string, number> = {};
48
+ /** What a new item is offered already filled in, still asked (a calendar's day and hour tapped). */
49
+ #filled: Record<string, unknown> = {};
50
+ /** Each field's control, by its name. */
51
+ #controls = new Map<string, Control>();
52
+ constructor(
53
+ readonly host: Host,
54
+ readonly spec: Spec,
55
+ readonly asks: Asked[],
56
+ items: () => Item[],
57
+ signal: AbortSignal,
58
+ ) {
59
+ const note = el("wa-callout", { className: "note", role: "status", hidden: true, size: "small" });
60
+ this.#form.append(...asks.map((f) => this.#asked(f).el), note);
61
+ const form = this.#form.id,
62
+ foot = el(
63
+ "footer",
64
+ {},
65
+ this.#gone,
66
+ el("button", { type: "button", className: "act", name: "never" }, "Never mind"),
67
+ this.#go,
68
+ );
69
+ // its deeds at the drawer's foot, outside the form, are its own still
70
+ for (const b of foot.querySelectorAll("button")) b.setAttribute("form", form);
71
+ this.#box = new Surface("drawer", this.#form, foot, () => this.show(null), { label: `A ${spec.one}` });
72
+ this.#new.append(el("h2", { slot: "label" }));
73
+ host.root.append(...this.#box.els, this.#ask, this.#new);
74
+ // its ×, Escape or a tap away: as Never mind
75
+ this.#new.addEventListener("wa-hide", (e) => e.target === this.#new && this.open === "new" && this.show(null));
76
+ this.#draft = new Draft(this.#form, note);
77
+ const ask = askFirst(this.#ask);
78
+ // Never mind: a new one let go; an item's changes put back
79
+ this.#draft.never(() => (this.open === "new" ? this.show(null) : this.show(this.open, this.#given, this.#filled)));
80
+ // said again once the control has drawn what's typed: Web Awesome's say whether they're valid only then
81
+ this.#form.addEventListener("input", (e) => {
82
+ this.#shown();
83
+ void (e.target as { updateComplete?: Promise<unknown> }).updateComplete?.then(() => this.#shown());
84
+ });
85
+ this.#form.addEventListener("submit", (e) => (e.preventDefault(), void this.#save()));
86
+ this.#gone.addEventListener("click", () => {
87
+ const it = this.open as Item;
88
+ ask(`Remove ${host.title(it)}?`, () => (this.show(null), host.said(it.removeItem!())));
89
+ });
90
+ this.#hash = inHash(
91
+ spec.one,
92
+ items,
93
+ () => (this.open === "new" ? null : this.open),
94
+ (it) => this.show(it, {}, {}, false),
95
+ signal,
96
+ );
97
+ }
98
+ #control = (name: string) => this.#form.elements.namedItem(name) as HTMLInputElement | HTMLSelectElement;
99
+ /** A field as the drawer asks for it, in its label, as its kind says (./fields.ts). */
100
+ #asked(f: Asked) {
101
+ const c = control(f, this.host.label(f), { choices: (f) => this.host.choices(f), changed: () => this.#shown() });
102
+ this.#controls.set(f.name, c);
103
+ return c;
104
+ }
105
+ /** What each control offers, as the page has it now (the items a link may be to, the people to choose). */
106
+ #choices() {
107
+ for (const f of this.asks) {
108
+ const c = this.#controls.get(f.name)!;
109
+ c.refresh();
110
+ // a link an add needs, chosen from a few: asked for by its select, where it's shown
111
+ if (f.how === "one") {
112
+ const select = this.#control(f.name) as HTMLSelectElement;
113
+ select.required = !!f.needed && !select.hidden && !this.#controls.get(f.name)!.el.hidden;
114
+ }
115
+ }
116
+ }
117
+ #values = (it: Item | null) =>
118
+ Object.fromEntries(
119
+ this.asks.map((f) => [
120
+ f.name,
121
+ this.#controls.get(f.name)!.shown(it?.data[f.name] ?? this.#given[f.name] ?? this.#filled[f.name]),
122
+ ]),
123
+ );
124
+ /** Whether what's typed differs from the item; of a new one, whether it has what an add needs. */
125
+ #shown() {
126
+ if (this.open === "new") return this.#draft.show("new", this.#form.checkValidity() && this.#needs());
127
+ // changed, and ready only where what it needs is still there (a title taken away isn't)
128
+ this.#draft.show(Object.keys(this.#draft.edits()).length ? "changed" : "kept", this.#form.checkValidity());
129
+ }
130
+ /** Whether each link an add needs is chosen (a typeahead's is kept unseen, where the form can't ask for it). */
131
+ #needs = () => this.asks.every((f) => !f.needed || !f.to || f.name in this.#given || this.#control(f.name).value);
132
+ /** The page's items came, went or changed: the choices follow; the one open, gone, closes the drawer; changed
133
+ * elsewhere, what isn't being typed in follows it. */
134
+ refresh() {
135
+ this.#choices();
136
+ this.#hash.again();
137
+ if (!this.open || this.open === "new") return;
138
+ if (!this.open.isConnected) return this.show(null);
139
+ this.#offer(this.open);
140
+ this.#draft.fill(this.#values(this.open));
141
+ this.#shown();
142
+ }
143
+ /** The drawer with an item in it, or a new one (given what it's added under, and offered what's `filled`, which it
144
+ * still asks), or closed; marked in the address. */
145
+ show(it: Item | "new" | null, given: Record<string, number> = {}, filled: Record<string, unknown> = {}, mark = true) {
146
+ this.open = it;
147
+ this.#given = it === "new" ? given : {};
148
+ this.#filled = it === "new" ? filled : {};
149
+ // what shows the collection marks the one open (design.md, "A row"), and no other
150
+ document.dispatchEvent(new CustomEvent("j-open", { detail: { for: this.spec.name } }));
151
+ this.#box.open = !!it && it !== "new";
152
+ this.#asking(it === "new");
153
+ if (mark) this.#hash.mark(it && it !== "new" ? it.key : null);
154
+ this.host.shown();
155
+ if (!it) return;
156
+ const item = it === "new" ? null : it;
157
+ this.#box.title.textContent = item ? `Editing “${this.host.title(item)}”` : `A new ${this.spec.one}`;
158
+ this.#offer(item);
159
+ this.#choices();
160
+ this.#draft.refuse("");
161
+ // afresh: WebKit keeps a datetime-local box set back to "" invalid until its form is reset
162
+ this.#form.reset();
163
+ void this.#draft.fill(this.#values(item), true).then(() => this.#shown());
164
+ this.#choices();
165
+ this.#shown();
166
+ }
167
+ /** The fields offered: of an item, what this person may change (its `may`, which may come after it, as a page
168
+ * opened on it from its address is sent it); of a new one, what it can't be without, and isn't given already. */
169
+ #offer(item: Item | null) {
170
+ const may = (item?.getAttribute("may") ?? "").split(" ");
171
+ for (const f of this.asks) {
172
+ const label = this.#controls.get(f.name)!.el;
173
+ // an item's: what this person may change; a new one's: what it can't be without, and isn't given already
174
+ label.hidden = item
175
+ ? !f.readonly && !may.includes(`replace:${f.name}`)
176
+ : f.name in this.#given || !(f.needed || f.name === this.spec.title || f.name in this.#filled);
177
+ if (!f.to && f.type !== "place") {
178
+ const box = this.#control(f.name);
179
+ box.required = !!f.needed && f.type !== "boolean" && !label.hidden;
180
+ // said once it's there, asked as it's added
181
+ if (f.readonly) (box.toggleAttribute("readonly", !!item), box.toggleAttribute("without-steppers", !!item));
182
+ }
183
+ }
184
+ this.#gone.hidden = !may.includes("remove");
185
+ }
186
+ /** A new one asked in the modal, its form and foot borrowed from the drawer; else they're the drawer's again. */
187
+ #asking(on: boolean) {
188
+ const foot = this.#go.parentElement!;
189
+ this.#go.replaceChildren(icon(on ? "add" : "check"), on ? "Add" : "Save");
190
+ if (on) {
191
+ this.#new.querySelector("h2")!.textContent = `A new ${this.spec.one}`;
192
+ foot.slot = "footer";
193
+ this.#new.append(this.#form, foot);
194
+ } else if (this.#form.parentElement === this.#new) this.#box.hold();
195
+ this.#new.open = on;
196
+ }
197
+ /** Save: the changes in one write, or the new item added; refused, why is said here, and what's typed stays. */
198
+ async #save() {
199
+ const adding = this.open === "new",
200
+ item = this.open as Item;
201
+ const typed = Object.entries(this.#draft.edits()).map(
202
+ ([name, v]) => [name, typeof v === "string" ? v.trim() : v] as const,
203
+ );
204
+ // an item's field emptied: words kept as none; anything else taken off it, where a story takes it off
205
+ const words = (name: string) => this.asks.find((f) => f.name === name)?.type === "string",
206
+ cleared = adding ? [] : typed.filter(([name, v]) => v === "" && !words(name)).map(([name]) => name),
207
+ stays = cleared.find((name) => !(item.getAttribute("may") ?? "").split(" ").includes(`remove:${name}`));
208
+ const edits = Object.fromEntries(
209
+ typed
210
+ .filter(([name, v]) => v !== "" || (!adding && words(name)))
211
+ .map(([name, v]) => [name, this.#controls.get(name)!.kept(v)]),
212
+ );
213
+ const why = adding
214
+ ? await this.host.add(this.spec, { ...this.#given, ...this.#filled, ...edits })
215
+ : stays
216
+ ? `${stays} can't be left empty`
217
+ : ((Object.keys(edits).length ? await item.replace!(edits) : undefined) ??
218
+ (cleared.length ? await item.removeField!(...cleared) : undefined));
219
+ this.#draft.refuse(why ?? "");
220
+ if (!why) this.show(adding ? null : this.open);
221
+ }
222
+ }