@blueshed/joined 0.2.2 → 0.3.1
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.
- package/.claude/skills/joined/SKILL.md +9 -1
- package/.claude/skills/joined/reference.md +316 -63
- package/.claude/skills/joined-door/SKILL.md +57 -0
- package/README.md +19 -50
- package/docs/design.md +158 -37
- package/docs/introduction.md +27 -27
- package/docs/light-and-shadow.svg +2 -1
- package/docs/links-and-trust.svg +1 -0
- package/docs/stories-to-code.svg +2 -1
- package/docs/wire-op.svg +1 -0
- package/examples/README.md +20 -0
- package/examples/account/account.json +1 -0
- package/examples/account/derive.ts +41 -0
- package/examples/account/generated/elements.ts +52 -0
- package/examples/account/generated/rows.ts +22 -0
- package/examples/account/generated/rules.ts +32 -0
- package/examples/account/routes.ts +5 -0
- package/examples/app.css +11 -1
- package/examples/cafe/books.html +2 -2
- package/examples/cafe/cafe.css +96 -85
- package/examples/cafe/generated/books.rows.ts +22 -0
- package/examples/cafe/generated/books.rules.ts +3 -2
- package/examples/cafe/generated/kitchen.rows.ts +27 -0
- package/examples/cafe/generated/kitchen.rules.ts +11 -1
- package/examples/cafe/generated/menu.rows.ts +22 -0
- package/examples/cafe/generated/menu.rules.ts +3 -2
- package/examples/cafe/generated/order.rows.ts +27 -0
- package/examples/cafe/generated/order.rules.ts +11 -1
- package/examples/cafe/generated/staff.elements.ts +7 -2
- package/examples/cafe/generated/staff.rows.ts +22 -0
- package/examples/cafe/generated/staff.rules.ts +9 -1
- package/examples/cafe/generated/supplies.queries.ts +4 -2
- package/examples/cafe/generated/supplies.rows.ts +22 -0
- package/examples/cafe/generated/supplies.rules.ts +17 -1
- package/examples/cafe/generated/till.queries.ts +2 -0
- package/examples/cafe/generated/till.rows.ts +27 -0
- package/examples/cafe/generated/till.rules.ts +11 -1
- package/examples/cafe/index.html +14 -15
- package/examples/cafe/kitchen.css +11 -19
- package/examples/cafe/kitchen.ts +7 -3
- package/examples/cafe/look.ts +30 -0
- package/examples/cafe/menu.html +10 -26
- package/examples/cafe/menu.ts +8 -80
- package/examples/cafe/migrations/002-guard-supplies.ts +15 -0
- package/examples/cafe/order.html +8 -7
- package/examples/cafe/order.ts +6 -2
- package/examples/cafe/routes.ts +0 -1
- package/examples/cafe/staff.json +5 -0
- package/examples/cafe/stories.jsonl +6 -2
- package/examples/cafe/supplies.html +35 -34
- package/examples/cafe/supplies.ts +59 -39
- package/examples/cafe/till.ts +19 -11
- package/examples/cafe/views.ts +7 -5
- package/examples/chart/arches.ts +150 -0
- package/examples/chart/chart.css +11 -0
- package/examples/chart/chart.json +87 -0
- package/examples/chart/generated/elements.ts +105 -0
- package/examples/chart/generated/queries.ts +36 -0
- package/examples/chart/generated/rows.ts +27 -0
- package/examples/chart/generated/rules.ts +111 -0
- package/examples/chart/grid.ts +118 -0
- package/examples/chart/index.html +78 -0
- package/examples/chart/migrations/001-chart.ts +6 -0
- package/examples/chart/migrations/002-create-conditions.ts +6 -0
- package/examples/chart/page.ts +20 -0
- package/examples/chart/routes.ts +7 -0
- package/examples/chart/stories.jsonl +14 -0
- package/examples/chart/teeth.ts +57 -0
- package/examples/desk/desk.css +6 -10
- package/examples/desk/generated/elements.ts +1 -1
- package/examples/desk/generated/rows.ts +22 -0
- package/examples/desk/generated/rules.ts +34 -1
- package/examples/desk/index.html +10 -3
- package/examples/desk/lanes.css +21 -19
- package/examples/desk/routes.ts +0 -2
- package/examples/desk/stories.jsonl +4 -3
- package/examples/desk/tally.css +1 -1
- package/examples/desk/thumbs.ts +2 -2
- package/examples/docs.ts +7 -4
- package/examples/hello/generated/elements.ts +2 -2
- package/examples/hello/generated/rules.ts +3 -2
- package/examples/hello/hello.css +2 -4
- package/examples/hello/index.html +10 -3
- package/examples/hello/page.ts +4 -4
- package/examples/index.html +20 -12
- package/examples/notes/generated/elements.ts +112 -0
- package/examples/notes/generated/queries.ts +42 -0
- package/examples/notes/generated/rows.ts +27 -0
- package/examples/notes/generated/rules.ts +133 -0
- package/examples/notes/index.html +32 -0
- package/examples/notes/look.ts +41 -0
- package/examples/notes/migrations/001-notes.ts +7 -0
- package/examples/notes/notes.json +78 -0
- package/examples/notes/page.ts +21 -0
- package/examples/notes/routes.ts +6 -0
- package/examples/notes/stories.jsonl +24 -0
- package/examples/page.ts +9 -2
- package/examples/people/generated/rows.ts +22 -0
- package/examples/people/generated/rules.ts +11 -1
- package/examples/people/links.ts +8 -9
- package/examples/people/people.css +1 -1
- package/examples/people/routes.ts +1 -3
- package/examples/people/stories.jsonl +1 -0
- package/examples/people/view.ts +5 -2
- package/examples/trip/generated/elements.ts +5 -3
- package/examples/trip/generated/queries.ts +12 -1
- package/examples/trip/generated/rows.ts +27 -0
- package/examples/trip/generated/rules.ts +56 -2
- package/examples/trip/index.html +58 -12
- package/examples/trip/line.css +186 -0
- package/examples/trip/line.ts +51 -40
- package/examples/trip/page.ts +65 -71
- package/examples/trip/routes.ts +9 -5
- package/examples/trip/stays.css +13 -13
- package/examples/trip/stays.ts +15 -10
- package/examples/trip/stories.jsonl +2 -0
- package/examples/trip/told.ts +3 -26
- package/examples/trip/trip.css +16 -0
- package/examples/trips/generated/elements.ts +1 -1
- package/examples/trips/generated/rows.ts +22 -0
- package/examples/trips/generated/rules.ts +9 -1
- package/examples/trips/index.html +10 -10
- package/examples/trips/map.ts +3 -14
- package/examples/trips/new-trip.ts +14 -16
- package/examples/trips/trips.css +24 -23
- package/examples/weeks/generated/elements.ts +1 -1
- package/examples/weeks/generated/queries.ts +11 -0
- package/examples/weeks/generated/rows.ts +22 -0
- package/examples/weeks/generated/rules.ts +11 -5
- package/examples/weeks/routes.ts +3 -2
- package/examples/weeks/stories.jsonl +1 -0
- package/examples/welcome.css +43 -33
- package/package.json +8 -2
- package/src/attachment.ts +17 -2
- package/src/awesome.ts +103 -0
- package/src/board.css +76 -0
- package/src/board.ts +173 -0
- package/src/cli.ts +28 -3
- package/src/compile.ts +821 -109
- package/src/definitions.ts +113 -27
- package/src/demo.css +2 -5
- package/src/draft.ts +48 -27
- package/src/drawer.ts +248 -0
- package/src/engine.ts +253 -170
- package/src/fields.ts +341 -0
- package/src/file-value.ts +12 -2
- package/src/files.ts +113 -33
- package/src/ground.ts +110 -0
- package/src/icon-names.ts +19 -2
- package/src/keep.ts +16 -3
- package/src/kit.ts +51 -0
- package/src/link.ts +3 -3
- package/src/map.css +140 -0
- package/src/map.ts +200 -0
- package/src/mcp.ts +129 -24
- package/src/members/demo.ts +13 -12
- package/src/members/generated/rows.ts +22 -0
- package/src/members/generated/rules.ts +3 -2
- package/src/members/group.ts +105 -55
- package/src/members/keys.ts +95 -79
- package/src/members/panel.css +40 -35
- package/src/members/panel.ts +58 -37
- package/src/migrate.ts +178 -125
- package/src/migrations/005-change-kept.ts +7 -0
- package/src/month.css +129 -0
- package/src/month.ts +179 -0
- package/src/ops.ts +40 -1
- package/src/page.css +11 -2
- package/src/page.ts +68 -17
- package/src/parts.css +614 -230
- package/src/parts.ts +259 -36
- package/src/plane.css +61 -0
- package/src/plane.ts +153 -0
- package/src/postgres-server.ts +60 -0
- package/src/reading.css +73 -0
- package/src/reading.ts +238 -0
- package/src/rows.css +289 -0
- package/src/rows.ts +864 -0
- package/src/search.css +80 -0
- package/src/search.ts +130 -0
- package/src/server.ts +355 -137
- package/src/settings.ts +9 -4
- package/src/sheet.ts +116 -0
- package/src/skill.ts +40 -0
- package/src/slots.css +90 -0
- package/src/slots.ts +213 -0
- package/src/specs.ts +38 -0
- package/src/starter.ts +19 -2
- package/src/store.ts +224 -161
- package/src/stories.ts +43 -11
- package/src/surface.ts +164 -0
- package/src/time.ts +2 -0
- package/src/timeline.css +111 -0
- package/src/timeline.ts +235 -0
- package/src/toast.ts +21 -48
- package/src/together.ts +15 -24
- package/src/tokens.css +89 -3
- package/src/tokens.json +287 -7
- package/src/tool.ts +210 -0
- package/src/upkeep.ts +47 -22
- package/src/views.css +43 -0
- package/src/views.ts +111 -0
- package/src/week.css +150 -0
- package/src/week.ts +254 -0
- package/src/write.ts +326 -78
- package/starter/CHANGELOG.md +8 -0
- package/starter/CLAUDE.md +9 -3
- package/starter/bunfig +5 -0
- package/starter/githooks/pre-commit +13 -0
- package/starter/prettierignore +5 -0
- package/starter/tests/hello.test.ts +2 -0
- package/starter/tests/records.test.ts +40 -0
- package/starter/todo.jsonl +0 -0
- package/docs/learning.md +0 -76
- package/examples/cafe/trust.ts +0 -20
- package/examples/desk/trust.ts +0 -24
- package/examples/people/trust.ts +0 -14
- package/examples/trip/answer-rows.ts +0 -70
- package/examples/trip/bar.ts +0 -89
- package/examples/trip/calendar-map.css +0 -711
- package/examples/trip/calendar-map.ts +0 -249
- package/examples/trip/drags.ts +0 -143
- package/examples/trip/drawer.ts +0 -250
- package/examples/trip/event-form.ts +0 -200
- package/examples/trip/holding.ts +0 -61
- package/examples/trip/map.ts +0 -234
- package/examples/trip/month.ts +0 -87
- package/examples/trip/other-weeks.ts +0 -67
- package/examples/trip/peek.ts +0 -71
- package/examples/trip/people-chips.ts +0 -74
- package/examples/trip/shell.ts +0 -39
- package/examples/trip/trust.ts +0 -51
- package/examples/trip/week.ts +0 -237
- package/examples/trip/writes.ts +0 -91
- package/examples/trip/zones.ts +0 -16
- package/examples/weeks/derive.ts +0 -23
- package/src/scrim.ts +0 -81
package/src/drawer.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// The drawer of a sheet (<j-sheet>, ./sheet.ts): one item's fields as this person may change them, held as a
|
|
2
|
+
// Draft until Save, one write; Remove at its foot, apart, asked first in a modal. A new item is a modal's, never the
|
|
3
|
+
// drawer's (design.md: new and delete are modal, edit is the drawer): asked only what it can't be without (what it
|
|
4
|
+
// needs, its title, what's offered filled in), Add off until it has it; the rest is filled in afterwards, in the
|
|
5
|
+
// drawer. A link to one is chosen as design.md's Choosing says: six or
|
|
6
|
+
// fewer, a select; more, a typeahead. What's open is in the address's hash (#option=2), so Back closes it.
|
|
7
|
+
|
|
8
|
+
import { Draft } from "./draft.ts";
|
|
9
|
+
import type { Answer } from "./page.ts";
|
|
10
|
+
import { control, el, type Asked, type Control } from "./fields.ts";
|
|
11
|
+
import { askFirst, inHash, surface } from "./parts.ts";
|
|
12
|
+
import { Surface } from "./surface.ts";
|
|
13
|
+
import { icon } from "./icons.ts";
|
|
14
|
+
import type { Field, Item, Spec } from "./rows.ts";
|
|
15
|
+
|
|
16
|
+
export { el };
|
|
17
|
+
|
|
18
|
+
let forms = 0;
|
|
19
|
+
|
|
20
|
+
/** What a drawer asks of the display it's in: where it's put, what things are called, and how a write is sent. */
|
|
21
|
+
export type Host = {
|
|
22
|
+
root: ShadowRoot;
|
|
23
|
+
label(f: Field): string;
|
|
24
|
+
title(it: Element): string;
|
|
25
|
+
/** The items a link may be to, as they're on the page: each its key and its title. */
|
|
26
|
+
choices(f: Field): [key: number, words: string][];
|
|
27
|
+
/** The document its items are of, as the page holds it (a guest's, with its code): a file is kept for it. */
|
|
28
|
+
doc(): string;
|
|
29
|
+
add(spec: Spec, value: object): Promise<Answer>;
|
|
30
|
+
/** What's said of a write refused once the drawer has closed (a removal). */
|
|
31
|
+
said(request: Promise<Answer>): void;
|
|
32
|
+
/** Told as the drawer opens on an item or closes: what's open is marked on its row. */
|
|
33
|
+
shown(): void;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export class Drawer {
|
|
37
|
+
/** The item in it: one of the document's, "new" for one being added, or none. */
|
|
38
|
+
open: Item | "new" | null = null;
|
|
39
|
+
#form = el("form", { id: `j-form-${++forms}` });
|
|
40
|
+
#box: Surface;
|
|
41
|
+
#ask = surface("wa-dialog", { className: "ask" });
|
|
42
|
+
/** Where a new one is asked: the drawer's form and foot, borrowed. */
|
|
43
|
+
#new = surface("wa-dialog", { className: "modal" });
|
|
44
|
+
#go = el("button", { className: "act ink", name: "go" });
|
|
45
|
+
#gone = el("button", { type: "button", className: "act danger", name: "gone" }, icon("remove"), "Remove");
|
|
46
|
+
#draft: Draft;
|
|
47
|
+
#hash: ReturnType<typeof inHash>;
|
|
48
|
+
/** What a new item is given without being asked (the item it's added under). */
|
|
49
|
+
#given: Record<string, number> = {};
|
|
50
|
+
/** What a new item is offered already filled in, still asked (a calendar's day and hour tapped). */
|
|
51
|
+
#filled: Record<string, unknown> = {};
|
|
52
|
+
/** Each field's control, by its name. */
|
|
53
|
+
#controls = new Map<string, Control>();
|
|
54
|
+
constructor(
|
|
55
|
+
readonly host: Host,
|
|
56
|
+
readonly spec: Spec,
|
|
57
|
+
readonly asks: Asked[],
|
|
58
|
+
items: () => Item[],
|
|
59
|
+
signal: AbortSignal,
|
|
60
|
+
) {
|
|
61
|
+
const note = el("wa-callout", { className: "note", role: "status", hidden: true, size: "small" });
|
|
62
|
+
this.#form.append(...asks.map((f) => this.#asked(f).el), note);
|
|
63
|
+
const form = this.#form.id,
|
|
64
|
+
foot = el(
|
|
65
|
+
"footer",
|
|
66
|
+
{ part: "foot" },
|
|
67
|
+
this.#gone,
|
|
68
|
+
el("button", { type: "button", className: "act", name: "never" }, "Never mind"),
|
|
69
|
+
this.#go,
|
|
70
|
+
);
|
|
71
|
+
// its deeds at the drawer's foot, outside the form, are its own still
|
|
72
|
+
for (const b of foot.querySelectorAll("button")) b.setAttribute("form", form);
|
|
73
|
+
this.#box = new Surface("drawer", this.#form, foot, () => this.show(null), { label: `A ${spec.one}` });
|
|
74
|
+
this.#new.append(el("h2", { slot: "label" }));
|
|
75
|
+
host.root.append(...this.#box.els, this.#ask, this.#new);
|
|
76
|
+
// its ×, Escape or a tap away: as Never mind
|
|
77
|
+
this.#new.addEventListener("wa-hide", (e) => e.target === this.#new && this.open === "new" && this.show(null));
|
|
78
|
+
this.#draft = new Draft(this.#form, note);
|
|
79
|
+
const ask = askFirst(this.#ask);
|
|
80
|
+
// Never mind: a new one let go; an item's changes put back
|
|
81
|
+
this.#draft.never(() => (this.open === "new" ? this.show(null) : this.show(this.open, this.#given, this.#filled)));
|
|
82
|
+
// said again once the control has drawn what's typed: Web Awesome's say whether they're valid only then
|
|
83
|
+
this.#form.addEventListener("input", (e) => {
|
|
84
|
+
this.#shown();
|
|
85
|
+
void (e.target as { updateComplete?: Promise<unknown> }).updateComplete?.then(() => this.#shown());
|
|
86
|
+
});
|
|
87
|
+
this.#form.addEventListener("submit", (e) => (e.preventDefault(), void this.#save()));
|
|
88
|
+
this.#gone.addEventListener("click", () => {
|
|
89
|
+
const it = this.open as Item;
|
|
90
|
+
ask(`${host.title(it)} will be removed.`, () => (this.show(null), host.said(it.removeItem!())));
|
|
91
|
+
});
|
|
92
|
+
this.#hash = inHash(
|
|
93
|
+
spec.one,
|
|
94
|
+
items,
|
|
95
|
+
() => (this.open === "new" ? null : this.open),
|
|
96
|
+
(it) => this.show(it, {}, {}, false),
|
|
97
|
+
signal,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
#control = (name: string) => this.#form.elements.namedItem(name) as HTMLInputElement | HTMLSelectElement;
|
|
101
|
+
/** A field as the drawer asks for it, in its label, as its kind says (./fields.ts). */
|
|
102
|
+
#asked(f: Asked) {
|
|
103
|
+
const c = control(f, this.host.label(f), {
|
|
104
|
+
choices: () => this.#offered(f),
|
|
105
|
+
// what's offered may follow what's chosen (a field's `not`)
|
|
106
|
+
changed: () => (this.#shown(), this.asks.some((a) => a.not) && this.#choices()),
|
|
107
|
+
doc: () => this.host.doc(),
|
|
108
|
+
});
|
|
109
|
+
this.#controls.set(f.name, c);
|
|
110
|
+
return c;
|
|
111
|
+
}
|
|
112
|
+
/** What a link may be to: the page's items, but none its field `not` holds now (an owner never offered as their own
|
|
113
|
+
* accountant), unless it's linked to them already. */
|
|
114
|
+
#offered(f: Asked) {
|
|
115
|
+
const all = this.host.choices(f);
|
|
116
|
+
if (!f.not) return all;
|
|
117
|
+
const out = new Set(this.#holds(f.not)),
|
|
118
|
+
mine = new Set(this.#holds(f.name));
|
|
119
|
+
return all.filter(([key]) => !out.has(key) || mine.has(key));
|
|
120
|
+
}
|
|
121
|
+
/** The ids a field holds now: as it's chosen in the form where it's asked, else as the open item has it. */
|
|
122
|
+
#holds(name: string): number[] {
|
|
123
|
+
const c = this.#controls.get(name),
|
|
124
|
+
box = this.#control(name);
|
|
125
|
+
const v = c && box ? c.kept(box.value) : this.open && this.open !== "new" ? this.open.data[name] : undefined;
|
|
126
|
+
return [v ?? []].flat() as number[];
|
|
127
|
+
}
|
|
128
|
+
/** What each control offers, as the page has it now (the items a link may be to, the people to choose). */
|
|
129
|
+
#choices() {
|
|
130
|
+
for (const f of this.asks) {
|
|
131
|
+
const c = this.#controls.get(f.name)!;
|
|
132
|
+
c.refresh();
|
|
133
|
+
// a link an add needs, chosen from a few: asked for by its select, where it's shown
|
|
134
|
+
if (f.how === "one") {
|
|
135
|
+
const select = this.#control(f.name) as HTMLSelectElement;
|
|
136
|
+
select.required = !!f.needed && !select.hidden && !this.#controls.get(f.name)!.el.hidden;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
#values = (it: Item | null) =>
|
|
141
|
+
Object.fromEntries(
|
|
142
|
+
this.asks.map((f) => [
|
|
143
|
+
f.name,
|
|
144
|
+
this.#controls.get(f.name)!.shown(it?.data[f.name] ?? this.#given[f.name] ?? this.#filled[f.name]),
|
|
145
|
+
]),
|
|
146
|
+
);
|
|
147
|
+
/** Whether what's typed differs from the item; of a new one, whether it has what an add needs. */
|
|
148
|
+
#shown() {
|
|
149
|
+
if (this.open === "new") return this.#draft.show("new", this.#form.checkValidity() && this.#needs());
|
|
150
|
+
// changed, and ready only where what it needs is still there (a title taken away isn't)
|
|
151
|
+
this.#draft.show(Object.keys(this.#draft.edits()).length ? "changed" : "kept", this.#form.checkValidity());
|
|
152
|
+
}
|
|
153
|
+
/** Whether each link an add needs is chosen (a typeahead's is kept unseen, where the form can't ask for it). */
|
|
154
|
+
#needs = () => this.asks.every((f) => !f.needed || !f.to || f.name in this.#given || this.#control(f.name).value);
|
|
155
|
+
/** The page's items came, went or changed: the choices follow; the one open, gone, closes the drawer; changed
|
|
156
|
+
* elsewhere, what isn't being typed in follows it. */
|
|
157
|
+
refresh() {
|
|
158
|
+
this.#choices();
|
|
159
|
+
this.#hash.again();
|
|
160
|
+
if (!this.open || this.open === "new") return;
|
|
161
|
+
if (!this.open.isConnected) return this.show(null);
|
|
162
|
+
this.#offer(this.open);
|
|
163
|
+
this.#draft.fill(this.#values(this.open));
|
|
164
|
+
this.#shown();
|
|
165
|
+
}
|
|
166
|
+
/** The drawer with an item in it, or a new one (given what it's added under, and offered what's `filled`, which it
|
|
167
|
+
* still asks), or closed; marked in the address. */
|
|
168
|
+
show(it: Item | "new" | null, given: Record<string, number> = {}, filled: Record<string, unknown> = {}, mark = true) {
|
|
169
|
+
this.open = it;
|
|
170
|
+
this.#given = it === "new" ? given : {};
|
|
171
|
+
this.#filled = it === "new" ? filled : {};
|
|
172
|
+
// what shows the collection marks the one open (design.md, "A row"), and no other
|
|
173
|
+
document.dispatchEvent(new CustomEvent("j-open", { detail: { for: this.spec.name } }));
|
|
174
|
+
this.#box.open = !!it && it !== "new";
|
|
175
|
+
this.#asking(it === "new");
|
|
176
|
+
if (mark) this.#hash.mark(it && it !== "new" ? it.key : null);
|
|
177
|
+
this.host.shown();
|
|
178
|
+
if (!it) return;
|
|
179
|
+
const item = it === "new" ? null : it;
|
|
180
|
+
this.#box.title.textContent = item ? `Editing “${this.host.title(item)}”` : `A new ${this.spec.one}`;
|
|
181
|
+
this.#offer(item);
|
|
182
|
+
this.#choices();
|
|
183
|
+
this.#draft.refuse("");
|
|
184
|
+
// afresh: WebKit keeps a datetime-local box set back to "" invalid until its form is reset
|
|
185
|
+
this.#form.reset();
|
|
186
|
+
void this.#draft.fill(this.#values(item), true).then(() => this.#shown());
|
|
187
|
+
this.#choices();
|
|
188
|
+
this.#shown();
|
|
189
|
+
}
|
|
190
|
+
/** The fields offered: of an item, what this person may change (its `may`, which may come after it, as a page
|
|
191
|
+
* opened on it from its address is sent it); of a new one, what it can't be without, and isn't given already. */
|
|
192
|
+
#offer(item: Item | null) {
|
|
193
|
+
const may = (item?.getAttribute("may") ?? "").split(" ");
|
|
194
|
+
for (const f of this.asks) {
|
|
195
|
+
const label = this.#controls.get(f.name)!.el;
|
|
196
|
+
// an item's: what this person may change; a new one's: what it can't be without, and isn't given already
|
|
197
|
+
label.hidden = item
|
|
198
|
+
? !f.readonly && !may.includes(`replace:${f.name}`)
|
|
199
|
+
: f.name in this.#given || !(f.needed || f.name === this.spec.title || f.name in this.#filled);
|
|
200
|
+
if (!f.to && f.type !== "place") {
|
|
201
|
+
const box = this.#control(f.name);
|
|
202
|
+
box.required = !!f.needed && f.type !== "boolean" && !label.hidden;
|
|
203
|
+
// said once it's there, asked as it's added
|
|
204
|
+
if (f.readonly) (box.toggleAttribute("readonly", !!item), box.toggleAttribute("without-steppers", !!item));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
this.#gone.hidden = !may.includes("remove");
|
|
208
|
+
}
|
|
209
|
+
/** A new one asked in the modal, its form and foot borrowed from the drawer; else they're the drawer's again. */
|
|
210
|
+
#asking(on: boolean) {
|
|
211
|
+
const foot = this.#go.parentElement!;
|
|
212
|
+
this.#go.replaceChildren(icon(on ? "add" : "check"), on ? "Add" : "Save");
|
|
213
|
+
if (on) {
|
|
214
|
+
this.#new.querySelector("h2")!.textContent = `A new ${this.spec.one}`;
|
|
215
|
+
foot.slot = "footer";
|
|
216
|
+
this.#new.append(this.#form, foot);
|
|
217
|
+
} else if (this.#form.parentElement === this.#new) this.#box.hold();
|
|
218
|
+
this.#new.open = on;
|
|
219
|
+
}
|
|
220
|
+
/** Save: the changes in one write, or the new item added; refused, why is said here, and what's typed stays. */
|
|
221
|
+
async #save() {
|
|
222
|
+
const adding = this.open === "new",
|
|
223
|
+
item = this.open as Item;
|
|
224
|
+
const typed = Object.entries(this.#draft.edits()).map(
|
|
225
|
+
([name, v]) => [name, typeof v === "string" ? v.trim() : v] as const,
|
|
226
|
+
);
|
|
227
|
+
// an item's field emptied: words kept as none; anything else taken off it, where a story takes it off
|
|
228
|
+
const words = (name: string) => this.asks.find((f) => f.name === name)?.type === "string",
|
|
229
|
+
cleared = adding ? [] : typed.filter(([name, v]) => v === "" && !words(name)).map(([name]) => name),
|
|
230
|
+
stays = cleared.find((name) => !(item.getAttribute("may") ?? "").split(" ").includes(`remove:${name}`));
|
|
231
|
+
const edits = Object.fromEntries(
|
|
232
|
+
typed
|
|
233
|
+
.filter(([name, v]) => v !== "" || (!adding && words(name)))
|
|
234
|
+
.map(([name, v]) => [name, this.#controls.get(name)!.kept(v)]),
|
|
235
|
+
);
|
|
236
|
+
const was = this.open;
|
|
237
|
+
const why = adding
|
|
238
|
+
? await this.host.add(this.spec, { ...this.#given, ...this.#filled, ...edits })
|
|
239
|
+
: stays
|
|
240
|
+
? `${stays} can't be left empty`
|
|
241
|
+
: ((Object.keys(edits).length ? await item.replace!(edits) : undefined) ??
|
|
242
|
+
(cleared.length ? await item.removeField!(...cleared) : undefined));
|
|
243
|
+
// what was saved, if it's open still: one opened meanwhile (a new one asked) is left as it is
|
|
244
|
+
if (this.open !== was) return;
|
|
245
|
+
this.#draft.refuse(why ?? "");
|
|
246
|
+
if (!why) this.show(adding ? null : was);
|
|
247
|
+
}
|
|
248
|
+
}
|