@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,60 @@
1
+ // Written by src/compile.ts from examples/trips/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 trips, 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 trips
13
+ const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "trips";
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
+ /** A trip's fields, as the stories give them. */
18
+ export type JTripData = { "name": string; "organisers": number[]; "travellers": number[] };
19
+ /** An add of a trip, as it's sent: its fields as the stories write them. */
20
+ export type JTripValue = { "name": string; "organisers": number[]; "travellers": number[] };
21
+
22
+ /** <j-trip key>: name (string), organisers (ids in members), travellers (ids in members), each an attribute. */
23
+ export class JTrip extends HTMLElement {
24
+ static observedAttributes = ["name","organisers","travellers","may"];
25
+ #data?: JTripData;
26
+ /** Its id in the document. */
27
+ get key() { return Number(this.getAttribute("key")); }
28
+ get path() { return `/trips/${this.key}`; }
29
+ /** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
30
+ get data(): JTripData {
31
+ return (this.#data ??= Object.freeze({ "name": this.getAttribute("name") ?? "", "organisers": Object.freeze((this.getAttribute("organisers") ?? "").split(" ").filter(Boolean).map(Number)), "travellers": Object.freeze((this.getAttribute("travellers") ?? "").split(" ").filter(Boolean).map(Number)) }) as JTripData);
32
+ }
33
+ /** How a view shows it, drawn again whenever an attribute changes (what its person may do, its may, too). */
34
+ draw?(): void;
35
+ attributeChangedCallback() { this.#data = undefined; this.draw?.(); }
36
+ /** Replace some of its fields (name): one write for all of them. */
37
+ replace(fields: Partial<Pick<JTripData, "name">>, why?: string) {
38
+ return send(this, Object.entries(fields).filter(([, value]) => value !== undefined).map(([field, value]) => ({ op: "replace", path: `${this.path}/${field.replace(".", "/")}`, value })), why);
39
+ }
40
+ }
41
+
42
+ /** <j-trips>: the document's trips, each a <j-trip> inside it. */
43
+ export class JTrips extends HTMLElement {
44
+ connectedCallback() { hold(this, docOf(this), "trips", "j-trip", {"name":"string","organisers":"ids","travellers":"ids"}); }
45
+ disconnectedCallback() { letGo(this); }
46
+ /** Its trips, as the document has them. */
47
+ get items() { return [...this.children].filter((el): el is JTrip => el.localName === "j-trip"); }
48
+ /** Add trips: one write for all of them. */
49
+ add(...values: JTripValue[]) { return send(this, values.map((value) => ({ op: "add", path: "/trips/-", value }))); }
50
+ }
51
+ customElements.define("j-trips", JTrips);
52
+
53
+ /** An op on trips the stories allow, in any of its collections. */
54
+ export type TripsOp =
55
+ | { op: "add"; path: "/trips/-"; value: JTripValue }
56
+ | { op: "replace"; path: `/trips/${number}/name`; value: string };
57
+
58
+ /** One write to trips across its collections (a move with its legs): sent from an element of it, checked
59
+ * whole by its rules, and made whole or not at all. */
60
+ export function write(from: Element, ops: TripsOp[], why?: string) { return send(from, ops, why); }
@@ -0,0 +1,28 @@
1
+ // Written by src/compile.ts from examples/trips/stories.jsonl: change the stories, not this.
2
+
3
+ /** Each collection's item, as one row of its table (`t`) is it: what the query gathers, and what the store keeps of a
4
+ * row a write changes, before and after, to tell each document holding it. */
5
+ export const items: Record<string, string> = {
6
+ "trips": "json_patch('{}', json_object('id', t.id,\n 'name', t.\"name\",\n 'organisers', json(NULLIF((SELECT json_group_array(p.\"organisers\" ORDER BY p.id) FROM \"trips_organisers\" p WHERE p.\"trips\" = t.id), '[]')),\n 'travellers', json(NULLIF((SELECT json_group_array(p.\"travellers\" ORDER BY p.id) FROM \"trips_travellers\" p WHERE p.\"trips\" = t.id), '[]'))))"
7
+ };
8
+
9
+ /** trips, whole, as one statement over its tables (SQLite): its parameters $doc, and $member, who's asking (none: all of it). */
10
+ export const query = `SELECT json_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),
11
+ 'trips', json((SELECT json_group_array(json_patch('{}', json_object('id', t.id,
12
+ 'name', t."name",
13
+ 'organisers', json(NULLIF((SELECT json_group_array(p."organisers" ORDER BY p.id) FROM "trips_organisers" p WHERE p."trips" = t.id), '[]')),
14
+ 'travellers', json(NULLIF((SELECT json_group_array(p."travellers" ORDER BY p.id) FROM "trips_travellers" p WHERE p."trips" = t.id), '[]')))) ORDER BY t.id)
15
+ FROM "trips" t WHERE ($member IS NULL OR ((EXISTS (SELECT 1 FROM "trips_organisers" p WHERE p."trips" = t.id AND p."organisers" = $member)) OR (EXISTS (SELECT 1 FROM "trips_travellers" p WHERE p."trips" = t.id AND p."travellers" = $member))))))) AS doc`;
16
+
17
+ /** Its rules over values (definitions' takes, gives and totals), as triggers the store makes in its database: run in
18
+ * each write's transaction, and told as any change is. */
19
+ export const triggers: string[] = [];
20
+
21
+ /** Its items, its query and its triggers, as Postgres says them. */
22
+ export const postgres = {
23
+ "items": {
24
+ "trips": "json_strip_nulls(json_build_object('id', t.id,\n 'name', t.\"name\",\n 'organisers', (SELECT json_agg(p.\"organisers\" ORDER BY p.id) FROM \"trips_organisers\" p WHERE p.\"trips\" = t.id),\n 'travellers', (SELECT json_agg(p.\"travellers\" ORDER BY p.id) FROM \"trips_travellers\" p WHERE p.\"trips\" = t.id)))"
25
+ },
26
+ "query": "SELECT json_build_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),\n 'trips', (SELECT COALESCE(json_agg(json_strip_nulls(json_build_object('id', t.id,\n 'name', t.\"name\",\n 'organisers', (SELECT json_agg(p.\"organisers\" ORDER BY p.id) FROM \"trips_organisers\" p WHERE p.\"trips\" = t.id),\n 'travellers', (SELECT json_agg(p.\"travellers\" ORDER BY p.id) FROM \"trips_travellers\" p WHERE p.\"trips\" = t.id))) ORDER BY t.id), '[]')\n FROM \"trips\" t WHERE ($member IS NULL OR ((EXISTS (SELECT 1 FROM \"trips_organisers\" p WHERE p.\"trips\" = t.id AND p.\"organisers\" = $member)) OR (EXISTS (SELECT 1 FROM \"trips_travellers\" p WHERE p.\"trips\" = t.id AND p.\"travellers\" = $member)))))) AS doc",
27
+ "triggers": []
28
+ };
@@ -0,0 +1,62 @@
1
+ // Written by src/compile.ts from examples/trips/stories.jsonl: change the stories, not this.
2
+
3
+ export type Op = { op: "add" | "replace" | "remove"; path: string; value?: any };
4
+
5
+ /** The document, and each collection: its element, the element its items are, their fields, those an add needs, and
6
+ * what each link is to. */
7
+ export const doc = "trips";
8
+ export const collections = {
9
+ "trips": {
10
+ "place": "j-trips",
11
+ "tag": "j-trip",
12
+ "fields": {
13
+ "name": "string",
14
+ "organisers": "ids",
15
+ "travellers": "ids"
16
+ },
17
+ "needs": [
18
+ "name",
19
+ "organisers",
20
+ "travellers"
21
+ ],
22
+ "links": {
23
+ "organisers": "members",
24
+ "travellers": "members"
25
+ }
26
+ }
27
+ } as const;
28
+ /** Whether every story is anonymous, none told as a member: where every app's is, the project has no members. */
29
+ export const anonymous = false;
30
+ /** What kind of document trips is, by the flows it uses: static, changed only with a release (a page reads it, and
31
+ * never subscribes); read-only, live, but no page writes it (derived); or shared, its members' to write. */
32
+ export const type = "shared";
33
+ /** The parts of trips a page may hold alone, by its query's parameters (its definitions' window): null, none. */
34
+ export const window = null;
35
+ /** The document as one statement over its tables (generated/queries.ts). */
36
+ export { query, postgres, items, triggers } from "./queries.ts";
37
+
38
+ /** trips is one document. */
39
+ export const kind = null;
40
+
41
+ /** Each grant, read off the stories: the roles whose stories do it (a member: anyone). An op no story
42
+ * does isn't one. */
43
+ export const grants: Record<string, string[]> = {
44
+ "add trips": [
45
+ "organiser"
46
+ ],
47
+ "replace trips.name": [
48
+ "organiser"
49
+ ]
50
+ };
51
+
52
+ /** Who someone is to trips (and to an item of it, `at`), beyond a member: each role whose who-path finds them. */
53
+ export function rolesOf(m: number, read: (doc: string) => any, doc: string, at?: any): string[] {
54
+ const item: any = at;
55
+ return [
56
+ (item?.["organisers"] === m || (Array.isArray(item?.["organisers"]) && item?.["organisers"].includes(m))) ? "organiser" : "",
57
+ (item?.["travellers"] === m || (Array.isArray(item?.["travellers"]) && item?.["travellers"].includes(m))) ? "traveller" : "",
58
+ ].filter(Boolean);
59
+ }
60
+
61
+ /** Each person is sent only what they see of trips (its definitions' owns): its query, by its member. */
62
+ export const sees = true;
@@ -0,0 +1,47 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <title>joined: your trips</title>
7
+ <link rel="stylesheet" href="joined/tokens.css" />
8
+ <link rel="stylesheet" href="joined/page.css" />
9
+ <link rel="stylesheet" href="../app.css" />
10
+ <link rel="stylesheet" href="./trips.css" />
11
+ </head>
12
+ <body class="trips">
13
+ <header>
14
+ <a class="brand" href="/"><img src="joined/icon.png" alt="" width="28" height="28" />joined</a>
15
+ <members-panel></members-panel>
16
+ </header>
17
+ <div class="head">
18
+ <div>
19
+ <h1>Your trips</h1>
20
+ <p class="lead">The trips you organise or travel on. Open one for its week and its map.</p>
21
+ </div>
22
+ <button type="button" class="act ink new members-only" data-icon="add">New trip</button>
23
+ </div>
24
+ <main>
25
+ <j-trips></j-trips>
26
+ <!-- each trip's events, held whole, for its card: its dates, its places, what's next and its map -->
27
+ <div class="held" hidden></div>
28
+ </main>
29
+ <p class="note">Only the trips you're on are here. <a href="/">← joined</a></p>
30
+ <dialog class="modal new-trip">
31
+ <form method="dialog">
32
+ <header><h2>A new trip</h2></header>
33
+ <label>Its name<input name="name" required autocomplete="off" /></label>
34
+ <fieldset>
35
+ <legend>Who travels</legend>
36
+ <div class="who"></div>
37
+ </fieldset>
38
+ <p class="said" role="status"></p>
39
+ <footer>
40
+ <button type="button" class="act" name="never">Never mind</button>
41
+ <button class="act ink" name="go" data-icon="add">Start it</button>
42
+ </footer>
43
+ </form>
44
+ </dialog>
45
+ <script type="module" src="./page.ts"></script>
46
+ </body>
47
+ </html>
@@ -0,0 +1,52 @@
1
+ // A trip's map, small, on its card: OpenStreetMap's tiles, as near as fits all its places, a pin for each event in
2
+ // its hue, drawn about its middle so any card's frame shows them; no places, said so.
3
+
4
+ import { hueOf } from "joined/hues";
5
+ import { TILE, bounds, geo, tileAt, world } from "../trip/mercator.ts";
6
+
7
+ /** The map of a trip's places, from its events: each its key (its pin's hue) and where it is, if it's settled. */
8
+ export function mapOf(events: { key: number; coordinates?: string }[]) {
9
+ const box = Object.assign(document.createElement("div"), { className: "map" });
10
+ const pins = events.flatMap((e) => {
11
+ const p = geo(e.coordinates);
12
+ return p ? [{ ...p, key: e.key }] : [];
13
+ });
14
+ if (!pins.length) {
15
+ box.classList.add("none");
16
+ box.append(Object.assign(document.createElement("span"), { textContent: "No places yet" }));
17
+ return box;
18
+ }
19
+ // the nearest zoom that fits every pin in the smallest frame a card gives its map
20
+ const [W, H, FIT_W, FIT_H] = [640, 480, 150, 110];
21
+ let z = 13;
22
+ for (; z > 2; z--) {
23
+ const [x0, x1, y0, y1] = bounds(pins.map((p) => world(p.lat, p.lng, z)));
24
+ if (x1 - x0 <= FIT_W && y1 - y0 <= FIT_H) break;
25
+ }
26
+ const xy = pins.map((p) => ({ ...world(p.lat, p.lng, z), key: p.key }));
27
+ const [left, right, top, bottom] = bounds(xy);
28
+ const [x0, y0] = [(left + right) / 2 - W / 2, (top + bottom) / 2 - H / 2];
29
+ const ground = Object.assign(document.createElement("div"), { className: "ground" });
30
+ const n = 2 ** z;
31
+ for (let ty = Math.floor(y0 / TILE); ty * TILE < y0 + H; ty++)
32
+ for (let tx = Math.floor(x0 / TILE); tx * TILE < x0 + W; tx++) {
33
+ if (ty < 0 || ty >= n) continue;
34
+ const img = Object.assign(document.createElement("img"), {
35
+ src: tileAt(z, tx, ty),
36
+ alt: "",
37
+ loading: "lazy",
38
+ });
39
+ img.style.left = `${tx * TILE - x0}px`;
40
+ img.style.top = `${ty * TILE - y0}px`;
41
+ ground.append(img);
42
+ }
43
+ for (const p of xy) {
44
+ const pin = Object.assign(document.createElement("i"), { className: "pin" });
45
+ pin.style.left = `${p.x - x0}px`;
46
+ pin.style.top = `${p.y - y0}px`;
47
+ pin.style.setProperty("--hue", hueOf(p.key));
48
+ ground.append(pin);
49
+ }
50
+ box.append(ground, Object.assign(document.createElement("small"), { textContent: "© OpenStreetMap" }));
51
+ return box;
52
+ }
@@ -0,0 +1,8 @@
1
+ // Made by `bun run compile` from the stories: what they say beyond the migrations before it.
2
+ import { createTable } from "joined/migrate";
3
+
4
+ export const changes = [
5
+ createTable("trips", { columns: { name: { type: "text", is: { type: "string", said: "name is a string" } } }, strict: true, needs: { columns: ["name"], said: "a trip needs name (string), organisers (ids in members), travellers (ids in members)" } }),
6
+ createTable("trips_organisers", { columns: { trips: { type: "integer", to: { table: "trips", said: "trips is one of trips", onDelete: "cascade" } }, organisers: { type: "integer", to: { table: "members", said: "organisers links only to members the members document holds", onDelete: "cascade" } } }, unique: [["trips", "organisers"]], strict: true, once: "organisers links to {organisers} already" }),
7
+ createTable("trips_travellers", { columns: { trips: { type: "integer", to: { table: "trips", said: "trips is one of trips", onDelete: "cascade" } }, travellers: { type: "integer", to: { table: "members", said: "travellers links only to members the members document holds", onDelete: "cascade" } } }, unique: [["trips", "travellers"]], strict: true, once: "travellers links to {travellers} already" }),
8
+ ];
@@ -0,0 +1,46 @@
1
+ // New trip: its name and who travels, asked in the app's modal (nobody joins a trip once it's made), and started with
2
+ // the person asking organising. Refused, it stays open with what was typed, and why is said in it.
3
+
4
+ import { members, signedIn } from "joined/panel";
5
+ import { said } from "joined/parts";
6
+ import type { JTrips } from "./generated/elements.ts";
7
+ import { collections } from "./generated/rules.ts";
8
+
9
+ let me: number | undefined;
10
+ signedIn((m) => (me = m));
11
+
12
+ const dialog = document.querySelector<HTMLDialogElement>("dialog.new-trip")!,
13
+ form = dialog.querySelector("form")!,
14
+ who = dialog.querySelector<HTMLElement>(".who")!;
15
+ document.querySelector("button.new")!.addEventListener("click", () => {
16
+ form.reset();
17
+ who.replaceChildren(
18
+ ...[...members]
19
+ .filter((m) => m.key !== me)
20
+ .map((m) => {
21
+ const label = Object.assign(document.createElement("label"), { className: "tick" });
22
+ const box = Object.assign(document.createElement("input"), {
23
+ type: "checkbox",
24
+ name: "traveller",
25
+ value: String(m.key),
26
+ });
27
+ label.append(m.data.name, box);
28
+ return label;
29
+ }),
30
+ );
31
+ dialog.showModal();
32
+ });
33
+ dialog.querySelector("[name=never]")!.addEventListener("click", () => dialog.close());
34
+ form.addEventListener("submit", (e) => {
35
+ e.preventDefault();
36
+ const name = (form.elements.namedItem("name") as HTMLInputElement).value.trim();
37
+ if (!name || me === undefined) return;
38
+ const travellers = [...form.querySelectorAll<HTMLInputElement>("[name=traveller]:checked")].map((b) =>
39
+ Number(b.value),
40
+ );
41
+ // refused, it stays open with what was typed, and why is said in it; kept, it closes
42
+ said(
43
+ form.querySelector(".said")!,
44
+ document.querySelector<JTrips>(collections.trips.place)!.add({ name, organisers: [me], travellers }),
45
+ ).then((why) => why || dialog.close());
46
+ });
@@ -0,0 +1,9 @@
1
+ // The trips' page: the trips its person sees (its owns: those they organise or travel on), each a card that is a link
2
+ // to its own page, /trip/<key> (./card.ts), drawn from its trip and that trip's events, held whole beside the trips.
3
+ // New trip asks its name and who travels, and starts it with its person organising (./new-trip.ts).
4
+
5
+ import "./card.ts";
6
+ import "./new-trip.ts";
7
+ import { iconify } from "joined/icons";
8
+
9
+ iconify();
@@ -0,0 +1,7 @@
1
+ // The trips: its document, each trip the root of a trip document (../trip, trip:1), and its page at /trips, the
2
+ // trips its person sees.
3
+
4
+ import page from "./index.html";
5
+
6
+ export * as document from "./generated/rules.ts";
7
+ export const routes = { "/trips": page };
@@ -0,0 +1,12 @@
1
+ {"cast":{"phone":"peter","stranger":null}}
2
+ {"links":{"organisers":"members","travellers":"members"}}
3
+ {"roles":{"organiser":"@organisers","traveller":"@travellers"}}
4
+ {"owns":"organiser | traveller"}
5
+ {"story":"Nora starts a trip to Okinawa, organising it, with Dev travelling: Peter and Nick, on neither side, aren't told of it","as":"nora","does":{"doc":"trips","ops":[{"op":"add","path":"/trips/-","value":{"name":"Okinawa","organisers":[3],"travellers":[4]}}]},"seen":["nora","dev"],"unseen":["peter","nick"]}
6
+ {"story":"Peter renames the Japan trip, and those on it see it","as":"peter","does":{"doc":"trips","ops":[{"op":"replace","path":"/trips/1/name","value":"Japan, November"}]},"seen":["peter","phone","nick","nora"],"unseen":["dev"]}
7
+ {"story":"Only an organiser renames a trip: Nick, travelling, can't","as":"nick","cannot":{"doc":"trips","ops":[{"op":"replace","path":"/trips/1/name","value":"Nick's Japan"}]},"refused":"you may not replace at /trips/1/name"}
8
+ {"story":"Dev can't start a trip for Nick to organise: you start a trip you organise","as":"dev","cannot":{"doc":"trips","ops":[{"op":"add","path":"/trips/-","value":{"name":"Mars","organisers":[2],"travellers":[4]}}]},"refused":"you may not add at /trips/-"}
9
+ {"story":"A trip's organisers are members of the group","as":"peter","cannot":{"doc":"trips","ops":[{"op":"add","path":"/trips/-","value":{"name":"Mars","organisers":[1,9],"travellers":[2]}}]}}
10
+ {"story":"A trip needs a name","as":"peter","cannot":{"doc":"trips","ops":[{"op":"add","path":"/trips/-","value":{"organisers":[1],"travellers":[2]}}]}}
11
+ {"story":"Nobody adds a traveller once a trip is made: Dev can't put himself on Japan","as":"dev","cannot":{"doc":"trips","ops":[{"op":"add","path":"/trips/1/travellers/-","value":4}]},"refused":"no add at /trips/1/travellers/-"}
12
+ {"story":"A stranger can't start a trip","as":"stranger","cannot":{"doc":"trips","ops":[{"op":"add","path":"/trips/-","value":{"name":"Nowhere","organisers":[1],"travellers":[2]}}]}}
@@ -0,0 +1,183 @@
1
+ /* The trips' look: a list page, as the root speaks: its name in `section`, a lead, New trip at its right. Each trip a
2
+ card that is its link: its map beside it (above it, on a phone), its name, its days and places, what's next in a
3
+ tint, and who's on it, each in their colour. A trip with no events says so, and how to start. */
4
+
5
+ .trips > header {
6
+ display: flex;
7
+ align-items: center;
8
+ gap: var(--space-16);
9
+ padding: var(--space-20) 0 0;
10
+ }
11
+ .trips .brand {
12
+ flex: 1;
13
+ margin: 0;
14
+ }
15
+ .trips .head {
16
+ display: flex;
17
+ flex-wrap: wrap;
18
+ align-items: flex-end;
19
+ gap: var(--space-16);
20
+ margin: var(--space-32) 0 var(--space-24);
21
+ }
22
+ .trips .head > div {
23
+ flex: 1;
24
+ min-width: 16rem;
25
+ }
26
+ .trips .lead {
27
+ margin: 0;
28
+ }
29
+ .trips .head .new {
30
+ min-height: 44px;
31
+ padding: 0 var(--space-16);
32
+ }
33
+
34
+ j-trips {
35
+ display: grid;
36
+ grid-template-columns: repeat(auto-fit, minmax(min(100%, 30rem), 1fr));
37
+ gap: var(--space-20);
38
+ }
39
+ j-trips:empty::before {
40
+ content: "No trips you're on yet: start one.";
41
+ color: var(--muted);
42
+ font: var(--font-row);
43
+ }
44
+ .card {
45
+ display: grid;
46
+ grid-template-columns: 12.5rem minmax(0, 1fr);
47
+ min-height: 18rem;
48
+ background: var(--card);
49
+ border: 1px solid var(--edge);
50
+ border-radius: 4px;
51
+ overflow: hidden;
52
+ color: var(--ink);
53
+ text-decoration: none;
54
+ }
55
+ .card:hover .name {
56
+ text-decoration: underline;
57
+ }
58
+
59
+ /* its map: the tiles about their middle, so any frame shows its pins */
60
+ .map {
61
+ position: relative;
62
+ overflow: hidden;
63
+ background: var(--well);
64
+ border-right: 1px solid var(--line);
65
+ }
66
+ .map .ground {
67
+ position: absolute;
68
+ left: 50%;
69
+ top: 50%;
70
+ width: 640px;
71
+ height: 480px;
72
+ translate: -50% -50%;
73
+ }
74
+ .map .ground img {
75
+ position: absolute;
76
+ width: 256px;
77
+ height: 256px;
78
+ }
79
+ .map .pin {
80
+ position: absolute;
81
+ width: 14px;
82
+ height: 14px;
83
+ margin: -7px 0 0 -7px;
84
+ border: 2px solid var(--card);
85
+ border-radius: 50%;
86
+ background: var(--hue);
87
+ box-shadow: 0 0 0 1px rgb(0 0 0 / 0.25);
88
+ }
89
+ .map small {
90
+ position: absolute;
91
+ right: 4px;
92
+ bottom: 4px;
93
+ padding: 0 4px;
94
+ border-radius: 3px;
95
+ background: rgb(255 255 255 / 0.8);
96
+ color: #333;
97
+ font: var(--font-tiny);
98
+ }
99
+ .map.none {
100
+ display: grid;
101
+ place-items: center;
102
+ background: color-mix(in srgb, var(--hue-3) var(--tint), var(--card));
103
+ color: var(--muted);
104
+ font: var(--font-small);
105
+ }
106
+
107
+ .card .body {
108
+ display: flex;
109
+ flex-direction: column;
110
+ gap: var(--space-12);
111
+ padding: var(--space-20) var(--space-20) var(--space-20);
112
+ }
113
+ .card .name {
114
+ font: var(--font-name);
115
+ }
116
+ .card .about {
117
+ margin-top: calc(-1 * var(--space-8));
118
+ color: var(--muted);
119
+ font: var(--font-row);
120
+ }
121
+ .next {
122
+ display: grid;
123
+ gap: 2px;
124
+ padding: var(--space-10) var(--space-12);
125
+ border-radius: 3px;
126
+ background: color-mix(in srgb, var(--hue-0) var(--tint), var(--card));
127
+ font: var(--font-small);
128
+ }
129
+ .next .label {
130
+ font: var(--font-label);
131
+ color: var(--muted);
132
+ }
133
+ .next b {
134
+ font: var(--font-title);
135
+ }
136
+ .next > span:last-child {
137
+ color: var(--muted);
138
+ }
139
+ .next.empty {
140
+ background: none;
141
+ border: 1px dashed var(--edge);
142
+ }
143
+ .people {
144
+ display: flex;
145
+ flex-wrap: wrap;
146
+ gap: var(--space-6);
147
+ margin-top: auto;
148
+ }
149
+ .trips > .note {
150
+ margin-top: var(--space-24);
151
+ }
152
+
153
+ /* New trip: its name, and who travels, a tick each */
154
+ dialog.new-trip fieldset {
155
+ display: grid;
156
+ gap: var(--space-6);
157
+ margin: 0;
158
+ padding: 0;
159
+ border: 0;
160
+ }
161
+ dialog.new-trip legend {
162
+ margin-bottom: var(--space-6);
163
+ font: var(--font-label);
164
+ }
165
+ dialog.new-trip label.tick {
166
+ display: flex;
167
+ justify-content: space-between;
168
+ align-items: center;
169
+ min-height: 36px;
170
+ font: var(--font-row);
171
+ }
172
+
173
+ /* on a phone: its map above it */
174
+ @media (max-width: 40rem) {
175
+ .card {
176
+ grid-template-columns: minmax(0, 1fr);
177
+ grid-template-rows: 9.5rem auto;
178
+ }
179
+ .map {
180
+ border-right: 0;
181
+ border-bottom: 1px solid var(--line);
182
+ }
183
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 1,
3
+ "trips": [
4
+ { "id": 1, "name": "Japan", "organisers": [1], "travellers": [2, 3] },
5
+ { "id": 2, "name": "Hokkaido", "organisers": [1], "travellers": [2] }
6
+ ]
7
+ }
@@ -0,0 +1,23 @@
1
+ // The weeks of each trip a member is on (../trips), and how many of its events each holds, derived from the trips:
2
+ // a trip's page holds one week (trip:1?week=2026-11-02), and says beneath it what's on its other weeks, a way to
3
+ // each. Read as the member sees each trip, so a trip they aren't on has no weeks of theirs; again on a write to one.
4
+
5
+ import type { Read } from "joined/server";
6
+ import { weekOf } from "../trip/time.ts";
7
+
8
+ export const doc = "weeks";
9
+ export const from = ["trips", "trip"];
10
+
11
+ export function derive(read: Read, member: number) {
12
+ const weeks: { id: number; trip: number; week: string; events: number }[] = [];
13
+ for (const { id } of read("trips", member).trips as { id: number }[]) {
14
+ const held = new Map<string, number>();
15
+ for (const e of read(`trip:${id}`, member).events as { start: string }[]) {
16
+ const week = weekOf(e.start);
17
+ held.set(week, (held.get(week) ?? 0) + 1);
18
+ }
19
+ for (const [week, events] of [...held].sort(([a], [b]) => a.localeCompare(b)))
20
+ weeks.push({ id: weeks.length + 1, trip: id, week, events });
21
+ }
22
+ return { weeks };
23
+ }
@@ -0,0 +1,52 @@
1
+ // Written by src/compile.ts from examples/weeks/derive.ts: change it and its example, 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 weeks, 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 weeks
13
+ const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "weeks";
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
+ /** A week's fields, as the stories give them. */
18
+ export type JWeekData = { "trip": number; "week": string; "events": number };
19
+ /** An add of a week, as it's sent: its fields as the stories write them. */
20
+ export type JWeekValue = { "trip": number; "week": string; "events": number };
21
+
22
+ /** <j-week key>: trip (integer), week (string), events (integer), each an attribute. */
23
+ export class JWeek extends HTMLElement {
24
+ static observedAttributes = ["trip","week","events","may"];
25
+ #data?: JWeekData;
26
+ /** Its id in the document. */
27
+ get key() { return Number(this.getAttribute("key")); }
28
+ get path() { return `/weeks/${this.key}`; }
29
+ /** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
30
+ get data(): JWeekData {
31
+ return (this.#data ??= Object.freeze({ "trip": Number(this.getAttribute("trip")), "week": this.getAttribute("week") ?? "", "events": Number(this.getAttribute("events")) }) as JWeekData);
32
+ }
33
+ /** How a view shows it, drawn again whenever an attribute changes (what its person may do, its may, too). */
34
+ draw?(): void;
35
+ attributeChangedCallback() { this.#data = undefined; this.draw?.(); }
36
+ }
37
+
38
+ /** <j-weeks>: the document's weeks, each a <j-week> inside it. */
39
+ export class JWeeks extends HTMLElement {
40
+ connectedCallback() { hold(this, docOf(this), "weeks", "j-week", {"trip":"integer","week":"string","events":"integer"}); }
41
+ disconnectedCallback() { letGo(this); }
42
+ /** Its weeks, as the document has them. */
43
+ get items() { return [...this.children].filter((el): el is JWeek => el.localName === "j-week"); }
44
+ }
45
+ customElements.define("j-weeks", JWeeks);
46
+
47
+ /** An op on weeks the stories allow, in any of its collections. */
48
+ export type WeeksOp = never;
49
+
50
+ /** One write to weeks across its collections (a move with its legs): sent from an element of it, checked
51
+ * whole by its rules, and made whole or not at all. */
52
+ export function write(from: Element, ops: WeeksOp[], why?: string) { return send(from, ops, why); }
@@ -0,0 +1,33 @@
1
+ // Written by src/compile.ts from examples/weeks/derive.ts: change it and its example, not this.
2
+
3
+ export type Op = { op: "add" | "replace" | "remove"; path: string; value?: any };
4
+
5
+ /** The document, and each collection: its element, the element its items are, their fields, those an add needs, and
6
+ * what each link is to. */
7
+ export const doc = "weeks";
8
+ export const collections = {
9
+ "weeks": {
10
+ "place": "j-weeks",
11
+ "tag": "j-week",
12
+ "fields": {
13
+ "trip": "integer",
14
+ "week": "string",
15
+ "events": "integer"
16
+ },
17
+ "needs": [
18
+ "trip",
19
+ "week",
20
+ "events"
21
+ ],
22
+ "links": {}
23
+ }
24
+ } as const;
25
+ /** Whether every story is anonymous, none told as a member: where every app's is, the project has no members. */
26
+ export const anonymous = true;
27
+ /** What kind of document weeks is, by the flows it uses: static, changed only with a release (a page reads it, and
28
+ * never subscribes); read-only, live, but no page writes it (derived); or shared, its members' to write. */
29
+ export const type = "read-only";
30
+ /** The parts of weeks a page may hold alone, by its query's parameters (its definitions' window): null, none. */
31
+ export const window = null;
32
+ /** The documents this one is derived from (its derive.ts): what the server derives it again on a write to. */
33
+ export const derived = ["trips","trip"];
@@ -0,0 +1,4 @@
1
+ // The weeks of each trip a member is on: a document derived from the trips (derive.ts), read by a trip's page.
2
+
3
+ export * as document from "./generated/rules.ts";
4
+ export { derive } from "./derive.ts";