@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,199 @@
1
+ // The trip's form: one event's fields, in <calendar-map>'s drawer to change the picked event, or in its
2
+ // modal to add one. It holds what's typed, dragged on the week or tapped on the map (a Draft, joined/draft)
3
+ // until Save, and what it holds, the display draws (its host's redraw): the picked event where the form puts it.
4
+ // Its parts are their own: the time zone found by typing (./zones.ts), the place searched for near the map's middle
5
+ // (./place-search.ts), a chip for each person (./people-chips.ts) and a row for each one's answer (./answer-rows.ts).
6
+
7
+ import type { JEventData } from "./generated/elements.ts";
8
+ import { Draft } from "joined/draft";
9
+ import { icon } from "joined/icons";
10
+ import { ANSWERS, answerOf, type Answer } from "./answers.ts";
11
+ import { AnswerRows, type Gates } from "./answer-rows.ts";
12
+ import { geo } from "./mercator.ts";
13
+ import { PeopleChips } from "./people-chips.ts";
14
+ import { PlaceSearch } from "./place-search.ts";
15
+ import type { Place } from "./places.ts";
16
+ import { hhmm, iso, MIN, minutes, seconds, SNAP, wall } from "./time.ts";
17
+ import { zones } from "./zones.ts";
18
+
19
+ const INPUTS = ["title", "day", "from", "to", "zone", "location", "coordinates"] as const;
20
+ export type Input = (typeof INPUTS)[number];
21
+ /** What the form asks of the display it's in: to draw what it holds, the people, and the map's middle. */
22
+ type Host = { redraw(): void; people(): Map<number, string>; near(): { lat: number; lng: number } };
23
+
24
+ const FORM = `<header><h2 class="mode"></h2><button type="button" class="icon-only" name="close" title="Close" aria-label="Close" data-icon="close"></button></header>
25
+ <label>Title<input name="title" autocomplete="off" required></label>
26
+ <div class="times"><label>Day<input name="day" type="date"></label><label>Starts<input name="from" type="time" step="900"></label><label>Ends<input name="to" type="time" step="900"></label></div>
27
+ <label>Time zone<input name="zone" autocomplete="off" placeholder="Where its clock is: Asia/Tokyo"></label>
28
+ <div class="j-choices zones" role="listbox" aria-label="Time zones" hidden></div>
29
+ <label>Place<span class="place"><input name="location" autocomplete="off" placeholder="Search, or tap the map"><button type="button" class="icon-only" name="unsettle" title="Not settled" aria-label="Not settled" data-icon="close"></button></span></label>
30
+ <details class="at"><summary></summary><input name="coordinates" aria-label="The exact spot" placeholder="geo:35.68,139.76" autocomplete="off"></details>
31
+ <div class="found j-choices" role="listbox" aria-label="Places" hidden></div>
32
+ <div class="j-chips" role="group" aria-label="People"></div>
33
+ <div class="someone" hidden><input aria-label="Add someone" placeholder="Add someone" autocomplete="off"><div class="j-choices" role="listbox" aria-label="People" hidden></div></div>
34
+ <div class="answers" role="group" aria-label="Their answers"></div>
35
+ <p class="note" role="status"></p>
36
+ <footer><button type="button" class="act danger" name="remove" data-icon="remove">Remove</button><button type="button" class="act" name="never">Never mind</button><button class="act ink" type="submit" name="go"><span>Save</span></button></footer>`;
37
+
38
+ /** The form's fields for an event as the document has it (or empty, for a new one). */
39
+ const fieldsOf = (d?: JEventData): Record<Input, string> => {
40
+ const at = wall(d?.start ?? "");
41
+ const when = Number.isFinite(at);
42
+ return {
43
+ title: d?.title ?? "",
44
+ day: when ? d!.start.slice(0, 10) : "",
45
+ from: when ? d!.start.slice(11, 16) : "",
46
+ to: when ? hhmm(at + seconds(d!.duration) * 1000) : "",
47
+ zone: d?.zone ?? "",
48
+ location: d?.location ?? "",
49
+ coordinates: d?.coordinates ?? "",
50
+ };
51
+ };
52
+ const same = (a: readonly number[], b: readonly number[]) => a.length === b.length && a.every((k, i) => k === b[i]);
53
+
54
+ export class EventForm {
55
+ readonly el = Object.assign(document.createElement("form"), { innerHTML: FORM });
56
+ /** What the form holds of the picked event, until Save. */
57
+ readonly held = new Draft(this.el, this.el.querySelector(".note")!);
58
+ /** Who the event in the form links to, as the form has it. */
59
+ chosen: number[] = [];
60
+ #was: number[] = []; // who the picked event links to, as the document last had it
61
+ /** Each chosen person's answer, as the form has it: yes unless said otherwise. */
62
+ answers = new Map<number, Answer>();
63
+ /** Who among the chosen comes or goes at the event, as the form has it. */
64
+ gates: Gates = { arrives: new Set(), leaves: new Set() };
65
+ #chips = new PeopleChips(this.el, {
66
+ people: () => this.host.people(),
67
+ chosen: () => this.chosen,
68
+ choose: (chosen) => this.#choose(chosen),
69
+ });
70
+ #answers = new AnswerRows(this.el.querySelector(".answers")!, this, () => this.host.redraw());
71
+ #places = new PlaceSearch(this.el.querySelector(".found")!, ({ name, coordinates }: Place) => {
72
+ this.set("location", name);
73
+ this.set("coordinates", coordinates);
74
+ });
75
+
76
+ constructor(readonly host: Host) {
77
+ zones(this.input("zone"), this.el.querySelector<HTMLElement>(".zones")!, (zone) => this.set("zone", zone));
78
+ }
79
+
80
+ input = (f: Input) => this.el.elements.namedItem(f) as HTMLInputElement;
81
+ /** Every field, as it's typed. */
82
+ values = () => Object.fromEntries(INPUTS.map((f) => [f, this.input(f).value])) as Record<Input, string>;
83
+ /** A field, from a drag, a tap or a pick: not saved until Save. */
84
+ set(field: Input, value: string) {
85
+ this.input(field).value = value;
86
+ this.host.redraw();
87
+ }
88
+ /** The event as the form has it. */
89
+ formed() {
90
+ const v = (f: Input) => this.input(f).value;
91
+ const [day, from, to] = [v("day"), v("from").slice(0, 5), v("to").slice(0, 5)];
92
+ let mins = from && to ? minutes(to) - minutes(from) : 60;
93
+ if (mins < 0) mins += 24 * 60; // ends the day after: drawn to midnight
94
+ mins ||= SNAP;
95
+ return {
96
+ title: v("title"),
97
+ start: day && from ? `${day}T${from}:00` : "",
98
+ duration: iso(mins),
99
+ zone: v("zone").trim(),
100
+ location: v("location"),
101
+ coordinates: v("coordinates"),
102
+ };
103
+ }
104
+ /** A new start keeps the event's length: its end moves with it. */
105
+ keepLength() {
106
+ const from = this.input("from"),
107
+ to = this.input("to"),
108
+ was = from.dataset.at;
109
+ if (was && from.value && to.value)
110
+ to.value = hhmm(((((minutes(to.value) + minutes(from.value) - minutes(was)) % 1440) + 1440) % 1440) * MIN);
111
+ from.dataset.at = from.value.slice(0, 5);
112
+ }
113
+
114
+ /** The form as the document has an event (or empty, for a new one). Opened afresh, every field is set; as the
115
+ * event changes elsewhere, only those not being changed here are. */
116
+ fill(d: JEventData | undefined, afresh = false) {
117
+ this.held.fill(fieldsOf(d), afresh);
118
+ const people = d?.people ?? [];
119
+ if (afresh || (d && same(this.chosen, this.#was))) {
120
+ this.chosen = [...people];
121
+ this.answers = new Map(people.map((k) => [k, answerOf(d ?? {}, k)]));
122
+ this.gates = { arrives: new Set(d?.arrives ?? []), leaves: new Set(d?.leaves ?? []) };
123
+ }
124
+ this.#was = [...people];
125
+ this.chips();
126
+ }
127
+ /** What the form has changed of an event: fields to replace, and places emptied, to remove. */
128
+ edits(d: JEventData) {
129
+ const f = this.formed();
130
+ const replaced: Partial<JEventData> = {},
131
+ removed: ("zone" | "location" | "coordinates" | (typeof ANSWERS)[number])[] = [];
132
+ if (f.title !== d.title) replaced.title = f.title;
133
+ if (f.start && f.start !== d.start) replaced.start = f.start;
134
+ if (f.start && seconds(f.duration) !== seconds(d.duration)) replaced.duration = f.duration;
135
+ for (const k of ["zone", "location", "coordinates"] as const)
136
+ if (f[k] !== (d[k] ?? "")) f[k] ? (replaced[k] = f[k]) : removed.push(k);
137
+ if (!same(this.chosen, d.people ?? [])) replaced.people = this.chosen;
138
+ for (const a of ANSWERS) {
139
+ const theirs = this.answered(a);
140
+ // an answer no one gives any more: the list gone, as an emptied place is
141
+ if (!same(theirs, d[a] ?? [])) theirs.length ? (replaced[a] = theirs) : removed.push(a);
142
+ }
143
+ return { replaced, removed };
144
+ }
145
+ /** What the form says it's doing (the picked event's, or a new one), and whether it's kept: true while it has
146
+ * changes not kept. */
147
+ show(d: JEventData | null) {
148
+ const edits = d && this.edits(d),
149
+ dirty = !!edits && Object.keys(edits.replaced).length + edits.removed.length + this.gateEdits(d!).length > 0;
150
+ const q = <T extends HTMLElement>(s: string) => this.el.querySelector<T>(s)!;
151
+ this.input("from").dataset.at = this.input("from").value.slice(0, 5); // the start the length is kept from
152
+ q(".mode").textContent = d ? `Editing “${d.title}”` : "New event";
153
+ const go = q<HTMLButtonElement>("[name=go]"),
154
+ mark = d ? "check" : "add";
155
+ // its icon swapped only as what it does changes: not on every change
156
+ if (go.dataset.mark !== mark) ((go.dataset.mark = mark), go.querySelector("svg")?.remove(), go.prepend(icon(mark)));
157
+ go.querySelector("span")!.textContent = d ? "Save" : "Add";
158
+ this.held.show(!d ? "new" : dirty ? "changed" : "kept");
159
+ q("[name=remove]").hidden = !d;
160
+ const at = geo(this.input("coordinates").value);
161
+ q("[name=unsettle]").hidden = !this.input("location").value && !at;
162
+ q(".at summary").textContent = at ? "Pinned on the map" : "No pin yet: tap the map, or search";
163
+ return dirty;
164
+ }
165
+
166
+ /** Who the event links to, as the form has it now. */
167
+ #choose(chosen: number[]) {
168
+ this.chosen = [...chosen].sort((a, b) => a - b);
169
+ for (const k of this.answers.keys()) if (!this.chosen.includes(k)) this.answers.delete(k); // off it, no answer
170
+ for (const g of Object.values(this.gates)) for (const k of g) if (!this.chosen.includes(k)) g.delete(k);
171
+ this.chips();
172
+ this.host.redraw();
173
+ }
174
+ /** Who comes or goes at the event as the form has it, against the document: each id to link, or to unlink. */
175
+ gateEdits(d: JEventData) {
176
+ return (["arrives", "leaves"] as const).flatMap((g) => [
177
+ ...[...this.gates[g]].filter((k) => !(d[g] ?? []).includes(k)).map((k) => ({ g, k, on: true })),
178
+ ...(d[g] ?? []).filter((k) => !this.gates[g].has(k)).map((k) => ({ g, k, on: false })),
179
+ ]);
180
+ }
181
+ /** Who among the chosen gave an answer, in key order. */
182
+ answered = (a: Answer) => this.chosen.filter((k) => (this.answers.get(k) ?? "yes") === a);
183
+ /** The people and their answers, as the form has them: a chip each (./people-chips.ts), and a row each for those
184
+ * chosen (./answer-rows.ts). */
185
+ chips() {
186
+ const people = this.host.people();
187
+ this.#chips.draw(people);
188
+ this.#answers.draw(people);
189
+ }
190
+
191
+ /** Places called what's typed, near the map's middle, offered to pick from: one picked puts its pin. */
192
+ search() {
193
+ this.#places.search(this.input("location").value.trim(), () => this.host.near());
194
+ }
195
+ /** The places offered (none: the offer let go). */
196
+ found(places: Place[]) {
197
+ this.#places.found(places);
198
+ }
199
+ }
@@ -0,0 +1,144 @@
1
+ // Written by src/compile.ts from examples/trip/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 trip, 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 trip
13
+ const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "trip";
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
+ /** An event's fields, as the stories give them. */
18
+ export type JEventData = { "title": string; "start": string; "duration": string; "location"?: string; "coordinates"?: string; "people"?: number[]; "zone"?: string; "asked"?: number[]; "keen"?: number[]; "declined"?: number[]; "arrives"?: number[]; "leaves"?: number[] };
19
+ /** An add of an event, as it's sent: its fields as the stories write them. */
20
+ export type JEventValue = { "title": string; "start": string; "duration": string; "location"?: string; "coordinates"?: string; "people"?: number[]; "zone"?: string; "asked"?: number[]; "keen"?: number[]; "declined"?: number[]; "arrives"?: number[]; "leaves"?: number[] };
21
+
22
+ /** <j-event key>: title (string), start (date-time), duration (duration), location (string), coordinates (place), people (ids in people), zone (zone), asked (ids in people), keen (ids in people), declined (ids in people), arrives (ids in people), leaves (ids in people), each an attribute. */
23
+ export class JEvent extends HTMLElement {
24
+ static observedAttributes = ["title","start","duration","location","coordinates","people","zone","asked","keen","declined","arrives","leaves","may"];
25
+ #data?: JEventData & Required<Pick<JEventData, "people" | "asked" | "keen" | "declined" | "arrives" | "leaves">>;
26
+ /** Its id in the document. */
27
+ get key() { return Number(this.getAttribute("key")); }
28
+ get path() { return `/events/${this.key}`; }
29
+ /** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
30
+ get data(): JEventData & Required<Pick<JEventData, "people" | "asked" | "keen" | "declined" | "arrives" | "leaves">> {
31
+ return (this.#data ??= Object.freeze({ "title": this.getAttribute("title") ?? "", "start": this.getAttribute("start") ?? "", "duration": this.getAttribute("duration") ?? "", "location": this.getAttribute("location") ?? undefined, "coordinates": this.getAttribute("coordinates") ?? undefined, "people": Object.freeze((this.getAttribute("people") ?? "").split(" ").filter(Boolean).map(Number)), "zone": this.getAttribute("zone") ?? undefined, "asked": Object.freeze((this.getAttribute("asked") ?? "").split(" ").filter(Boolean).map(Number)), "keen": Object.freeze((this.getAttribute("keen") ?? "").split(" ").filter(Boolean).map(Number)), "declined": Object.freeze((this.getAttribute("declined") ?? "").split(" ").filter(Boolean).map(Number)), "arrives": Object.freeze((this.getAttribute("arrives") ?? "").split(" ").filter(Boolean).map(Number)), "leaves": Object.freeze((this.getAttribute("leaves") ?? "").split(" ").filter(Boolean).map(Number)) }) as JEventData & Required<Pick<JEventData, "people" | "asked" | "keen" | "declined" | "arrives" | "leaves">>);
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 (start, title, duration, location, coordinates, zone, people, keen, declined, asked): one write for all of them; a list of ids (people, keen, declined, asked) by the ids added to it and taken from it. */
37
+ replace(fields: Partial<Pick<JEventData, "start" | "title" | "duration" | "location" | "coordinates" | "zone" | "people" | "keen" | "declined" | "asked">>, why?: string) {
38
+ return send(this, Object.entries(fields).filter(([, value]) => value !== undefined).flatMap(([field, value]) => ["people","keen","declined","asked"].includes(field) ? this.#ids(field as "people" | "keen" | "declined" | "asked", value as number[]) : [{ op: "replace", path: `${this.path}/${field.replace(".", "/")}`, value }]), why);
39
+ }
40
+ /** A list's ids, as the ids added to it and taken from it. */
41
+ #ids(field: "people" | "keen" | "declined" | "asked", ids: number[]) {
42
+ const had = this.data[field];
43
+ return [...ids.filter((id) => !had.includes(id)).map((id) => ({ op: "add", path: `${this.path}/${field}/-`, value: id })), ...had.filter((id) => !ids.includes(id)).map((id) => ({ op: "remove", path: `${this.path}/${field}/${id}` }))];
44
+ }
45
+ /** Link it to one more id in a list (people, asked, keen, declined, arrives, leaves): the ids there already are left as they are. */
46
+ link(field: "people" | "asked" | "keen" | "declined" | "arrives" | "leaves", id: number, why?: string) { return send(this, [{ op: "add", path: `${this.path}/${field}/-`, value: id }], why); }
47
+ /** Take one id from a list (people, asked, declined, keen, leaves, arrives). */
48
+ unlink(field: "people" | "asked" | "declined" | "keen" | "leaves" | "arrives", id: number, why?: string) { return send(this, [{ op: "remove", path: `${this.path}/${field}/${id}` }], why); }
49
+ /** Remove some of its fields (zone, location, coordinates, asked, declined, keen), so that it has none. */
50
+ removeField(...fields: ("zone" | "location" | "coordinates" | "asked" | "declined" | "keen")[]) { return send(this, fields.map((field) => ({ op: "remove", path: `${this.path}/${field}` }))); }
51
+ /** Remove it from the document. */
52
+ removeItem(why?: string) { return send(this, [{ op: "remove", path: this.path }], why); }
53
+ }
54
+
55
+ /** <j-events>: the document's events, each a <j-event> inside it. */
56
+ export class JEvents extends HTMLElement {
57
+ connectedCallback() { hold(this, docOf(this), "events", "j-event", {"title":"string","start":"date-time","duration":"duration","location":"string","coordinates":"place","people":"ids","zone":"zone","asked":"ids","keen":"ids","declined":"ids","arrives":"ids","leaves":"ids"}); }
58
+ disconnectedCallback() { letGo(this); }
59
+ /** Its events, as the document has them. */
60
+ get items() { return [...this.children].filter((el): el is JEvent => el.localName === "j-event"); }
61
+ /** Add events: one write for all of them. */
62
+ add(...values: JEventValue[]) { return send(this, values.map((value) => ({ op: "add", path: "/events/-", value }))); }
63
+ }
64
+ customElements.define("j-events", JEvents);
65
+
66
+ /** A stay's fields, as the stories give them. */
67
+ export type JStayData = { "name": string; "from": string; "until": string; "location"?: string; "coordinates"?: string };
68
+ /** An add of a stay, as it's sent: its fields as the stories write them. */
69
+ export type JStayValue = { "name": string; "from": string; "until": string; "location"?: string; "coordinates"?: string };
70
+
71
+ /** <j-stay key>: name (string), from (date-time), until (date-time), location (string), coordinates (place), each an attribute. */
72
+ export class JStay extends HTMLElement {
73
+ static observedAttributes = ["name","from","until","location","coordinates","may"];
74
+ #data?: JStayData;
75
+ /** Its id in the document. */
76
+ get key() { return Number(this.getAttribute("key")); }
77
+ get path() { return `/stays/${this.key}`; }
78
+ /** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
79
+ get data(): JStayData {
80
+ return (this.#data ??= Object.freeze({ "name": this.getAttribute("name") ?? "", "from": this.getAttribute("from") ?? "", "until": this.getAttribute("until") ?? "", "location": this.getAttribute("location") ?? undefined, "coordinates": this.getAttribute("coordinates") ?? undefined }) as JStayData);
81
+ }
82
+ /** How a view shows it, drawn again whenever an attribute changes (what its person may do, its may, too). */
83
+ draw?(): void;
84
+ attributeChangedCallback() { this.#data = undefined; this.draw?.(); }
85
+ /** Replace some of its fields (name, until, from): one write for all of them. */
86
+ replace(fields: Partial<Pick<JStayData, "name" | "until" | "from">>, why?: string) {
87
+ return send(this, Object.entries(fields).filter(([, value]) => value !== undefined).map(([field, value]) => ({ op: "replace", path: `${this.path}/${field.replace(".", "/")}`, value })), why);
88
+ }
89
+ /** Remove it from the document. */
90
+ removeItem(why?: string) { return send(this, [{ op: "remove", path: this.path }], why); }
91
+ }
92
+
93
+ /** <j-stays>: the document's stays, each a <j-stay> inside it. */
94
+ export class JStays extends HTMLElement {
95
+ connectedCallback() { hold(this, docOf(this), "stays", "j-stay", {"name":"string","from":"date-time","until":"date-time","location":"string","coordinates":"place"}); }
96
+ disconnectedCallback() { letGo(this); }
97
+ /** Its stays, as the document has them. */
98
+ get items() { return [...this.children].filter((el): el is JStay => el.localName === "j-stay"); }
99
+ /** Add stays: one write for all of them. */
100
+ add(...values: JStayValue[]) { return send(this, values.map((value) => ({ op: "add", path: "/stays/-", value }))); }
101
+ }
102
+ customElements.define("j-stays", JStays);
103
+
104
+ /** An op on trip the stories allow, in any of its collections. */
105
+ export type TripOp =
106
+ | { op: "add"; path: "/events/-"; value: JEventValue }
107
+ | { op: "replace"; path: `/events/${number}/start`; value: string }
108
+ | { op: "replace"; path: `/events/${number}/title`; value: string }
109
+ | { op: "replace"; path: `/events/${number}/duration`; value: string }
110
+ | { op: "replace"; path: `/events/${number}/location`; value: string }
111
+ | { op: "replace"; path: `/events/${number}/coordinates`; value: string }
112
+ | { op: "replace"; path: `/events/${number}/zone`; value: string }
113
+ | { op: "replace"; path: `/events/${number}/people`; value: number[] }
114
+ | { op: "replace"; path: `/events/${number}/keen`; value: number[] }
115
+ | { op: "replace"; path: `/events/${number}/declined`; value: number[] }
116
+ | { op: "replace"; path: `/events/${number}/asked`; value: number[] }
117
+ | { op: "remove"; path: `/events/${number}/zone` }
118
+ | { op: "remove"; path: `/events/${number}/location` }
119
+ | { op: "remove"; path: `/events/${number}/coordinates` }
120
+ | { op: "remove"; path: `/events/${number}/asked` }
121
+ | { op: "remove"; path: `/events/${number}/declined` }
122
+ | { op: "remove"; path: `/events/${number}/keen` }
123
+ | { op: "add"; path: `/events/${number}/people/-`; value: number }
124
+ | { op: "add"; path: `/events/${number}/asked/-`; value: number }
125
+ | { op: "add"; path: `/events/${number}/keen/-`; value: number }
126
+ | { op: "add"; path: `/events/${number}/declined/-`; value: number }
127
+ | { op: "add"; path: `/events/${number}/arrives/-`; value: number }
128
+ | { op: "add"; path: `/events/${number}/leaves/-`; value: number }
129
+ | { op: "remove"; path: `/events/${number}/people/${number}` }
130
+ | { op: "remove"; path: `/events/${number}/asked/${number}` }
131
+ | { op: "remove"; path: `/events/${number}/declined/${number}` }
132
+ | { op: "remove"; path: `/events/${number}/keen/${number}` }
133
+ | { op: "remove"; path: `/events/${number}/leaves/${number}` }
134
+ | { op: "remove"; path: `/events/${number}/arrives/${number}` }
135
+ | { op: "remove"; path: `/events/${number}` }
136
+ | { op: "add"; path: "/stays/-"; value: JStayValue }
137
+ | { op: "replace"; path: `/stays/${number}/name`; value: string }
138
+ | { op: "replace"; path: `/stays/${number}/until`; value: string }
139
+ | { op: "replace"; path: `/stays/${number}/from`; value: string }
140
+ | { op: "remove"; path: `/stays/${number}` };
141
+
142
+ /** One write to trip across its collections (a move with its legs): sent from an element of it, checked
143
+ * whole by its rules, and made whole or not at all. */
144
+ export function write(from: Element, ops: TripOp[], why?: string) { return send(from, ops, why); }
@@ -0,0 +1,46 @@
1
+ // Written by src/compile.ts from examples/trip/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
+ "events": "json_patch('{}', json_object('id', t.id,\n 'title', t.\"title\",\n 'start', t.\"start\",\n 'duration', t.\"duration\",\n 'location', t.\"location\",\n 'coordinates', t.\"coordinates\",\n 'people', json(NULLIF((SELECT json_group_array(p.\"people\" ORDER BY p.id) FROM \"events_people\" p WHERE p.\"events\" = t.id), '[]')),\n 'zone', t.\"zone\",\n 'asked', json(NULLIF((SELECT json_group_array(p.\"asked\" ORDER BY p.id) FROM \"events_asked\" p WHERE p.\"events\" = t.id), '[]')),\n 'keen', json(NULLIF((SELECT json_group_array(p.\"keen\" ORDER BY p.id) FROM \"events_keen\" p WHERE p.\"events\" = t.id), '[]')),\n 'declined', json(NULLIF((SELECT json_group_array(p.\"declined\" ORDER BY p.id) FROM \"events_declined\" p WHERE p.\"events\" = t.id), '[]')),\n 'arrives', json(NULLIF((SELECT json_group_array(p.\"arrives\" ORDER BY p.id) FROM \"events_arrives\" p WHERE p.\"events\" = t.id), '[]')),\n 'leaves', json(NULLIF((SELECT json_group_array(p.\"leaves\" ORDER BY p.id) FROM \"events_leaves\" p WHERE p.\"events\" = t.id), '[]'))))",
7
+ "stays": "json_patch('{}', json_object('id', t.id,\n 'name', t.\"name\",\n 'from', t.\"from\",\n 'until', t.\"until\",\n 'location', t.\"location\",\n 'coordinates', t.\"coordinates\"))"
8
+ };
9
+
10
+ /** trip, whole, as one statement over its tables (SQLite): its parameters $doc, and $key, the instance (trip:1), and its window's, $week, $month, and $member, who's asking (none: all of it). */
11
+ export const query = `SELECT json_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),
12
+ 'events', json((SELECT json_group_array(json_patch('{}', json_object('id', t.id,
13
+ 'title', t."title",
14
+ 'start', t."start",
15
+ 'duration', t."duration",
16
+ 'location', t."location",
17
+ 'coordinates', t."coordinates",
18
+ 'people', json(NULLIF((SELECT json_group_array(p."people" ORDER BY p.id) FROM "events_people" p WHERE p."events" = t.id), '[]')),
19
+ 'zone', t."zone",
20
+ 'asked', json(NULLIF((SELECT json_group_array(p."asked" ORDER BY p.id) FROM "events_asked" p WHERE p."events" = t.id), '[]')),
21
+ 'keen', json(NULLIF((SELECT json_group_array(p."keen" ORDER BY p.id) FROM "events_keen" p WHERE p."events" = t.id), '[]')),
22
+ 'declined', json(NULLIF((SELECT json_group_array(p."declined" ORDER BY p.id) FROM "events_declined" p WHERE p."events" = t.id), '[]')),
23
+ 'arrives', json(NULLIF((SELECT json_group_array(p."arrives" ORDER BY p.id) FROM "events_arrives" p WHERE p."events" = t.id), '[]')),
24
+ 'leaves', json(NULLIF((SELECT json_group_array(p."leaves" ORDER BY p.id) FROM "events_leaves" p WHERE p."events" = t.id), '[]')))) ORDER BY t.id)
25
+ FROM "events" t WHERE t."trip" = $key AND ($week IS NULL OR (t."start" < strftime('%Y-%m-%dT%H:%M:%S', $week, '+7 days') AND ends(t."start", t."duration") > strftime('%Y-%m-%dT%H:%M:%S', $week, '+0 days'))) AND ($month IS NULL OR (t."start" < strftime('%Y-%m-%dT%H:%M:%S', $month, '+42 days') AND ends(t."start", t."duration") > strftime('%Y-%m-%dT%H:%M:%S', $month, '+0 days'))) AND ($member IS NULL OR ((EXISTS (SELECT 1 FROM "trips_organisers" p WHERE p."trips" = $key AND p."organisers" = $member)) OR (EXISTS (SELECT 1 FROM "trips_travellers" p WHERE p."trips" = $key AND p."travellers" = $member)))))),
26
+ 'stays', json((SELECT json_group_array(json_patch('{}', json_object('id', t.id,
27
+ 'name', t."name",
28
+ 'from', t."from",
29
+ 'until', t."until",
30
+ 'location', t."location",
31
+ 'coordinates', t."coordinates")) ORDER BY t.id)
32
+ FROM "stays" t WHERE t."trip" = $key AND ($member IS NULL OR ((EXISTS (SELECT 1 FROM "trips_organisers" p WHERE p."trips" = $key AND p."organisers" = $member)) OR (EXISTS (SELECT 1 FROM "trips_travellers" p WHERE p."trips" = $key AND p."travellers" = $member))))))) AS doc`;
33
+
34
+ /** Its rules over values (definitions' takes, gives and totals), as triggers the store makes in its database: run in
35
+ * each write's transaction, and told as any change is. */
36
+ export const triggers: string[] = [];
37
+
38
+ /** Its items, its query and its triggers, as Postgres says them. */
39
+ export const postgres = {
40
+ "items": {
41
+ "events": "json_strip_nulls(json_build_object('id', t.id,\n 'title', t.\"title\",\n 'start', t.\"start\",\n 'duration', t.\"duration\",\n 'location', t.\"location\",\n 'coordinates', t.\"coordinates\",\n 'people', (SELECT json_agg(p.\"people\" ORDER BY p.id) FROM \"events_people\" p WHERE p.\"events\" = t.id),\n 'zone', t.\"zone\",\n 'asked', (SELECT json_agg(p.\"asked\" ORDER BY p.id) FROM \"events_asked\" p WHERE p.\"events\" = t.id),\n 'keen', (SELECT json_agg(p.\"keen\" ORDER BY p.id) FROM \"events_keen\" p WHERE p.\"events\" = t.id),\n 'declined', (SELECT json_agg(p.\"declined\" ORDER BY p.id) FROM \"events_declined\" p WHERE p.\"events\" = t.id),\n 'arrives', (SELECT json_agg(p.\"arrives\" ORDER BY p.id) FROM \"events_arrives\" p WHERE p.\"events\" = t.id),\n 'leaves', (SELECT json_agg(p.\"leaves\" ORDER BY p.id) FROM \"events_leaves\" p WHERE p.\"events\" = t.id)))",
42
+ "stays": "json_strip_nulls(json_build_object('id', t.id,\n 'name', t.\"name\",\n 'from', t.\"from\",\n 'until', t.\"until\",\n 'location', t.\"location\",\n 'coordinates', t.\"coordinates\"))"
43
+ },
44
+ "query": "SELECT json_build_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),\n 'events', (SELECT COALESCE(json_agg(json_strip_nulls(json_build_object('id', t.id,\n 'title', t.\"title\",\n 'start', t.\"start\",\n 'duration', t.\"duration\",\n 'location', t.\"location\",\n 'coordinates', t.\"coordinates\",\n 'people', (SELECT json_agg(p.\"people\" ORDER BY p.id) FROM \"events_people\" p WHERE p.\"events\" = t.id),\n 'zone', t.\"zone\",\n 'asked', (SELECT json_agg(p.\"asked\" ORDER BY p.id) FROM \"events_asked\" p WHERE p.\"events\" = t.id),\n 'keen', (SELECT json_agg(p.\"keen\" ORDER BY p.id) FROM \"events_keen\" p WHERE p.\"events\" = t.id),\n 'declined', (SELECT json_agg(p.\"declined\" ORDER BY p.id) FROM \"events_declined\" p WHERE p.\"events\" = t.id),\n 'arrives', (SELECT json_agg(p.\"arrives\" ORDER BY p.id) FROM \"events_arrives\" p WHERE p.\"events\" = t.id),\n 'leaves', (SELECT json_agg(p.\"leaves\" ORDER BY p.id) FROM \"events_leaves\" p WHERE p.\"events\" = t.id))) ORDER BY t.id), '[]')\n FROM \"events\" t WHERE t.\"trip\" = $key AND ($week IS NULL OR (t.\"start\" < to_char(CAST($week AS timestamp) + interval '7 days', 'YYYY-MM-DD\"T\"HH24:MI:SS') AND to_char(CAST(t.\"start\" AS timestamp) + CAST(t.\"duration\" AS interval), 'YYYY-MM-DD\"T\"HH24:MI:SS') > to_char(CAST($week AS timestamp) + interval '0 days', 'YYYY-MM-DD\"T\"HH24:MI:SS'))) AND ($month IS NULL OR (t.\"start\" < to_char(CAST($month AS timestamp) + interval '42 days', 'YYYY-MM-DD\"T\"HH24:MI:SS') AND to_char(CAST(t.\"start\" AS timestamp) + CAST(t.\"duration\" AS interval), 'YYYY-MM-DD\"T\"HH24:MI:SS') > to_char(CAST($month AS timestamp) + interval '0 days', 'YYYY-MM-DD\"T\"HH24:MI:SS'))) AND ($member IS NULL OR ((EXISTS (SELECT 1 FROM \"trips_organisers\" p WHERE p.\"trips\" = $key AND p.\"organisers\" = $member)) OR (EXISTS (SELECT 1 FROM \"trips_travellers\" p WHERE p.\"trips\" = $key AND p.\"travellers\" = $member))))),\n 'stays', (SELECT COALESCE(json_agg(json_strip_nulls(json_build_object('id', t.id,\n 'name', t.\"name\",\n 'from', t.\"from\",\n 'until', t.\"until\",\n 'location', t.\"location\",\n 'coordinates', t.\"coordinates\")) ORDER BY t.id), '[]')\n FROM \"stays\" t WHERE t.\"trip\" = $key AND ($member IS NULL OR ((EXISTS (SELECT 1 FROM \"trips_organisers\" p WHERE p.\"trips\" = $key AND p.\"organisers\" = $member)) OR (EXISTS (SELECT 1 FROM \"trips_travellers\" p WHERE p.\"trips\" = $key AND p.\"travellers\" = $member)))))) AS doc",
45
+ "triggers": []
46
+ };
@@ -0,0 +1,239 @@
1
+ // Written by src/compile.ts from examples/trip/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 = "trip";
8
+ export const collections = {
9
+ "events": {
10
+ "place": "j-events",
11
+ "tag": "j-event",
12
+ "fields": {
13
+ "title": "string",
14
+ "start": "date-time",
15
+ "duration": "duration",
16
+ "location": "string",
17
+ "coordinates": "place",
18
+ "people": "ids",
19
+ "zone": "zone",
20
+ "asked": "ids",
21
+ "keen": "ids",
22
+ "declined": "ids",
23
+ "arrives": "ids",
24
+ "leaves": "ids"
25
+ },
26
+ "needs": [
27
+ "title",
28
+ "start",
29
+ "duration"
30
+ ],
31
+ "links": {
32
+ "people": "people",
33
+ "asked": "people",
34
+ "keen": "people",
35
+ "declined": "people",
36
+ "arrives": "people",
37
+ "leaves": "people"
38
+ }
39
+ },
40
+ "stays": {
41
+ "place": "j-stays",
42
+ "tag": "j-stay",
43
+ "fields": {
44
+ "name": "string",
45
+ "from": "date-time",
46
+ "until": "date-time",
47
+ "location": "string",
48
+ "coordinates": "place"
49
+ },
50
+ "needs": [
51
+ "name",
52
+ "from",
53
+ "until"
54
+ ],
55
+ "links": {}
56
+ }
57
+ } as const;
58
+ /** Whether every story is anonymous, none told as a member: where every app's is, the project has no members. */
59
+ export const anonymous = false;
60
+ /** What kind of document trip is, by the flows it uses: static, changed only with a release (a page reads it, and
61
+ * never subscribes); read-only, live, but no page writes it (derived); or shared, its members' to write. */
62
+ export const type = "shared";
63
+ /** The parts of trip a page may hold alone, by its query's parameters (its definitions' window): null, none. */
64
+ export const window = {"events":{"week":{"from":"start","for":"P7D","length":"duration"},"month":{"from":"start","for":"P42D","length":"duration"}}};
65
+
66
+ /** Whether a window (its parameters, each given or null) holds an item of a collection: what its query's WHERE asks. */
67
+ export function inWindow(coll: string, item: any, p: Record<string, string | null>): boolean {
68
+ // a date's days on, as SQLite's strftime writes it: midnight, to the second
69
+ const days = (d: string, n: number) => new Date(Date.parse(d + "T00:00:00Z") + n * 864e5).toISOString().slice(0, 19);
70
+ // where a time ends, a length (a duration) on, as SQLite's ends() writes it
71
+ const ends = (s: string, l: string) => { const [d = 0, h = 0, m = 0, x = 0] = /^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/.exec(String(l))?.slice(1).map((n) => Number(n ?? 0)) ?? []; return new Date(Date.parse(s + "Z") + (((d * 24 + h) * 60 + m) * 60 + x) * 1000).toISOString().slice(0, 19); };
72
+ return (coll !== "events" || ((p.week == null || (typeof item.start === "string" && item.start < days(p.week, 7) && ends(item.start, item.duration) > days(p.week, 0))) && (p.month == null || (typeof item.start === "string" && item.start < days(p.month, 42) && ends(item.start, item.duration) > days(p.month, 0)))));
73
+ }
74
+ /** The document as one statement over its tables (generated/queries.ts). */
75
+ export { query, postgres, items, triggers } from "./queries.ts";
76
+
77
+ /** Why the document, as a write leaves it, breaks what its definitions hold (unique, noOverlap): each item the
78
+ * write touched, against the rest of its collection. Null if it holds. */
79
+ export function holds(d: Record<string, unknown>, touched: Record<string, number[]>): string | null {
80
+ for (const id of touched["stays"] ?? []) {
81
+ const all = (d["stays"] ?? []) as any[];
82
+ const a = all.find((i) => i.id === id);
83
+ if (!a) continue;
84
+ const span = (i: any): [number, number] => [(i["from"] === undefined ? -Infinity : Date.parse(i["from"] + "Z")), (i["until"] === undefined ? Infinity : Date.parse(i["until"] + "Z"))];
85
+ const [from, until] = span(a);
86
+ if (until < from) return "stays " + a.id + " ends before it starts";
87
+ const c = all.find((i) => i.id !== a.id && span(i)[0] < until && from < span(i)[1]);
88
+ if (c) return "stays " + a.id + " and " + c.id + " are at one time: move or remove one first";
89
+ }
90
+ return null;
91
+ }
92
+
93
+ /** trip is one of many: a document for each of trips's trips (trip:1), its items each kept with it. */
94
+ export const kind = {"root":"trips","doc":"trips","column":"trip"};
95
+
96
+ /** Each grant, read off the stories: the roles whose stories do it (a member: anyone). An op no story
97
+ * does isn't one. */
98
+ export const grants: Record<string, string[]> = {
99
+ "add events": [
100
+ "traveller",
101
+ "organiser"
102
+ ],
103
+ "add events.arrives[]": [
104
+ "organiser",
105
+ "traveller"
106
+ ],
107
+ "add events.asked[]": [
108
+ "organiser",
109
+ "traveller"
110
+ ],
111
+ "add events.declined[]": [
112
+ "traveller",
113
+ "organiser"
114
+ ],
115
+ "add events.keen[]": [
116
+ "traveller",
117
+ "organiser"
118
+ ],
119
+ "add events.leaves[]": [
120
+ "organiser",
121
+ "traveller"
122
+ ],
123
+ "add events.people[]": [
124
+ "traveller",
125
+ "organiser"
126
+ ],
127
+ "add stays": [
128
+ "organiser",
129
+ "traveller"
130
+ ],
131
+ "remove events": [
132
+ "organiser"
133
+ ],
134
+ "remove events.arrives[]": [
135
+ "organiser",
136
+ "traveller"
137
+ ],
138
+ "remove events.asked": [
139
+ "organiser",
140
+ "traveller"
141
+ ],
142
+ "remove events.asked[]": [
143
+ "traveller"
144
+ ],
145
+ "remove events.coordinates": [
146
+ "organiser"
147
+ ],
148
+ "remove events.declined": [
149
+ "organiser",
150
+ "traveller"
151
+ ],
152
+ "remove events.declined[]": [
153
+ "organiser"
154
+ ],
155
+ "remove events.keen": [
156
+ "organiser",
157
+ "traveller"
158
+ ],
159
+ "remove events.keen[]": [
160
+ "traveller"
161
+ ],
162
+ "remove events.leaves[]": [
163
+ "organiser",
164
+ "traveller"
165
+ ],
166
+ "remove events.location": [
167
+ "organiser"
168
+ ],
169
+ "remove events.people[]": [
170
+ "traveller",
171
+ "organiser"
172
+ ],
173
+ "remove events.zone": [
174
+ "organiser"
175
+ ],
176
+ "remove stays": [
177
+ "organiser"
178
+ ],
179
+ "replace events.asked": [
180
+ "organiser",
181
+ "traveller"
182
+ ],
183
+ "replace events.coordinates": [
184
+ "traveller",
185
+ "organiser"
186
+ ],
187
+ "replace events.declined": [
188
+ "organiser",
189
+ "traveller"
190
+ ],
191
+ "replace events.duration": [
192
+ "traveller",
193
+ "organiser"
194
+ ],
195
+ "replace events.keen": [
196
+ "organiser",
197
+ "traveller"
198
+ ],
199
+ "replace events.location": [
200
+ "traveller",
201
+ "organiser"
202
+ ],
203
+ "replace events.people": [
204
+ "organiser",
205
+ "traveller"
206
+ ],
207
+ "replace events.start": [
208
+ "traveller",
209
+ "organiser"
210
+ ],
211
+ "replace events.title": [
212
+ "traveller",
213
+ "organiser"
214
+ ],
215
+ "replace events.zone": [
216
+ "organiser"
217
+ ],
218
+ "replace stays.from": [
219
+ "organiser"
220
+ ],
221
+ "replace stays.name": [
222
+ "traveller"
223
+ ],
224
+ "replace stays.until": [
225
+ "organiser"
226
+ ]
227
+ };
228
+
229
+ /** Who someone is to trip (and to an item of it, `at`), beyond a member: each role whose who-path finds them. */
230
+ export function rolesOf(m: number, read: (doc: string) => any, doc: string, at?: any): string[] {
231
+ const item: any = (read("trips")["trips"] ?? []).find((i: any) => i.id === Number(doc.split(/[:?]/)[1]));
232
+ return [
233
+ ((v: any) => v === m || (Array.isArray(v) && v.includes(m)))((at != null && "organisers" in at ? at : item)?.["organisers"]) ? "organiser" : "",
234
+ ((v: any) => v === m || (Array.isArray(v) && v.includes(m)))((at != null && "travellers" in at ? at : item)?.["travellers"]) ? "traveller" : "",
235
+ ].filter(Boolean);
236
+ }
237
+
238
+ /** Each person is sent only what they see of trip (its definitions' owns): its query, by its member. */
239
+ export const sees = true;