@sincpro/web-framework 0.4.1 → 1.0.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 +48 -44
- package/dist/core/discovery.d.ts +47 -0
- package/dist/core/discovery.js +68 -0
- package/dist/core/edit.d.ts +35 -6
- package/dist/core/edit.js +57 -4
- package/dist/core/entity.d.ts +18 -3
- package/dist/core/entity.js +27 -1
- package/dist/core/ids.d.ts +17 -0
- package/dist/core/ids.js +52 -0
- package/dist/core/index.d.ts +3 -2
- package/dist/core/index.js +3 -2
- package/dist/core/meta.d.ts +7 -0
- package/dist/core/meta.js +1 -1
- package/dist/core/metas.d.ts +4 -5
- package/dist/core/rest.d.ts +8 -0
- package/dist/core/rest.js +62 -3
- package/dist/core/schema.d.ts +5 -12
- package/dist/core/trail.js +1 -4
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +1 -0
- package/dist/react/scope.d.ts +23 -0
- package/dist/react/scope.js +12 -0
- package/dist/react/useGroups.js +1 -2
- package/dist/react/useHistory.js +1 -1
- package/dist/react/useListing.js +14 -9
- package/dist/react/useLookup.js +2 -3
- package/dist/react/usePivot.js +0 -1
- package/dist/react/useRecord.d.ts +9 -2
- package/dist/react/useRecord.js +53 -12
- package/dist/react/useRelated.js +2 -2
- package/dist/web/list/EntitySearchBar.js +2 -2
- package/dist/web/list/search.js +1 -1
- package/dist/web/record/EntityPicker.d.ts +14 -7
- package/dist/web/record/EntityPicker.js +21 -6
- package/dist/web/record/EntityTags.d.ts +5 -3
- package/dist/web/record/EntityTags.js +29 -6
- package/dist/web/record/control.d.ts +4 -4
- package/dist/web/record/control.js +7 -17
- package/dist/web/record/lines.js +28 -11
- package/dist/web/record/opening.d.ts +17 -3
- package/dist/web/record/opening.js +12 -0
- package/dist/web/workspace/composer.js +9 -7
- package/dist/web/workspace/workspace.d.ts +6 -2
- package/dist/web/workspace/workspace.js +6 -2
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -150,6 +150,12 @@ export const transport = restTransport({
|
|
|
150
150
|
});
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
+
The definition of every model — its fields, operators, hints, widgets, formulas — is read from
|
|
154
|
+
the service's discovery document (`/openapi.json` by default; `document:` names another, `null`
|
|
155
|
+
reads none), once: each answer says only its `model_version`, and another version reads the
|
|
156
|
+
document again, revalidated by its `ETag`. A form computes a published formula itself and asks the
|
|
157
|
+
server for a preview only when a change reaches something it cannot answer.
|
|
158
|
+
|
|
153
159
|
**3. The entities** — declared once: where each is read and written, and its key in the
|
|
154
160
|
workspace (`domain:kind`). `summary` only when its fields are not the conventional ones.
|
|
155
161
|
|
|
@@ -262,13 +268,9 @@ function InvoiceView({ api, resource }: ViewProps) {
|
|
|
262
268
|
<RecordField
|
|
263
269
|
record={invoice}
|
|
264
270
|
name="customer_id"
|
|
271
|
+
// Inside a workspace the picker opens the chosen and «see all…» there by itself.
|
|
265
272
|
as={({ value, set }) => (
|
|
266
|
-
<EntityPicker
|
|
267
|
-
entity={customers}
|
|
268
|
-
value={String(value ?? "")}
|
|
269
|
-
onChange={set}
|
|
270
|
-
onBrowseAll={chooserIn(api, customers, set)}
|
|
271
|
-
/>
|
|
273
|
+
<EntityPicker entity={customers} value={String(value ?? "")} onChange={set} />
|
|
272
274
|
)}
|
|
273
275
|
/>
|
|
274
276
|
<RecordField record={invoice} name="notes" />
|
|
@@ -394,44 +396,46 @@ Everything is imported from `@sincpro/web-framework`.
|
|
|
394
396
|
|
|
395
397
|
## Where it stands
|
|
396
398
|
|
|
397
|
-
| Piece
|
|
398
|
-
|
|
|
399
|
-
| `restTransport`, `memoryTransport`
|
|
400
|
-
| `defineMeta`, `metaFromSchema`
|
|
401
|
-
| `useListing`, `EntityList`
|
|
402
|
-
| `useRecord`, `factsOf`, `RecordLines`
|
|
403
|
-
| Preloaded definitions
|
|
404
|
-
| `useLookup`, `EntityPicker`
|
|
405
|
-
| `useRecord` + `RecordField`, `dirtyOf`
|
|
406
|
-
| `useCommand`, `CommandButton`
|
|
407
|
-
| `useHistory`
|
|
408
|
-
| `entityView`
|
|
409
|
-
| `<SincproApp>`
|
|
410
|
-
| `useRecordInWorkspace`
|
|
411
|
-
| Paging by pages, ceiling on «more»
|
|
412
|
-
| `cachedTransport`
|
|
413
|
-
| `EntitySearchBar`
|
|
414
|
-
| `EntityList` views
|
|
415
|
-
| `useRelated`, `RelatedList`
|
|
416
|
-
| `EntityPicker` chooser
|
|
417
|
-
| `useRecord(entity, "")`, `create`
|
|
418
|
-
| `retryKeys`, `Idempotency-Key`
|
|
419
|
-
| `entity.newRef()`, «New» in the list
|
|
420
|
-
| `EntityPicker criteria`
|
|
421
|
-
| `chooserIn`, `entity.chooserRef()`
|
|
422
|
-
| `
|
|
423
|
-
| `
|
|
424
|
-
| `
|
|
425
|
-
| `
|
|
426
|
-
| `
|
|
427
|
-
| `
|
|
428
|
-
| `
|
|
429
|
-
|
|
|
430
|
-
| `
|
|
431
|
-
|
|
|
432
|
-
| `
|
|
433
|
-
| `
|
|
434
|
-
| `
|
|
399
|
+
| Piece | What it solves | Status |
|
|
400
|
+
| --------------------------------------- | -------------------------------------------------------------------------------------------- | ------ |
|
|
401
|
+
| `restTransport`, `memoryTransport` | The backend, or the same engine in memory | ✅ |
|
|
402
|
+
| `defineMeta`, `metaFromSchema` | A model by hand, or completed from OpenAPI | ✅ |
|
|
403
|
+
| `useListing`, `EntityList` | N records: criteria, search bar, builder, paging | ✅ |
|
|
404
|
+
| `useRecord`, `factsOf`, `RecordLines` | One record: facts, relations with their real total | ✅ |
|
|
405
|
+
| Preloaded definitions | `FrameworkProvider metas`: the models of the discovery document, or of an exported metamodel | ✅ |
|
|
406
|
+
| `useLookup`, `EntityPicker` | Literal search: a many2one select, ⌘K | ✅ |
|
|
407
|
+
| `useRecord` + `RecordField`, `dirtyOf` | Editing: hints evaluated live, preview, save with version (409), server drafts | ✅ |
|
|
408
|
+
| `useCommand`, `CommandButton` | An entity's actions, with a dry run before what destroys | ✅ |
|
|
409
|
+
| `useHistory` | Business events in `Record.Activity` (comments: backend first) | ✅ |
|
|
410
|
+
| `entityView` | An entity as a composer view: list, record, ⌘K search | ✅ |
|
|
411
|
+
| `<SincproApp>` | The application context in one place, the workspace dressed as web-ui (`webUiSkin`) | ✅ |
|
|
412
|
+
| `useRecordInWorkspace` | A record tells the workspace its real name and its unsaved work (composer 1.0) | ✅ |
|
|
413
|
+
| Paging by pages, ceiling on «more» | One page on screen, walked by the server's cursor: a million rows never freeze it | ✅ |
|
|
414
|
+
| `cachedTransport` | Repeated reads from memory, forgotten when a write touches the entity (on in `SincproApp`) | ✅ |
|
|
415
|
+
| `EntitySearchBar` | The search bar + criteria builder as a loose piece, for any `List` | ✅ |
|
|
416
|
+
| `EntityList` views | The same set as table, rows or board | ✅ |
|
|
417
|
+
| `useRelated`, `RelatedList` | A record's relation as a live list: searched and paged by the server, inside a form | ✅ |
|
|
418
|
+
| `EntityPicker` chooser | «see all…» opens the whole set, searched on the server: a many2one over millions | ✅ |
|
|
419
|
+
| `useRecord(entity, "")`, `create` | A new record: the model's defaults, saved to `create` (or `edit`), then it IS that record | ✅ |
|
|
420
|
+
| `retryKeys`, `Idempotency-Key` | A Command or a create retried after a lost answer runs once, not twice | ✅ |
|
|
421
|
+
| `entity.newRef()`, «New» in the list | The record's form with no id, in a page or a window; replaced by the record once created | ✅ |
|
|
422
|
+
| `EntityPicker criteria` | A many2one narrowed by another field: the invoices OF the chosen customer | ✅ |
|
|
423
|
+
| `chooserIn`, `entity.chooserRef()` | «see all…» as the composer's chooser: a row answers (`api.pick`) | ✅ |
|
|
424
|
+
| `EntityPicker` / `EntityTags` in a view | Open and choose in the workspace by themselves; the chooser carries the picker's cut | ✅ |
|
|
425
|
+
| `newEntityId`, `edit` on a new record | A new record born under a UUID v7 minted here: its draft and its save go through `Edit` | ✅ |
|
|
426
|
+
| `useGroups`, «Group by» | Groups counted by the server over the whole set; a group's rows read when it is opened | ✅ |
|
|
427
|
+
| `defineList`, `listView` | A named list (a fixed scope, its columns and filters) as data, and as a workspace view | ✅ |
|
|
428
|
+
| `EntityPicker` `browse`, `columns` | The chooser always a click away, as a table searched literally by the server | ✅ |
|
|
429
|
+
| `useFollowed` | A detail panel follows the record the person is on, without knowing the list | ✅ |
|
|
430
|
+
| `commands`, `useRowCommands` | An entity's Commands on each row, idempotent, offered only where `when` holds | ✅ |
|
|
431
|
+
| `summaryOf`, `cardOf`, `relationOf` | Any record at any size: name, description, state, type, picture, who made and changed it | ✅ |
|
|
432
|
+
| `openIn`, `RelatedRecord` | A related record opens in a modal; «Abrir en…» for the rest; never a modal over a modal | ✅ |
|
|
433
|
+
| Literal search everywhere | Picker: the first ten, the server's search as you type; tables: «Buscar «X»» on Enter | ✅ |
|
|
434
|
+
| `EntityTags` | A many2many as chips over thousands: the server's search, faces, the chosen never again | ✅ |
|
|
435
|
+
| `RecordField` + `entities` | A many2one / one2many drawn from the model alone, opened where the view says | ✅ |
|
|
436
|
+
| `EntityRecord`, `formOf` | Any entity's record screen without writing it | ✅ |
|
|
437
|
+
| `Trail`, `Record.Pager` | Previous / next from a record, across the server's pages | ✅ |
|
|
438
|
+
| `EntityList sums` | Each group says how much it adds up, summed by the server over all of it | ✅ |
|
|
435
439
|
|
|
436
440
|
What it does **not** do: mobile (`sincpro_mobile` has its own core), sessions and login (the
|
|
437
441
|
application's), routing (composer's), string-named widget registries, technical logs on a
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type Meta } from "@sincpro/criteria";
|
|
2
|
+
/**
|
|
3
|
+
* The discovery document of a bounded context, as far as this reads it: its paths, each
|
|
4
|
+
* operation with the entities it belongs to, and the published contract of every model.
|
|
5
|
+
*
|
|
6
|
+
* Context: sincpro_framework describes a model once — JSON Schema with `x-sincpro`, under the
|
|
7
|
+
* document's `components.schemas` — and every read answers only the version of it
|
|
8
|
+
* (`model_version`). So the definition a list, a form and a select are built from is read here,
|
|
9
|
+
* once, and asked for again only when an answer says another version.
|
|
10
|
+
*/
|
|
11
|
+
export type DiscoveryDocument = {
|
|
12
|
+
paths?: Readonly<Record<string, Readonly<Record<string, DiscoveryOperation>>>>;
|
|
13
|
+
components?: {
|
|
14
|
+
schemas?: Readonly<Record<string, DocumentSchema>>;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export type DiscoveryOperation = {
|
|
18
|
+
"x-sincpro-entities"?: readonly string[];
|
|
19
|
+
responses?: Readonly<Record<string, {
|
|
20
|
+
content?: Readonly<Record<string, {
|
|
21
|
+
schema?: DocumentSchema;
|
|
22
|
+
}>>;
|
|
23
|
+
}>>;
|
|
24
|
+
};
|
|
25
|
+
type DocumentSchema = {
|
|
26
|
+
$ref?: string;
|
|
27
|
+
items?: DocumentSchema;
|
|
28
|
+
anyOf?: readonly DocumentSchema[];
|
|
29
|
+
properties?: Readonly<Record<string, DocumentSchema>>;
|
|
30
|
+
"x-sincpro"?: {
|
|
31
|
+
entity?: string;
|
|
32
|
+
version?: string;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* The model a request to `path` answers, by the name of its contract in the document: the
|
|
37
|
+
* entity the operation says it belongs to, else the one its answer holds.
|
|
38
|
+
*
|
|
39
|
+
* modelAt(published, "/v1/projects/p1", "get") // "Project"
|
|
40
|
+
*/
|
|
41
|
+
export declare function modelAt(published: DiscoveryDocument, path: string, method: string): string | undefined;
|
|
42
|
+
/** The definition of the model named `name`, read from its contract, and its version. */
|
|
43
|
+
export declare function definitionOf(published: DiscoveryDocument, name: string): {
|
|
44
|
+
meta: Meta;
|
|
45
|
+
version: string | null;
|
|
46
|
+
} | undefined;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { metaFromContract, versionOf } from "@sincpro/criteria";
|
|
2
|
+
function nameOfRef(ref) {
|
|
3
|
+
return ref === undefined ? undefined : ref.slice(ref.lastIndexOf("/") + 1);
|
|
4
|
+
}
|
|
5
|
+
/** Whether a document path (`/v1/projects/{id}`) answers a request path (`/v1/projects/p1`). */
|
|
6
|
+
function answers(template, path) {
|
|
7
|
+
const pattern = template
|
|
8
|
+
.split("/")
|
|
9
|
+
.map((part) => /^\{[^}]+\}$/.test(part) ? "[^/]+" : part.replace(/[.*+?^$()|[\]\\]/g, "\\$&"))
|
|
10
|
+
.join("/");
|
|
11
|
+
return new RegExp(`^${pattern}$`).test(path);
|
|
12
|
+
}
|
|
13
|
+
/** The entity a schema describes, or the one its records hold: an answer's records field. */
|
|
14
|
+
function entityIn(schema, schemas, seen = new Set()) {
|
|
15
|
+
if (schema === undefined)
|
|
16
|
+
return undefined;
|
|
17
|
+
const named = nameOfRef(schema.$ref);
|
|
18
|
+
if (named !== undefined) {
|
|
19
|
+
if (seen.has(named))
|
|
20
|
+
return undefined;
|
|
21
|
+
seen.add(named);
|
|
22
|
+
const target = schemas[named];
|
|
23
|
+
return target?.["x-sincpro"]?.entity !== undefined
|
|
24
|
+
? named
|
|
25
|
+
: entityIn(target, schemas, seen);
|
|
26
|
+
}
|
|
27
|
+
for (const one of [schema.items, ...(schema.anyOf ?? [])]) {
|
|
28
|
+
const found = entityIn(one, schemas, seen);
|
|
29
|
+
if (found !== undefined)
|
|
30
|
+
return found;
|
|
31
|
+
}
|
|
32
|
+
for (const one of Object.values(schema.properties ?? {})) {
|
|
33
|
+
const found = entityIn(one, schemas, seen);
|
|
34
|
+
if (found !== undefined)
|
|
35
|
+
return found;
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The model a request to `path` answers, by the name of its contract in the document: the
|
|
41
|
+
* entity the operation says it belongs to, else the one its answer holds.
|
|
42
|
+
*
|
|
43
|
+
* modelAt(published, "/v1/projects/p1", "get") // "Project"
|
|
44
|
+
*/
|
|
45
|
+
export function modelAt(published, path, method) {
|
|
46
|
+
const schemas = published.components?.schemas ?? {};
|
|
47
|
+
for (const [template, item] of Object.entries(published.paths ?? {})) {
|
|
48
|
+
if (!answers(template, path))
|
|
49
|
+
continue;
|
|
50
|
+
const operation = item[method.toLowerCase()];
|
|
51
|
+
if (operation === undefined)
|
|
52
|
+
continue;
|
|
53
|
+
const declared = operation["x-sincpro-entities"]?.[0];
|
|
54
|
+
if (declared !== undefined && schemas[declared] !== undefined)
|
|
55
|
+
return declared;
|
|
56
|
+
const answered = operation.responses?.["200"]?.content?.["application/json"]?.schema;
|
|
57
|
+
return entityIn(answered, schemas);
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
/** The definition of the model named `name`, read from its contract, and its version. */
|
|
62
|
+
export function definitionOf(published, name) {
|
|
63
|
+
const schemas = (published.components?.schemas ?? {});
|
|
64
|
+
const contract = schemas[name];
|
|
65
|
+
if (contract?.["x-sincpro"] === undefined)
|
|
66
|
+
return undefined;
|
|
67
|
+
return { meta: metaFromContract(contract, schemas), version: versionOf(contract) };
|
|
68
|
+
}
|
package/dist/core/edit.d.ts
CHANGED
|
@@ -18,23 +18,52 @@ export type FieldState = {
|
|
|
18
18
|
};
|
|
19
19
|
/**
|
|
20
20
|
* What a server preview said about a field. A list of records — an invoice's lines — carries
|
|
21
|
-
* each one's own in `items`,
|
|
22
|
-
* `FieldState.items`).
|
|
21
|
+
* each one's own in `items`, by the line: its identity, or its position for a line that has
|
|
22
|
+
* none (sincpro_framework's `FieldState.items`).
|
|
23
23
|
*/
|
|
24
24
|
export type ServerFieldState = Partial<FieldState> & {
|
|
25
|
-
items?:
|
|
25
|
+
items?: Readonly<Record<string, Readonly<Record<string, ServerFieldState>>>>;
|
|
26
26
|
};
|
|
27
27
|
export type FieldStates = Readonly<Record<string, ServerFieldState>>;
|
|
28
28
|
/** What the server's preview warned about, pointing where it belongs (`path`: the field, or a
|
|
29
|
-
* line's cell — `["lines",
|
|
29
|
+
* line's cell — `["lines", "0192…", "price"]`, the line by its identity, by its position when
|
|
30
|
+
* it has none). It refuses nothing. sincpro_framework's warning. */
|
|
30
31
|
export type Advice = {
|
|
31
32
|
message: string;
|
|
32
33
|
field?: string | null;
|
|
33
34
|
path?: readonly (string | number)[];
|
|
35
|
+
/** The rule that said it (`Sale.delivery_when_confirmed`), when one did. */
|
|
36
|
+
rule?: string | null;
|
|
37
|
+
/** A rule the save enforces (`@check`): the save will be refused while it holds. */
|
|
38
|
+
blocks_save?: boolean;
|
|
34
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* What a change of `name` moves, computed here when every field it reaches says how
|
|
42
|
+
* (`computes`, the formula the server published): each in the order the server runs them, a
|
|
43
|
+
* computation of a computation reading the one before. `undefined` when one of them does not
|
|
44
|
+
* say — the server's preview answers it — or the change reaches nothing.
|
|
45
|
+
*
|
|
46
|
+
* computedLocally(saleMeta, { subtotal: "25.30", discount: "1.5" }, "discount")
|
|
47
|
+
* -> { total: "22.105" }
|
|
48
|
+
*/
|
|
49
|
+
export declare function computedLocally(meta: Meta | null | undefined, values: AnyRecord, name: string): AnyRecord | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Whether a change of `name` needs the server's preview: when the model says what each change
|
|
52
|
+
* reaches (`affects`, `judged_by` — sincpro_framework publishes them in the contract), only a
|
|
53
|
+
* field that reaches something does; a model that says nothing of it is always asked.
|
|
54
|
+
*/
|
|
55
|
+
export declare function needsPreview(meta: Meta | null | undefined, name: string): boolean;
|
|
35
56
|
export declare function fieldStateOf(meta: Meta | null | undefined, name: string, values: AnyRecord, fromServer?: FieldStates): FieldState;
|
|
36
|
-
/**
|
|
37
|
-
|
|
57
|
+
/**
|
|
58
|
+
* The states the server said of one line of a list of records: under the line's identity —
|
|
59
|
+
* the same line wherever it moved — or under its position when it has none.
|
|
60
|
+
*/
|
|
61
|
+
export declare function lineStatesOf(fromServer: FieldStates | undefined, name: string, line: AnyRecord | undefined, index: number, identity: string): FieldStates | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Where the server names a line in a path, most specific first: `lines/0192…` by its identity,
|
|
64
|
+
* `lines/2` by its position — an error, a warning and a state are looked for under each.
|
|
65
|
+
*/
|
|
66
|
+
export declare function linePlacesOf(name: string, line: AnyRecord, index: number, identity: string): string[];
|
|
38
67
|
/** The fields whose value differs from the record's — what a save or a draft sends. */
|
|
39
68
|
export declare function changesOf(record: AnyRecord, values: AnyRecord): AnyRecord;
|
|
40
69
|
/** What is missing before it can be saved: a required, visible, editable field left empty. */
|
package/dist/core/edit.js
CHANGED
|
@@ -1,4 +1,41 @@
|
|
|
1
|
-
import { matches, } from "@sincpro/criteria";
|
|
1
|
+
import { matches, valueOf, } from "@sincpro/criteria";
|
|
2
|
+
/**
|
|
3
|
+
* What a change of `name` moves, computed here when every field it reaches says how
|
|
4
|
+
* (`computes`, the formula the server published): each in the order the server runs them, a
|
|
5
|
+
* computation of a computation reading the one before. `undefined` when one of them does not
|
|
6
|
+
* say — the server's preview answers it — or the change reaches nothing.
|
|
7
|
+
*
|
|
8
|
+
* computedLocally(saleMeta, { subtotal: "25.30", discount: "1.5" }, "discount")
|
|
9
|
+
* -> { total: "22.105" }
|
|
10
|
+
*/
|
|
11
|
+
export function computedLocally(meta, values, name) {
|
|
12
|
+
const reached = meta?.fields[name]?.affects ?? [];
|
|
13
|
+
if (meta == null || reached.length === 0)
|
|
14
|
+
return undefined;
|
|
15
|
+
const working = { ...values };
|
|
16
|
+
const moved = {};
|
|
17
|
+
for (const one of reached) {
|
|
18
|
+
const formula = meta.fields[one]?.computes;
|
|
19
|
+
if (formula === undefined || formula === null)
|
|
20
|
+
return undefined;
|
|
21
|
+
working[one] = valueOf(formula, working);
|
|
22
|
+
moved[one] = working[one];
|
|
23
|
+
}
|
|
24
|
+
return moved;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Whether a change of `name` needs the server's preview: when the model says what each change
|
|
28
|
+
* reaches (`affects`, `judged_by` — sincpro_framework publishes them in the contract), only a
|
|
29
|
+
* field that reaches something does; a model that says nothing of it is always asked.
|
|
30
|
+
*/
|
|
31
|
+
export function needsPreview(meta, name) {
|
|
32
|
+
const fields = Object.values(meta?.fields ?? {});
|
|
33
|
+
const says = fields.some((one) => one.affects !== undefined || one.judged_by !== undefined);
|
|
34
|
+
if (!says)
|
|
35
|
+
return true;
|
|
36
|
+
const field = meta?.fields[name];
|
|
37
|
+
return (field?.affects?.length ?? 0) > 0 || (field?.judged_by?.length ?? 0) > 0;
|
|
38
|
+
}
|
|
2
39
|
function holds(values, condition, meta) {
|
|
3
40
|
if (condition === null || condition === undefined)
|
|
4
41
|
return false;
|
|
@@ -19,9 +56,25 @@ export function fieldStateOf(meta, name, values, fromServer) {
|
|
|
19
56
|
const { items: _lines, ...server } = fromServer?.[name] ?? {};
|
|
20
57
|
return { ...own, ...server };
|
|
21
58
|
}
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
59
|
+
/**
|
|
60
|
+
* The states the server said of one line of a list of records: under the line's identity —
|
|
61
|
+
* the same line wherever it moved — or under its position when it has none.
|
|
62
|
+
*/
|
|
63
|
+
export function lineStatesOf(fromServer, name, line, index, identity) {
|
|
64
|
+
const items = fromServer?.[name]?.items ?? {};
|
|
65
|
+
const own = line?.[identity];
|
|
66
|
+
const named = own === undefined || own === null || own === "" ? undefined : items[String(own)];
|
|
67
|
+
return named ?? items[String(index)];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Where the server names a line in a path, most specific first: `lines/0192…` by its identity,
|
|
71
|
+
* `lines/2` by its position — an error, a warning and a state are looked for under each.
|
|
72
|
+
*/
|
|
73
|
+
export function linePlacesOf(name, line, index, identity) {
|
|
74
|
+
const own = line[identity];
|
|
75
|
+
return own === undefined || own === null || own === ""
|
|
76
|
+
? [`${name}/${index}`]
|
|
77
|
+
: [`${name}/${String(own)}`, `${name}/${index}`];
|
|
25
78
|
}
|
|
26
79
|
/** The fields whose value differs from the record's — what a save or a draft sends. */
|
|
27
80
|
export function changesOf(record, values) {
|
package/dist/core/entity.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AnyRecord, type Expression } from "@sincpro/criteria";
|
|
2
2
|
import type { SummaryFields } from "./summary.js";
|
|
3
3
|
import type { Method } from "./transport.js";
|
|
4
4
|
/**
|
|
@@ -34,8 +34,9 @@ export type Entity<T extends AnyRecord = AnyRecord> = {
|
|
|
34
34
|
* from its customer starts with that customer (`startingValuesOf`). */
|
|
35
35
|
newRef: (values?: AnyRecord) => EntityRef;
|
|
36
36
|
/** Its list opened to CHOOSE one: a row answers whoever asked (composer's `api.pick`). With
|
|
37
|
-
* `query`, the list opens on that literal search — what was typed in the picker
|
|
38
|
-
|
|
37
|
+
* `query`, the list opens on that literal search — what was typed in the picker; with
|
|
38
|
+
* `scope`, it offers only what the picker may choose (`scopeOfRef`). */
|
|
39
|
+
chooserRef: (query?: string, scope?: Expression) => EntityRef;
|
|
39
40
|
/** The route of its list. */
|
|
40
41
|
list: string;
|
|
41
42
|
/** The route of one record. */
|
|
@@ -89,6 +90,9 @@ export declare const TRAIL_PARAM = "from";
|
|
|
89
90
|
/** What a new record starts with, as JSON: one param keeps each value's type (a number stays
|
|
90
91
|
* a number), where a param per field would turn all of them into text. */
|
|
91
92
|
export declare const WITH_PARAM = "with";
|
|
93
|
+
/** What a chooser may offer at all, as JSON: the picker's cut — its criteria, the keys it
|
|
94
|
+
* excludes, its named list's scope — that the list it opens ANDs under everything. */
|
|
95
|
+
export declare const SCOPE_PARAM = "scope";
|
|
92
96
|
/**
|
|
93
97
|
* What a new record's reference says it starts with (`newRef(values)`), or nothing.
|
|
94
98
|
*
|
|
@@ -96,6 +100,17 @@ export declare const WITH_PARAM = "with";
|
|
|
96
100
|
* typed in the address bar — a value that is not a JSON object is not a start, never an error.
|
|
97
101
|
*/
|
|
98
102
|
export declare function startingValuesOf(ref: Pick<EntityRef, "params">): AnyRecord;
|
|
103
|
+
/**
|
|
104
|
+
* The cut a chooser's reference carries (`chooserRef(query, scope)`), or nothing.
|
|
105
|
+
*
|
|
106
|
+
* Context: a picker that may offer only some records — the contacts of the chosen customer, the
|
|
107
|
+
* tenants a project is not shared with yet — opened the workspace's chooser on the WHOLE
|
|
108
|
+
* entity, and offered the rest. The cut travels in the reference, so it survives a reload; read
|
|
109
|
+
* from an address bar it is untyped, so it is read the way criteria reads a filter from outside
|
|
110
|
+
* (`readExpression`), and one that does not read is no cut, never an error. It narrows what is
|
|
111
|
+
* OFFERED; the server still decides what may be stored.
|
|
112
|
+
*/
|
|
113
|
+
export declare function scopeOfRef(ref: Pick<EntityRef, "params">): Expression | undefined;
|
|
99
114
|
/** A write: its method and its path, `{id}` where the record's key goes. */
|
|
100
115
|
export type WriteRoute = {
|
|
101
116
|
method: Method;
|
package/dist/core/entity.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { readExpression } from "@sincpro/criteria";
|
|
1
2
|
/**
|
|
2
3
|
* The params that say what a reference without an id is for: `new` is the form of a record
|
|
3
4
|
* that does not exist yet, `pick` is the list opened to choose one. Params, because they are
|
|
@@ -12,6 +13,9 @@ export const TRAIL_PARAM = "from";
|
|
|
12
13
|
/** What a new record starts with, as JSON: one param keeps each value's type (a number stays
|
|
13
14
|
* a number), where a param per field would turn all of them into text. */
|
|
14
15
|
export const WITH_PARAM = "with";
|
|
16
|
+
/** What a chooser may offer at all, as JSON: the picker's cut — its criteria, the keys it
|
|
17
|
+
* excludes, its named list's scope — that the list it opens ANDs under everything. */
|
|
18
|
+
export const SCOPE_PARAM = "scope";
|
|
15
19
|
/**
|
|
16
20
|
* What a new record's reference says it starts with (`newRef(values)`), or nothing.
|
|
17
21
|
*
|
|
@@ -32,6 +36,27 @@ export function startingValuesOf(ref) {
|
|
|
32
36
|
return {};
|
|
33
37
|
}
|
|
34
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* The cut a chooser's reference carries (`chooserRef(query, scope)`), or nothing.
|
|
41
|
+
*
|
|
42
|
+
* Context: a picker that may offer only some records — the contacts of the chosen customer, the
|
|
43
|
+
* tenants a project is not shared with yet — opened the workspace's chooser on the WHOLE
|
|
44
|
+
* entity, and offered the rest. The cut travels in the reference, so it survives a reload; read
|
|
45
|
+
* from an address bar it is untyped, so it is read the way criteria reads a filter from outside
|
|
46
|
+
* (`readExpression`), and one that does not read is no cut, never an error. It narrows what is
|
|
47
|
+
* OFFERED; the server still decides what may be stored.
|
|
48
|
+
*/
|
|
49
|
+
export function scopeOfRef(ref) {
|
|
50
|
+
const written = ref.params?.[SCOPE_PARAM];
|
|
51
|
+
if (written === undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
try {
|
|
54
|
+
return readExpression(JSON.parse(written));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
35
60
|
const METHODS = new Set(["POST", "PUT", "PATCH", "DELETE"]);
|
|
36
61
|
/** `"POST /invoices/{id}/confirm"` → its method and path. A write without a method is a bug. */
|
|
37
62
|
export function writeRoute(text) {
|
|
@@ -91,7 +116,7 @@ export function entity(key, routes) {
|
|
|
91
116
|
: { [WITH_PARAM]: JSON.stringify(values) }),
|
|
92
117
|
},
|
|
93
118
|
}),
|
|
94
|
-
chooserRef: (query) => ({
|
|
119
|
+
chooserRef: (query, scope) => ({
|
|
95
120
|
domain,
|
|
96
121
|
kind,
|
|
97
122
|
params: {
|
|
@@ -99,6 +124,7 @@ export function entity(key, routes) {
|
|
|
99
124
|
...(query === undefined || query.trim() === ""
|
|
100
125
|
? {}
|
|
101
126
|
: { [QUERY_PARAM]: query.trim() }),
|
|
127
|
+
...(scope === undefined ? {} : { [SCOPE_PARAM]: JSON.stringify(scope) }),
|
|
102
128
|
},
|
|
103
129
|
}),
|
|
104
130
|
list: routes.list,
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A new record's key, minted where the record is born: a UUID v7 as 32 hex characters —
|
|
3
|
+
* sincpro_framework's `new_entity_id`.
|
|
4
|
+
*
|
|
5
|
+
* Context: sincpro_framework's `Edit` creates a new record under the id the CLIENT names («a
|
|
6
|
+
* UUID v7 it mints»), and the draft and the stored record share it from the first draft save.
|
|
7
|
+
* A form that waited for the server to name it could neither keep a draft of a new record nor
|
|
8
|
+
* reach the edit route at all (`PUT /invoices/{id}/edit` needs the id in its path). The layout
|
|
9
|
+
* is the backend's (RFC 9562): 48 bits of unix milliseconds, the version, a 12-bit counter
|
|
10
|
+
* within the millisecond, the variant and 62 random bits — so ids minted here sort by when
|
|
11
|
+
* they were minted, like the server's, and ordering by `id` is still ordering by creation.
|
|
12
|
+
*
|
|
13
|
+
* newEntityId() // "019a4c2e7f1074a8b13fa739ccbdcd53"
|
|
14
|
+
*/
|
|
15
|
+
export declare function newEntityId(now?: number): string;
|
|
16
|
+
/** A key that is a UUID v7 as sincpro_framework writes it: 32 hex, version 7, variant `10`. */
|
|
17
|
+
export declare function isEntityId(text: string): boolean;
|
package/dist/core/ids.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A new record's key, minted where the record is born: a UUID v7 as 32 hex characters —
|
|
3
|
+
* sincpro_framework's `new_entity_id`.
|
|
4
|
+
*
|
|
5
|
+
* Context: sincpro_framework's `Edit` creates a new record under the id the CLIENT names («a
|
|
6
|
+
* UUID v7 it mints»), and the draft and the stored record share it from the first draft save.
|
|
7
|
+
* A form that waited for the server to name it could neither keep a draft of a new record nor
|
|
8
|
+
* reach the edit route at all (`PUT /invoices/{id}/edit` needs the id in its path). The layout
|
|
9
|
+
* is the backend's (RFC 9562): 48 bits of unix milliseconds, the version, a 12-bit counter
|
|
10
|
+
* within the millisecond, the variant and 62 random bits — so ids minted here sort by when
|
|
11
|
+
* they were minted, like the server's, and ordering by `id` is still ordering by creation.
|
|
12
|
+
*
|
|
13
|
+
* newEntityId() // "019a4c2e7f1074a8b13fa739ccbdcd53"
|
|
14
|
+
*/
|
|
15
|
+
const COUNTER_MAX = 0xfff;
|
|
16
|
+
let lastMillis = -1;
|
|
17
|
+
let counter = 0;
|
|
18
|
+
function randomBytes(count) {
|
|
19
|
+
return globalThis.crypto.getRandomValues(new Uint8Array(count));
|
|
20
|
+
}
|
|
21
|
+
function hex(value, digits) {
|
|
22
|
+
return value.toString(16).padStart(digits, "0");
|
|
23
|
+
}
|
|
24
|
+
export function newEntityId(now = Date.now()) {
|
|
25
|
+
// The clock never runs backwards for these ids: the same millisecond (or an earlier one, a
|
|
26
|
+
// clock moved back) counts up; a full counter borrows the next millisecond.
|
|
27
|
+
let millis = now;
|
|
28
|
+
if (millis <= lastMillis) {
|
|
29
|
+
millis = lastMillis;
|
|
30
|
+
counter += 1;
|
|
31
|
+
if (counter > COUNTER_MAX) {
|
|
32
|
+
millis += 1;
|
|
33
|
+
counter = 0;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
// A fresh millisecond starts the counter at a random point of its lower half: room to count.
|
|
38
|
+
const [high = 0, low = 0] = randomBytes(2);
|
|
39
|
+
counter = ((high << 8) | low) & 0x7ff;
|
|
40
|
+
}
|
|
41
|
+
lastMillis = millis;
|
|
42
|
+
const tail = randomBytes(8);
|
|
43
|
+
tail[0] = ((tail[0] ?? 0) & 0x3f) | 0x80; // the variant, `10`
|
|
44
|
+
return (hex(millis, 12) +
|
|
45
|
+
"7" + // the version
|
|
46
|
+
hex(counter, 3) +
|
|
47
|
+
Array.from(tail, (byte) => hex(byte, 2)).join(""));
|
|
48
|
+
}
|
|
49
|
+
/** A key that is a UUID v7 as sincpro_framework writes it: 32 hex, version 7, variant `10`. */
|
|
50
|
+
export function isEntityId(text) {
|
|
51
|
+
return /^[0-9a-f]{12}7[0-9a-f]{3}[89ab][0-9a-f]{15}$/.test(text);
|
|
52
|
+
}
|
package/dist/core/index.d.ts
CHANGED
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export { type CachedTransport, cachedTransport, type CacheOptions, } from "./cache.js";
|
|
7
7
|
export { ENGLISH_FRAMEWORK_COPY, type FrameworkCopy, frameworkCopy, installFrameworkCopy, SPANISH_FRAMEWORK_COPY, } from "./copy.js";
|
|
8
|
-
export { type Advice, adviceAt, changesOf, creationOf, defaultsOf, errorAt, errorsByField, type FieldState, fieldStateOf, type FieldStates, lineStatesOf, missingOf, pathOf, type ServerFieldState, } from "./edit.js";
|
|
9
|
-
export { type Entity, entity, type EntityRef, type EntityRoutes, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, startingValuesOf, TRAIL_PARAM, WITH_PARAM, type WriteRoute, writeRoute, } from "./entity.js";
|
|
8
|
+
export { type Advice, adviceAt, changesOf, computedLocally, creationOf, defaultsOf, errorAt, errorsByField, type FieldState, fieldStateOf, type FieldStates, linePlacesOf, lineStatesOf, missingOf, needsPreview, pathOf, type ServerFieldState, } from "./edit.js";
|
|
9
|
+
export { type Entity, entity, type EntityRef, type EntityRoutes, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, SCOPE_PARAM, scopeOfRef, startingValuesOf, TRAIL_PARAM, WITH_PARAM, type WriteRoute, writeRoute, } from "./entity.js";
|
|
10
10
|
export { type FormLayout, formOf } from "./form.js";
|
|
11
11
|
export { type HistoryEntry, historyOf } from "./history.js";
|
|
12
|
+
export { isEntityId, newEntityId } from "./ids.js";
|
|
12
13
|
export { addingOf, type AddingRule, blankLine, type Declared, editableLines, type LineAdding, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
|
|
13
14
|
export { defineList, type GroupLevel, isList, type ListDefinition, type ListSpec, type NamedFilter, scopeOf, } from "./list.js";
|
|
14
15
|
export { lookupOf, type LookupOption } from "./lookup.js";
|
package/dist/core/index.js
CHANGED
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export { cachedTransport, } from "./cache.js";
|
|
7
7
|
export { ENGLISH_FRAMEWORK_COPY, frameworkCopy, installFrameworkCopy, SPANISH_FRAMEWORK_COPY, } from "./copy.js";
|
|
8
|
-
export { adviceAt, changesOf, creationOf, defaultsOf, errorAt, errorsByField, fieldStateOf, lineStatesOf, missingOf, pathOf, } from "./edit.js";
|
|
9
|
-
export { entity, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, startingValuesOf, TRAIL_PARAM, WITH_PARAM, writeRoute, } from "./entity.js";
|
|
8
|
+
export { adviceAt, changesOf, computedLocally, creationOf, defaultsOf, errorAt, errorsByField, fieldStateOf, linePlacesOf, lineStatesOf, missingOf, needsPreview, pathOf, } from "./edit.js";
|
|
9
|
+
export { entity, listRoute, NEW_PARAM, pathFor, PICK_PARAM, QUERY_PARAM, SCOPE_PARAM, scopeOfRef, startingValuesOf, TRAIL_PARAM, WITH_PARAM, writeRoute, } from "./entity.js";
|
|
10
10
|
export { formOf } from "./form.js";
|
|
11
11
|
export { historyOf } from "./history.js";
|
|
12
|
+
export { isEntityId, newEntityId } from "./ids.js";
|
|
12
13
|
export { addingOf, blankLine, editableLines, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
|
|
13
14
|
export { defineList, isList, scopeOf, } from "./list.js";
|
|
14
15
|
export { lookupOf } from "./lookup.js";
|
package/dist/core/meta.d.ts
CHANGED
|
@@ -63,6 +63,13 @@ export declare const field: {
|
|
|
63
63
|
visible_when?: Expression | null;
|
|
64
64
|
sortable: boolean;
|
|
65
65
|
ops: Operator[];
|
|
66
|
+
widget?: string;
|
|
67
|
+
affects?: string[];
|
|
68
|
+
judged_by?: string[];
|
|
69
|
+
holds?: Expression | null;
|
|
70
|
+
computes?: import("@sincpro/criteria").Formula | null;
|
|
71
|
+
navigable?: boolean;
|
|
72
|
+
total?: string | null;
|
|
66
73
|
many?: boolean;
|
|
67
74
|
relation?: string | null;
|
|
68
75
|
identified_by?: string | null;
|
package/dist/core/meta.js
CHANGED
|
@@ -109,7 +109,7 @@ function searchTemplate(fields) {
|
|
|
109
109
|
});
|
|
110
110
|
const [only] = fields;
|
|
111
111
|
return {
|
|
112
|
-
|
|
112
|
+
selection: fields.length === 1 && only !== undefined ? like(only) : { any: fields.map(like) },
|
|
113
113
|
};
|
|
114
114
|
}
|
|
115
115
|
/** A whole definition, from its fields. See the file header. */
|
package/dist/core/metas.d.ts
CHANGED
|
@@ -2,11 +2,10 @@ import type { Meta } from "@sincpro/criteria";
|
|
|
2
2
|
/**
|
|
3
3
|
* The model definitions already known, by entity key (`"billing:invoice"`).
|
|
4
4
|
*
|
|
5
|
-
* Context: sincpro_framework
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* ask with `meta: false` and draw their labels before the rows arrive.
|
|
5
|
+
* Context: sincpro_framework publishes each model once, in the discovery document the
|
|
6
|
+
* transport reads (`core/discovery`); an answer carries only its version. This keeps the
|
|
7
|
+
* definitions ACROSS readings, so the second list of the same entity, the record opened from it
|
|
8
|
+
* and the select that points at it draw their labels before the rows arrive.
|
|
10
9
|
*
|
|
11
10
|
* Seeded, it is the preload: definitions written at build time (a snapshot of the server's,
|
|
12
11
|
* or `defineMeta`) reach the first screen with no request at all.
|