@sincpro/web-framework 0.2.0 → 0.4.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.
- package/README.md +56 -19
- package/dist/core/copy.d.ts +2 -0
- package/dist/core/copy.js +2 -0
- package/dist/core/edit.d.ts +35 -3
- package/dist/core/edit.js +49 -5
- package/dist/core/entity.d.ts +14 -2
- package/dist/core/entity.js +33 -1
- package/dist/core/index.d.ts +4 -2
- package/dist/core/index.js +4 -2
- package/dist/core/lines.d.ts +52 -0
- package/dist/core/lines.js +120 -0
- package/dist/core/list.d.ts +1 -1
- package/dist/core/lookup.d.ts +2 -0
- package/dist/core/lookup.js +3 -1
- package/dist/core/present.js +7 -2
- package/dist/core/rest.d.ts +5 -9
- package/dist/core/rest.js +14 -6
- package/dist/core/transport.d.ts +9 -1
- package/dist/core/zone.d.ts +27 -0
- package/dist/core/zone.js +75 -0
- package/dist/react/context.d.ts +13 -1
- package/dist/react/context.js +10 -2
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +1 -0
- package/dist/react/useGroups.d.ts +13 -1
- package/dist/react/useGroups.js +48 -7
- package/dist/react/useListing.d.ts +8 -4
- package/dist/react/useListing.js +11 -5
- package/dist/react/usePivot.d.ts +33 -0
- package/dist/react/usePivot.js +59 -0
- package/dist/react/useRecord.d.ts +13 -9
- package/dist/react/useRecord.js +55 -13
- package/dist/web/index.d.ts +7 -1
- package/dist/web/index.js +7 -1
- package/dist/web/list/EntityList.d.ts +46 -7
- package/dist/web/list/EntityList.js +132 -21
- package/dist/web/list/EntitySearchBar.js +19 -1
- package/dist/web/list/columns.d.ts +0 -13
- package/dist/web/list/columns.js +20 -1
- package/dist/web/list/groups.d.ts +3 -1
- package/dist/web/list/groups.js +8 -1
- package/dist/web/list/openIn.d.ts +41 -0
- package/dist/web/list/openIn.js +55 -0
- package/dist/web/list/pivot.d.ts +23 -0
- package/dist/web/list/pivot.js +64 -0
- package/dist/web/list/presentation.d.ts +51 -0
- package/dist/web/list/presentation.js +20 -0
- package/dist/web/record/EntityPicker.d.ts +3 -1
- package/dist/web/record/EntityPicker.js +15 -5
- package/dist/web/record/EntityTags.js +16 -5
- package/dist/web/record/RelatedList.d.ts +6 -0
- package/dist/web/record/RelatedList.js +26 -2
- package/dist/web/record/control.d.ts +43 -0
- package/dist/web/record/control.js +67 -0
- package/dist/web/record/edit.d.ts +16 -8
- package/dist/web/record/edit.js +57 -49
- package/dist/web/record/lines.d.ts +84 -0
- package/dist/web/record/lines.js +190 -0
- package/dist/web/record/opening.d.ts +8 -0
- package/dist/web/record/summary.js +14 -6
- package/dist/web/workspace/SincproApp.d.ts +4 -1
- package/dist/web/workspace/SincproApp.js +2 -2
- package/dist/web/workspace/composer.d.ts +2 -1
- package/dist/web/workspace/composer.js +27 -16
- package/dist/web/workspace/open.d.ts +2 -14
- package/dist/web/workspace/open.js +2 -13
- package/dist/web/workspace/record.js +6 -3
- package/dist/web/workspace/workspace.d.ts +4 -2
- package/dist/web/workspace/workspace.js +10 -4
- package/package.json +9 -9
package/dist/web/record/edit.js
CHANGED
|
@@ -2,15 +2,42 @@
|
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
3
|
import { choicesOf } from "@sincpro/criteria";
|
|
4
4
|
import { frameworkCopy } from "../../core/copy.js";
|
|
5
|
+
import { errorAt } from "../../core/edit.js";
|
|
6
|
+
import { editableLines } from "../../core/lines.js";
|
|
5
7
|
import { textOf, viewOf } from "../../core/present.js";
|
|
6
8
|
import { relationFieldOf } from "../../core/relation.js";
|
|
7
9
|
import { useEntityFor, useFramework } from "../../react/context.js";
|
|
8
|
-
import {
|
|
10
|
+
import { controlFor, RelationPicker, } from "./control.js";
|
|
11
|
+
import { RecordLinesEditor } from "./lines.js";
|
|
9
12
|
import { useRecordOpening } from "./opening.js";
|
|
10
13
|
import { RecordLines } from "./record.js";
|
|
11
14
|
import { RelatedList } from "./RelatedList.js";
|
|
12
|
-
import { Checkbox,
|
|
13
|
-
|
|
15
|
+
import { Checkbox, Field, FormSection, ReadValue, RelationValue, } from "@sincpro/web-ui";
|
|
16
|
+
/**
|
|
17
|
+
* One field of a record being edited, from the model: its word, whether it is required or
|
|
18
|
+
* read-only now (the hints, evaluated over what is on screen), the server's error for it, and a
|
|
19
|
+
* control chosen by its type.
|
|
20
|
+
*
|
|
21
|
+
* Context: forge's forms wrote `<Field label="Nombre" required><TextInput … /></Field>` once per
|
|
22
|
+
* field, with the label, the «required» and the value plumbing by hand, while the server already
|
|
23
|
+
* said all three. Here a field is a NAME. The control by type is a default, never the only way:
|
|
24
|
+
* `as` draws any control — a rich text, a domain widget — and still receives the value, the
|
|
25
|
+
* setter and the state.
|
|
26
|
+
*
|
|
27
|
+
* A many2one needs no `as`: the model says what it points at (`relation: "Cliente"`, the key
|
|
28
|
+
* `customer_id` it is written by), the application declared that entity
|
|
29
|
+
* (`SincproApp entities`), so the field is an `EntityPicker` of it — the server's literal
|
|
30
|
+
* search, the ten first, «Buscar más…» — and, read-only, the record's name as a link. Where it
|
|
31
|
+
* opens is the view's (`RecordOpening`: a modal in a workspace, nothing outside one).
|
|
32
|
+
*
|
|
33
|
+
* A one2many is a block: its lines (`lines`) or its related list (`list`), each with every piece
|
|
34
|
+
* replaceable the screen's way.
|
|
35
|
+
*
|
|
36
|
+
* <RecordField record={invoice} name="due_date" />
|
|
37
|
+
* <RecordField record={invoice} name="customer_id" />
|
|
38
|
+
* <RecordField record={invoice} name="lines" lines={{ controls: { qty: Stepper } }} />
|
|
39
|
+
*/
|
|
40
|
+
export function RecordField({ record, name, as, bare = false, lines: look, list: listed, }) {
|
|
14
41
|
const { meta, locale, edit } = record;
|
|
15
42
|
const view = viewOf(meta, name, locale);
|
|
16
43
|
const state = edit.state(name);
|
|
@@ -20,28 +47,43 @@ export function RecordField({ record, name, as, bare = false, }) {
|
|
|
20
47
|
const { metas } = useFramework();
|
|
21
48
|
if (!state.visible)
|
|
22
49
|
return null;
|
|
50
|
+
// A one2many is a BLOCK, named by a section — never a `Field`: web-ui's controls read the
|
|
51
|
+
// id and the label of the `Field` around them, and every cell and the list's search bar
|
|
52
|
+
// took the same id and the relation's name.
|
|
23
53
|
// A one2many of a declared entity is that entity's own list, filtered to this record by
|
|
24
54
|
// the server — never the page the record happened to bring — and a row opens its record.
|
|
25
|
-
// A one2many of no declared entity (the lines of an invoice):
|
|
26
|
-
//
|
|
55
|
+
// A one2many of no declared entity (the lines of an invoice): edited inside the record as a
|
|
56
|
+
// whole list (`RecordLinesEditor`) — or, when the record brought only a page, that page read
|
|
57
|
+
// only with its real total.
|
|
27
58
|
if (as === undefined && relation?.field.type === "one2many" && target === undefined) {
|
|
28
|
-
|
|
29
|
-
|
|
59
|
+
// Editable when the record holds ALL its lines; a page of them stays read only.
|
|
60
|
+
const whole = editableLines(edit.values[name]);
|
|
61
|
+
const lines = whole === null ? (_jsx(RecordLines, { record: record.record ?? {}, meta: meta, name: name, locale: locale })) : (_jsx(RecordLinesEditor, { record: record, name: name, lines: whole, ...(look === undefined ? {} : { look }) }));
|
|
62
|
+
return bare ? lines : _jsx(FormSection, { title: view.label, children: lines });
|
|
30
63
|
}
|
|
31
64
|
if (as === undefined && relation?.field.type === "one2many" && target !== undefined) {
|
|
32
65
|
const list = (_jsx(RelatedList, { record: record, name: name, entity: target, ...(opening === null
|
|
33
66
|
? {}
|
|
34
|
-
: {
|
|
35
|
-
|
|
67
|
+
: (() => {
|
|
68
|
+
const refOf = (row) => {
|
|
36
69
|
const known = metas.get(target.key);
|
|
37
70
|
const key = known?.get_id || known?.identity || "id";
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
71
|
+
return target.ref(String(row[key]));
|
|
72
|
+
};
|
|
73
|
+
// A press opens where the view says first; «Abrir en…» offers every other place.
|
|
74
|
+
return {
|
|
75
|
+
onOpen: (row) => opening.open(refOf(row), opening.targets[0] ?? "page"),
|
|
76
|
+
onOpenIn: (row, to) => opening.open(refOf(row), to),
|
|
77
|
+
openTargets: opening.targets,
|
|
78
|
+
// Not on a page, a list inside the form starts as rows; the table stays a click
|
|
79
|
+
// away.
|
|
80
|
+
...(opening.narrow === true ? { views: ["rows", "table"] } : {}),
|
|
81
|
+
};
|
|
82
|
+
})()), ...listed }));
|
|
83
|
+
return bare ? list : _jsx(FormSection, { title: view.label, children: list });
|
|
42
84
|
}
|
|
43
85
|
const value = edit.values[name];
|
|
44
|
-
const error = edit.errors
|
|
86
|
+
const error = errorAt(edit.errors, name);
|
|
45
87
|
const control = {
|
|
46
88
|
value,
|
|
47
89
|
set: (next) => edit.set(name, next),
|
|
@@ -64,46 +106,12 @@ export function RecordField({ record, name, as, bare = false, }) {
|
|
|
64
106
|
onOpenIn: (to) => opening.open(target.ref(id), to),
|
|
65
107
|
openTargets: opening.targets,
|
|
66
108
|
}) }));
|
|
67
|
-
|
|
68
|
-
return (_jsx(EntityPicker, { entity: target, value: id, onChange: answer,
|
|
69
|
-
// «Buscar más…» is an option of the picker already: no second button beside it.
|
|
70
|
-
browse: false, "aria-label": view.label, ...(opening === null
|
|
71
|
-
? {}
|
|
72
|
-
: {
|
|
73
|
-
onOpen: (chosen, to) => opening.open(target.ref(chosen), to),
|
|
74
|
-
openTargets: opening.targets,
|
|
75
|
-
...(opening.choose === undefined
|
|
76
|
-
? {}
|
|
77
|
-
: {
|
|
78
|
-
onBrowseAll: (query) => opening.choose?.(target, answer, query),
|
|
79
|
-
}),
|
|
80
|
-
}) }));
|
|
109
|
+
return (_jsx(RelationPicker, { target: target, value: id, onChange: (next) => edit.set(relation.key, next), label: view.label }));
|
|
81
110
|
}
|
|
82
111
|
if (view.type === "boolean" && as === undefined)
|
|
83
112
|
return (_jsx(Checkbox, { checked: value === true, onChange: control.set, label: view.label, disabled: state.readonly }));
|
|
84
113
|
return (_jsx(Field, { label: view.label, required: state.required && !state.readonly, ...(view.help === undefined ? {} : { hint: view.help }), ...(error === undefined ? {} : { error }), children: as !== undefined ? (as(control)) : relation !== undefined && !relation.many && target !== undefined ? (manyToOne()) : state.readonly ? (_jsx(ReadValue, { value: textOf(meta, name, value, locale) ?? "—" })) : (controlFor(control, view.type, field?.exact === true, choicesOf(meta, name, locale))) }));
|
|
85
114
|
}
|
|
86
|
-
function controlFor(control, type, exact, choices) {
|
|
87
|
-
const { value, set } = control;
|
|
88
|
-
if (choices.length > 0)
|
|
89
|
-
return (_jsx(Select, { value: value === null || value === undefined ? "" : String(value), options: choices.map((one) => ({ value: String(one.value), label: one.label })), onChange: set }));
|
|
90
|
-
switch (type) {
|
|
91
|
-
case "integer":
|
|
92
|
-
case "number":
|
|
93
|
-
return (_jsx(NumberInput, { value: value === null || value === undefined || value === "" ? "" : Number(value),
|
|
94
|
-
// A decimal travels as text so no digit is lost; the server reads it that way back.
|
|
95
|
-
onChange: (next) => set(next === "" ? null : exact ? String(next) : next) }));
|
|
96
|
-
case "date":
|
|
97
|
-
return (_jsx(DatePicker, { value: typeof value === "string" && value !== "" ? value : null, onChange: set }));
|
|
98
|
-
case "text[]": {
|
|
99
|
-
// A list of words is chips: each word is its own key, and typing a new one adds it.
|
|
100
|
-
const words = Array.isArray(value) ? value.map(String) : [];
|
|
101
|
-
return (_jsx(TagsField, { value: words, options: words.map((word) => ({ value: word, label: word })), onChange: set, onCreate: (word) => word }));
|
|
102
|
-
}
|
|
103
|
-
default:
|
|
104
|
-
return (_jsx(TextInput, { value: value === null || value === undefined ? "" : String(value), onChange: set }));
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
115
|
/**
|
|
108
116
|
* The record's «save / discard», for `<Record dirty={…}>`: present only while something changed,
|
|
109
117
|
* off while saving, and off with the reason while a required field is empty.
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type AnyRecord } from "@sincpro/criteria";
|
|
2
|
+
import { type LineAdding } from "../../core/lines.js";
|
|
3
|
+
import type { RecordReading } from "../../react/useRecord.js";
|
|
4
|
+
import { type FieldControl } from "./control.js";
|
|
5
|
+
import { type LineEditorState } from "@sincpro/web-ui";
|
|
6
|
+
import { type ReactNode } from "react";
|
|
7
|
+
/** What a control of the screen's own receives for a field of a line: the field's value and
|
|
8
|
+
* how to change it, as any field control, plus the whole line — a widget may write several
|
|
9
|
+
* fields at once (a product and its price). */
|
|
10
|
+
export type LineControl = FieldControl & {
|
|
11
|
+
line: AnyRecord;
|
|
12
|
+
setLine: (next: AnyRecord) => void;
|
|
13
|
+
};
|
|
14
|
+
/** A column that is not a field of the line — its delivery, its buttons: read only. */
|
|
15
|
+
export type LineColumn = {
|
|
16
|
+
/** Must not be the name of a field of the line. */
|
|
17
|
+
id: string;
|
|
18
|
+
header: string;
|
|
19
|
+
cell: (line: AnyRecord) => ReactNode;
|
|
20
|
+
/** What the lines' search reads in it. */
|
|
21
|
+
text?: (line: AnyRecord) => string;
|
|
22
|
+
numeric?: boolean;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* How the lines look and are written, when the screen wants it its way — every piece optional,
|
|
26
|
+
* every one falling back to the model's.
|
|
27
|
+
*
|
|
28
|
+
* Context: «defaults, never cages». The model decides the columns and their controls; a screen
|
|
29
|
+
* can choose and order the columns, draw a cell its way, write a field with a widget of its
|
|
30
|
+
* own (in the cell and in the dialog), add columns that are not fields (a line's picking with a
|
|
31
|
+
* button that opens it), or write the whole dialog. What it does not get to do is break where
|
|
32
|
+
* a line is written: a field with its own control fits a cell, and a required field left out of
|
|
33
|
+
* the columns sends the line to the dialog (`addingOf`).
|
|
34
|
+
*/
|
|
35
|
+
export type LinesLook = {
|
|
36
|
+
/** Where a line is written: the table when it can be (the default), or the dialog. */
|
|
37
|
+
adding?: LineAdding;
|
|
38
|
+
/** The fields drawn as columns, in order. The dialog still holds every field. */
|
|
39
|
+
columns?: readonly string[];
|
|
40
|
+
/** A field's cell drawn the screen's way, while it reads. */
|
|
41
|
+
cells?: Readonly<Partial<Record<string, (line: AnyRecord) => ReactNode>>>;
|
|
42
|
+
/** A field written with the screen's own widget — in its cell and in the dialog. */
|
|
43
|
+
controls?: Readonly<Partial<Record<string, (control: LineControl) => ReactNode>>>;
|
|
44
|
+
/** Columns that are not fields, after the fields. */
|
|
45
|
+
extra?: readonly LineColumn[];
|
|
46
|
+
/** The whole line's dialog, the screen's way. */
|
|
47
|
+
editor?: (draft: AnyRecord, set: (next: AnyRecord) => void, state: LineEditorState) => ReactNode;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* The lines of an aggregate, edited inside its record: web-ui's `LineItems` over the record's
|
|
51
|
+
* edit — what the screen holds is the whole list, and every change writes it back with
|
|
52
|
+
* `edit.set(name, next)`, so it is dirty, previewed, saved and discarded like any field.
|
|
53
|
+
*
|
|
54
|
+
* Context: a one2many of no entity of its own (the lines of an invoice) used to be drawn read
|
|
55
|
+
* only (`RecordLines`). sincpro_framework writes such lines in the aggregate's own command, as
|
|
56
|
+
* the whole list in order; a new line goes without an id (`core/lines`). What `LineItems` does
|
|
57
|
+
* — add, edit, remove with «Deshacer», reorder — is the screen's: only the list that results
|
|
58
|
+
* travels. Each column is a field of the line model (`meta.fields[name].definition`, its
|
|
59
|
+
* scalars: `lineFields`), its control the one its type asks for — a many2one, the picker of its
|
|
60
|
+
* entity (`LineValue`) — its state the line model's hints over the line refined by the server's
|
|
61
|
+
* `items[i]` (`edit.cell`). A server error or warning that points into a cell
|
|
62
|
+
* (`#/values/lines/1/qty`, `["lines", 1, "price"]`) is said IN that cell (web-ui 1.6
|
|
63
|
+
* `cellState`); one about the line as a whole, on the line (`flag`).
|
|
64
|
+
*
|
|
65
|
+
* WHERE a line is written is one decision (`addingOf`): in the TABLE — «Agregar una línea» is
|
|
66
|
+
* its last row, a row is written where it is, the pencil opens the whole line in its dialog —
|
|
67
|
+
* whenever every column a person writes fits a cell; in the DIALOG otherwise, or when the
|
|
68
|
+
* screen asks (`look.adding="editor"`), and then the cells only read. The screen may draw it
|
|
69
|
+
* its way (`LinesLook`) without breaking that rule. A row that cannot be finished where
|
|
70
|
+
* it was opened is a trap, so «inline» is never forced. On a narrow box web-ui draws the lines
|
|
71
|
+
* as cards that open the dialog.
|
|
72
|
+
*
|
|
73
|
+
* Only a WHOLE list is editable: a record that brought one page of 1 000 lines draws them read
|
|
74
|
+
* only (`RecordLines`, «se muestran 50 de 1000»), because a page written back whole deletes
|
|
75
|
+
* every line it did not bring.
|
|
76
|
+
*/
|
|
77
|
+
export declare function RecordLinesEditor<T extends AnyRecord>({ record, name, lines, look, }: {
|
|
78
|
+
record: RecordReading<T>;
|
|
79
|
+
name: string;
|
|
80
|
+
/** The list as on screen (`editableLines(edit.values[name])`), never a page. */
|
|
81
|
+
lines: readonly AnyRecord[];
|
|
82
|
+
/** The screen's way, piece by piece. */
|
|
83
|
+
look?: LinesLook;
|
|
84
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { choicesOf } from "@sincpro/criteria";
|
|
4
|
+
import { adviceAt, defaultsOf, errorAt, pathOf } from "../../core/edit.js";
|
|
5
|
+
import { addingOf, blankLine, lineFields, lineKey, moved, sameLine, writtenInCell, } from "../../core/lines.js";
|
|
6
|
+
import { textOf, viewOf } from "../../core/present.js";
|
|
7
|
+
import { entityFor, relationFieldOf, } from "../../core/relation.js";
|
|
8
|
+
import { useEntityFor, useFramework } from "../../react/context.js";
|
|
9
|
+
import { controlFor, RelationPicker, } from "./control.js";
|
|
10
|
+
import { Field, LineItems, ReadValue, } from "@sincpro/web-ui";
|
|
11
|
+
import { useRef } from "react";
|
|
12
|
+
/** How a field of a line reads: a many2one's key by the name of what it points at. */
|
|
13
|
+
function textIn(definition, field, relation, line, locale) {
|
|
14
|
+
const named = relation === undefined
|
|
15
|
+
? null
|
|
16
|
+
: textOf(definition, relation.relation, line[relation.relation], locale);
|
|
17
|
+
return named ?? textOf(definition, field, line[field], locale) ?? "";
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* One field of a line, written — in its cell or in its dialog: a many2one of a declared entity
|
|
21
|
+
* is its picker, anything else the control its type asks for.
|
|
22
|
+
*
|
|
23
|
+
* Context: every change is a COPY of the line that keeps its local key (`sameLine`): a new
|
|
24
|
+
* line has no id, and a row whose key changed is another row to the table — it would lose the
|
|
25
|
+
* focus mid-word. A chosen record is kept beside its key so the cell names it at once.
|
|
26
|
+
*/
|
|
27
|
+
function LineValue({ definition, field, line, set, label, invalid, locale, inCell, own, }) {
|
|
28
|
+
const relation = relationFieldOf(definition, field);
|
|
29
|
+
const target = useEntityFor(relation?.target);
|
|
30
|
+
const write = (changes) => set(sameLine(line, { ...line, ...changes }));
|
|
31
|
+
if (own !== undefined)
|
|
32
|
+
return own({
|
|
33
|
+
value: line[field],
|
|
34
|
+
set: (next) => write({ [field]: next }),
|
|
35
|
+
readonly: false,
|
|
36
|
+
invalid,
|
|
37
|
+
label,
|
|
38
|
+
standalone: inCell,
|
|
39
|
+
line,
|
|
40
|
+
setLine: (next) => set(sameLine(line, next)),
|
|
41
|
+
});
|
|
42
|
+
if (relation !== undefined && !relation.many && target !== undefined) {
|
|
43
|
+
const key = line[relation.key];
|
|
44
|
+
return (_jsx(RelationPicker, { target: target, value: key === null || key === undefined ? "" : String(key), label: label, ownChooser: true, standalone: inCell, onChange: (id, chosen) => write({ [relation.key]: id, [relation.relation]: chosen ?? null }) }));
|
|
45
|
+
}
|
|
46
|
+
return controlFor({
|
|
47
|
+
value: line[field],
|
|
48
|
+
set: (next) => write({ [field]: next }),
|
|
49
|
+
readonly: false,
|
|
50
|
+
invalid,
|
|
51
|
+
label,
|
|
52
|
+
standalone: inCell,
|
|
53
|
+
}, viewOf(definition, field, locale).type, definition?.fields[field]?.exact === true, choicesOf(definition, field, locale));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The lines of an aggregate, edited inside its record: web-ui's `LineItems` over the record's
|
|
57
|
+
* edit — what the screen holds is the whole list, and every change writes it back with
|
|
58
|
+
* `edit.set(name, next)`, so it is dirty, previewed, saved and discarded like any field.
|
|
59
|
+
*
|
|
60
|
+
* Context: a one2many of no entity of its own (the lines of an invoice) used to be drawn read
|
|
61
|
+
* only (`RecordLines`). sincpro_framework writes such lines in the aggregate's own command, as
|
|
62
|
+
* the whole list in order; a new line goes without an id (`core/lines`). What `LineItems` does
|
|
63
|
+
* — add, edit, remove with «Deshacer», reorder — is the screen's: only the list that results
|
|
64
|
+
* travels. Each column is a field of the line model (`meta.fields[name].definition`, its
|
|
65
|
+
* scalars: `lineFields`), its control the one its type asks for — a many2one, the picker of its
|
|
66
|
+
* entity (`LineValue`) — its state the line model's hints over the line refined by the server's
|
|
67
|
+
* `items[i]` (`edit.cell`). A server error or warning that points into a cell
|
|
68
|
+
* (`#/values/lines/1/qty`, `["lines", 1, "price"]`) is said IN that cell (web-ui 1.6
|
|
69
|
+
* `cellState`); one about the line as a whole, on the line (`flag`).
|
|
70
|
+
*
|
|
71
|
+
* WHERE a line is written is one decision (`addingOf`): in the TABLE — «Agregar una línea» is
|
|
72
|
+
* its last row, a row is written where it is, the pencil opens the whole line in its dialog —
|
|
73
|
+
* whenever every column a person writes fits a cell; in the DIALOG otherwise, or when the
|
|
74
|
+
* screen asks (`look.adding="editor"`), and then the cells only read. The screen may draw it
|
|
75
|
+
* its way (`LinesLook`) without breaking that rule. A row that cannot be finished where
|
|
76
|
+
* it was opened is a trap, so «inline» is never forced. On a narrow box web-ui draws the lines
|
|
77
|
+
* as cards that open the dialog.
|
|
78
|
+
*
|
|
79
|
+
* Only a WHOLE list is editable: a record that brought one page of 1 000 lines draws them read
|
|
80
|
+
* only (`RecordLines`, «se muestran 50 de 1000»), because a page written back whole deletes
|
|
81
|
+
* every line it did not bring.
|
|
82
|
+
*/
|
|
83
|
+
export function RecordLinesEditor({ record, name, lines, look = {}, }) {
|
|
84
|
+
const { meta, locale, edit } = record;
|
|
85
|
+
const { entities, metas } = useFramework();
|
|
86
|
+
const definition = meta?.fields[name]?.definition ?? null;
|
|
87
|
+
const identity = definition?.identity || "id";
|
|
88
|
+
const fields = lineFields(definition);
|
|
89
|
+
const shown = look.columns === undefined ? fields : look.columns.filter((one) => fields.includes(one));
|
|
90
|
+
const controls = look.controls ?? {};
|
|
91
|
+
const own = new Set(Object.keys(controls));
|
|
92
|
+
const declared = (target) => entityFor(entities, target, metas.get) !== undefined;
|
|
93
|
+
const adding = addingOf(definition, {
|
|
94
|
+
declared,
|
|
95
|
+
shown,
|
|
96
|
+
own,
|
|
97
|
+
...(look.adding === undefined ? {} : { asked: look.adding }),
|
|
98
|
+
});
|
|
99
|
+
const writing = !edit.state(name).readonly;
|
|
100
|
+
// Once per render, not per cell: 2 000 lines × 8 fields asked the model 16 000 times.
|
|
101
|
+
const relations = new Map(fields.map((field) => [field, relationFieldOf(definition, field)]));
|
|
102
|
+
const keyOf = (line) => lineKey(line, identity);
|
|
103
|
+
// Where each line is, by its key — once per render: a `findIndex` per line drawn made the
|
|
104
|
+
// flags of 2 000 lines cost 50 ms (quadratic); with the map, a fraction of one.
|
|
105
|
+
const positions = new Map(lines.map((one, i) => [keyOf(one), i]));
|
|
106
|
+
const indexOf = (line) => positions.get(keyOf(line)) ?? lines.length;
|
|
107
|
+
// A callback of `LineItems` can run long after its render — «Deshacer» in a toast, seconds
|
|
108
|
+
// after the line went — so every change starts from the list on screen NOW, never the one of
|
|
109
|
+
// the render that made the callback.
|
|
110
|
+
const now = useRef(lines);
|
|
111
|
+
now.current = lines;
|
|
112
|
+
const write = (change) => edit.set(name, change(now.current));
|
|
113
|
+
const drawn = new Set(shown);
|
|
114
|
+
const fieldColumns = shown.map((field) => {
|
|
115
|
+
const view = viewOf(definition, field, locale);
|
|
116
|
+
const relation = relations.get(field);
|
|
117
|
+
const inCell = writing &&
|
|
118
|
+
adding === "inline" &&
|
|
119
|
+
definition !== null &&
|
|
120
|
+
definition.fields[field]?.readonly !== true &&
|
|
121
|
+
(own.has(field) || writtenInCell(definition, field, declared));
|
|
122
|
+
const ownCell = look.cells?.[field];
|
|
123
|
+
return {
|
|
124
|
+
id: field,
|
|
125
|
+
header: view.label,
|
|
126
|
+
value: (line) => line[field],
|
|
127
|
+
// Drawn as read: «Cemento 50 kg», not «CEM-50»; «1.552,00», not «1552.00».
|
|
128
|
+
cell: ownCell ?? ((line) => textIn(definition, field, relation, line, locale)),
|
|
129
|
+
text: (line) => textIn(definition, field, relation, line, locale),
|
|
130
|
+
numeric: view.type === "number" || view.type === "integer",
|
|
131
|
+
...(inCell
|
|
132
|
+
? {
|
|
133
|
+
edit: (line, set) => (_jsx(LineValue, { definition: definition, field: field, line: line, set: set, label: view.label, invalid: false, locale: locale, inCell: true, own: controls[field] })),
|
|
134
|
+
}
|
|
135
|
+
: {}),
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
const columns = [
|
|
139
|
+
...fieldColumns,
|
|
140
|
+
...(look.extra ?? []).map((one) => ({
|
|
141
|
+
id: one.id,
|
|
142
|
+
header: one.header,
|
|
143
|
+
value: () => null,
|
|
144
|
+
cell: one.cell,
|
|
145
|
+
...(one.text === undefined ? {} : { text: one.text }),
|
|
146
|
+
...(one.numeric === undefined ? {} : { numeric: one.numeric }),
|
|
147
|
+
})),
|
|
148
|
+
];
|
|
149
|
+
return (_jsx(LineItems, { lines: lines, columns: columns, getLineId: keyOf, mode: writing ? "edit" : "read", adding: adding, onChange: (key, next) => write((all) => all.map((one) => (keyOf(one) === key ? sameLine(one, next) : one))), newLine: () => blankLine(defaultsOf(definition), identity), onAdd: (line) => write((all) => [...all, line]), onRemove: (key) => write((all) => all.filter((one) => keyOf(one) !== key)), onRestore: (line, index) => write((all) => [...all.slice(0, index), line, ...all.slice(index)]), onReorder: (key, before) => write((all) => moved(all, identity, key, before)),
|
|
150
|
+
// A cell says its own (web-ui 1.6 `cellState`): the line model's hints refined by the
|
|
151
|
+
// server's `items[i]`, the error pointing at it (under its value) and its warning.
|
|
152
|
+
cellState: (line, column) => {
|
|
153
|
+
const index = indexOf(line);
|
|
154
|
+
const at = `${name}/${index}/${column}`;
|
|
155
|
+
const state = edit.cell(name, index, column);
|
|
156
|
+
const error = errorAt(edit.errors, at);
|
|
157
|
+
const warned = adviceAt(edit.advice, at)[0];
|
|
158
|
+
return {
|
|
159
|
+
readonly: state.readonly,
|
|
160
|
+
required: state.required,
|
|
161
|
+
hidden: !state.visible,
|
|
162
|
+
...(error === undefined ? {} : { error }),
|
|
163
|
+
...(warned === undefined ? {} : { flag: { tone: "warn", label: warned.message } }),
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
// The line's own flag: what points at the line and at no cell drawn — the line as a
|
|
167
|
+
// whole, or a field it has no column for (an id the server does not know).
|
|
168
|
+
flag: (line) => {
|
|
169
|
+
const at = `${name}/${indexOf(line)}`;
|
|
170
|
+
const onLine = (key) => key === at ||
|
|
171
|
+
(key.startsWith(`${at}/`) && !drawn.has(key.slice(at.length + 1).split("/")[0]));
|
|
172
|
+
const error = Object.entries(edit.errors).find(([key]) => onLine(key))?.[1];
|
|
173
|
+
if (error !== undefined)
|
|
174
|
+
return { tone: "bad", label: error };
|
|
175
|
+
const warned = edit.advice.find((one) => onLine(pathOf(one.path ?? [one.field ?? ""])));
|
|
176
|
+
return warned === undefined ? null : { tone: "warn", label: warned.message };
|
|
177
|
+
}, editor: (draft, set, state) => {
|
|
178
|
+
if (look.editor !== undefined)
|
|
179
|
+
return look.editor(draft, set, state);
|
|
180
|
+
const index = indexOf(draft);
|
|
181
|
+
return (_jsx("div", { className: "grid gap-3", children: fields.map((field) => {
|
|
182
|
+
const state = edit.cell(name, index, field);
|
|
183
|
+
if (!state.visible)
|
|
184
|
+
return null;
|
|
185
|
+
const view = viewOf(definition, field, locale);
|
|
186
|
+
const error = edit.errors[`${name}/${index}/${field}`];
|
|
187
|
+
return (_jsx(Field, { label: view.label, required: state.required && !state.readonly, ...(error === undefined ? {} : { error }), children: state.readonly ? (_jsx(ReadValue, { value: textIn(definition, field, relations.get(field), draft, locale) || "—" })) : (_jsx(LineValue, { definition: definition, field: field, line: draft, set: set, label: view.label, invalid: error !== undefined, locale: locale, inCell: false, own: controls[field] })) }, field));
|
|
188
|
+
}) }));
|
|
189
|
+
} }));
|
|
190
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AnyRecord } from "@sincpro/criteria";
|
|
1
2
|
import type { Entity, EntityRef } from "../../core/entity.js";
|
|
2
3
|
import type { OpenTarget } from "@sincpro/web-ui";
|
|
3
4
|
import { type ReactNode } from "react";
|
|
@@ -15,9 +16,16 @@ export type RecordOpening = {
|
|
|
15
16
|
open: (ref: EntityRef, to: OpenTarget) => void;
|
|
16
17
|
/** Where it may open from here, the default first. */
|
|
17
18
|
targets: readonly OpenTarget[];
|
|
19
|
+
/** The form is not on a page — a panel, a peek, a modal: a list inside it starts as rows
|
|
20
|
+
* (a table of seven columns in 760 px breaks every number and date into three lines). */
|
|
21
|
+
narrow?: boolean;
|
|
18
22
|
/** «Buscar más…»: the entity's own list opened to choose (in composer, a modal that answers),
|
|
19
23
|
* on what was typed. Without it the picker brings its own chooser. */
|
|
20
24
|
choose?: (entity: Entity, answer: (id: string) => void, query: string) => void;
|
|
25
|
+
/** «Agregar» on a one2many: the form of a new record of `entity`, starting with `values`
|
|
26
|
+
* (the key that points back here), opened where it answers; `created` once it exists.
|
|
27
|
+
* Without it a record's one2many only reads. */
|
|
28
|
+
create?: (entity: Entity, values: AnyRecord, created: () => void) => void;
|
|
21
29
|
};
|
|
22
30
|
export declare function RecordOpeningProvider({ value, children, }: {
|
|
23
31
|
value: RecordOpening;
|
|
@@ -8,15 +8,27 @@ function lastChange(summary) {
|
|
|
8
8
|
return copy.updatedLine(updated.at, updated.by);
|
|
9
9
|
return created.at === null ? null : copy.createdLine(created.at, created.by);
|
|
10
10
|
}
|
|
11
|
+
/** Its picture: a person's or a company's face is round, with the initials when there is no
|
|
12
|
+
* photo (`person`); anything else is its square photo, or nothing. The same in every size. */
|
|
13
|
+
function mediaOf(summary) {
|
|
14
|
+
if (summary.person)
|
|
15
|
+
return {
|
|
16
|
+
person: true,
|
|
17
|
+
alt: summary.name,
|
|
18
|
+
...(summary.image === null ? {} : { src: summary.image }),
|
|
19
|
+
};
|
|
20
|
+
return summary.image === null ? undefined : { src: summary.image, alt: summary.name };
|
|
21
|
+
}
|
|
11
22
|
/** The board's card: the state above, the name, what it is, its description, its last change. */
|
|
12
23
|
export function cardOf(summary) {
|
|
13
24
|
const footer = lastChange(summary);
|
|
25
|
+
const media = mediaOf(summary);
|
|
14
26
|
return {
|
|
15
27
|
title: summary.name,
|
|
16
28
|
...(summary.type === null ? {} : { subtitle: summary.type }),
|
|
17
29
|
...(summary.state === null ? {} : { tags: _jsx(Tag, { tone: "neutral", children: summary.state }) }),
|
|
18
30
|
...(summary.description === null ? {} : { meta: summary.description }),
|
|
19
|
-
...(
|
|
31
|
+
...(media === undefined ? {} : { media }),
|
|
20
32
|
...(footer === null
|
|
21
33
|
? {}
|
|
22
34
|
: { footer: _jsx("span", { className: "text-micro text-muted", children: footer }) }),
|
|
@@ -36,11 +48,7 @@ export function relationOf(summary) {
|
|
|
36
48
|
? []
|
|
37
49
|
: [{ label: copy.updated, value: copy.when(summary.updated.at, summary.updated.by) }]),
|
|
38
50
|
];
|
|
39
|
-
const media = summary
|
|
40
|
-
? { person: true, ...(summary.image === null ? {} : { src: summary.image }) }
|
|
41
|
-
: summary.image === null
|
|
42
|
-
? undefined
|
|
43
|
-
: { src: summary.image, alt: summary.name };
|
|
51
|
+
const media = mediaOf(summary);
|
|
44
52
|
const secondary = summary.type ?? (summary.id === summary.name ? null : summary.id);
|
|
45
53
|
const card = {
|
|
46
54
|
...(summary.state === null ? {} : { tag: { label: summary.state } }),
|
|
@@ -15,7 +15,7 @@ import { type ReactNode } from "react";
|
|
|
15
15
|
* What it does NOT own: the session (the app builds the transport with its headers) and the
|
|
16
16
|
* application bar (the app draws its own, around this or beside it).
|
|
17
17
|
*/
|
|
18
|
-
export declare function SincproApp({ transport, views, start, language, metas, entities, empty, skin, cache, }: {
|
|
18
|
+
export declare function SincproApp({ transport, views, start, language, metas, entities, empty, skin, cache, timeZone, }: {
|
|
19
19
|
transport: Transport;
|
|
20
20
|
/** The entities as composer views — `entityView(…)` — and any other view of the app. */
|
|
21
21
|
views: readonly ViewSpec[];
|
|
@@ -37,4 +37,7 @@ export declare function SincproApp({ transport, views, start, language, metas, e
|
|
|
37
37
|
* `false` asks the server every time.
|
|
38
38
|
*/
|
|
39
39
|
cache?: CacheOptions | false;
|
|
40
|
+
/** Where the business is (`"America/La_Paz"`): every date and time on its wall clock.
|
|
41
|
+
* Without it, the browser's. */
|
|
42
|
+
timeZone?: string;
|
|
40
43
|
}): import("react").JSX.Element;
|
|
@@ -19,7 +19,7 @@ import { useEffect, useMemo, useState } from "react";
|
|
|
19
19
|
* What it does NOT own: the session (the app builds the transport with its headers) and the
|
|
20
20
|
* application bar (the app draws its own, around this or beside it).
|
|
21
21
|
*/
|
|
22
|
-
export function SincproApp({ transport, views, start, language = "es", metas, entities, empty, skin = webUiSkin, cache = {}, }) {
|
|
22
|
+
export function SincproApp({ transport, views, start, language = "es", metas, entities, empty, skin = webUiSkin, cache = {}, timeZone, }) {
|
|
23
23
|
// Installed once, before anything draws: the three tables are module state of their packages.
|
|
24
24
|
useState(() => {
|
|
25
25
|
if (language === "es") {
|
|
@@ -41,5 +41,5 @@ export function SincproApp({ transport, views, start, language = "es", metas, en
|
|
|
41
41
|
// The first screen only: a re-render must not push it again.
|
|
42
42
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
43
43
|
}, [workspace]);
|
|
44
|
-
return (_jsx(FrameworkProvider, { transport: cached, locale: language, ...(metas === undefined ? {} : { metas }), ...(entities === undefined ? {} : { entities }), children: _jsx(ToastProvider, { children: _jsx("div", { className: "flex h-dvh min-h-0 flex-col bg-ground", children: _jsx(Composer, { workspace: workspace, registry: registry, skin: skin, ...(empty === undefined ? {} : { empty }) }) }) }) }));
|
|
44
|
+
return (_jsx(FrameworkProvider, { transport: cached, locale: language, ...(metas === undefined ? {} : { metas }), ...(entities === undefined ? {} : { entities }), ...(timeZone === undefined ? {} : { timeZone }), children: _jsx(ToastProvider, { children: _jsx("div", { className: "flex h-dvh min-h-0 flex-col bg-ground", children: _jsx(Composer, { workspace: workspace, registry: registry, skin: skin, ...(empty === undefined ? {} : { empty }) }) }) }) }));
|
|
45
45
|
}
|
|
@@ -16,7 +16,8 @@ import { type ComponentType } from "react";
|
|
|
16
16
|
*
|
|
17
17
|
* A reference with no id is the list — opened to choose one when it carries `pick` — and one
|
|
18
18
|
* with `new` is the record's own view with no id: the form of a record that does not exist yet
|
|
19
|
-
* (`entity.newRef()`), which `useRecord(entity,
|
|
19
|
+
* (`entity.newRef(values)`), which `useRecord(entity, resource)` fills with the model's defaults
|
|
20
|
+
* and what the reference carries.
|
|
20
21
|
*
|
|
21
22
|
* ⌘K asks the server with `matching` — 8 records, identity and display — the same question a
|
|
22
23
|
* many2one asks. The model definition is asked for once, on the first search, unless `meta`
|
|
@@ -5,26 +5,31 @@ import { NEW_PARAM } from "../../core/entity.js";
|
|
|
5
5
|
import { lookupOf } from "../../core/lookup.js";
|
|
6
6
|
import { useListing } from "../../react/useListing.js";
|
|
7
7
|
import { EntityList } from "../list/EntityList.js";
|
|
8
|
+
import { presentationOf } from "../list/presentation.js";
|
|
8
9
|
import { RecordOpeningProvider, } from "../record/opening.js";
|
|
9
10
|
import { openIn, RECORD_SIZE, targetsFor } from "./open.js";
|
|
10
11
|
import { EntityRecord } from "./record.js";
|
|
11
12
|
import { useMemo } from "react";
|
|
12
13
|
/**
|
|
13
14
|
* The fields of a view open their records in the workspace: a modal over this surface by
|
|
14
|
-
* default, beside it from inside a modal,
|
|
15
|
+
* default, beside it from inside a modal, «Buscar más…» as the entity's list that answers, and
|
|
16
|
+
* a one2many's «Agregar» as the new record's form that answers once it exists.
|
|
15
17
|
*/
|
|
16
18
|
function InWorkspace({ api, children }) {
|
|
17
19
|
const targets = targetsFor(api);
|
|
18
|
-
const opening = useMemo(() =>
|
|
19
|
-
|
|
20
|
-
targets
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
const opening = useMemo(() => {
|
|
21
|
+
// Never a modal over a modal: from inside one, what answers docks beside it.
|
|
22
|
+
const answering = targets[0] === "modal"
|
|
23
|
+
? { to: "modal", size: RECORD_SIZE }
|
|
24
|
+
: { to: "side" };
|
|
25
|
+
return {
|
|
26
|
+
open: (ref, to) => openIn(api, ref, to),
|
|
27
|
+
targets,
|
|
28
|
+
narrow: api.surface !== "page",
|
|
29
|
+
choose: (entity, answer, query) => api.pick(entity.chooserRef(query), (ref) => ref.id === undefined || answer(ref.id), answering),
|
|
30
|
+
create: (entity, values, created) => api.pick(entity.newRef(values), () => created(), answering),
|
|
31
|
+
};
|
|
32
|
+
}, [api, targets]);
|
|
28
33
|
return _jsx(RecordOpeningProvider, { value: opening, children: children });
|
|
29
34
|
}
|
|
30
35
|
/** The entity's list when the application writes none. */
|
|
@@ -65,11 +70,17 @@ export function entityView(entity, options) {
|
|
|
65
70
|
...matching(meta, query),
|
|
66
71
|
meta: false,
|
|
67
72
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
// ⌘K writes text: of the entity's look of a choice, its name and its meta.
|
|
74
|
+
const look = presentationOf(entity).option;
|
|
75
|
+
return lookupOf(meta, page.rows, entity.summary).map((one) => {
|
|
76
|
+
const own = look === undefined || one.record === undefined ? {} : look(one.record);
|
|
77
|
+
const detail = own.meta ?? one.detail;
|
|
78
|
+
return {
|
|
79
|
+
ref: entity.ref(one.id),
|
|
80
|
+
label: own.name ?? one.label,
|
|
81
|
+
...(detail === undefined ? {} : { detail }),
|
|
82
|
+
};
|
|
83
|
+
});
|
|
73
84
|
},
|
|
74
85
|
load: async () => EntityView,
|
|
75
86
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AnyRecord, Meta } from "@sincpro/criteria";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ResourceRef, WorkspaceApi } from "@sincpro/web-composer";
|
|
3
3
|
import type { Entity } from "../../core/entity.js";
|
|
4
4
|
import { type OpenTarget } from "@sincpro/web-ui";
|
|
5
5
|
import type { ReactNode } from "react";
|
|
@@ -19,19 +19,7 @@ import type { ReactNode } from "react";
|
|
|
19
19
|
*
|
|
20
20
|
* The phone is composer's: a modal becomes a sheet, a side panel the page. No view asks.
|
|
21
21
|
*/
|
|
22
|
-
|
|
23
|
-
export declare const RECORD_SIZE: {
|
|
24
|
-
readonly w: 760;
|
|
25
|
-
readonly h: 560;
|
|
26
|
-
};
|
|
27
|
-
/** Where a related record may open from this view, the default first. */
|
|
28
|
-
export declare function targetsFor(api: Pick<WorkspaceApi, "over">): readonly OpenTarget[];
|
|
29
|
-
/** web-ui's target as composer's options. Destinations share their names, so only the size is
|
|
30
|
-
* added — to the two that have one. */
|
|
31
|
-
export declare function openOptions(to: OpenTarget, size?: {
|
|
32
|
-
w: number;
|
|
33
|
-
h: number;
|
|
34
|
-
}): OpenOptions;
|
|
22
|
+
export { openOptions, RECORD_SIZE, targetsFor } from "../list/openIn.js";
|
|
35
23
|
/**
|
|
36
24
|
* Open a record where it was asked, or where it belongs from here.
|
|
37
25
|
*
|