@sincpro/web-framework 3.18.0 → 3.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +83 -54
  2. package/dist/core/copy.d.ts +14 -0
  3. package/dist/core/copy.js +57 -2
  4. package/dist/core/edit.d.ts +13 -0
  5. package/dist/core/edit.js +14 -0
  6. package/dist/core/explain.d.ts +4 -0
  7. package/dist/core/explain.js +4 -0
  8. package/dist/core/export.d.ts +29 -0
  9. package/dist/core/export.js +47 -0
  10. package/dist/core/groups.d.ts +3 -1
  11. package/dist/core/groups.js +7 -0
  12. package/dist/core/index.d.ts +4 -3
  13. package/dist/core/index.js +4 -3
  14. package/dist/core/lines.d.ts +10 -0
  15. package/dist/core/lines.js +24 -0
  16. package/dist/core/list.d.ts +12 -0
  17. package/dist/react/context.d.ts +44 -1
  18. package/dist/react/context.js +50 -1
  19. package/dist/react/index.d.ts +4 -2
  20. package/dist/react/index.js +4 -2
  21. package/dist/react/useCounts.d.ts +23 -0
  22. package/dist/react/useCounts.js +43 -0
  23. package/dist/react/useGroups.js +38 -5
  24. package/dist/react/useNameOf.d.ts +13 -0
  25. package/dist/react/useNameOf.js +32 -0
  26. package/dist/react/usePeopleOf.d.ts +14 -0
  27. package/dist/react/usePeopleOf.js +35 -0
  28. package/dist/react/useRecord.js +2 -2
  29. package/dist/react/useServiceReading.d.ts +8 -0
  30. package/dist/react/useServiceReading.js +12 -2
  31. package/dist/web/index.d.ts +4 -2
  32. package/dist/web/index.js +4 -2
  33. package/dist/web/list/EntityList.d.ts +36 -177
  34. package/dist/web/list/EntityList.js +175 -536
  35. package/dist/web/list/entityList/choosing.d.ts +50 -0
  36. package/dist/web/list/entityList/choosing.js +77 -0
  37. package/dist/web/list/entityList/faces.d.ts +53 -0
  38. package/dist/web/list/entityList/faces.js +155 -0
  39. package/dist/web/list/entityList/grouping.d.ts +50 -0
  40. package/dist/web/list/entityList/grouping.js +97 -0
  41. package/dist/web/list/entityList/opening.d.ts +68 -0
  42. package/dist/web/list/entityList/opening.js +149 -0
  43. package/dist/web/list/entityList/paging.d.ts +30 -0
  44. package/dist/web/list/entityList/paging.js +39 -0
  45. package/dist/web/list/entityList/props.d.ts +174 -0
  46. package/dist/web/list/entityList/props.js +16 -0
  47. package/dist/web/list/entityList/rowActions.d.ts +31 -0
  48. package/dist/web/list/entityList/rowActions.js +40 -0
  49. package/dist/web/list/export.d.ts +13 -0
  50. package/dist/web/list/export.js +61 -0
  51. package/dist/web/list/groups.d.ts +16 -0
  52. package/dist/web/list/groups.js +39 -0
  53. package/dist/web/list/useEntityList.d.ts +183 -0
  54. package/dist/web/list/useEntityList.js +225 -0
  55. package/dist/web/parts.d.ts +42 -0
  56. package/dist/web/parts.js +88 -0
  57. package/dist/web/record/lines.d.ts +3 -0
  58. package/dist/web/record/lines.js +75 -9
  59. package/dist/web/workspace/RecordScreen.d.ts +206 -0
  60. package/dist/web/workspace/RecordScreen.js +251 -0
  61. package/dist/web/workspace/SincproApp.d.ts +14 -2
  62. package/dist/web/workspace/SincproApp.js +27 -9
  63. package/dist/web/workspace/composer.js +1 -1
  64. package/dist/web/workspace/record.d.ts +21 -27
  65. package/dist/web/workspace/record.js +29 -77
  66. package/dist/web/workspace/workspace.d.ts +2 -0
  67. package/dist/web/workspace/workspace.js +1 -1
  68. package/package.json +7 -7
@@ -1,547 +1,186 @@
1
1
  "use client";
2
- import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { PICK_PARAM, QUERY_PARAM, TRAIL_PARAM, } from "@sincpro/client-framework";
4
- import { nameOf } from "@sincpro/criteria";
5
- import { RecordLink, useCrumbs, usePhone } from "@sincpro/web-composer";
2
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
6
3
  import { frameworkCopy } from "../../core/copy.js";
7
- import { ownCrumbs } from "../../core/crumbs.js";
8
- import { explain } from "../../core/explain.js";
9
- import { choosesMany, } from "../../core/list.js";
10
- import { textOf, viewOf } from "../../core/present.js";
11
- import { summaryFieldsOf } from "../../core/summary.js";
12
- import { trailKeyOf } from "../../core/trail.js";
13
- import { useFramework } from "../../react/context.js";
14
- import { useGroups } from "../../react/useGroups.js";
15
- import { usePivot } from "../../react/usePivot.js";
16
- import { useSelection } from "../../react/useSelection.js";
17
- import { columnsOf } from "./columns.js";
18
- import { actionsOf } from "./dock.js";
4
+ import { textOf } from "../../core/present.js";
19
5
  import { EntitySearchBar } from "./EntitySearchBar.js";
20
- import { pilesOf } from "./groups.js";
21
- import { OpenInMenu, openOptions, targetsFor } from "./openIn.js";
22
6
  import { pivotOf } from "./pivot.js";
23
- import { over, presentationOf, } from "./presentation.js";
24
- import { useReorder } from "./reorder.js";
25
- import { RESTORE, useRowCommands, } from "./rowCommands.js";
26
- import { Button, Callout, List, Pagination, pathKey, RecordCard, SearchField, Tag, } from "@sincpro/web-ui";
27
- import { Plus, RotateCcw } from "lucide-react";
28
- import { useEffect, useId, useMemo, useRef, useState } from "react";
29
- /** What `useCrumbs` reads when the list is not in a workspace: no trail, and nothing to follow. */
30
- const NO_TRAIL = [];
31
- const NO_WORKSPACE = {
32
- crumbs: () => NO_TRAIL,
33
- context: { subscribe: () => () => undefined },
34
- layout: { phone: () => false, subscribe: () => () => undefined },
35
- };
36
- /** How many lists' trails are kept: the records opened from the last fifty lists page on. */
37
- const TRAILS = 50;
38
- /** web-ui's empty for this list: what the screen said, over the framework's words. */
39
- function emptyOf(asked, words) {
40
- const own = typeof asked === "string" ? { sentence: asked } : (asked ?? {});
41
- return {
42
- ...own,
43
- sentence: own.sentence ?? words.empty,
44
- whenFiltered: {
45
- ...own.whenFiltered,
46
- sentence: own.whenFiltered?.sentence ?? words.emptyFiltered,
47
- },
48
- };
7
+ import { useEntityList, } from "./useEntityList.js";
8
+ import { kindsIn, part, resolveParts } from "../parts.js";
9
+ import { Button, Callout, List, SearchField } from "@sincpro/web-ui";
10
+ import { createContext, useContext } from "react";
11
+ /**
12
+ * A whole collection from a `Listing`: the bar, the search, the set in the faces it offers, and
13
+ * the way through it — written as a tree of the framework's parts.
14
+ *
15
+ * <EntityList listing={invoices} api={api} /> ← the whole default
16
+ *
17
+ * <EntityList listing={invoices} api={api}>
18
+ * <EntityList.Bar /> ← crumbs, title, search, pager
19
+ * <EntityList.Table />
20
+ * <EntityList.Board />
21
+ * <InvoicesMap /> ← the application's own block
22
+ * </EntityList>
23
+ *
24
+ * The same rules as `RecordScreen` (docs/PATTERNS.md): with no children it draws the default;
25
+ * with children only what is written — the faces offered are the faces written; a part written
26
+ * empty draws its default content. A block of the application reads the list with
27
+ * `useScreenListing`.
28
+ *
29
+ * Built for sets of any size: in `pages` mode (the default) the screen holds ONE page and walks
30
+ * the server's cursor, so a million rows cost what fifty do; in `more` mode it stops growing at
31
+ * its ceiling and asks to narrow the search. Nothing is filtered, ordered or counted on the
32
+ * page: every gesture is a question to the server. Grouping obeys the same rule: the SERVER
33
+ * counts the groups, and a group's rows are read when it is opened (`useGroups`).
34
+ *
35
+ * Where a record opens is the composer's question: with `api`, a row opens the page and the
36
+ * identity column is the composer's `RecordLink` (peek, side panel, window); without it, the
37
+ * screen passes `onOpen`. What the list knows before it draws is `useEntityList`'s.
38
+ */
39
+ const ListContext = createContext(null);
40
+ /**
41
+ * The listing on the list around it, for a block the application writes inside an
42
+ * `EntityList` — a map of the same set, a summary over its page. The entity is only its type.
43
+ *
44
+ * function InvoicesMap() {
45
+ * const invoices = useScreenListing(facturas);
46
+ * return <Map points={invoices.rows.map(pointOf)} />;
47
+ * }
48
+ */
49
+ export function useScreenListing(_entity) {
50
+ const state = useContext(ListContext);
51
+ if (state === null)
52
+ throw new Error("useScreenListing is called inside an EntityList.");
53
+ return state.listing;
49
54
  }
50
- /** No commands: one constant, so the hook is not handed a new array on every render. */
51
- const NO_COMMANDS = [];
52
- /** What a list offers when nobody said: every face that needs no configuration. */
53
- const EVERY_VIEW = ["table", "rows", "board", "sheet"];
54
- export function EntityList({ listing, api, columns: askedColumns, cells, yesNo, views: askedViews, title: askedTitle, description: askedDescription, crumbs, actions, onOpen, onPeek, openTargets, onOpenIn, pivot: pivotAsked, empty, search = "bar", recent: askedRecent = false, sums, filters: askedFilters, facets: askedFacets, groupable: askedGroupable, selectable = false, selection: held, selectionActions, embedded = false, create = true, commands = NO_COMMANDS, rowActions: askedRowActions, faces, }) {
55
- // A named list (`defineList`) brings its own columns, filters, faces and words; what the
56
- // screen passes wins.
57
- const definition = listing.definition;
58
- // The entity's own look (`presented`): its columns, the tones of its choices, its row, card, tile.
59
- const look = presentationOf(listing.entity);
60
- const recent = askedRecent === false
61
- ? undefined
62
- : askedRecent === true
63
- ? (definition?.key ?? listing.entity.kind)
64
- : askedRecent;
65
- const columns = askedColumns ?? definition?.columns ?? look.columns;
66
- const views = askedViews ??
67
- definition?.views ??
68
- (embedded ? ["table"] : pivotAsked === undefined ? EVERY_VIEW : [...EVERY_VIEW, "pivot"]);
69
- const filters = askedFilters ?? definition?.filters ?? [];
70
- const groupable = askedGroupable ?? definition?.groupable;
71
- const facets = askedFacets ?? definition?.facets;
72
- const title = askedTitle ?? definition?.title;
73
- const description = askedDescription ?? definition?.description;
55
+ /** A part of the list, resolved against its state. */
56
+ function listPart(name, resolve, kind) {
57
+ return part(`EntityList.${name}`, resolve, kind);
58
+ }
59
+ /* ----------------------------------------------------------------------------------------- *
60
+ * The bar
61
+ * ----------------------------------------------------------------------------------------- */
62
+ /** Where the list stands in the workspace: the crumbs that lead back. */
63
+ const Crumbs = listPart("Crumbs", (_props, { bar }) => bar.trail === undefined || bar.trail.length === 0 ? null : (_jsx(List.Crumbs, { crumbs: bar.trail })));
64
+ /** Its name and what it is for. Not inside a record: there the field's label says it. */
65
+ const Identity = listPart("Identity", (_props, { bar, embedded }) => embedded ? null : (_jsx(List.Identity, { title: bar.title, ...(bar.description === undefined ? {} : { description: bar.description }) })));
66
+ /** The search: the full bar with the criteria builder, or one box over the search template. */
67
+ const Search = listPart("Search", (_props, { bar, listing, meta }) => {
68
+ if (bar.search === "bar")
69
+ return (_jsx(List.Search, { children: _jsx(EntitySearchBar, { listing: listing, filters: bar.filters, groups: bar.groups, ...(bar.groupable === undefined ? {} : { groupable: bar.groupable }), ...(bar.facets === undefined ? {} : { facets: bar.facets }), ...(bar.asked === "" ? {} : { seed: bar.asked }), ...(bar.recent === undefined ? {} : { recentKey: bar.recent }) }) }));
70
+ if (bar.search !== "box" || meta?.search == null)
71
+ return null;
74
72
  const copy = frameworkCopy().list;
75
- const { meta, locale, count, page } = listing;
76
- const identity = meta?.identity ?? "id";
77
- const idOf = (row) => String(row[identity]);
78
- const failed = listing.status === "failed" ? explain(listing.error) : null;
79
- const dropped = listing.dropped.map((d) => viewOf(meta, d.field, locale).label);
80
- const [present, setPresent] = useState(views[0] ?? "table");
81
- // Opened to choose on what was typed in a picker (`chooserRef(query)`): it starts searched.
82
- const asked = api?.self.params?.[QUERY_PARAM] ?? "";
83
73
  // The box's text is a draft until Enter: typing asks the server nothing.
84
- const [draft, setDraft] = useState(asked === "" ? listing.text : asked);
85
- useEffect(() => {
86
- if (asked !== "" && search === "box")
87
- listing.search(asked, { now: true });
88
- // Once, as it opens: what the person types next is theirs.
89
- // eslint-disable-next-line react-hooks/exhaustive-deps
90
- }, []);
91
- // Opened by `api.pick` (`entity.chooserRef()`): a row ANSWERS whoever asked, it does not open.
92
- const choosing = api?.self.params?.[PICK_PARAM] !== undefined;
93
- // Asked for SEVERAL (`CHOOSE_MANY`, a many2many's «Buscar más…»): a row press ticks it, and
94
- // «Agregar (N)» answers what was chosen — the keys, or every record the list matches.
95
- const choosingMany = choosing && choosesMany(api?.choosing?.question);
96
- const own = useSelection(listing);
97
- const chosen = held ?? own;
98
- // Actions for the chosen rows make the rows choosable: given without `selectable` they were
99
- // silently never shown — no ticks, nothing to act on.
100
- const ticking = selectable || choosingMany || selectionActions !== undefined;
101
- // The record opened from a row knows the page it came from: «3 / 64» and its neighbours.
102
- const { trails, dateStyle } = useFramework();
103
- const trailKey = trailKeyOf(useId());
104
- // Kept current with the page on screen: the row and its name (`RecordLink`) open the same
105
- // reference, and whichever opens it, the record finds where it came from.
106
- useEffect(() => {
107
- if (api === undefined)
108
- return;
109
- const pages = listing.paging === "pages";
110
- trails.set(trailKey, {
111
- route: listing.route,
112
- question: listing.question,
113
- key: identity,
114
- ids: listing.rows.map(idOf),
115
- offset: pages ? Math.max(0, listing.page.from - 1) : 0,
116
- size: listing.page.size,
117
- cursors: pages ? listing.page.cursors : [null],
118
- after: pages ? listing.page.after : null,
119
- total: pages ? (listing.count?.value ?? null) : listing.rows.length,
120
- });
121
- // Never forgotten when the list unmounts — the record it opened replaced it and still walks
122
- // on it — so the oldest go past a ceiling instead: one per list ever shown, fifty at most.
123
- for (const old of trails.keys()) {
124
- if (trails.size <= TRAILS)
125
- break;
126
- if (old !== trailKey)
127
- trails.delete(old);
128
- }
129
- // The page on screen is what the trail is: its rows, its cursors, its count.
130
- // eslint-disable-next-line react-hooks/exhaustive-deps
131
- }, [
132
- api,
133
- trails,
134
- trailKey,
135
- listing.rows,
136
- listing.count,
137
- listing.page.from,
138
- listing.page.after,
139
- ]);
140
- const refOf = (row) => {
141
- const ref = listing.entity.ref(idOf(row));
142
- return { ...ref, params: { ...ref.params, [TRAIL_PARAM]: trailKey } };
143
- };
144
- const openWithTrail = (row) => api?.open(refOf(row));
145
- const opening = onOpen ??
146
- (api === undefined
147
- ? undefined
148
- : choosingMany
149
- ? (row) => chosen.toggle(idOf(row))
150
- : choosing
151
- ? (row) => api.answer(listing.entity.ref(idOf(row)))
152
- : openWithTrail);
153
- const creating = create &&
154
- api !== undefined &&
155
- !choosing &&
156
- !embedded &&
157
- listing.entity.create !== undefined
158
- ? () => api.open(listing.entity.newRef())
159
- : undefined;
160
- // What the list starts, as data: «Nuevo» — the primary — and the screen's, drawn by the view
161
- // style (`List.Dock`). Actions drawn by hand stay in the bar, with «Nuevo» beside them.
162
- const started = actionsOf(actions);
163
- const startDock = started.drawn !== null
164
- ? []
165
- : [
166
- ...(creating === undefined
167
- ? []
168
- : [
169
- {
170
- id: "create",
171
- label: frameworkCopy().record.create,
172
- icon: Plus,
173
- primary: true,
174
- onSelect: creating,
175
- },
176
- ]),
177
- ...started.dock,
178
- ];
179
- // A press opens the page — the composer's one predictable gesture. Every other place the
180
- // record may open is among the row's ways in, web-ui's (`openTargets` + `onOpenIn`, web-ui
181
- // 2.10): 👁 for a look (`onPeek` with `rowClick="open"`: the press stays «open»), and «Abrir
182
- // en…» with the rest — in ONE place at the row's end, a pill in the modern style. Drawn beside
183
- // the name as well, a row offered three unaligned sets. A chooser only chooses.
184
- const openingIn = onOpenIn ??
185
- (api === undefined
74
+ return (_jsx(List.Search, { children: _jsx(SearchField, { value: bar.draft, onChange: (next) => {
75
+ bar.setDraft(next);
76
+ // Emptied (or cleared with ×): the whole set again, at once.
77
+ if (next === "" && listing.text !== "")
78
+ listing.search("", { now: true });
79
+ }, onSubmit: (text) => listing.search(text, { now: true }), placeholder: copy.search, "aria-label": copy.search }) }));
80
+ });
81
+ /** What the list starts — «Nuevo» and the screen's — floating in the dock as data, or in the
82
+ * bar when drawn by hand; and what is done to the chosen rows. */
83
+ const Actions = listPart("Actions", (_props, { bar }) => [
84
+ bar.drawn === null ? null : (_jsxs(List.Actions, { children: [bar.drawn, bar.creating === undefined ? null : (_jsx(Button, { role: "primary", size: "sm", onClick: bar.creating, children: frameworkCopy().record.create }))] }, "actions")),
85
+ bar.startDock.length === 0 && bar.chosenDock.length === 0 ? null : (_jsx(List.Dock, { ...(bar.startDock.length === 0 ? {} : { actions: bar.startDock }), ...(bar.chosenDock.length === 0 ? {} : { selection: bar.chosenDock }) }, "dock")),
86
+ ]);
87
+ /** The way through the set: the pager, «more», the groups counted. Written only when it says
88
+ * something — web-ui draws an embedded list's bar for any slot written, an empty one too. */
89
+ const Pager = listPart("Pager", (_props, { bar }) => bar.paging === null ? null : _jsx(List.Pagination, { children: bar.paging }));
90
+ /** The bar over the set. Empty, it draws crumbs, name, search, actions and the pager. */
91
+ const Bar = listPart("Bar", ({ children }, state) => (_jsx(List.Bar, { children: resolveParts(children ?? [
92
+ _jsx(Crumbs, {}, "crumbs"),
93
+ _jsx(Identity, {}, "identity"),
94
+ _jsx(Search, {}, "search"),
95
+ _jsx(Actions, {}, "actions"),
96
+ _jsx(Pager, {}, "pager"),
97
+ ], state) })));
98
+ /* ----------------------------------------------------------------------------------------- *
99
+ * The faces: the same set, read several ways
100
+ * ----------------------------------------------------------------------------------------- */
101
+ /** Columns: the fields shown, their totals at the foot, the row's actions, a grip to reorder. */
102
+ const Table = listPart("Table", (_props, { face }) => (_jsx(List.Table, { columns: face.columns, ...(face.totalled ? { totals: true } : {}), ...(face.rowActions === undefined ? {} : { rowActions: face.rowActions }), ...(face.reorder === undefined ? {} : { onReorder: face.reorder }) })), "table");
103
+ /** A name and its facts per row: what a narrow panel wants. */
104
+ const Rows = listPart("Rows", (_props, { face }) => _jsx(List.Rows, { row: face.rowOf }), "rows");
105
+ /** A spreadsheet of the page: every cell a value, the states in their tones. */
106
+ const Sheet = listPart("Sheet", (_props, { face, meta, locale }) => (_jsx(List.Sheet, {
107
+ // The set comes in pages (50 by default): the sheet grows with its page, as the table
108
+ // does, instead of web-ui's fixed box — which suits a sheet of thousands, not a page.
109
+ height: "auto", columns: face.sheetColumns, cell: (row, column) => {
110
+ const value = row[column.id];
111
+ return value === null || value === undefined
112
+ ? null
113
+ : (textOf(meta, column.id, value, locale) ?? String(value));
114
+ },
115
+ // The tones the application gave its choices (`presented(entity, { tones })`): the same
116
+ // colour a state has in the table, the record and its tag.
117
+ tone: (row, column) => {
118
+ const tones = face.tones?.[column.id];
119
+ const value = row[column.id];
120
+ return tones === undefined || value === null || value === undefined
186
121
  ? undefined
187
- : (row, to) => to === "page" ? openWithTrail(row) : api.open(refOf(row), openOptions(to)));
188
- const targets = choosing || openingIn === undefined
189
- ? []
190
- : (openTargets ?? (api === undefined ? [] : targetsFor(api)));
191
- const elsewhere = targets.filter((to) => to !== "page");
192
- const peeking = onPeek ??
193
- (targets.includes("peek") && openingIn !== undefined
194
- ? (row) => openingIn(row, "peek")
195
- : undefined);
196
- // The cells are memoised (below) and web-ui holds the row ways: both reach the opener through
197
- // a ref, so a screen's inline `onOpenIn` is always the current one without redrawing.
198
- const openingNow = useRef(openingIn);
199
- openingNow.current = openingIn;
200
- // The rows face (not the table) takes «Abrir en…» as its row's actions.
201
- const openInOf = (row) => elsewhere.length === 0 || openingIn === undefined ? null : (_jsx(OpenInMenu, { targets: elsewhere, name: idOf(row), onOpen: (to) => openingNow.current?.(row, to) }));
202
- // The column that names a row carries its link: the identity, or — in a list that does not
203
- // show it (a relation inside a record) — the record's name, else the first. The link is only
204
- // the name (`compact`): the places it opens in are the row's ways.
205
- const named = columns === undefined || columns.includes(identity)
206
- ? identity
207
- : meta?.display && columns.includes(meta.display)
208
- ? meta.display
209
- : (columns[0] ?? identity);
210
- const rowName = (row) => named === identity ? idOf(row) : (textOf(meta, named, row[named], locale) ?? idOf(row));
211
- const linked = choosing || cells?.[named] !== undefined || api === undefined
212
- ? cells
213
- : {
214
- ...cells,
215
- [named]: (row) => (_jsx(RecordLink, { api: api, resource: refOf(row), compact: true, children: rowName(row) })),
216
- };
217
- // `useCrumbs`, not `api.crumbs()`: the crumb must follow a name given later (`setTitle`).
218
- // Without a workspace it reads a trail that never changes — a hook cannot be skipped.
219
- const following = useCrumbs(api ?? NO_WORKSPACE);
220
- usePhone(api ?? NO_WORKSPACE); // where a row opens follows the mode (`targetsFor`)
221
- // The workspace's crumbs say each view's whole name (its tab's); in the trail a crumb says only
222
- // what is its own — «Pagos», not «Pagos · María Fernanda» under «María Fernanda».
223
- const trail = crumbs ?? (embedded || api === undefined ? undefined : ownCrumbs(following));
224
- // The workspace hears what is chosen: the keys, or — «all that match» — the ones on screen.
225
- const published = (chosen.selection.by === "keys"
226
- ? chosen.selection.keys
227
- : listing.rows.map((row) => idOf(row)).filter((id) => chosen.isChosen(id))).join("\u0000");
228
- const heard = useRef(published);
229
- useEffect(() => {
230
- if (!ticking || heard.current === published)
231
- return;
232
- heard.current = published;
233
- api?.select(published === "" ? [] : published.split("\u0000").map((id) => listing.entity.ref(id)));
234
- }, [api, ticking, published, listing.entity]);
235
- // «Elegir los 187 que coinciden» only past the page, and only with the server's exact count.
236
- const total = listing.count?.exact === true ? listing.count.value : null;
237
- // What is done to the chosen rows: answering whoever asked for several, else the screen's.
238
- const onChosen = choosingMany
239
- ? [
240
- {
241
- id: "add-chosen",
242
- label: frameworkCopy().picker.addChosen(chosen.size),
243
- primary: true,
244
- onSelect: () => api?.answer(chosen.selection),
245
- },
246
- ]
247
- : typeof selectionActions === "function"
248
- ? selectionActions(chosen)
249
- : selectionActions;
250
- // As data they go to the dock, where the view style draws them; by hand, in the bar.
251
- const forChosen = actionsOf(onChosen);
252
- const selection = ticking
253
- ? {
254
- has: chosen.isChosen,
255
- toggle: chosen.toggle,
256
- count: chosen.size ?? 0,
257
- clear: chosen.clear,
258
- ...(total !== null && total > listing.rows.length
259
- ? { matching: { total, onSelect: chosen.chooseAllMatching } }
260
- : {}),
261
- ...(forChosen.drawn === null ? {} : { actions: forChosen.drawn }),
262
- }
263
- : undefined;
264
- // Rebuilt only when what it reads changes: a new set of columns on every render redraws
265
- // every cell of the table. `columns` is compared by content (it is usually a literal);
266
- // `cells` by identity, since its functions may close over state — memoise it to keep this.
267
- const names = columns?.join("\u0000");
268
- const shown = useMemo(() => columnsOf(meta, columns, {
269
- locale,
270
- ...(linked ? { cells: linked } : {}),
271
- ...(look.tones === undefined ? {} : { tones: look.tones }),
272
- ...(yesNo === undefined ? {} : { yesNo }),
273
- }),
274
- // eslint-disable-next-line react-hooks/exhaustive-deps
275
- [
276
- meta,
277
- locale,
278
- names,
279
- cells,
280
- yesNo,
281
- api,
282
- elsewhere.join(),
283
- openingIn === undefined,
284
- dateStyle,
285
- // The tags' colours: a new map of tones redraws the table's cells, as it does the sheet's.
286
- look.tones,
287
- ]);
288
- // A state the application gave colours to is the row's tag — and not again among its facts.
289
- const stateField = summaryFieldsOf(meta, {}, listing.entity.summary).state;
290
- const stateTones = stateField === undefined ? undefined : look.tones?.[stateField];
291
- // A row and a card read the record by its display and the first fields shown beside it.
292
- const display = meta?.display || identity;
293
- const facts = shown
294
- .filter((c) => c.id !== display &&
295
- c.id !== identity &&
296
- !c.defaultHidden &&
297
- (stateTones === undefined || c.id !== stateField))
298
- .slice(0, 3);
299
- // The pivot asks the server only while it is the face on screen.
300
- const axes = pivotAsked === undefined || present !== "pivot" || !views.includes("pivot")
301
- ? null
302
- : {
303
- rows: typeof pivotAsked.rows === "string"
304
- ? { field: pivotAsked.rows }
305
- : pivotAsked.rows,
306
- columns: typeof pivotAsked.columns === "string"
307
- ? { field: pivotAsked.columns }
308
- : pivotAsked.columns,
309
- ...(pivotAsked.measures === undefined ? {} : { measures: pivotAsked.measures }),
310
- };
311
- const crossing = usePivot(listing, axes);
312
- const folded = axes === null
313
- ? null
314
- : pivotOf({
315
- crossed: crossing.crossed,
316
- byColumn: crossing.columns,
317
- rows: axes.rows,
318
- columns: axes.columns,
319
- measures: crossing.measures,
122
+ : (tones[String(value)] ?? "neutral");
123
+ } })), "sheet");
124
+ /** The set crossed by two fields, counted by the server (`EntityList pivot`). */
125
+ const Pivot = listPart("Pivot", (_props, { face, meta, locale }) => (_jsx(List.Pivot, { folded: face.folded ??
126
+ pivotOf({
127
+ crossed: [],
128
+ byColumn: [],
129
+ rows: { field: "" },
130
+ columns: { field: "" },
131
+ measures: [],
320
132
  meta,
321
133
  locale,
322
- });
323
- // The sheet's columns are the table's on screen: one set of fields, two ways to read them.
324
- const sheetColumns = useMemo(() => shown
325
- .filter((column) => !column.defaultHidden)
326
- .map((column) => ({
327
- id: column.id,
328
- header: String(column.header),
329
- ...(column.numeric === true ? { numeric: true } : {}),
330
- // A state in a toned tag, as the table draws it — not a painted cell, which is how
331
- // a sheet marks a value out of range.
332
- ...(look.tones?.[column.id] === undefined
333
- ? {}
334
- : { look: "tag" }),
335
- })), [shown, look.tones]);
336
- const sortStates = useMemo(() => listing.order.map((one) => ({
337
- key: one.field,
338
- direction: one.descending ? "desc" : "asc",
339
- })), [listing.order]);
340
- const sortable = useMemo(() => shown
341
- .filter((column) => column.sortable === true && column.field !== undefined)
342
- .map((column) => ({ field: column.field, label: String(column.header) })), [shown]);
343
- const rowOf = (row) => over({
344
- name: textOf(meta, display, row[display], locale) ?? idOf(row),
345
- meta: [
346
- // The key only where the list reads it: one that leaves it out (a relation inside a
347
- // record) does not print a UUID on every row.
348
- ...(display === identity || named !== identity ? [] : [idOf(row)]),
349
- ...facts.map((c) => textOf(meta, c.id, row[c.id], locale)).filter(Boolean),
350
- ].join(" · "),
351
- ...(elsewhere.length === 0 ? {} : { actions: openInOf(row) }),
352
- ...(stateTones === undefined || stateField === undefined
353
- ? {}
354
- : (() => {
355
- const word = textOf(meta, stateField, row[stateField], locale);
356
- return word === null
357
- ? {}
358
- : {
359
- tags: (_jsx(Tag, { tone: stateTones[String(row[stateField])] ?? "neutral", children: word })),
360
- };
361
- })()),
362
- }, look.row?.(row));
363
- // Without a look of its own, the board's card — and the tile — is web-ui's, made of the row.
364
- // With one, the same card — the row's name, meta and tags — under it, and pressing it is still
365
- // the list's: web-ui (1.5) hands an own card the list's press rule (`ways.press`: open, or
366
- // choose). A tile is the same card in its `tile` shape (web-ui 2.13), under its own look.
367
- const cardFrom = (own, layout) => own === undefined
368
- ? undefined
369
- : (row, ways) => {
370
- const d = rowOf(row);
371
- return (_jsx(RecordCard, { ...over({
372
- title: d.name,
373
- ...(d.tags === undefined ? {} : { tags: d.tags }),
374
- ...(d.meta === undefined ? {} : { meta: d.meta }),
375
- }, own(row)), ...(layout === undefined ? {} : { layout }), ...(ways.press === undefined ? {} : { onOpen: ways.press }) }));
376
- };
377
- const cardOfRow = cardFrom(look.card);
378
- const tileOfRow = cardFrom(look.tile, "tile");
379
- // Grouped: the SERVER counts the groups, a page of them at a time; a level below and a group's
380
- // rows are read when it is opened, so every group starts closed. Every face of web-ui (1.4)
381
- // draws these piles, the table included; each says what its next «more» brings (1.5), and a
382
- // level that holds fewer groups than it counts offers the next page of them (2.19).
383
- const groups = useGroups(listing, sums === undefined ? {} : { sums: sums || [] });
384
- const grouped = groups.levels.length > 0;
385
- // A table and a sheet say a group's sums under their columns (web-ui 2.9): beside the group's
386
- // name, only what no column shown adds up.
387
- const columnar = present === "table" || present === "sheet";
388
- const underColumns = new Set(columnar
389
- ? shown.flatMap((column) => column.aggregate === undefined || column.defaultHidden || column.field === undefined
390
- ? []
391
- : [column.field])
392
- : []);
393
- const { piles, bucketAt } = pilesOf(groups.buckets, groups.levels, meta, locale, groups.rowsOf, groups.brings, underColumns, groups);
394
- // What is open is the groups' (it survives a search); a group above another is drawn open
395
- // only once its level arrived — an empty open group would read as one with no rows.
396
- const collapsed = [...bucketAt].flatMap(([key, bucket]) => groups.isOpen(bucket) &&
397
- (groups.isDeepest(bucket) || groups.groupsOf(bucket) !== undefined)
398
- ? []
399
- : [key]);
400
- const toggle = (key) => {
401
- const bucket = bucketAt.get(key);
402
- if (bucket !== undefined)
403
- groups.toggle(bucket);
404
- };
405
- const failure = grouped && groups.status === "failed" ? explain(groups.error) : grouped ? null : failed;
406
- const morePiles = groups.bringsGroups(null);
407
- const grouping = grouped
408
- ? {
409
- group: {
410
- levels: groups.levels,
411
- of: (row, level) => String(row[level.field] ?? ""),
412
- piles,
413
- collapsed,
414
- onToggle: toggle,
415
- // «See N more» inside an open group: its next page, on the server's cursor (web-ui 1.4).
416
- onMore: (pile) => {
417
- const bucket = bucketAt.get(pathKey(pile.path));
418
- if (bucket !== undefined)
419
- groups.more(bucket);
420
- },
421
- // Odoo's pager on an open group's line — «51–100 / 2.328 ‹ ›» — turns its page in
422
- // place of its rows (web-ui's `onPage`); «See N more» stays under it.
423
- onPage: (pile, step) => {
424
- const bucket = bucketAt.get(pathKey(pile.path));
425
- if (bucket !== undefined)
426
- groups.page(bucket, step);
427
- },
428
- // «Ver N grupos más» after the last group of a level: the first level, or the one
429
- // under an open group — counted by the server (`groups_count`).
430
- ...(groups.groupsCount === null ? {} : { pilesCount: groups.groupsCount }),
431
- ...(morePiles === undefined ? {} : { morePiles }),
432
- onMorePiles: (parent) => {
433
- if (parent === null)
434
- return groups.moreGroups(null);
435
- const bucket = bucketAt.get(pathKey(parent.path));
436
- if (bucket !== undefined)
437
- groups.moreGroups(bucket);
438
- },
439
- },
440
- }
441
- : {};
442
- // How many groups and records the SERVER counted — never the sum of the groups in hand.
443
- const records = groups.records ?? groups.buckets.reduce((sum, bucket) => sum + bucket.count, 0);
444
- // The foot adds up what the model says adds up (`total`) — over the rows in hand, never a
445
- // grouped set (each group says its own, counted by the server). While the rows are not the
446
- // whole set the list says the figures are this page's (`pageTotals`): the server's total over
447
- // everything filtered waits on a reading of measures (sincpro_framework, PRD_09).
448
- const partial = listing.paging === "pages"
449
- ? page.hasPrev || page.hasNext
450
- : listing.hasMore || listing.capped;
451
- // Inside a record or a panel a list is its rows: a page's sum there reads as THE total, so an
452
- // embedded list adds up only when every row is in hand.
453
- const totalled = !grouped &&
454
- !(embedded && partial) &&
455
- listing.rows.length > 0 &&
456
- shown.some((column) => column.aggregate !== undefined && !column.defaultHidden);
457
- // A row's actions: the entity's Commands, then whatever else the screen offers.
458
- // The trash (`trashOf`): each row is brought back from where it is, with the entity's own
459
- // `restore` — it can be undone, so it runs on the click; the list reads again after.
460
- const restoring = listing.definition?.trash === true && listing.entity.restore !== undefined;
461
- const offered = useMemo(() => restoring
462
- ? [
463
- ...commands,
464
- {
465
- command: RESTORE,
466
- label: frameworkCopy().record.restore,
467
- icon: RotateCcw,
468
- },
469
- ]
470
- : commands, [restoring, commands]);
471
- const rowCommands = useRowCommands(listing, offered);
472
- const rowActions = offered.length === 0 && askedRowActions === undefined
473
- ? undefined
474
- : (row) => [...rowCommands.rowActions(row), ...(askedRowActions?.(row) ?? [])];
475
- // Rows put in their order by hand: only on the whole order, never grouped or while choosing.
476
- const reorder = useReorder(listing, idOf, !grouped && !choosing);
477
- const refusal = rowCommands.error ?? reorder.error;
478
- const refused = refusal === null ? null : explain(refusal);
479
- // The way through the set. Inside a record or a panel, only while there is more than what is
480
- // on screen: «1–5 / 5» repeats the rows.
481
- const fits = embedded && !partial;
482
- const through = grouped ? (groups.status !== "ready" ? null : (_jsx("span", { className: "text-meta text-muted", children: copy.groups(groups.groupsCount ?? groups.buckets.length, records) }))) : fits ? null : listing.paging === "pages" ? (page.from === 0 ? null : (_jsx(Pagination, { from: page.from, to: page.to, total: count?.value ?? page.to,
483
- /* The range is touched to choose how many rows fit — in both styles. Without these two
484
- the modern pill and the plain range drew a size nobody could change. */
485
- pageSize: page.size, onPageSizeChange: listing.resize, ...(page.hasPrev ? { onPrev: listing.prev } : {}), ...(page.hasNext ? { onNext: listing.next } : {}) }))) : (_jsxs(_Fragment, { children: [count === null ? null : (_jsx("span", { className: "text-meta text-muted", children: copy.shown(listing.rows.length, count.value, count.exact) })), listing.capped ? (_jsx("span", { className: "text-meta text-muted", children: copy.capped(listing.rows.length) })) : listing.hasMore ? (_jsx(Button, { size: "sm", role: "secondary", disabled: listing.status === "loadingMore", onClick: listing.more, children: listing.status === "loadingMore" ? copy.loading : copy.loadMore })) : null] }));
486
- const pageTotals = totalled && partial && present === "table";
487
- // Written only when it says something: web-ui draws an embedded list's bar for any slot
488
- // written, an empty one too. Its parts side by side and apart: the slot does not space them,
489
- // and «Totales de esta página» read glued to «1–50 / 112».
490
- const paging = !pageTotals && dropped.length === 0 && through === null ? null : (_jsxs("span", { className: "flex items-center gap-3", children: [pageTotals ? _jsx("span", { className: "text-meta text-muted", children: copy.pageTotals }) : null, dropped.length === 0 ? null : (_jsx(Tag, { tone: "warn", title: copy.dropped(dropped), children: copy.dropped(dropped) })), through] }));
491
- return (_jsxs(List, { items: reorder.rows, keyOf: idOf, embedded: embedded, present: present, onPresentChange: setPresent, ...grouping,
492
- // The whole order, as web-ui 1.7 shows and sets it: ⇧-click on a header adds a field,
493
- // the sort menu reorders them — the same list the criteria builder writes.
494
- // While the rows can be dragged the order shown IS their position: the table gets no column
495
- // sort (web-ui turns its grip off under one), the list keeps its order as it is.
496
- sort: reorder.move === undefined ? sortStates : [], ...(embedded ? {} : { sortable }), onSortChange: (next) => listing.orderBy(next.map((one) => ({ field: one.key, descending: one.direction === "desc" }))), state: {
497
- // Only the FIRST reading shows the skeleton. Refining (typing, a filter, the next
498
- // page) keeps the rows on screen until the server answers: web-ui swaps the body for
499
- // a skeleton while `loading`, and a row that vanished under the pointer lost a click.
500
- // Meanwhile `refreshing` (web-ui 1.4) draws a quiet bar over the rows: asking, not gone.
501
- loading: grouped
502
- ? groups.status === "loading" && groups.buckets.length === 0
503
- : listing.status === "loading" && listing.rows.length === 0,
504
- refreshing: grouped
505
- ? groups.status === "loading" && groups.buckets.length > 0
506
- : listing.status === "loading" && listing.rows.length > 0,
507
- error: failure === null
508
- ? null
509
- : [failure.heading, failure.sentence].filter(Boolean).join(". "),
510
- }, filtered: listing.selection !== undefined || listing.text !== "", empty: emptyOf(empty, copy), ...(selection === undefined ? {} : { selection }), ...(opening === undefined ? {} : { onOpen: opening }), ...(peeking === undefined ? {} : { onPeek: peeking, rowClick: "open" }), ...(elsewhere.length === 0 || openingIn === undefined
511
- ? {}
512
- : {
513
- openTargets: elsewhere,
514
- onOpenIn: (row, to) => openingNow.current?.(row, to),
515
- }), children: [_jsxs(List.Bar, { children: [trail === undefined || trail.length === 0 ? null : _jsx(List.Crumbs, { crumbs: trail }), embedded ? null : (_jsx(List.Identity, { title: title ?? nameOf(meta, locale), ...(description === undefined ? {} : { description }) })), search === "bar" ? (_jsx(List.Search, { children: _jsx(EntitySearchBar, { listing: listing, filters: filters, groups: groups, ...(groupable === undefined ? {} : { groupable }), ...(facets === undefined ? {} : { facets }), ...(asked === "" ? {} : { seed: asked }), ...(recent === undefined ? {} : { recentKey: recent }) }) })) : null, search === "box" && meta?.search != null ? (_jsx(List.Search, { children: _jsx(SearchField, { value: draft, onChange: (next) => {
516
- setDraft(next);
517
- // Emptied (or cleared with ×): the whole set again, at once.
518
- if (next === "" && listing.text !== "")
519
- listing.search("", { now: true });
520
- }, onSubmit: (text) => listing.search(text, { now: true }), placeholder: copy.search, "aria-label": copy.search }) })) : null, started.drawn === null ? null : (_jsxs(List.Actions, { children: [started.drawn, creating === undefined ? null : (_jsx(Button, { role: "primary", size: "sm", onClick: creating, children: frameworkCopy().record.create }))] })), startDock.length === 0 && forChosen.dock.length === 0 ? null : (_jsx(List.Dock, { ...(startDock.length === 0 ? {} : { actions: startDock }), ...(forChosen.dock.length === 0 ? {} : { selection: forChosen.dock }) })), paging === null ? null : _jsx(List.Pagination, { children: paging })] }), views.includes("table") ? (_jsx(List.Table, { columns: shown, ...(totalled ? { totals: true } : {}), ...(rowActions === undefined ? {} : { rowActions }), ...(reorder.move === undefined ? {} : { onReorder: reorder.move }) })) : null, _jsx(List.Rows, { row: rowOf }), views.includes("sheet") ? (_jsx(List.Sheet, {
521
- // The set comes in pages (50 by default): the sheet grows with its page, as the table
522
- // does, instead of web-ui's fixed box — which suits a sheet of thousands, not a page.
523
- height: "auto", columns: sheetColumns, cell: (row, column) => {
524
- const value = row[column.id];
525
- return value === null || value === undefined
526
- ? null
527
- : (textOf(meta, column.id, value, locale) ?? String(value));
528
- },
529
- // The tones the application gave its choices (`presented(entity, { tones })`): the
530
- // same colour a state has in the table, the record and its tag.
531
- tone: (row, column) => {
532
- const tones = look.tones?.[column.id];
533
- const value = row[column.id];
534
- return tones === undefined || value === null || value === undefined
535
- ? undefined
536
- : (tones[String(value)] ?? "neutral");
537
- } })) : null, views.includes("pivot") ? (_jsx(List.Pivot, { folded: folded ??
538
- pivotOf({
539
- crossed: [],
540
- byColumn: [],
541
- rows: { field: "" },
542
- columns: { field: "" },
543
- measures: [],
544
- meta,
545
- locale,
546
- }) })) : null, views.includes("board") ? (_jsx(List.Board, { ...(cardOfRow === undefined ? {} : { card: cardOfRow }) })) : null, views.includes("tiles") ? (_jsx(List.Tiles, { ...(tileOfRow === undefined ? {} : { card: tileOfRow }) })) : null, (faces ?? []).map((face) => (_jsx(List.Present, { id: face.id, label: face.label, icon: face.icon, ...(face.wide === undefined ? {} : { wide: face.wide }), children: face.render }, face.id))), reorder.elsewhere ? (_jsx(Callout, { tone: "warn", onDismiss: reorder.dismiss, children: copy.reorderElsewhere })) : null, refused === null ? null : (_jsx(Callout, { tone: "bad", title: refused.heading, children: refused.sentence }))] }));
134
+ }) })), "pivot");
135
+ /** Cards: the entity's own (`presented card`), else web-ui's made of the row. */
136
+ const Board = listPart("Board", (_props, { face }) => (_jsx(List.Board, { ...(face.cardOfRow === undefined ? {} : { card: face.cardOfRow }) })), "board");
137
+ /** A grid of pictures — a till's products, a shop's items. */
138
+ const Tiles = listPart("Tiles", (_props, { face }) => (_jsx(List.Tiles, { ...(face.tileOfRow === undefined ? {} : { card: face.tileOfRow }) })), "tiles");
139
+ /** The faces the screen gave as data (`faces`), each one web-ui's `List.Present`. */
140
+ const Faces = listPart("Faces", (_props, { face }) => face.faces.map((one) => (_jsx(List.Present, { id: one.id, label: one.label, icon: one.icon, ...(one.wide === undefined ? {} : { wide: one.wide }), children: one.render }, one.id))));
141
+ /** What happened to a gesture on the list: rows moved by someone else, a command refused. */
142
+ const Notices = listPart("Notices", (_props, { notices }) => [
143
+ notices.movedElsewhere ? (_jsx(Callout, { tone: "warn", onDismiss: notices.dismissMoved, children: frameworkCopy().list.reorderElsewhere }, "moved")) : null,
144
+ notices.refused === null ? null : (_jsx(Callout, { tone: "bad", title: notices.refused.heading, children: notices.refused.sentence }, "refused")),
145
+ ]);
146
+ /** The built-in faces in the order the switcher offers them. */
147
+ const FACES = [
148
+ ["table", Table],
149
+ ["rows", Rows],
150
+ ["sheet", Sheet],
151
+ ["pivot", Pivot],
152
+ ["board", Board],
153
+ ["tiles", Tiles],
154
+ ];
155
+ /* ----------------------------------------------------------------------------------------- *
156
+ * The root
157
+ * ----------------------------------------------------------------------------------------- */
158
+ function EntityListRoot(props) {
159
+ const { children, ...asked } = props;
160
+ const written = children === undefined ? undefined : kindsIn(children);
161
+ const state = useEntityList(asked, written === undefined || written.length === 0 ? undefined : written);
162
+ // A rows face is always there, asked for or not: web-ui draws a NARROW list — a window, a side
163
+ // panel, a phone — as rows whatever was asked, and a list with no rows face draws nothing there.
164
+ const tree = children ?? (_jsxs(_Fragment, { children: [_jsx(Bar, {}), FACES.flatMap(([view, Face]) => view === "rows" || state.views.includes(view) ? [_jsx(Face, {}, view)] : []), _jsx(Faces, {}), _jsx(Notices, {})] }));
165
+ const rows = written === undefined || written.includes("rows") ? null : _jsx(Rows, {}, "rows");
166
+ return (_jsx(ListContext.Provider, { value: state, children: _jsx(List, { ...state.root, children: resolveParts(_jsxs(_Fragment, { children: [tree, rows] }), state) }) }));
547
167
  }
168
+ /**
169
+ * A list by parts: the framework's default, or any part of it replaced. See the top of this file.
170
+ */
171
+ export const EntityList = Object.assign(EntityListRoot, {
172
+ Bar,
173
+ Crumbs,
174
+ Identity,
175
+ Search,
176
+ Actions,
177
+ Pager,
178
+ Table,
179
+ Rows,
180
+ Sheet,
181
+ Pivot,
182
+ Board,
183
+ Tiles,
184
+ Faces,
185
+ Notices,
186
+ });