@atscript/ui 0.1.203 → 0.1.205
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/dist/form/create-form-def.cjs +52 -21
- package/dist/form/create-form-def.d.cts +23 -18
- package/dist/form/create-form-def.d.mts +23 -18
- package/dist/form/create-form-def.mjs +52 -21
- package/dist/form/decimal-format.cjs +9 -11
- package/dist/form/decimal-format.mjs +9 -11
- package/dist/form/diff.cjs +32 -14
- package/dist/form/diff.d.cts +7 -4
- package/dist/form/diff.d.mts +7 -4
- package/dist/form/diff.mjs +32 -14
- package/dist/form/loaded-empty.cjs +56 -0
- package/dist/form/loaded-empty.d.cts +25 -0
- package/dist/form/loaded-empty.d.mts +25 -0
- package/dist/form/loaded-empty.mjs +56 -0
- package/dist/form/omit-derived.cjs +38 -0
- package/dist/form/omit-derived.d.cts +17 -0
- package/dist/form/omit-derived.d.mts +17 -0
- package/dist/form/omit-derived.mjs +38 -0
- package/dist/form/optional-mode.cjs +85 -0
- package/dist/form/optional-mode.d.cts +58 -0
- package/dist/form/optional-mode.d.mts +58 -0
- package/dist/form/optional-mode.mjs +82 -0
- package/dist/form/path-utils.cjs +4 -5
- package/dist/form/path-utils.mjs +4 -5
- package/dist/form/types.d.cts +15 -14
- package/dist/form/types.d.mts +15 -14
- package/dist/form/validate.cjs +2 -2
- package/dist/form/validate.mjs +2 -2
- package/dist/index.cjs +23 -4
- package/dist/index.d.cts +7 -3
- package/dist/index.d.mts +7 -3
- package/dist/index.mjs +10 -6
- package/dist/plugin/annotations.cjs +12 -0
- package/dist/plugin/annotations.mjs +13 -1
- package/dist/plugin/value-help-validation.cjs +25 -1
- package/dist/plugin/value-help-validation.mjs +25 -2
- package/dist/shared/annotation-keys.cjs +3 -0
- package/dist/shared/annotation-keys.d.cts +3 -1
- package/dist/shared/annotation-keys.d.mts +3 -1
- package/dist/shared/annotation-keys.mjs +3 -1
- package/dist/shared/exact-number.cjs +12 -0
- package/dist/shared/exact-number.mjs +12 -0
- package/dist/shared/meta-cache.cjs +1 -1
- package/dist/shared/meta-cache.mjs +1 -1
- package/dist/table/create-table-def.cjs +4 -1
- package/dist/table/create-table-def.mjs +4 -2
- package/dist/table/types.d.cts +23 -16
- package/dist/table/types.d.mts +23 -16
- package/dist/table/value-kind.cjs +37 -2
- package/dist/table/value-kind.d.cts +22 -2
- package/dist/table/value-kind.d.mts +22 -2
- package/dist/table/value-kind.mjs +36 -3
- package/dist/value-help/distinct-client.cjs +5 -4
- package/dist/value-help/distinct-client.d.cts +8 -2
- package/dist/value-help/distinct-client.d.mts +8 -2
- package/dist/value-help/distinct-client.mjs +5 -4
- package/dist/value-help/value-help-client.cjs +45 -9
- package/dist/value-help/value-help-client.mjs +45 -9
- package/package.json +11 -12
package/dist/form/diff.mjs
CHANGED
|
@@ -15,7 +15,11 @@ import { isNavRelation, mergeStrategyPaths } from "@atscript/db/validator";
|
|
|
15
15
|
* (`{ value: domainData }`) so this reuses {@link getByPath}.
|
|
16
16
|
*
|
|
17
17
|
* Revert-aware: a value edited back to its baseline produces no change and no
|
|
18
|
-
* patch entry.
|
|
18
|
+
* patch entry. A value stored unset (`null` / absent) that the form now holds
|
|
19
|
+
* as `""` is no change either, at any depth — a field typed into and emptied
|
|
20
|
+
* again, an optional text emptied on an edit form (since 0.1.205). The reverse
|
|
21
|
+
* is a change: a stored `""` that is now unset is sent as `null`. Emptying a
|
|
22
|
+
* stored value is a change to `""` / `null` / `false`.
|
|
19
23
|
*
|
|
20
24
|
* Snapshot contract: the result is NOT a deep copy. `$insert` items, `$replace`
|
|
21
25
|
* arrays, scalar leaf values, and `changes[].before/after` all hold live
|
|
@@ -76,7 +80,7 @@ function isNavKey(def, key) {
|
|
|
76
80
|
*/
|
|
77
81
|
function diffFields(fields, prefix, baseline, current, changes, patch, mergePaths, versionColumn) {
|
|
78
82
|
for (const field of fields) {
|
|
79
|
-
if (field.phantom || field.derived
|
|
83
|
+
if (field.phantom || field.derived || versionColumn !== void 0 && !prefix && field.path === versionColumn) continue;
|
|
80
84
|
const fullPath = prefix ? `${prefix}.${field.path}` : field.path;
|
|
81
85
|
if (isArrayField(field)) {
|
|
82
86
|
diffArrayField(field, fullPath, baseline, current, changes, patch, mergePaths);
|
|
@@ -111,7 +115,7 @@ function diffLeafRoot(def, baseline, current, changes, patch) {
|
|
|
111
115
|
const field = def.rootField;
|
|
112
116
|
const before = getByPath(baseline, "");
|
|
113
117
|
const after = getByPath(current, "");
|
|
114
|
-
if (
|
|
118
|
+
if (sameStored(before, after)) return;
|
|
115
119
|
const mergePaths = leafRootMergePaths(def.type);
|
|
116
120
|
if (isArrayField(field)) {
|
|
117
121
|
const arrayPatch = diffArray(field, before, after, mergePaths.has(LEAF_ROOT_KEY));
|
|
@@ -137,7 +141,7 @@ function diffLeafRoot(def, baseline, current, changes, patch) {
|
|
|
137
141
|
function diffScalarField(field, fullPath, baseline, current, changes, patch, mergePaths) {
|
|
138
142
|
const before = getByPath(baseline, fullPath);
|
|
139
143
|
const after = getByPath(current, fullPath);
|
|
140
|
-
if (
|
|
144
|
+
if (sameStored(before, after)) return;
|
|
141
145
|
changes.push({
|
|
142
146
|
path: fullPath,
|
|
143
147
|
kind: "set",
|
|
@@ -225,7 +229,7 @@ function diffObjectField(field, fullPath, baseline, current, changes, patch, mer
|
|
|
225
229
|
function diffArrayField(field, fullPath, baseline, current, changes, patch, mergePaths) {
|
|
226
230
|
const before = getByPath(baseline, fullPath);
|
|
227
231
|
const after = getByPath(current, fullPath);
|
|
228
|
-
if (
|
|
232
|
+
if (sameStored(before, after)) return;
|
|
229
233
|
const value = getFieldMeta(field.prop, "db.json") !== void 0 || getFieldMeta(field.prop, "db.encrypted") !== void 0 ? after ?? null : diffArray(field, before, after, mergePaths.has(fullPath));
|
|
230
234
|
if (value === void 0) return;
|
|
231
235
|
changes.push({
|
|
@@ -318,7 +322,7 @@ function diffKeyedArray(before, after, keyProps, merge) {
|
|
|
318
322
|
continue;
|
|
319
323
|
}
|
|
320
324
|
if (afterKeys[kept++] !== k) return { $replace: after };
|
|
321
|
-
if (
|
|
325
|
+
if (sameStored(prev, el)) continue;
|
|
322
326
|
const partial = buildKeyedUpdate(prev, el, keyProps);
|
|
323
327
|
if (!partial) continue;
|
|
324
328
|
if (incomplete(el)) return { $replace: after };
|
|
@@ -347,7 +351,7 @@ function buildKeyedUpdate(prev, next, keyProps) {
|
|
|
347
351
|
if (keyProps.includes(k)) continue;
|
|
348
352
|
const a = prev[k];
|
|
349
353
|
const b = next[k];
|
|
350
|
-
if (!
|
|
354
|
+
if (!sameStored(a, b)) {
|
|
351
355
|
partial[k] = b === void 0 ? null : b;
|
|
352
356
|
changed = true;
|
|
353
357
|
}
|
|
@@ -487,13 +491,27 @@ function stableKey(v) {
|
|
|
487
491
|
* unchanged. The patch cannot tell them apart either: it sends a cleared value
|
|
488
492
|
* as `null`.
|
|
489
493
|
*
|
|
490
|
-
* The single comparator shared across the form engine
|
|
491
|
-
* detection ({@link buildFormRebase})
|
|
492
|
-
* reimplement equality elsewhere.
|
|
494
|
+
* The single comparator shared across the form engine — the diff and conflict
|
|
495
|
+
* detection ({@link buildFormRebase}) — never reimplement equality elsewhere.
|
|
493
496
|
*/
|
|
494
497
|
function deepEqual(a, b) {
|
|
498
|
+
return equal(a, b, false);
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Whether the form's `after` leaves the stored `before` unchanged: {@link deepEqual},
|
|
502
|
+
* and a value stored unset (`null` / `undefined` / absent) that now holds `""` is
|
|
503
|
+
* unchanged too, at any depth — a field the user typed into and emptied again, an
|
|
504
|
+
* optional text emptied on an edit form (business mode), a field switched on and
|
|
505
|
+
* left empty. Asymmetric: a stored `""` that is now unset is a change. Since 0.1.205.
|
|
506
|
+
*/
|
|
507
|
+
function sameStored(before, after) {
|
|
508
|
+
return equal(before, after, true);
|
|
509
|
+
}
|
|
510
|
+
/** {@link deepEqual}; with `emptyText`, also `b === ""` over an unset `a` ({@link sameStored}). */
|
|
511
|
+
function equal(a, b, emptyText) {
|
|
495
512
|
if (a === b) return true;
|
|
496
|
-
|
|
513
|
+
const aUnset = a === null || a === void 0;
|
|
514
|
+
if (aUnset || b === null || b === void 0) return aUnset && (b === null || b === void 0 || emptyText && b === "");
|
|
497
515
|
if (typeof a === "number" && typeof b === "number") return Number.isNaN(a) && Number.isNaN(b);
|
|
498
516
|
if (typeof a !== "object" || typeof b !== "object") return false;
|
|
499
517
|
const aIsArr = Array.isArray(a);
|
|
@@ -501,13 +519,13 @@ function deepEqual(a, b) {
|
|
|
501
519
|
if (aIsArr !== bIsArr) return false;
|
|
502
520
|
if (aIsArr && bIsArr) {
|
|
503
521
|
if (a.length !== b.length) return false;
|
|
504
|
-
for (let i = 0; i < a.length; i++) if (!
|
|
522
|
+
for (let i = 0; i < a.length; i++) if (!equal(a[i], b[i], emptyText)) return false;
|
|
505
523
|
return true;
|
|
506
524
|
}
|
|
507
525
|
const ao = a;
|
|
508
526
|
const bo = b;
|
|
509
|
-
for (const k of Object.keys(ao)) if (!
|
|
510
|
-
for (const k of Object.keys(bo)) if (!Object.hasOwn(ao, k) &&
|
|
527
|
+
for (const k of Object.keys(ao)) if (!equal(ao[k], Object.hasOwn(bo, k) ? bo[k] : void 0, emptyText)) return false;
|
|
528
|
+
for (const k of Object.keys(bo)) if (!Object.hasOwn(ao, k) && !equal(void 0, bo[k], emptyText)) return false;
|
|
511
529
|
return true;
|
|
512
530
|
}
|
|
513
531
|
//#endregion
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const require_types = require("./types.cjs");
|
|
2
|
+
const require_path_utils = require("./path-utils.cjs");
|
|
3
|
+
const require_optional_mode = require("./optional-mode.cjs");
|
|
4
|
+
//#region src/form/loaded-empty.ts
|
|
5
|
+
/**
|
|
6
|
+
* Normalizes, in place, a `""` that loaded form data holds on an optional
|
|
7
|
+
* scalar field whose type refuses `""` — an e-mail, a pattern, a number, a
|
|
8
|
+
* date, a selection, a key: it becomes the field's emptied value
|
|
9
|
+
* ({@link emptiedValue}) — `null` on a business edit form, unset (the key
|
|
10
|
+
* removed) on a create form or in the technical mode. Otherwise a value the
|
|
11
|
+
* user never touched fails validation and blocks every save. A plain text
|
|
12
|
+
* field that accepts `""` keeps it; a union field is left as it is. At any
|
|
13
|
+
* depth: nested objects, array rows, tuples and union variants.
|
|
14
|
+
*
|
|
15
|
+
* `value` is the unwrapped domain data of `def`'s type; `mode` the form's
|
|
16
|
+
* optional mode (`<AsForm optional-mode>`), `edit` whether it edits a stored
|
|
17
|
+
* record (`FormDef.edit`). `<AsForm>` runs it whenever its data arrives,
|
|
18
|
+
* before change tracking takes its baseline. Since 0.1.205.
|
|
19
|
+
*/
|
|
20
|
+
function normalizeLoadedEmpty(def, value, opts = {}) {
|
|
21
|
+
if (!require_path_utils.isPlainObject(value)) return;
|
|
22
|
+
const data = { value };
|
|
23
|
+
for (const field of def.fields) {
|
|
24
|
+
if (!field.path || field.phantom || field.derived) continue;
|
|
25
|
+
const v = require_path_utils.getByPath(data, field.path);
|
|
26
|
+
if (v === "") {
|
|
27
|
+
if (!require_types.isUnionField(field)) emptyRefused(field, data, opts);
|
|
28
|
+
} else walkField(field, v, opts);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function walkField(field, value, opts) {
|
|
32
|
+
if (value === null || typeof value !== "object") return;
|
|
33
|
+
if (require_types.isObjectField(field)) normalizeLoadedEmpty(field.objectDef, value, opts);
|
|
34
|
+
else if (require_types.isArrayField(field)) {
|
|
35
|
+
if (Array.isArray(value)) for (const item of value) walkField(field.itemField, item, opts);
|
|
36
|
+
} else if (require_types.isTupleField(field)) {
|
|
37
|
+
if (Array.isArray(value)) field.itemFields.forEach((f, i) => walkField(f, value[i], opts));
|
|
38
|
+
} else if (require_types.isUnionField(field)) {
|
|
39
|
+
const v = field.unionVariants[require_path_utils.detectUnionVariant(value, field.unionVariants)];
|
|
40
|
+
if (v?.def) normalizeLoadedEmpty(v.def, value, opts);
|
|
41
|
+
else if (v?.itemField) walkField(v.itemField, value, opts);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** `field`'s `""` → its emptied value, unless the field is required or its type takes `""`. */
|
|
45
|
+
function emptyRefused(field, data, opts) {
|
|
46
|
+
if (!field.prop.optional) return;
|
|
47
|
+
if (require_optional_mode.emptiedValue(field, { edit: true }) === "") return;
|
|
48
|
+
const emptied = require_optional_mode.emptiedValue(field, {
|
|
49
|
+
mode: require_optional_mode.resolveOptionalMode(field, opts.mode),
|
|
50
|
+
edit: opts.edit
|
|
51
|
+
});
|
|
52
|
+
if (emptied === void 0) require_path_utils.deleteByPath(data, field.path);
|
|
53
|
+
else require_path_utils.setByPath(data, field.path, emptied);
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
exports.normalizeLoadedEmpty = normalizeLoadedEmpty;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { FormDef } from "./types.cjs";
|
|
2
|
+
import { TOptionalMode } from "./optional-mode.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/form/loaded-empty.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Normalizes, in place, a `""` that loaded form data holds on an optional
|
|
7
|
+
* scalar field whose type refuses `""` — an e-mail, a pattern, a number, a
|
|
8
|
+
* date, a selection, a key: it becomes the field's emptied value
|
|
9
|
+
* ({@link emptiedValue}) — `null` on a business edit form, unset (the key
|
|
10
|
+
* removed) on a create form or in the technical mode. Otherwise a value the
|
|
11
|
+
* user never touched fails validation and blocks every save. A plain text
|
|
12
|
+
* field that accepts `""` keeps it; a union field is left as it is. At any
|
|
13
|
+
* depth: nested objects, array rows, tuples and union variants.
|
|
14
|
+
*
|
|
15
|
+
* `value` is the unwrapped domain data of `def`'s type; `mode` the form's
|
|
16
|
+
* optional mode (`<AsForm optional-mode>`), `edit` whether it edits a stored
|
|
17
|
+
* record (`FormDef.edit`). `<AsForm>` runs it whenever its data arrives,
|
|
18
|
+
* before change tracking takes its baseline. Since 0.1.205.
|
|
19
|
+
*/
|
|
20
|
+
declare function normalizeLoadedEmpty(def: FormDef, value: unknown, opts?: {
|
|
21
|
+
mode?: TOptionalMode;
|
|
22
|
+
edit?: boolean;
|
|
23
|
+
}): void;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { normalizeLoadedEmpty };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { FormDef } from "./types.mjs";
|
|
2
|
+
import { TOptionalMode } from "./optional-mode.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/form/loaded-empty.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Normalizes, in place, a `""` that loaded form data holds on an optional
|
|
7
|
+
* scalar field whose type refuses `""` — an e-mail, a pattern, a number, a
|
|
8
|
+
* date, a selection, a key: it becomes the field's emptied value
|
|
9
|
+
* ({@link emptiedValue}) — `null` on a business edit form, unset (the key
|
|
10
|
+
* removed) on a create form or in the technical mode. Otherwise a value the
|
|
11
|
+
* user never touched fails validation and blocks every save. A plain text
|
|
12
|
+
* field that accepts `""` keeps it; a union field is left as it is. At any
|
|
13
|
+
* depth: nested objects, array rows, tuples and union variants.
|
|
14
|
+
*
|
|
15
|
+
* `value` is the unwrapped domain data of `def`'s type; `mode` the form's
|
|
16
|
+
* optional mode (`<AsForm optional-mode>`), `edit` whether it edits a stored
|
|
17
|
+
* record (`FormDef.edit`). `<AsForm>` runs it whenever its data arrives,
|
|
18
|
+
* before change tracking takes its baseline. Since 0.1.205.
|
|
19
|
+
*/
|
|
20
|
+
declare function normalizeLoadedEmpty(def: FormDef, value: unknown, opts?: {
|
|
21
|
+
mode?: TOptionalMode;
|
|
22
|
+
edit?: boolean;
|
|
23
|
+
}): void;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { normalizeLoadedEmpty };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { isArrayField, isObjectField, isTupleField, isUnionField } from "./types.mjs";
|
|
2
|
+
import { deleteByPath, detectUnionVariant, getByPath, isPlainObject, setByPath } from "./path-utils.mjs";
|
|
3
|
+
import { emptiedValue, resolveOptionalMode } from "./optional-mode.mjs";
|
|
4
|
+
//#region src/form/loaded-empty.ts
|
|
5
|
+
/**
|
|
6
|
+
* Normalizes, in place, a `""` that loaded form data holds on an optional
|
|
7
|
+
* scalar field whose type refuses `""` — an e-mail, a pattern, a number, a
|
|
8
|
+
* date, a selection, a key: it becomes the field's emptied value
|
|
9
|
+
* ({@link emptiedValue}) — `null` on a business edit form, unset (the key
|
|
10
|
+
* removed) on a create form or in the technical mode. Otherwise a value the
|
|
11
|
+
* user never touched fails validation and blocks every save. A plain text
|
|
12
|
+
* field that accepts `""` keeps it; a union field is left as it is. At any
|
|
13
|
+
* depth: nested objects, array rows, tuples and union variants.
|
|
14
|
+
*
|
|
15
|
+
* `value` is the unwrapped domain data of `def`'s type; `mode` the form's
|
|
16
|
+
* optional mode (`<AsForm optional-mode>`), `edit` whether it edits a stored
|
|
17
|
+
* record (`FormDef.edit`). `<AsForm>` runs it whenever its data arrives,
|
|
18
|
+
* before change tracking takes its baseline. Since 0.1.205.
|
|
19
|
+
*/
|
|
20
|
+
function normalizeLoadedEmpty(def, value, opts = {}) {
|
|
21
|
+
if (!isPlainObject(value)) return;
|
|
22
|
+
const data = { value };
|
|
23
|
+
for (const field of def.fields) {
|
|
24
|
+
if (!field.path || field.phantom || field.derived) continue;
|
|
25
|
+
const v = getByPath(data, field.path);
|
|
26
|
+
if (v === "") {
|
|
27
|
+
if (!isUnionField(field)) emptyRefused(field, data, opts);
|
|
28
|
+
} else walkField(field, v, opts);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function walkField(field, value, opts) {
|
|
32
|
+
if (value === null || typeof value !== "object") return;
|
|
33
|
+
if (isObjectField(field)) normalizeLoadedEmpty(field.objectDef, value, opts);
|
|
34
|
+
else if (isArrayField(field)) {
|
|
35
|
+
if (Array.isArray(value)) for (const item of value) walkField(field.itemField, item, opts);
|
|
36
|
+
} else if (isTupleField(field)) {
|
|
37
|
+
if (Array.isArray(value)) field.itemFields.forEach((f, i) => walkField(f, value[i], opts));
|
|
38
|
+
} else if (isUnionField(field)) {
|
|
39
|
+
const v = field.unionVariants[detectUnionVariant(value, field.unionVariants)];
|
|
40
|
+
if (v?.def) normalizeLoadedEmpty(v.def, value, opts);
|
|
41
|
+
else if (v?.itemField) walkField(v.itemField, value, opts);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** `field`'s `""` → its emptied value, unless the field is required or its type takes `""`. */
|
|
45
|
+
function emptyRefused(field, data, opts) {
|
|
46
|
+
if (!field.prop.optional) return;
|
|
47
|
+
if (emptiedValue(field, { edit: true }) === "") return;
|
|
48
|
+
const emptied = emptiedValue(field, {
|
|
49
|
+
mode: resolveOptionalMode(field, opts.mode),
|
|
50
|
+
edit: opts.edit
|
|
51
|
+
});
|
|
52
|
+
if (emptied === void 0) deleteByPath(data, field.path);
|
|
53
|
+
else setByPath(data, field.path, emptied);
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
export { normalizeLoadedEmpty };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const require_path_utils = require("./path-utils.cjs");
|
|
2
|
+
//#region src/form/omit-derived.ts
|
|
3
|
+
/**
|
|
4
|
+
* Form data `value` (the unwrapped domain data of `def`'s type) without the
|
|
5
|
+
* fields the server owns — the root fields `createFormDef` marked `derived`: a
|
|
6
|
+
* served field `/meta` marks `readOnly`, a writable view's own field, a
|
|
7
|
+
* computed one, an edit form's update stamp. A create or update payload never
|
|
8
|
+
* carries them (the server would drop the keys); a field the form writes
|
|
9
|
+
* itself (a pick's fill target) is no `derived` field and stays. Root fields
|
|
10
|
+
* only, a dotted leaf of an object rendered inline included: `readOnly` fields
|
|
11
|
+
* are top-level. A copy when it removes something (only the objects on a
|
|
12
|
+
* removed path are copied), else `value` itself. Since 0.1.205.
|
|
13
|
+
*/
|
|
14
|
+
function omitDerivedFields(def, value) {
|
|
15
|
+
if (!require_path_utils.isPlainObject(value)) return value;
|
|
16
|
+
let out = value;
|
|
17
|
+
for (const f of def.fields) if (f.derived && f.path !== "") out = without(out, f.path.split("."), 0);
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
/** `obj` without the value at `keys` (from `i`), copying only the objects on that path. */
|
|
21
|
+
function without(obj, keys, i) {
|
|
22
|
+
const key = keys[i];
|
|
23
|
+
const v = obj[key];
|
|
24
|
+
if (v === void 0) return obj;
|
|
25
|
+
if (i < keys.length - 1) {
|
|
26
|
+
if (!require_path_utils.isPlainObject(v)) return obj;
|
|
27
|
+
const inner = without(v, keys, i + 1);
|
|
28
|
+
return inner === v ? obj : {
|
|
29
|
+
...obj,
|
|
30
|
+
[key]: inner
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const rest = { ...obj };
|
|
34
|
+
delete rest[key];
|
|
35
|
+
return rest;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
exports.omitDerivedFields = omitDerivedFields;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FormDef } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/form/omit-derived.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Form data `value` (the unwrapped domain data of `def`'s type) without the
|
|
6
|
+
* fields the server owns — the root fields `createFormDef` marked `derived`: a
|
|
7
|
+
* served field `/meta` marks `readOnly`, a writable view's own field, a
|
|
8
|
+
* computed one, an edit form's update stamp. A create or update payload never
|
|
9
|
+
* carries them (the server would drop the keys); a field the form writes
|
|
10
|
+
* itself (a pick's fill target) is no `derived` field and stays. Root fields
|
|
11
|
+
* only, a dotted leaf of an object rendered inline included: `readOnly` fields
|
|
12
|
+
* are top-level. A copy when it removes something (only the objects on a
|
|
13
|
+
* removed path are copied), else `value` itself. Since 0.1.205.
|
|
14
|
+
*/
|
|
15
|
+
declare function omitDerivedFields<T>(def: FormDef, value: T): T;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { omitDerivedFields };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FormDef } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/form/omit-derived.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Form data `value` (the unwrapped domain data of `def`'s type) without the
|
|
6
|
+
* fields the server owns — the root fields `createFormDef` marked `derived`: a
|
|
7
|
+
* served field `/meta` marks `readOnly`, a writable view's own field, a
|
|
8
|
+
* computed one, an edit form's update stamp. A create or update payload never
|
|
9
|
+
* carries them (the server would drop the keys); a field the form writes
|
|
10
|
+
* itself (a pick's fill target) is no `derived` field and stays. Root fields
|
|
11
|
+
* only, a dotted leaf of an object rendered inline included: `readOnly` fields
|
|
12
|
+
* are top-level. A copy when it removes something (only the objects on a
|
|
13
|
+
* removed path are copied), else `value` itself. Since 0.1.205.
|
|
14
|
+
*/
|
|
15
|
+
declare function omitDerivedFields<T>(def: FormDef, value: T): T;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { omitDerivedFields };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { isPlainObject } from "./path-utils.mjs";
|
|
2
|
+
//#region src/form/omit-derived.ts
|
|
3
|
+
/**
|
|
4
|
+
* Form data `value` (the unwrapped domain data of `def`'s type) without the
|
|
5
|
+
* fields the server owns — the root fields `createFormDef` marked `derived`: a
|
|
6
|
+
* served field `/meta` marks `readOnly`, a writable view's own field, a
|
|
7
|
+
* computed one, an edit form's update stamp. A create or update payload never
|
|
8
|
+
* carries them (the server would drop the keys); a field the form writes
|
|
9
|
+
* itself (a pick's fill target) is no `derived` field and stays. Root fields
|
|
10
|
+
* only, a dotted leaf of an object rendered inline included: `readOnly` fields
|
|
11
|
+
* are top-level. A copy when it removes something (only the objects on a
|
|
12
|
+
* removed path are copied), else `value` itself. Since 0.1.205.
|
|
13
|
+
*/
|
|
14
|
+
function omitDerivedFields(def, value) {
|
|
15
|
+
if (!isPlainObject(value)) return value;
|
|
16
|
+
let out = value;
|
|
17
|
+
for (const f of def.fields) if (f.derived && f.path !== "") out = without(out, f.path.split("."), 0);
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
/** `obj` without the value at `keys` (from `i`), copying only the objects on that path. */
|
|
21
|
+
function without(obj, keys, i) {
|
|
22
|
+
const key = keys[i];
|
|
23
|
+
const v = obj[key];
|
|
24
|
+
if (v === void 0) return obj;
|
|
25
|
+
if (i < keys.length - 1) {
|
|
26
|
+
if (!isPlainObject(v)) return obj;
|
|
27
|
+
const inner = without(v, keys, i + 1);
|
|
28
|
+
return inner === v ? obj : {
|
|
29
|
+
...obj,
|
|
30
|
+
[key]: inner
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const rest = { ...obj };
|
|
34
|
+
delete rest[key];
|
|
35
|
+
return rest;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { omitDerivedFields };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const require_annotation_keys = require("../shared/annotation-keys.cjs");
|
|
2
|
+
const require_types = require("./types.cjs");
|
|
3
|
+
const require_field_resolver = require("../shared/field-resolver.cjs");
|
|
4
|
+
const require_extract_ref = require("../value-help/extract-ref.cjs");
|
|
5
|
+
const require_exact_number = require("../shared/exact-number.cjs");
|
|
6
|
+
//#region src/form/optional-mode.ts
|
|
7
|
+
let defaultOptionalMode = "business";
|
|
8
|
+
/**
|
|
9
|
+
* Set the app-wide {@link TOptionalMode} — call it once at startup, next to
|
|
10
|
+
* `createDefaultTypes()`. A form's `optional-mode` prop and a field's
|
|
11
|
+
* `@ui.form.optionalMode` take precedence. Since 0.1.205.
|
|
12
|
+
*/
|
|
13
|
+
function setDefaultOptionalMode(mode) {
|
|
14
|
+
defaultOptionalMode = mode;
|
|
15
|
+
}
|
|
16
|
+
/** The app-wide {@link TOptionalMode}: `"business"` unless {@link setDefaultOptionalMode} set another. Since 0.1.205. */
|
|
17
|
+
function getDefaultOptionalMode() {
|
|
18
|
+
return defaultOptionalMode;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The {@link TOptionalMode} of an optional field: its own
|
|
22
|
+
* `@ui.form.optionalMode` ▸ `formMode` (the form's `optional-mode`) ▸ the
|
|
23
|
+
* app-wide default ▸ `"business"`. An object, array (multiselect and tags
|
|
24
|
+
* included), tuple or union field is always `"technical"`. Since 0.1.205.
|
|
25
|
+
*/
|
|
26
|
+
function resolveOptionalMode(field, formMode) {
|
|
27
|
+
if (require_types.isObjectField(field) || require_types.isArrayField(field) || require_types.isTupleField(field) || require_types.isUnionField(field)) return "technical";
|
|
28
|
+
const own = require_field_resolver.getFieldMeta(field.prop, require_annotation_keys.UI_FORM_OPTIONAL_MODE);
|
|
29
|
+
if (own === "business" || own === "technical") return own;
|
|
30
|
+
return formMode ?? defaultOptionalMode;
|
|
31
|
+
}
|
|
32
|
+
/** Field types the default text control renders: an emptied one holds `""`. */
|
|
33
|
+
const TEXT = new Set([
|
|
34
|
+
"text",
|
|
35
|
+
"textarea",
|
|
36
|
+
"password"
|
|
37
|
+
]);
|
|
38
|
+
const takesEmptyTextCache = /* @__PURE__ */ new WeakMap();
|
|
39
|
+
/** Whether `""` is a valid value of `prop` (no pattern, e-mail, minimum length, `@meta.required`). */
|
|
40
|
+
function takesEmptyText(prop) {
|
|
41
|
+
let takes = takesEmptyTextCache.get(prop);
|
|
42
|
+
if (takes === void 0) {
|
|
43
|
+
takes = prop.validator().validate("", true);
|
|
44
|
+
takesEmptyTextCache.set(prop, takes);
|
|
45
|
+
}
|
|
46
|
+
return takes;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The value an input of `field` writes when the user empties it. `mode` is the
|
|
50
|
+
* field's {@link resolveOptionalMode} (`"business"` when left out); `edit`
|
|
51
|
+
* whether the form edits a stored record (`createFormDef(type, { mode: "edit" })`,
|
|
52
|
+
* `FormDef.edit`; `false` when left out). Both matter for an optional field
|
|
53
|
+
* only. Since 0.1.205.
|
|
54
|
+
*
|
|
55
|
+
* An optional field:
|
|
56
|
+
* - `"technical"`, or a create form: `undefined` — the field is unset again and
|
|
57
|
+
* a create leaves it out, so column defaults apply;
|
|
58
|
+
* - `"business"` on an edit form: its empty value, so a whole-record update
|
|
59
|
+
* clears it too — `false` for a boolean, `""` for text whose type accepts
|
|
60
|
+
* `""`, else `null` (a number, a date, a selection, a reference or foreign
|
|
61
|
+
* key, and text whose type rejects `""`: a pattern, an e-mail, a minimum
|
|
62
|
+
* length, `@meta.required`);
|
|
63
|
+
* - a set-only field (`@db.writeOnly`): always `undefined` — an empty input
|
|
64
|
+
* keeps the stored secret.
|
|
65
|
+
*
|
|
66
|
+
* A required field: `""` for text and a string key, else `null`.
|
|
67
|
+
*/
|
|
68
|
+
function emptiedValue(field, opts) {
|
|
69
|
+
const prop = field.prop;
|
|
70
|
+
const dt = prop.type.kind === "" ? prop.type.designType : void 0;
|
|
71
|
+
const key = !!field.valueHelpInfo || field.type === "ref" || require_extract_ref.isForeignKey(prop);
|
|
72
|
+
const text = TEXT.has(field.type) && !key && !require_exact_number.isExactNumberText(prop);
|
|
73
|
+
if (prop.optional) {
|
|
74
|
+
if (opts?.mode === "technical" || !opts?.edit || prop.metadata.has("db.writeOnly")) return;
|
|
75
|
+
if (dt === "boolean") return false;
|
|
76
|
+
return text && takesEmptyText(prop) ? "" : null;
|
|
77
|
+
}
|
|
78
|
+
if (key) return dt === "string" ? "" : null;
|
|
79
|
+
return text ? "" : null;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
exports.emptiedValue = emptiedValue;
|
|
83
|
+
exports.getDefaultOptionalMode = getDefaultOptionalMode;
|
|
84
|
+
exports.resolveOptionalMode = resolveOptionalMode;
|
|
85
|
+
exports.setDefaultOptionalMode = setDefaultOptionalMode;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { FormFieldDef } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/form/optional-mode.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* How a form offers an optional scalar field (`name?: T`). Since 0.1.205.
|
|
6
|
+
*
|
|
7
|
+
* - `"business"` (default): a plain input, as for a required field — no unset
|
|
8
|
+
* "×", no "Not set" placeholder, no click-to-edit. Left untouched the field
|
|
9
|
+
* stays unset (a create leaves it out); emptied on an edit form it holds its
|
|
10
|
+
* empty value ({@link emptiedValue}).
|
|
11
|
+
* - `"technical"`: the tri-state — "Not set" until the user switches the field
|
|
12
|
+
* on, an unset "×" while it holds a value.
|
|
13
|
+
*
|
|
14
|
+
* Optional objects, arrays, tuples and unions always keep their "Add …" /
|
|
15
|
+
* remove controls (`"technical"`).
|
|
16
|
+
*/
|
|
17
|
+
type TOptionalMode = "business" | "technical";
|
|
18
|
+
/**
|
|
19
|
+
* Set the app-wide {@link TOptionalMode} — call it once at startup, next to
|
|
20
|
+
* `createDefaultTypes()`. A form's `optional-mode` prop and a field's
|
|
21
|
+
* `@ui.form.optionalMode` take precedence. Since 0.1.205.
|
|
22
|
+
*/
|
|
23
|
+
declare function setDefaultOptionalMode(mode: TOptionalMode): void;
|
|
24
|
+
/** The app-wide {@link TOptionalMode}: `"business"` unless {@link setDefaultOptionalMode} set another. Since 0.1.205. */
|
|
25
|
+
declare function getDefaultOptionalMode(): TOptionalMode;
|
|
26
|
+
/**
|
|
27
|
+
* The {@link TOptionalMode} of an optional field: its own
|
|
28
|
+
* `@ui.form.optionalMode` ▸ `formMode` (the form's `optional-mode`) ▸ the
|
|
29
|
+
* app-wide default ▸ `"business"`. An object, array (multiselect and tags
|
|
30
|
+
* included), tuple or union field is always `"technical"`. Since 0.1.205.
|
|
31
|
+
*/
|
|
32
|
+
declare function resolveOptionalMode(field: FormFieldDef, formMode?: TOptionalMode): TOptionalMode;
|
|
33
|
+
/**
|
|
34
|
+
* The value an input of `field` writes when the user empties it. `mode` is the
|
|
35
|
+
* field's {@link resolveOptionalMode} (`"business"` when left out); `edit`
|
|
36
|
+
* whether the form edits a stored record (`createFormDef(type, { mode: "edit" })`,
|
|
37
|
+
* `FormDef.edit`; `false` when left out). Both matter for an optional field
|
|
38
|
+
* only. Since 0.1.205.
|
|
39
|
+
*
|
|
40
|
+
* An optional field:
|
|
41
|
+
* - `"technical"`, or a create form: `undefined` — the field is unset again and
|
|
42
|
+
* a create leaves it out, so column defaults apply;
|
|
43
|
+
* - `"business"` on an edit form: its empty value, so a whole-record update
|
|
44
|
+
* clears it too — `false` for a boolean, `""` for text whose type accepts
|
|
45
|
+
* `""`, else `null` (a number, a date, a selection, a reference or foreign
|
|
46
|
+
* key, and text whose type rejects `""`: a pattern, an e-mail, a minimum
|
|
47
|
+
* length, `@meta.required`);
|
|
48
|
+
* - a set-only field (`@db.writeOnly`): always `undefined` — an empty input
|
|
49
|
+
* keeps the stored secret.
|
|
50
|
+
*
|
|
51
|
+
* A required field: `""` for text and a string key, else `null`.
|
|
52
|
+
*/
|
|
53
|
+
declare function emptiedValue(field: FormFieldDef, opts?: {
|
|
54
|
+
mode?: TOptionalMode;
|
|
55
|
+
edit?: boolean;
|
|
56
|
+
}): unknown;
|
|
57
|
+
//#endregion
|
|
58
|
+
export { TOptionalMode, emptiedValue, getDefaultOptionalMode, resolveOptionalMode, setDefaultOptionalMode };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { FormFieldDef } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/form/optional-mode.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* How a form offers an optional scalar field (`name?: T`). Since 0.1.205.
|
|
6
|
+
*
|
|
7
|
+
* - `"business"` (default): a plain input, as for a required field — no unset
|
|
8
|
+
* "×", no "Not set" placeholder, no click-to-edit. Left untouched the field
|
|
9
|
+
* stays unset (a create leaves it out); emptied on an edit form it holds its
|
|
10
|
+
* empty value ({@link emptiedValue}).
|
|
11
|
+
* - `"technical"`: the tri-state — "Not set" until the user switches the field
|
|
12
|
+
* on, an unset "×" while it holds a value.
|
|
13
|
+
*
|
|
14
|
+
* Optional objects, arrays, tuples and unions always keep their "Add …" /
|
|
15
|
+
* remove controls (`"technical"`).
|
|
16
|
+
*/
|
|
17
|
+
type TOptionalMode = "business" | "technical";
|
|
18
|
+
/**
|
|
19
|
+
* Set the app-wide {@link TOptionalMode} — call it once at startup, next to
|
|
20
|
+
* `createDefaultTypes()`. A form's `optional-mode` prop and a field's
|
|
21
|
+
* `@ui.form.optionalMode` take precedence. Since 0.1.205.
|
|
22
|
+
*/
|
|
23
|
+
declare function setDefaultOptionalMode(mode: TOptionalMode): void;
|
|
24
|
+
/** The app-wide {@link TOptionalMode}: `"business"` unless {@link setDefaultOptionalMode} set another. Since 0.1.205. */
|
|
25
|
+
declare function getDefaultOptionalMode(): TOptionalMode;
|
|
26
|
+
/**
|
|
27
|
+
* The {@link TOptionalMode} of an optional field: its own
|
|
28
|
+
* `@ui.form.optionalMode` ▸ `formMode` (the form's `optional-mode`) ▸ the
|
|
29
|
+
* app-wide default ▸ `"business"`. An object, array (multiselect and tags
|
|
30
|
+
* included), tuple or union field is always `"technical"`. Since 0.1.205.
|
|
31
|
+
*/
|
|
32
|
+
declare function resolveOptionalMode(field: FormFieldDef, formMode?: TOptionalMode): TOptionalMode;
|
|
33
|
+
/**
|
|
34
|
+
* The value an input of `field` writes when the user empties it. `mode` is the
|
|
35
|
+
* field's {@link resolveOptionalMode} (`"business"` when left out); `edit`
|
|
36
|
+
* whether the form edits a stored record (`createFormDef(type, { mode: "edit" })`,
|
|
37
|
+
* `FormDef.edit`; `false` when left out). Both matter for an optional field
|
|
38
|
+
* only. Since 0.1.205.
|
|
39
|
+
*
|
|
40
|
+
* An optional field:
|
|
41
|
+
* - `"technical"`, or a create form: `undefined` — the field is unset again and
|
|
42
|
+
* a create leaves it out, so column defaults apply;
|
|
43
|
+
* - `"business"` on an edit form: its empty value, so a whole-record update
|
|
44
|
+
* clears it too — `false` for a boolean, `""` for text whose type accepts
|
|
45
|
+
* `""`, else `null` (a number, a date, a selection, a reference or foreign
|
|
46
|
+
* key, and text whose type rejects `""`: a pattern, an e-mail, a minimum
|
|
47
|
+
* length, `@meta.required`);
|
|
48
|
+
* - a set-only field (`@db.writeOnly`): always `undefined` — an empty input
|
|
49
|
+
* keeps the stored secret.
|
|
50
|
+
*
|
|
51
|
+
* A required field: `""` for text and a string key, else `null`.
|
|
52
|
+
*/
|
|
53
|
+
declare function emptiedValue(field: FormFieldDef, opts?: {
|
|
54
|
+
mode?: TOptionalMode;
|
|
55
|
+
edit?: boolean;
|
|
56
|
+
}): unknown;
|
|
57
|
+
//#endregion
|
|
58
|
+
export { TOptionalMode, emptiedValue, getDefaultOptionalMode, resolveOptionalMode, setDefaultOptionalMode };
|