@blueshed/joined 0.0.0-stage → 0.1.20

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 (213) hide show
  1. package/.claude/skills/joined/SKILL.md +17 -0
  2. package/.claude/skills/joined/reference.md +768 -0
  3. package/LICENSE +19 -0
  4. package/README.md +115 -2
  5. package/docs/design.md +148 -0
  6. package/docs/introduction.md +193 -0
  7. package/docs/learning.md +76 -0
  8. package/docs/stories.md +82 -0
  9. package/examples/app.css +27 -0
  10. package/examples/cafe/books.html +38 -0
  11. package/examples/cafe/books.json +13 -0
  12. package/examples/cafe/books.ts +64 -0
  13. package/examples/cafe/cafe.css +512 -0
  14. package/examples/cafe/front.html +115 -0
  15. package/examples/cafe/front.ts +7 -0
  16. package/examples/cafe/generated/books.elements.ts +52 -0
  17. package/examples/cafe/generated/books.queries.ts +11 -0
  18. package/examples/cafe/generated/books.rules.ts +42 -0
  19. package/examples/cafe/generated/kitchen.elements.ts +87 -0
  20. package/examples/cafe/generated/kitchen.queries.ts +47 -0
  21. package/examples/cafe/generated/kitchen.rules.ts +98 -0
  22. package/examples/cafe/generated/menu.elements.ts +61 -0
  23. package/examples/cafe/generated/menu.queries.ts +28 -0
  24. package/examples/cafe/generated/menu.rules.ts +65 -0
  25. package/examples/cafe/generated/order.elements.ts +87 -0
  26. package/examples/cafe/generated/order.queries.ts +47 -0
  27. package/examples/cafe/generated/order.rules.ts +95 -0
  28. package/examples/cafe/generated/staff.elements.ts +55 -0
  29. package/examples/cafe/generated/staff.queries.ts +27 -0
  30. package/examples/cafe/generated/staff.rules.ts +59 -0
  31. package/examples/cafe/generated/supplies.elements.ts +62 -0
  32. package/examples/cafe/generated/supplies.queries.ts +43 -0
  33. package/examples/cafe/generated/supplies.rules.ts +84 -0
  34. package/examples/cafe/generated/till.elements.ts +93 -0
  35. package/examples/cafe/generated/till.queries.ts +57 -0
  36. package/examples/cafe/generated/till.rules.ts +113 -0
  37. package/examples/cafe/index.html +70 -0
  38. package/examples/cafe/kitchen.css +107 -0
  39. package/examples/cafe/kitchen.html +39 -0
  40. package/examples/cafe/kitchen.ts +125 -0
  41. package/examples/cafe/menu.html +55 -0
  42. package/examples/cafe/menu.json +41 -0
  43. package/examples/cafe/menu.ts +84 -0
  44. package/examples/cafe/migrations/001-cafe.ts +11 -0
  45. package/examples/cafe/order.html +37 -0
  46. package/examples/cafe/order.ts +45 -0
  47. package/examples/cafe/rail-drag.ts +30 -0
  48. package/examples/cafe/routes.ts +32 -0
  49. package/examples/cafe/staff.json +20 -0
  50. package/examples/cafe/stories.jsonl +43 -0
  51. package/examples/cafe/supplies.html +77 -0
  52. package/examples/cafe/supplies.json +33 -0
  53. package/examples/cafe/supplies.ts +170 -0
  54. package/examples/cafe/till.json +95 -0
  55. package/examples/cafe/till.ts +122 -0
  56. package/examples/cafe/trust.ts +20 -0
  57. package/examples/cafe/views.ts +171 -0
  58. package/examples/docs.ts +28 -0
  59. package/examples/film.jpg +0 -0
  60. package/examples/hello/generated/elements.ts +45 -0
  61. package/examples/hello/generated/queries.ts +24 -0
  62. package/examples/hello/generated/rules.ts +45 -0
  63. package/examples/hello/hello.css +27 -0
  64. package/examples/hello/hello.json +1 -0
  65. package/examples/hello/index.html +32 -0
  66. package/examples/hello/migrations/001-hello.ts +6 -0
  67. package/examples/hello/page.ts +49 -0
  68. package/examples/hello/routes.ts +6 -0
  69. package/examples/hello/stories.jsonl +3 -0
  70. package/examples/index.html +146 -0
  71. package/examples/page.ts +11 -0
  72. package/examples/people/adding.ts +20 -0
  73. package/examples/people/generated/elements.ts +68 -0
  74. package/examples/people/generated/queries.ts +28 -0
  75. package/examples/people/generated/rules.ts +85 -0
  76. package/examples/people/links.ts +124 -0
  77. package/examples/people/migrations/001-people.ts +6 -0
  78. package/examples/people/migrations/002-add-people-member-and-guard-people.ts +7 -0
  79. package/examples/people/migrations/003-add-people-rank-and-guard-people.ts +7 -0
  80. package/examples/people/order.ts +61 -0
  81. package/examples/people/people.css +65 -0
  82. package/examples/people/people.json +26 -0
  83. package/examples/people/routes.ts +12 -0
  84. package/examples/people/stories.jsonl +20 -0
  85. package/examples/people/trust.ts +14 -0
  86. package/examples/people/view.ts +115 -0
  87. package/examples/routes.ts +11 -0
  88. package/examples/trip/answer-rows.ts +70 -0
  89. package/examples/trip/answers.ts +11 -0
  90. package/examples/trip/bar.ts +89 -0
  91. package/examples/trip/calendar-map.css +709 -0
  92. package/examples/trip/calendar-map.ts +249 -0
  93. package/examples/trip/drags.ts +143 -0
  94. package/examples/trip/drawer.ts +250 -0
  95. package/examples/trip/event-form.ts +199 -0
  96. package/examples/trip/generated/elements.ts +144 -0
  97. package/examples/trip/generated/queries.ts +46 -0
  98. package/examples/trip/generated/rules.ts +239 -0
  99. package/examples/trip/holding.ts +61 -0
  100. package/examples/trip/ics.ts +117 -0
  101. package/examples/trip/index.html +82 -0
  102. package/examples/trip/line-stops.ts +92 -0
  103. package/examples/trip/line-words.ts +79 -0
  104. package/examples/trip/line.ts +121 -0
  105. package/examples/trip/map.ts +234 -0
  106. package/examples/trip/mercator.ts +28 -0
  107. package/examples/trip/migrations/001-trip.ts +7 -0
  108. package/examples/trip/migrations/002-add-events-zone-and-guard-events.ts +7 -0
  109. package/examples/trip/migrations/003-guard-events-and-create-events_asked-and-more.ts +9 -0
  110. package/examples/trip/migrations/004-create-stays.ts +6 -0
  111. package/examples/trip/migrations/005-guard-events-and-create-events_arrives-and-more.ts +8 -0
  112. package/examples/trip/month.ts +87 -0
  113. package/examples/trip/other-weeks.ts +67 -0
  114. package/examples/trip/page.ts +121 -0
  115. package/examples/trip/peek.ts +71 -0
  116. package/examples/trip/people-chips.ts +74 -0
  117. package/examples/trip/place-search.ts +51 -0
  118. package/examples/trip/places.ts +34 -0
  119. package/examples/trip/routes.ts +27 -0
  120. package/examples/trip/shell.ts +39 -0
  121. package/examples/trip/stays.css +51 -0
  122. package/examples/trip/stays.ts +68 -0
  123. package/examples/trip/stories.jsonl +72 -0
  124. package/examples/trip/time.ts +46 -0
  125. package/examples/trip/told.ts +48 -0
  126. package/examples/trip/trip.json +69 -0
  127. package/examples/trip/trust.ts +51 -0
  128. package/examples/trip/week.ts +237 -0
  129. package/examples/trip/words.ts +4 -0
  130. package/examples/trip/writes.ts +91 -0
  131. package/examples/trip/zones.ts +16 -0
  132. package/examples/trips/card.ts +149 -0
  133. package/examples/trips/generated/elements.ts +60 -0
  134. package/examples/trips/generated/queries.ts +28 -0
  135. package/examples/trips/generated/rules.ts +62 -0
  136. package/examples/trips/index.html +47 -0
  137. package/examples/trips/map.ts +52 -0
  138. package/examples/trips/migrations/001-trips.ts +8 -0
  139. package/examples/trips/new-trip.ts +46 -0
  140. package/examples/trips/page.ts +9 -0
  141. package/examples/trips/routes.ts +7 -0
  142. package/examples/trips/stories.jsonl +12 -0
  143. package/examples/trips/trips.css +183 -0
  144. package/examples/trips/trips.json +7 -0
  145. package/examples/weeks/derive.ts +23 -0
  146. package/examples/weeks/generated/elements.ts +52 -0
  147. package/examples/weeks/generated/rules.ts +33 -0
  148. package/examples/weeks/routes.ts +4 -0
  149. package/examples/weeks/weeks.json +1 -0
  150. package/examples/welcome.css +299 -0
  151. package/package.json +55 -4
  152. package/src/attachment.ts +70 -0
  153. package/src/cli.ts +52 -0
  154. package/src/client.ts +98 -0
  155. package/src/compile.ts +2034 -0
  156. package/src/css.d.ts +5 -0
  157. package/src/definitions.ts +268 -0
  158. package/src/demo.css +29 -0
  159. package/src/demo.ts +42 -0
  160. package/src/draft.ts +69 -0
  161. package/src/engine.ts +288 -0
  162. package/src/file-value.ts +13 -0
  163. package/src/files.ts +187 -0
  164. package/src/follow.ts +56 -0
  165. package/src/hues.ts +3 -0
  166. package/src/icon-names.ts +77 -0
  167. package/src/icon.png +0 -0
  168. package/src/icons.ts +32 -0
  169. package/src/keep.ts +33 -0
  170. package/src/link.ts +31 -0
  171. package/src/mcp.ts +252 -0
  172. package/src/members/demo.ts +53 -0
  173. package/src/members/generated/elements.ts +68 -0
  174. package/src/members/generated/queries.ts +29 -0
  175. package/src/members/generated/rules.ts +65 -0
  176. package/src/members/group.ts +294 -0
  177. package/src/members/keys.ts +221 -0
  178. package/src/members/members.json +9 -0
  179. package/src/members/migrations/001-members.ts +6 -0
  180. package/src/members/migrations/002-guard-members.ts +11 -0
  181. package/src/members/migrations/003-add-members-act-and-guard-members.ts +7 -0
  182. package/src/members/panel.css +208 -0
  183. package/src/members/panel.ts +398 -0
  184. package/src/members/routes.ts +4 -0
  185. package/src/members/stories.jsonl +27 -0
  186. package/src/members/trust.ts +116 -0
  187. package/src/migrate.ts +597 -0
  188. package/src/migrations/001-joined.ts +13 -0
  189. package/src/migrations/002-group-keys.ts +16 -0
  190. package/src/migrations/003-change-inverse.ts +10 -0
  191. package/src/migrations/004-change-followed.ts +6 -0
  192. package/src/ops.ts +61 -0
  193. package/src/page.css +79 -0
  194. package/src/page.ts +413 -0
  195. package/src/parts.css +471 -0
  196. package/src/parts.ts +152 -0
  197. package/src/server.ts +862 -0
  198. package/src/settings.ts +94 -0
  199. package/src/starter.ts +89 -0
  200. package/src/store.ts +815 -0
  201. package/src/stories.ts +387 -0
  202. package/src/toast.ts +52 -0
  203. package/src/together.ts +167 -0
  204. package/src/tokens.css +85 -0
  205. package/src/tokens.json +457 -0
  206. package/src/upkeep.ts +86 -0
  207. package/src/visitors.ts +43 -0
  208. package/src/write.ts +567 -0
  209. package/starter/CLAUDE.md +37 -0
  210. package/starter/gitignore +3 -0
  211. package/starter/tests/hello-page.test.ts +14 -0
  212. package/starter/tests/hello.test.ts +8 -0
  213. package/starter/tsconfig.json +17 -0
@@ -0,0 +1,170 @@
1
+ // The supplies page: the supplies its person is party to, as buyer or supplier, each read from their side and saying
2
+ // whose each part is. The supplier sets the price and the days to pay (in the menu's drawer: a Draft, in the hash as
3
+ // #supply=1) and says it's delivered, as its may says; the buyer is offered none of it. Delivered, the menu has more
4
+ // (its gives), and both sides' books have it. Anyone orders one, as its buyer, from another member.
5
+
6
+ import { members, signedIn } from "joined/panel";
7
+ import "joined/toast";
8
+ import { Draft } from "joined/draft";
9
+ import { iconButton, iconify } from "joined/icons";
10
+ import { inHash, said } from "joined/parts";
11
+ import * as menu from "./generated/menu.rules.ts";
12
+ import { JItem } from "./generated/menu.elements.ts";
13
+ import * as supplies from "./generated/supplies.rules.ts";
14
+ import { JSupply, type JSupplies } from "./generated/supplies.elements.ts";
15
+ import { money, nameOf } from "./views.ts";
16
+
17
+ /** Who this page is signed in as: none until the group tells it. */
18
+ let me: number | null = null;
19
+ signedIn((m) => ((me = m), fill()));
20
+ /** The menu's items on the page, a live list. */
21
+ const items = document.getElementsByTagName(menu.collections.items.tag) as HTMLCollectionOf<JItem>;
22
+
23
+ /** Who's who on a supply, from this page's side: you, or their name. */
24
+ const who = (id?: number) => (id === me ? "you" : nameOf(members, id));
25
+
26
+ /** A supply: what, how many, at what price and terms, from whom to whom, and whose each part is; where its person is
27
+ * its supplier, the way into the drawer for its price and days to pay, and Delivered. */
28
+ class Supply extends JSupply {
29
+ #head = Object.assign(document.createElement("p"), { className: "head" });
30
+ #what = Object.assign(document.createElement("p"), { className: "what" });
31
+ #whose = Object.assign(document.createElement("p"), { className: "whose" });
32
+ #acts = Object.assign(document.createElement("p"), { className: "acts" });
33
+ #said = Object.assign(document.createElement("p"), { className: "said", role: "status" });
34
+ #edit = iconButton("edit", "", () => show(this));
35
+ #delivered = Object.assign(document.createElement("button"), {
36
+ type: "button",
37
+ className: "act ink",
38
+ textContent: "Delivered",
39
+ onclick: () => said(this.#said, this.replace({ delivered: true })),
40
+ });
41
+ connectedCallback() {
42
+ if (!this.childElementCount) this.append(this.#head, this.#what, this.#whose, this.#acts, this.#said);
43
+ this.draw();
44
+ hash.again(); // named in the address before it was on the page: opened now it is
45
+ }
46
+ draw() {
47
+ if (!this.isConnected) return;
48
+ const d = this.data;
49
+ const item = [...items].find((i) => i.key === d.item)?.data.name ?? `item ${d.item}`;
50
+ this.#head.replaceChildren(
51
+ Object.assign(document.createElement("b"), { textContent: d.title }),
52
+ Object.assign(document.createElement("span"), {
53
+ className: `pill${d.delivered ? " here" : ""}`,
54
+ textContent: d.delivered ? "delivered" : "on its way",
55
+ }),
56
+ );
57
+ this.#what.textContent = `${d.qty} × ${item} at ${money(d.price)} each, ${money(d.qty * d.price)} · ${d["terms.days"]} days to pay`;
58
+ // whose each part is, as this page's person reads it
59
+ const buying = d.buyer === me,
60
+ supplying = d.supplier === me;
61
+ this.#whose.textContent = `${cap(who(d.buyer))} ${buying ? "buy" : "buys"} from ${who(d.supplier)}. ${cap(who(d.supplier))} ${supplying ? "set" : "sets"} the price and the days to pay, and ${supplying ? "say" : "says"} when it's delivered.`;
62
+ this.#edit.ariaLabel = `Change your terms for ${d.title}`;
63
+ this.#acts.replaceChildren(
64
+ // its terms, until it's delivered: then both books have them, kept (trust.ts)
65
+ ...(this.matches('[may~="replace:price"], [may~="replace:terms.days"]') && !d.delivered ? [this.#edit] : []),
66
+ ...(this.matches('[may~="replace:delivered"]') && !d.delivered ? [this.#delivered] : []),
67
+ );
68
+ // the one in the drawer, changed elsewhere: what isn't being typed in follows it
69
+ if (this === open) (draft.fill(fields(this)), shown());
70
+ }
71
+ }
72
+ const cap = (s: string) => s[0]!.toUpperCase() + s.slice(1);
73
+
74
+ // the supplier's terms, in a drawer, as the menu's item is: held until Save, put back by Never mind
75
+ const drawer = document.querySelector<HTMLElement>("aside.drawer")!,
76
+ terms = drawer.querySelector("form")!,
77
+ draft = new Draft(terms, terms.querySelector(".note")!);
78
+ /** The supply in the drawer, if one is. */
79
+ let open: Supply | null = null;
80
+ /** A supply's terms as the drawer holds them: its price in pounds, as it's typed, and its days to pay. */
81
+ const fields = (s: Supply) => ({ price: (s.data.price / 100).toFixed(2), days: String(s.data["terms.days"]) });
82
+ /** Whether what's typed differs from the supply: Save and Never mind offered then, and said. */
83
+ const shown = () => draft.show(Object.keys(draft.edits()).length ? "changed" : "kept");
84
+ /** The drawer, with a supply in it, or closed; marked in the hash as it's opened or closed. */
85
+ function show(s: Supply | null, mark = true) {
86
+ open = s;
87
+ drawer.hidden = !s;
88
+ if (s) {
89
+ drawer.querySelector("h2")!.textContent = `Your terms: ${s.data.title}`;
90
+ draft.refuse("");
91
+ draft.fill(fields(s), true);
92
+ shown();
93
+ }
94
+ if (mark) hash.mark(s?.key ?? null);
95
+ }
96
+ terms.addEventListener("input", shown);
97
+ terms.addEventListener("submit", async (e) => {
98
+ e.preventDefault();
99
+ if (!open) return;
100
+ const { price, days } = draft.edits() as { price?: string; days?: string };
101
+ const why = await open.replace({
102
+ ...(price !== undefined ? { price: Math.round(Number(price) * 100) } : {}),
103
+ ...(days !== undefined ? { "terms.days": Number(days) } : {}),
104
+ });
105
+ draft.refuse(why ?? "");
106
+ // kept: what the drawer holds is the supply as it is now
107
+ if (!why && open) (draft.fill(fields(open), true), shown());
108
+ });
109
+ draft.inside(
110
+ drawer,
111
+ () => show(null),
112
+ () => open && (draft.refuse(""), draft.fill(fields(open), true), shown()),
113
+ );
114
+ iconify();
115
+
116
+ const form = document.querySelector<HTMLFormElement>("form.buy")!;
117
+ // the menu and the members, to choose from, as they come
118
+ const fill = () => {
119
+ const options = (sel: string, list: { value: number; text: string }[]) => {
120
+ const s = form.elements.namedItem(sel) as HTMLSelectElement;
121
+ const was = s.value;
122
+ s.replaceChildren(...list.map((o) => new Option(o.text, String(o.value))));
123
+ if (was) s.value = was;
124
+ // what was chosen gone from the list (you, before the page knew you): the first chosen, never none
125
+ if (s.selectedIndex < 0) s.selectedIndex = 0;
126
+ };
127
+ options(
128
+ "item",
129
+ [...items].map((i) => ({ value: i.key, text: i.data.name })),
130
+ );
131
+ options(
132
+ "supplier",
133
+ [...members].filter((m) => m.key !== me).map((m) => ({ value: m.key, text: m.data.name })),
134
+ );
135
+ for (const s of listed) s.draw();
136
+ };
137
+ /** The supplies on the page, a live list. */
138
+ const listed = document.getElementsByTagName(supplies.collections.supplies.tag) as HTMLCollectionOf<Supply>;
139
+ const hash = inHash(
140
+ "supply",
141
+ () => [...listed],
142
+ () => open,
143
+ (s) => show(s, false),
144
+ );
145
+ // (the menu and the members, not the supplies it draws: those it draws again, and never watches)
146
+ const watching = new MutationObserver(fill);
147
+ for (const el of [document.querySelector(menu.collections.items.place), document.querySelector("members-panel")])
148
+ if (el) watching.observe(el, { subtree: true, childList: true, attributes: true });
149
+ form.addEventListener("submit", (e) => {
150
+ e.preventDefault();
151
+ if (me === null) return; // not yet told who's buying
152
+ const n = (f: string) => Number((form.elements.namedItem(f) as HTMLInputElement).value);
153
+ said(
154
+ form.querySelector(".said")!,
155
+ document.querySelector<JSupplies>(supplies.collections.supplies.place)!.add({
156
+ title: (form.elements.namedItem("title") as HTMLInputElement).value,
157
+ item: n("item"),
158
+ qty: n("qty"),
159
+ price: Math.round(n("price") * 100), // in pence, as the store keeps money
160
+ buyer: me,
161
+ supplier: n("supplier"),
162
+ terms: { days: 30, by: "invoice" },
163
+ delivered: false,
164
+ }),
165
+ );
166
+ });
167
+
168
+ // defined last, once all they draw with is made: the items first, so a supply drawn as it's defined names its item
169
+ customElements.define(menu.collections.items.tag, class extends JItem {});
170
+ customElements.define(supplies.collections.supplies.tag, Supply);
@@ -0,0 +1,95 @@
1
+ {
2
+ "version": 1,
3
+ "orders": [
4
+ {
5
+ "id": 1,
6
+ "customer": "Ann",
7
+ "code": "q3Zt9xKcW1aB",
8
+ "status": "placed",
9
+ "placed": "2026-10-31T09:00:00Z",
10
+ "total": 640
11
+ },
12
+ {
13
+ "id": 2,
14
+ "customer": "Bo",
15
+ "code": "Bo7pL2mRv8Yd",
16
+ "status": "being made",
17
+ "placed": "2026-10-31T08:55:00Z",
18
+ "total": 650
19
+ },
20
+ {
21
+ "id": 3,
22
+ "customer": "Cy",
23
+ "code": "Cy4nH6sJe0Qx",
24
+ "status": "ready",
25
+ "placed": "2026-10-31T08:50:00Z",
26
+ "total": 500
27
+ },
28
+ {
29
+ "id": 4,
30
+ "customer": "Jo",
31
+ "code": "Jo9kT3wFz5Uh",
32
+ "status": "placed",
33
+ "placed": "2026-10-31T09:02:00Z",
34
+ "total": 340
35
+ },
36
+ {
37
+ "id": 5,
38
+ "customer": "Kim",
39
+ "code": "Km2rQ8vNt4Ls",
40
+ "status": "collected",
41
+ "placed": "2026-10-31T08:30:00Z",
42
+ "total": 250
43
+ }
44
+ ],
45
+ "lines": [
46
+ {
47
+ "id": 1,
48
+ "order": 1,
49
+ "item": 3,
50
+ "qty": 1,
51
+ "name": "Latte",
52
+ "price": 360
53
+ },
54
+ {
55
+ "id": 2,
56
+ "order": 1,
57
+ "item": 4,
58
+ "qty": 1,
59
+ "name": "Pot of tea",
60
+ "price": 280
61
+ },
62
+ {
63
+ "id": 3,
64
+ "order": 2,
65
+ "item": 6,
66
+ "qty": 1,
67
+ "name": "Cheese toastie",
68
+ "price": 650
69
+ },
70
+ {
71
+ "id": 4,
72
+ "order": 3,
73
+ "item": 1,
74
+ "qty": 2,
75
+ "name": "Espresso",
76
+ "price": 250
77
+ },
78
+ {
79
+ "id": 5,
80
+ "order": 4,
81
+ "item": 2,
82
+ "qty": 1,
83
+ "name": "Flat white",
84
+ "price": 340
85
+ },
86
+ {
87
+ "id": 6,
88
+ "order": 5,
89
+ "item": 1,
90
+ "qty": 1,
91
+ "name": "Espresso",
92
+ "price": 250
93
+ }
94
+ ]
95
+ }
@@ -0,0 +1,122 @@
1
+ // The till's page: the menu, to place an order by how many of each, and the till's orders (in the kitchen, ready
2
+ // to collect, and the last eight finished), each with the moves the till makes: cancel one not yet started, hand
3
+ // one over. Each item says one thing, how many are wanted of how many are left: one more or one fewer, never more
4
+ // than there are (what's left is the store's stock, falling as any till takes it); the order builds beside them,
5
+ // its lines and total. Placing is one write: the order and its lines, each line naming the order it adds ("$1").
6
+
7
+ import "joined/panel";
8
+ import "joined/toast";
9
+ import { iconify } from "joined/icons";
10
+ import { askFirst, said } from "joined/parts";
11
+ import * as menu from "./generated/menu.rules.ts";
12
+ import { JItem } from "./generated/menu.elements.ts";
13
+ import * as till from "./generated/till.rules.ts";
14
+ import { JLine, JOrder, write } from "./generated/till.elements.ts";
15
+ import { lineView, money, orderView } from "./views.ts";
16
+
17
+ /** A menu item, as the till places it: how many this order wants of how many are left, one more or one fewer. */
18
+ class Item extends JItem {
19
+ qty = 0;
20
+ #name = document.createElement("b");
21
+ #price = Object.assign(document.createElement("span"), { className: "price" });
22
+ #count = Object.assign(document.createElement("output"), { value: "0" });
23
+ #less = this.#step(-1, "−", "act");
24
+ #more = this.#step(1, "+", "act ink");
25
+ #bar = document.createElement("i");
26
+ #left = Object.assign(document.createElement("p"), { className: "left" });
27
+ connectedCallback() {
28
+ if (!this.childElementCount) {
29
+ const head = Object.assign(document.createElement("p"), { className: "head" }),
30
+ steps = Object.assign(document.createElement("p"), { className: "steps" }),
31
+ bar = Object.assign(document.createElement("span"), { className: "bar" });
32
+ head.append(this.#name, " ", this.#price);
33
+ steps.append(this.#less, this.#count, this.#more);
34
+ bar.append(this.#bar);
35
+ this.append(head, steps, bar, this.#left);
36
+ }
37
+ this.draw();
38
+ }
39
+ #step(by: number, words: string, className: string) {
40
+ const b = Object.assign(document.createElement("button"), { type: "button", className, textContent: words });
41
+ b.addEventListener("click", () => ((this.qty += by), this.draw()));
42
+ return b;
43
+ }
44
+ /** Its words, and how many of what's left: never more than there are, though another till takes some meanwhile. */
45
+ draw() {
46
+ const { name, price, stock } = this.data;
47
+ this.qty = Math.max(0, Math.min(this.qty, stock));
48
+ this.#name.textContent = name;
49
+ this.#price.textContent = money(price);
50
+ this.#count.value = String(this.qty);
51
+ this.#less.ariaLabel = `One fewer ${name}`;
52
+ this.#more.ariaLabel = `One more ${name}`;
53
+ this.#less.disabled = this.qty === 0;
54
+ this.#more.disabled = this.qty >= stock;
55
+ this.#bar.style.width = `${stock ? (this.qty / stock) * 100 : 0}%`;
56
+ this.#left.textContent = !stock
57
+ ? "none left"
58
+ : !this.qty
59
+ ? `${stock} left`
60
+ : `${this.qty} of ${stock} left${this.qty === stock ? ": that's all of them" : ""}`;
61
+ this.toggleAttribute("none", !stock);
62
+ this.toggleAttribute("all", !!stock && this.qty === stock);
63
+ ordered();
64
+ }
65
+ }
66
+
67
+ /** The items wanted, those asked for one or more of. */
68
+ const wanted = () => [...form.querySelectorAll<Item>(menu.collections.items.tag)].filter((i) => i.qty > 0);
69
+ /** The order as it's built: a line for each item wanted, its cost, and the total. */
70
+ function ordered() {
71
+ const total = wanted().reduce((n, i) => n + i.qty * i.data.price, 0);
72
+ form.querySelector(".lines")!.replaceChildren(
73
+ ...wanted().map((i) => {
74
+ const line = document.createElement("li");
75
+ line.append(
76
+ `${i.qty} × ${i.data.name}`,
77
+ Object.assign(document.createElement("span"), { textContent: money(i.qty * i.data.price) }),
78
+ );
79
+ return line;
80
+ }),
81
+ );
82
+ form.querySelector(".total b")!.textContent = money(total);
83
+ }
84
+ iconify();
85
+
86
+ const form = document.querySelector<HTMLFormElement>("form.place")!;
87
+ form.addEventListener("submit", async (e) => {
88
+ e.preventDefault();
89
+ const items = wanted();
90
+ const customer = (form.elements.namedItem("customer") as HTMLInputElement).value.trim();
91
+ if (!items.length) return (form.querySelector(".said")!.textContent = "Choose something first");
92
+ const why = await said(
93
+ form.querySelector(".said")!,
94
+ write(document.querySelector(till.collections.orders.place)!, [
95
+ { op: "add", path: "/orders/-", value: { customer } },
96
+ ...items.map((i) => ({
97
+ op: "add" as const,
98
+ path: "/lines/-" as const,
99
+ value: { order: "$1" as const, item: i.key, qty: i.qty }, // the order added first, by the id it will be given
100
+ })),
101
+ ]),
102
+ );
103
+ if (why) return;
104
+ form.reset();
105
+ for (const i of items) ((i.qty = 0), i.draw());
106
+ });
107
+
108
+ // defined last, once all they draw with is made
109
+ customElements.define(menu.collections.items.tag, Item);
110
+ customElements.define(
111
+ till.collections.orders.tag,
112
+ orderView(
113
+ JOrder,
114
+ [
115
+ { words: "Cancel", from: "placed", to: "cancelled", ask: "Cancel this order?" },
116
+ { words: "Hand over", from: "ready", to: "collected" },
117
+ ],
118
+ askFirst(document.querySelector("dialog.ask")!),
119
+ { link: true }, // the customer's own link, to give them
120
+ ),
121
+ );
122
+ customElements.define(till.collections.lines.tag, lineView(JLine));
@@ -0,0 +1,20 @@
1
+ // The cafe's own rules, which no definition can say: an order's lines are set in the write that places it, and
2
+ // never after (a line added to an order there already is refused); and a supply delivered keeps its price and terms,
3
+ // as both sides' books have them.
4
+ import type { Trust } from "joined/server";
5
+
6
+ export const trust: Trust = ({ doc, ops }, _who, read) => {
7
+ if (
8
+ ops.some((o) => o.op === "add" && o.path === "/lines/-") &&
9
+ !ops.some((o) => o.op === "add" && o.path === "/orders/-")
10
+ )
11
+ return "an order's lines are set when it's placed";
12
+ if (doc === "supplies") {
13
+ const delivered = new Set(
14
+ (read("supplies").supplies as { id: number; delivered: boolean }[]).filter((s) => s.delivered).map((s) => s.id),
15
+ );
16
+ const kept = (path: string) => delivered.has(Number(path.match(/^\/supplies\/(\d+)\/(price|terms)/)?.[1]));
17
+ if (ops.some((o) => kept(o.path))) return "a delivered supply keeps its price and terms";
18
+ }
19
+ return null;
20
+ };
@@ -0,0 +1,171 @@
1
+ // The cafe's views, shared by the till and a customer's page: an order, drawn as its number, its customer, its total,
2
+ // its lines (each an element of the same document, linked by its order), and the moves this screen makes on it that
3
+ // its person may (its may); a line, which draws again the order it's of. Money is in pence, shown in pounds. (The
4
+ // kitchen draws its orders as a display: ./kitchen.ts.)
5
+
6
+ import type { Answer } from "joined/page";
7
+ import { said } from "joined/parts";
8
+
9
+ export const money = (pence: number) => `£${(pence / 100).toFixed(2)}`;
10
+ /** A member's name, as the group's members on the page have it; none, once they've left (a link to them is cleared as
11
+ * they're removed). */
12
+ export const nameOf = (members: Iterable<{ key: number; data: { name: string } }>, id?: number) =>
13
+ id === undefined ? "someone who's left" : ([...members].find((m) => m.key === id)?.data.name ?? `member ${id}`);
14
+ /** A wait, as a person says it: 4 min, 2 h, 3 days. */
15
+ export const waitOf = (minutes: number) =>
16
+ minutes < 60
17
+ ? `${minutes} min`
18
+ : minutes < 1440
19
+ ? `${Math.floor(minutes / 60)} h`
20
+ : `${Math.floor(minutes / 1440)} day${minutes < 2880 ? "" : "s"}`;
21
+
22
+ /** How long an order's waited, in whole minutes, from when it was placed. */
23
+ export const waitedSince = (placed: string) => Math.max(0, Math.floor((Date.now() - Date.parse(placed)) / 60_000)) || 0;
24
+
25
+ type Order = HTMLElement & {
26
+ key: number;
27
+ data: { customer: string; status?: string; total?: number; code?: string; placed?: string };
28
+ replace(fields: { status?: string }, why?: string): Promise<Answer>;
29
+ draw?(): void;
30
+ };
31
+ type Line = HTMLElement & {
32
+ key: number;
33
+ data: { order: number; qty: number; name?: string; price?: number };
34
+ attributeChangedCallback?(): void;
35
+ disconnectedCallback?(): void;
36
+ };
37
+ /** A move this screen makes on an order: its words, the status it's from and to, and whether to ask first. */
38
+ export type Move = { words: string; from: string; to: string; ask?: string };
39
+
40
+ /** Where an order and its lines are held together (the element holding both its collections): its orders by key, and
41
+ * the lines linking to each, kept as each view comes and goes, so an order draws its lines without a search. */
42
+ type Held = { orders: Map<number, Order>; lines: Map<number, Set<Line>> };
43
+ const helds = new WeakMap<Node, Held>();
44
+ const heldOf = (el: Element) => {
45
+ const at = el.closest("[doc]")?.parentElement ?? document;
46
+ return helds.get(at) ?? helds.set(at, { orders: new Map(), lines: new Map() }).get(at)!;
47
+ };
48
+ /** Every order drawn, and how long each has waited said again each minute. */
49
+ const drawn = new Set<Order>();
50
+ let ticking: ReturnType<typeof setInterval> | undefined;
51
+
52
+ /** A view of an order (its document's compiled class): the number, the customer, the total, its lines, how long
53
+ * it's waited, and a button for each move this screen makes from where it is, where its person may make it. */
54
+ export function orderView<T extends new (...a: any[]) => Order>(
55
+ Base: T,
56
+ moves: Move[],
57
+ ask?: (q: string, then: () => void) => void,
58
+ {
59
+ link = false,
60
+ late = true,
61
+ }: {
62
+ /** the customer's own link, to give them (the till's) */
63
+ link?: boolean;
64
+ /** whether it says it's late, after ten minutes: the staff's to know, never the customer's */
65
+ late?: boolean;
66
+ } = {},
67
+ ) {
68
+ return class extends Base {
69
+ #head = document.createElement("p");
70
+ #lines = document.createElement("ul");
71
+ #acts = document.createElement("p");
72
+ #said = Object.assign(document.createElement("p"), { className: "said", role: "status" });
73
+ #held?: Held;
74
+ #acted = ""; // where it was and what its person might, as its moves were last made
75
+ connectedCallback() {
76
+ if (!this.childElementCount) {
77
+ this.#head.className = "head";
78
+ this.#acts.className = "acts";
79
+ this.append(this.#head, this.#lines, this.#acts, this.#said);
80
+ }
81
+ (this.#held = heldOf(this)).orders.set(this.key, this);
82
+ drawn.add(this);
83
+ ticking ??= setInterval(() => drawn.forEach((o) => o.draw?.()), 60_000);
84
+ this.draw();
85
+ }
86
+ disconnectedCallback() {
87
+ if (this.#held?.orders.get(this.key) === this) this.#held.orders.delete(this.key);
88
+ drawn.delete(this);
89
+ // none left to draw: the minute's clock let go, to start again with the next
90
+ if (!drawn.size) ticking = void clearInterval(ticking);
91
+ }
92
+ draw() {
93
+ if (!this.isConnected) return;
94
+ const { customer, status = "", total = 0, placed = "" } = this.data;
95
+ const waited = waitedSince(placed);
96
+ this.toggleAttribute(
97
+ "late",
98
+ late && status !== "ready" && waited >= 10 && !this.matches("[status=collected],[status=cancelled]"),
99
+ );
100
+ this.#head.replaceChildren(
101
+ Object.assign(document.createElement("b"), { textContent: `#${this.key} ${customer}` }),
102
+ ` ${money(total)} `,
103
+ Object.assign(document.createElement("span"), {
104
+ className: "state",
105
+ textContent: status === "placed" || status === "being made" ? `${status} · ${waitOf(waited)}` : status,
106
+ }),
107
+ ...(link && this.data.code
108
+ ? [
109
+ " · ",
110
+ Object.assign(document.createElement("a"), {
111
+ href: `/cafe/order/${this.data.code}`,
112
+ textContent: `${customer}'s link`,
113
+ }),
114
+ ]
115
+ : []),
116
+ );
117
+ this.#lines.replaceChildren(
118
+ ...[...(this.#held?.lines.get(this.key) ?? [])].map((l) =>
119
+ Object.assign(document.createElement("li"), { textContent: `${l.data.qty} × ${l.data.name}` }),
120
+ ),
121
+ );
122
+ // its moves made again only as where it is, or what its person may, changes: not each minute, under a finger
123
+ const acted = `${status} ${this.getAttribute("may") ?? ""}`;
124
+ if (acted === this.#acted) return;
125
+ this.#acted = acted;
126
+ this.#acts.replaceChildren(
127
+ ...moves
128
+ .filter((m) => m.from === status && this.matches(`[may~="replace:status>${encodeURIComponent(m.to)}"]`))
129
+ .map((m) => {
130
+ const b = Object.assign(document.createElement("button"), {
131
+ type: "button",
132
+ className: m.to === "cancelled" ? "act danger" : "act ink",
133
+ textContent: m.words,
134
+ });
135
+ const go = () => said(this.#said, this.replace({ status: m.to }));
136
+ b.addEventListener("click", () => (m.ask && ask ? ask(m.ask, go) : go()));
137
+ return b;
138
+ }),
139
+ );
140
+ }
141
+ };
142
+ }
143
+
144
+ /** A view of a line: drawn by its order, which it draws again as it comes, changes (even to another order) or goes. */
145
+ export function lineView<T extends new (...a: any[]) => Line>(Base: T) {
146
+ return class extends Base {
147
+ #held?: Held;
148
+ #of?: number; // the order it's kept under
149
+ /** Kept under the order it's of now, and that order (and any it's left) drawn again. */
150
+ #link(of?: number) {
151
+ const held = this.#held!,
152
+ was = this.#of;
153
+ if (was !== undefined) held.lines.get(was)?.delete(this);
154
+ if (of !== undefined) (held.lines.get(of) ?? held.lines.set(of, new Set()).get(of)!).add(this);
155
+ this.#of = of;
156
+ for (const k of new Set([was, of])) if (k !== undefined) held.orders.get(k)?.draw?.();
157
+ }
158
+ connectedCallback() {
159
+ this.#held = heldOf(this);
160
+ this.#link(this.data.order);
161
+ }
162
+ disconnectedCallback() {
163
+ super.disconnectedCallback?.();
164
+ this.#link(undefined);
165
+ }
166
+ attributeChangedCallback() {
167
+ super.attributeChangedCallback?.();
168
+ if (this.#held) this.#link(this.data.order);
169
+ }
170
+ };
171
+ }
@@ -0,0 +1,28 @@
1
+ // The docs/ folder at /docs/:file: markdown rendered by Bun.markdown, images as files.
2
+
3
+ import tokens from "joined/tokens.css" with { type: "text" };
4
+ import page from "joined/page.css" with { type: "text" };
5
+ import style from "./app.css" with { type: "text" };
6
+
7
+ const DOCS = `${import.meta.dir}/../docs`;
8
+
9
+ export async function docs(req: { params: { file: string } }) {
10
+ // a name in docs/, and only that: no dots or slashes, however written (..%2F.env read joined's .env)
11
+ if (!/^[a-z0-9-]+(\.[a-z0-9]+)?$/.test(req.params.file)) return new Response("Not found", { status: 404 });
12
+ const name = req.params.file.replace(/\.md$/, "");
13
+ const md = Bun.file(`${DOCS}/${name}.md`);
14
+ if (await md.exists())
15
+ return new Response(
16
+ `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>joined — ${name}</title><style>${tokens}${page}${style}
17
+ img { max-width: 100%; height: auto; display: block; margin: var(--space-20) 0; }
18
+ pre { padding: var(--space-12) var(--space-16); background: var(--well); border: 1px solid var(--line); border-radius: 4px; overflow-x: auto; font-size: 0.85rem; }
19
+ code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em; }
20
+ hr { border: 0; border-top: 1px solid var(--line); margin: var(--space-24) 0; }
21
+ table { width: 100%; border-collapse: collapse; margin: var(--space-16) 0; font-size: 0.95rem; }
22
+ th, td { text-align: left; padding: var(--space-8) var(--space-10); border-bottom: 1px solid var(--line); }
23
+ </style></head><body><p class="bar"><a href="/">← joined</a></p>${Bun.markdown.html(await md.text(), { tables: true })}</body></html>`,
24
+ { headers: { "content-type": "text/html; charset=utf-8" } },
25
+ );
26
+ const raw = Bun.file(`${DOCS}/${req.params.file}`);
27
+ return (await raw.exists()) ? new Response(raw) : new Response("Not found", { status: 404 });
28
+ }
Binary file
@@ -0,0 +1,45 @@
1
+ // Written by src/compile.ts from examples/hello/stories.jsonl: change the stories, not this.
2
+
3
+ // Each collection is an element, defined here: on a page, it holds the document, and its items
4
+ // are put inside it. Each item is an element, defined by the page: as it is, or by a view that
5
+ // extends it. A field is an attribute (a list of ids as IDREFS: "1 3"), and an item's key the
6
+ // document's id for it. How an item looks is the app's: its view, or a display holding the
7
+ // collection draws it. Ops go up as an "ops" event, for the page to send to hello, with why they
8
+ // were made if it's said; only the ops the stories allow can be sent. Each returns its write's answer.
9
+
10
+ import { hold, letGo, type Answer } from "joined/page";
11
+
12
+ // the document an element is of: its collection's doc attribute (an instance of a kind, shelf:1), or hello
13
+ const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "hello";
14
+ // an element's ops go up as an "ops" event; what it returns is the write's answer, done or refused
15
+ const send = (from: Element, ops: object[], why?: string) => new Promise<Answer>((answer) => from.dispatchEvent(new CustomEvent("ops", { bubbles: true, composed: true, detail: { doc: docOf(from), ops, why, answer } })));
16
+
17
+ /** hello's own fields, as the stories give them. */
18
+ export type JHelloData = { "greeting"?: string };
19
+
20
+ /** <j-hello>: hello itself, holding it, its own fields each an attribute. */
21
+ export class JHello extends HTMLElement {
22
+ static observedAttributes = ["greeting","may"];
23
+ #data?: JHelloData;
24
+ /** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
25
+ get data(): JHelloData {
26
+ return (this.#data ??= Object.freeze({ "greeting": this.getAttribute("greeting") ?? undefined }));
27
+ }
28
+ /** How a view shows it, drawn again whenever an attribute changes (what its person may do, its may, too). */
29
+ draw?(): void;
30
+ attributeChangedCallback() { this.#data = undefined; this.draw?.(); }
31
+ connectedCallback() { hold(this, docOf(this), "hello", "j-hello", {"greeting":"string"}, true); }
32
+ disconnectedCallback() { letGo(this); }
33
+ /** Replace some of its fields (greeting): one write for all of them. */
34
+ replace(fields: Partial<Pick<JHelloData, "greeting">>) {
35
+ return send(this, Object.entries(fields).filter(([, value]) => value !== undefined).map(([field, value]) => ({ op: "replace", path: `/${field}`, value })));
36
+ }
37
+ }
38
+
39
+ /** An op on hello the stories allow, in any of its collections. */
40
+ export type HelloOp =
41
+ | { op: "replace"; path: "/greeting"; value: string };
42
+
43
+ /** One write to hello across its collections (a move with its legs): sent from an element of it, checked
44
+ * whole by its rules, and made whole or not at all. */
45
+ export function write(from: Element, ops: HelloOp[], why?: string) { return send(from, ops, why); }