@atscript/ui 0.1.162 → 0.1.164
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/client-factory.cjs +27 -0
- package/dist/client-factory.d.cts +53 -0
- package/dist/client-factory.d.mts +53 -0
- package/dist/client-factory.mjs +25 -0
- package/dist/form/apply.cjs +38 -0
- package/dist/form/apply.d.cts +36 -0
- package/dist/form/apply.d.mts +36 -0
- package/dist/form/apply.mjs +38 -0
- package/dist/form/clone.cjs +30 -0
- package/dist/form/clone.d.cts +23 -0
- package/dist/form/clone.d.mts +23 -0
- package/dist/form/clone.mjs +30 -0
- package/dist/form/create-form-def.cjs +440 -0
- package/dist/form/create-form-def.d.cts +67 -0
- package/dist/form/create-form-def.d.mts +67 -0
- package/dist/form/create-form-def.mjs +438 -0
- package/dist/form/decimal-format.cjs +324 -0
- package/dist/form/decimal-format.d.cts +86 -0
- package/dist/form/decimal-format.d.mts +86 -0
- package/dist/form/decimal-format.mjs +315 -0
- package/dist/form/diff.cjs +458 -0
- package/dist/form/diff.d.cts +80 -0
- package/dist/form/diff.d.mts +80 -0
- package/dist/form/diff.mjs +457 -0
- package/dist/form/dirty.cjs +65 -0
- package/dist/form/dirty.d.cts +47 -0
- package/dist/form/dirty.d.mts +47 -0
- package/dist/form/dirty.mjs +64 -0
- package/dist/form/error-utils.cjs +97 -0
- package/dist/form/error-utils.d.cts +58 -0
- package/dist/form/error-utils.d.mts +58 -0
- package/dist/form/error-utils.mjs +92 -0
- package/dist/form/form-actions.cjs +30 -0
- package/dist/form/form-actions.d.cts +17 -0
- package/dist/form/form-actions.d.mts +17 -0
- package/dist/form/form-actions.mjs +30 -0
- package/dist/form/grid.cjs +69 -0
- package/dist/form/grid.d.cts +49 -0
- package/dist/form/grid.d.mts +49 -0
- package/dist/form/grid.mjs +64 -0
- package/dist/form/labels.cjs +10 -0
- package/dist/form/labels.d.cts +7 -0
- package/dist/form/labels.d.mts +7 -0
- package/dist/form/labels.mjs +10 -0
- package/dist/form/measurement.cjs +20 -0
- package/dist/form/measurement.d.cts +29 -0
- package/dist/form/measurement.d.mts +29 -0
- package/dist/form/measurement.mjs +20 -0
- package/dist/form/path-utils.cjs +182 -0
- package/dist/form/path-utils.d.cts +47 -0
- package/dist/form/path-utils.d.mts +47 -0
- package/dist/form/path-utils.mjs +175 -0
- package/dist/form/rebase.cjs +89 -0
- package/dist/form/rebase.d.cts +61 -0
- package/dist/form/rebase.d.mts +61 -0
- package/dist/form/rebase.mjs +89 -0
- package/dist/form/skipped-nav.cjs +100 -0
- package/dist/form/skipped-nav.d.cts +13 -0
- package/dist/form/skipped-nav.d.mts +13 -0
- package/dist/form/skipped-nav.mjs +98 -0
- package/dist/form/types.cjs +40 -0
- package/dist/form/types.d.cts +133 -0
- package/dist/form/types.d.mts +133 -0
- package/dist/form/types.mjs +36 -0
- package/dist/form/union-detect.cjs +46 -0
- package/dist/form/union-detect.d.cts +22 -0
- package/dist/form/union-detect.d.mts +22 -0
- package/dist/form/union-detect.mjs +46 -0
- package/dist/form/validate.cjs +150 -0
- package/dist/form/validate.d.cts +50 -0
- package/dist/form/validate.d.mts +50 -0
- package/dist/form/validate.mjs +147 -0
- package/dist/index.cjs +217 -3360
- package/dist/index.d.cts +38 -1693
- package/dist/index.d.mts +38 -1693
- package/dist/index.mjs +35 -3179
- package/dist/nav/model-routes.cjs +59 -0
- package/dist/nav/model-routes.d.cts +33 -0
- package/dist/nav/model-routes.d.mts +33 -0
- package/dist/nav/model-routes.mjs +59 -0
- package/dist/plugin/annotations.cjs +577 -0
- package/dist/plugin/annotations.mjs +577 -0
- package/dist/plugin/primitives.cjs +30 -0
- package/dist/plugin/primitives.mjs +30 -0
- package/dist/plugin/value-help-validation.cjs +419 -0
- package/dist/plugin/value-help-validation.mjs +413 -0
- package/dist/plugin.cjs +4 -1001
- package/dist/plugin.mjs +2 -999
- package/dist/shared/annotation-keys.cjs +189 -0
- package/dist/shared/annotation-keys.d.cts +82 -0
- package/dist/shared/annotation-keys.d.mts +82 -0
- package/dist/shared/annotation-keys.mjs +97 -0
- package/dist/shared/field-resolver.cjs +123 -0
- package/dist/shared/field-resolver.d.cts +77 -0
- package/dist/shared/field-resolver.d.mts +77 -0
- package/dist/shared/field-resolver.mjs +110 -0
- package/dist/shared/meta-cache.cjs +244 -0
- package/dist/shared/meta-cache.d.cts +110 -0
- package/dist/shared/meta-cache.d.mts +110 -0
- package/dist/shared/meta-cache.mjs +237 -0
- package/dist/shared/str.cjs +19 -0
- package/dist/shared/str.d.cts +7 -0
- package/dist/shared/str.d.mts +7 -0
- package/dist/shared/str.mjs +17 -0
- package/dist/table/column-resolver.cjs +17 -0
- package/dist/table/column-resolver.d.cts +11 -0
- package/dist/table/column-resolver.d.mts +11 -0
- package/dist/table/column-resolver.mjs +15 -0
- package/dist/table/create-table-def.cjs +233 -0
- package/dist/table/create-table-def.d.cts +15 -0
- package/dist/table/create-table-def.d.mts +15 -0
- package/dist/table/create-table-def.mjs +233 -0
- package/dist/table/navigate-href.cjs +45 -0
- package/dist/table/navigate-href.d.cts +31 -0
- package/dist/table/navigate-href.d.mts +31 -0
- package/dist/table/navigate-href.mjs +45 -0
- package/dist/table/types.d.cts +287 -0
- package/dist/table/types.d.mts +287 -0
- package/dist/table/value-kind.cjs +20 -0
- package/dist/table/value-kind.d.cts +15 -0
- package/dist/table/value-kind.d.mts +15 -0
- package/dist/table/value-kind.mjs +19 -0
- package/dist/value-help/dict-paths.cjs +17 -0
- package/dist/value-help/dict-paths.d.cts +11 -0
- package/dist/value-help/dict-paths.d.mts +11 -0
- package/dist/value-help/dict-paths.mjs +17 -0
- package/dist/value-help/distinct-client.cjs +51 -0
- package/dist/value-help/distinct-client.d.cts +40 -0
- package/dist/value-help/distinct-client.d.mts +40 -0
- package/dist/value-help/distinct-client.mjs +50 -0
- package/dist/value-help/extract-literals.cjs +76 -0
- package/dist/value-help/extract-literals.d.cts +32 -0
- package/dist/value-help/extract-literals.d.mts +32 -0
- package/dist/value-help/extract-literals.mjs +74 -0
- package/dist/value-help/extract-ref.cjs +100 -0
- package/dist/value-help/extract-ref.d.cts +37 -0
- package/dist/value-help/extract-ref.d.mts +37 -0
- package/dist/value-help/extract-ref.mjs +100 -0
- package/dist/value-help/filter-tree.cjs +45 -0
- package/dist/value-help/filter-tree.mjs +45 -0
- package/dist/value-help/index.d.mts +8 -0
- package/dist/value-help/resolve-options.cjs +64 -0
- package/dist/value-help/resolve-options.d.cts +35 -0
- package/dist/value-help/resolve-options.d.mts +35 -0
- package/dist/value-help/resolve-options.mjs +60 -0
- package/dist/value-help/resolve.cjs +62 -0
- package/dist/value-help/resolve.d.cts +31 -0
- package/dist/value-help/resolve.d.mts +31 -0
- package/dist/value-help/resolve.mjs +61 -0
- package/dist/value-help/types.d.cts +36 -0
- package/dist/value-help/types.d.mts +36 -0
- package/dist/value-help/value-help-client.cjs +136 -0
- package/dist/value-help/value-help-client.d.cts +67 -0
- package/dist/value-help/value-help-client.d.mts +67 -0
- package/dist/value-help/value-help-client.mjs +134 -0
- package/package.json +10 -10
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
const require_annotation_keys = require("../shared/annotation-keys.cjs");
|
|
2
|
+
const require_field_resolver = require("../shared/field-resolver.cjs");
|
|
3
|
+
const require_extract_literals = require("../value-help/extract-literals.cjs");
|
|
4
|
+
const require_extract_ref = require("../value-help/extract-ref.cjs");
|
|
5
|
+
const require_skipped_nav = require("./skipped-nav.cjs");
|
|
6
|
+
let _atscript_typescript_utils = require("@atscript/typescript/utils");
|
|
7
|
+
let _atscript_db_validator = require("@atscript/db/validator");
|
|
8
|
+
//#region src/form/create-form-def.ts
|
|
9
|
+
/** Known atscript primitive extension tags that map directly to field types. */
|
|
10
|
+
const UI_TAGS = new Set([
|
|
11
|
+
"action",
|
|
12
|
+
"paragraph",
|
|
13
|
+
"select",
|
|
14
|
+
"radio",
|
|
15
|
+
"checkbox"
|
|
16
|
+
]);
|
|
17
|
+
/**
|
|
18
|
+
* The object defs, and the array / union / tuple field defs, under
|
|
19
|
+
* construction (innermost last). A recursive type reached again inside its own
|
|
20
|
+
* build reuses the def being built, so the def graph gets a cycle instead of
|
|
21
|
+
* an endless expansion: a tree's `children` items are the same def at every
|
|
22
|
+
* depth, built once. A field def is reused only at the same path relative to
|
|
23
|
+
* its object def, so a reused field always sits where it was built. Both are
|
|
24
|
+
* per mode (`edit`), which shapes the defs.
|
|
25
|
+
*/
|
|
26
|
+
const defsInProgress = [];
|
|
27
|
+
const fieldsInProgress = [];
|
|
28
|
+
/**
|
|
29
|
+
* `field` without its name (a tuple item). A field def still under
|
|
30
|
+
* construction (a recursion back to it) gets a copy filled when it is built.
|
|
31
|
+
*/
|
|
32
|
+
function unnamed(field) {
|
|
33
|
+
const building = fieldsInProgress.find((e) => e.field === field);
|
|
34
|
+
if (!building) return {
|
|
35
|
+
...field,
|
|
36
|
+
name: ""
|
|
37
|
+
};
|
|
38
|
+
const copy = { name: "" };
|
|
39
|
+
building.copies.push(copy);
|
|
40
|
+
return copy;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Converts an ATScript annotated type into a FormDef.
|
|
44
|
+
*
|
|
45
|
+
* - **Object types** (`kind === 'object'`): produces an object root with nested fields.
|
|
46
|
+
* - **Non-object types** (primitive, array, union, etc.): produces a single leaf root field
|
|
47
|
+
* with `path: ''`.
|
|
48
|
+
*
|
|
49
|
+
* A `@db.column.derived` field is marked `derived` straight from its metadata;
|
|
50
|
+
* a computed view column, from `opts.metaFields`; an update-stamped field
|
|
51
|
+
* (see `CreateFormDefOptions.mode`), when `opts.mode` is `"edit"`.
|
|
52
|
+
*
|
|
53
|
+
* A navigation field (`@db.rel.from` / `.to` / `.via`) is left out unless it
|
|
54
|
+
* carries a `@ui.form.*` annotation of its own (see {@link isSkippedNavField}).
|
|
55
|
+
*
|
|
56
|
+
* Recursive types are safe at any depth: a type reached again inside its own
|
|
57
|
+
* build reuses the def being built (the def graph is cyclic, not infinite).
|
|
58
|
+
* A recursive object that would otherwise render inline (no `@ui.form.*`)
|
|
59
|
+
* becomes a nested object field, since inlining it would never end.
|
|
60
|
+
*/
|
|
61
|
+
function createFormDef(type, opts) {
|
|
62
|
+
return buildFormDef(type, opts?.mode === "edit", opts ?? {});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The def of an object (or a leaf) type. `root`: the caller's options, for the
|
|
66
|
+
* form's own type only — nested objects get none of the root-table ones.
|
|
67
|
+
*/
|
|
68
|
+
function buildFormDef(type, edit, root) {
|
|
69
|
+
if (type.type.kind !== "object") {
|
|
70
|
+
const rootField = createFieldDef("", type, edit);
|
|
71
|
+
const fields = [rootField];
|
|
72
|
+
return {
|
|
73
|
+
type,
|
|
74
|
+
rootField,
|
|
75
|
+
fields,
|
|
76
|
+
mainFields: fields,
|
|
77
|
+
pushDownFields: [],
|
|
78
|
+
flatMap: /* @__PURE__ */ new Map()
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (!root) {
|
|
82
|
+
const reached = defsInProgress.find((e) => e.type === type && e.edit === edit);
|
|
83
|
+
if (reached) return reached.def;
|
|
84
|
+
}
|
|
85
|
+
const objectType = type;
|
|
86
|
+
const flatMap = (0, _atscript_typescript_utils.flattenAnnotatedType)(objectType, { excludePhantomTypes: false });
|
|
87
|
+
const opts = root && {
|
|
88
|
+
...root,
|
|
89
|
+
versionColumn: root.versionColumn ?? (edit ? findVersionColumn(flatMap) : void 0)
|
|
90
|
+
};
|
|
91
|
+
const fields = [];
|
|
92
|
+
const rootField = {
|
|
93
|
+
path: "",
|
|
94
|
+
prop: type,
|
|
95
|
+
type: "object",
|
|
96
|
+
phantom: false,
|
|
97
|
+
name: "",
|
|
98
|
+
allStatic: false,
|
|
99
|
+
pushDown: false
|
|
100
|
+
};
|
|
101
|
+
const def = {
|
|
102
|
+
type,
|
|
103
|
+
rootField,
|
|
104
|
+
fields,
|
|
105
|
+
mainFields: fields,
|
|
106
|
+
pushDownFields: [],
|
|
107
|
+
flatMap
|
|
108
|
+
};
|
|
109
|
+
rootField.objectDef = def;
|
|
110
|
+
let collected;
|
|
111
|
+
if (!root) defsInProgress.push({
|
|
112
|
+
type,
|
|
113
|
+
edit,
|
|
114
|
+
def
|
|
115
|
+
});
|
|
116
|
+
try {
|
|
117
|
+
collected = collectFields(objectType, flatMap, edit, opts);
|
|
118
|
+
} finally {
|
|
119
|
+
if (!root) defsInProgress.pop();
|
|
120
|
+
}
|
|
121
|
+
collected.sort((a, b) => a.order - b.order);
|
|
122
|
+
for (const c of collected) fields.push(c.field);
|
|
123
|
+
if (collected.some((c) => c.pushDown)) {
|
|
124
|
+
def.mainFields = [];
|
|
125
|
+
for (const c of collected) (c.pushDown ? def.pushDownFields : def.mainFields).push(c.field);
|
|
126
|
+
}
|
|
127
|
+
return def;
|
|
128
|
+
}
|
|
129
|
+
/** Builds the top-level fields of an object def from its flattened props, unsorted. */
|
|
130
|
+
function collectFields(objectType, flatMap, edit, opts) {
|
|
131
|
+
const fields = [];
|
|
132
|
+
const structuredPrefixes = /* @__PURE__ */ new Set();
|
|
133
|
+
for (const [path, prop] of flatMap.entries()) {
|
|
134
|
+
if (path === "") continue;
|
|
135
|
+
if (path === opts?.versionColumn) continue;
|
|
136
|
+
if (isChildOfStructured(path, structuredPrefixes)) continue;
|
|
137
|
+
const originalProp = resolveOriginalProp(objectType, path) ?? prop;
|
|
138
|
+
const kind = originalProp.type.kind;
|
|
139
|
+
if (require_skipped_nav.isSkippedNavField(originalProp)) {
|
|
140
|
+
structuredPrefixes.add(path + ".");
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (kind === "object" && !isStructuredObject(originalProp) && !(0, _atscript_db_validator.isNavRelation)(originalProp) && !isUnexpandedObject(path, originalProp, flatMap)) continue;
|
|
144
|
+
if (kind === "array") {
|
|
145
|
+
if (originalProp.type.of.type.kind === "array" && !require_field_resolver.getFieldMeta(originalProp, "ui.form.component")) continue;
|
|
146
|
+
}
|
|
147
|
+
if (kind === "array" || kind === "object" || kind === "union" || kind === "tuple") structuredPrefixes.add(path + ".");
|
|
148
|
+
const field = createFieldDef(path, originalProp, edit);
|
|
149
|
+
fields.push({
|
|
150
|
+
field: opts?.metaFields?.[path]?.computed ? {
|
|
151
|
+
...field,
|
|
152
|
+
derived: true
|
|
153
|
+
} : field,
|
|
154
|
+
order: require_field_resolver.getFieldMeta(originalProp, "ui.form.order") ?? Infinity,
|
|
155
|
+
pushDown: require_field_resolver.getFieldMeta(originalProp, require_annotation_keys.UI_FORM_PUSH_DOWN) !== void 0
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return fields;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Creates a FormFieldDef from any ATScript annotated type. `edit`: the form
|
|
162
|
+
* edits a stored row (`CreateFormDefOptions.mode`).
|
|
163
|
+
*
|
|
164
|
+
* An array, union or tuple reached again at the same path inside its own
|
|
165
|
+
* build (an array item, a union variant or a tuple item of a recursive alias)
|
|
166
|
+
* gets the field def under construction. Every other recursion passes an
|
|
167
|
+
* object def, which `defsInProgress` catches, so the build always ends.
|
|
168
|
+
*/
|
|
169
|
+
function createFieldDef(path, prop, edit) {
|
|
170
|
+
const kind = prop.type.kind;
|
|
171
|
+
if (kind === "" || kind === "object") return buildFieldDef(path, prop, edit);
|
|
172
|
+
const reached = fieldsInProgress.find((e) => e.prop === prop && e.path === path && e.edit === edit);
|
|
173
|
+
if (reached) return reached.field;
|
|
174
|
+
const building = {
|
|
175
|
+
prop,
|
|
176
|
+
path,
|
|
177
|
+
edit,
|
|
178
|
+
field: {},
|
|
179
|
+
copies: []
|
|
180
|
+
};
|
|
181
|
+
fieldsInProgress.push(building);
|
|
182
|
+
try {
|
|
183
|
+
const field = Object.assign(building.field, buildFieldDef(path, prop, edit));
|
|
184
|
+
for (const copy of building.copies) Object.assign(copy, field, { name: "" });
|
|
185
|
+
return field;
|
|
186
|
+
} finally {
|
|
187
|
+
fieldsInProgress.pop();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function buildFieldDef(path, prop, edit) {
|
|
191
|
+
const kind = prop.type.kind;
|
|
192
|
+
const name = path.slice(path.lastIndexOf(".") + 1);
|
|
193
|
+
const allStatic = !require_field_resolver.hasComputedAnnotations(prop);
|
|
194
|
+
const declaredType = require_field_resolver.getFieldMeta(prop, "ui.form.type") ?? require_field_resolver.getFieldMeta(prop, "ui.type");
|
|
195
|
+
const uiType = declaredType === "tags" && kind !== "array" ? void 0 : declaredType;
|
|
196
|
+
const pushDown = require_field_resolver.getFieldMeta(prop, require_annotation_keys.UI_FORM_PUSH_DOWN) !== void 0;
|
|
197
|
+
const valueHelpInfo = require_extract_ref.extractValueHelp(prop);
|
|
198
|
+
const derived = require_field_resolver.getFieldMeta(prop, "db.column.derived") !== void 0 || edit && path !== "" && isUpdateStamped(prop);
|
|
199
|
+
const base = {
|
|
200
|
+
path,
|
|
201
|
+
prop,
|
|
202
|
+
phantom: false,
|
|
203
|
+
name,
|
|
204
|
+
allStatic,
|
|
205
|
+
pushDown,
|
|
206
|
+
valueHelpInfo,
|
|
207
|
+
...derived && { derived: true }
|
|
208
|
+
};
|
|
209
|
+
const customType = uiType;
|
|
210
|
+
if (kind === "array") {
|
|
211
|
+
const arrayType = prop.type;
|
|
212
|
+
const hasOptions = require_field_resolver.getFieldMeta(prop, "ui.form.options") !== void 0 || require_field_resolver.getFieldMeta(prop, "ui.form.fn.options") !== void 0 || require_extract_literals.isPureLiteralUnion(arrayType.of);
|
|
213
|
+
const isTags = customType === "tags" || customType === "multiselect" && !hasOptions;
|
|
214
|
+
const isMultiselect = customType === "multiselect" || hasOptions;
|
|
215
|
+
return {
|
|
216
|
+
...base,
|
|
217
|
+
type: "array",
|
|
218
|
+
customType: isTags ? "tags" : isMultiselect ? "multiselect" : customType,
|
|
219
|
+
itemType: arrayType.of,
|
|
220
|
+
itemField: createFieldDef("", arrayType.of, edit)
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
if (kind === "object") return {
|
|
224
|
+
...base,
|
|
225
|
+
type: "object",
|
|
226
|
+
customType,
|
|
227
|
+
objectDef: buildFormDef(prop, edit)
|
|
228
|
+
};
|
|
229
|
+
if (kind === "union") {
|
|
230
|
+
if (require_extract_literals.isPureLiteralUnion(prop)) return {
|
|
231
|
+
...base,
|
|
232
|
+
type: uiType ?? "select"
|
|
233
|
+
};
|
|
234
|
+
const unionVariants = createUnionVariants(prop, edit && soleValueMember(prop) !== void 0);
|
|
235
|
+
if (unionVariants.length > 1) return {
|
|
236
|
+
...base,
|
|
237
|
+
type: "union",
|
|
238
|
+
customType,
|
|
239
|
+
unionVariants
|
|
240
|
+
};
|
|
241
|
+
const v = unionVariants[0];
|
|
242
|
+
if (v?.itemField) return {
|
|
243
|
+
...v.itemField,
|
|
244
|
+
path,
|
|
245
|
+
name,
|
|
246
|
+
allStatic,
|
|
247
|
+
pushDown,
|
|
248
|
+
...derived && { derived: true }
|
|
249
|
+
};
|
|
250
|
+
if (v?.def) return {
|
|
251
|
+
...base,
|
|
252
|
+
type: "object",
|
|
253
|
+
customType,
|
|
254
|
+
objectDef: v.def
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
if (kind === "tuple") {
|
|
258
|
+
const tupleType = prop.type;
|
|
259
|
+
return {
|
|
260
|
+
...base,
|
|
261
|
+
type: "tuple",
|
|
262
|
+
customType,
|
|
263
|
+
itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item, false)))
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
if (valueHelpInfo) return {
|
|
267
|
+
...base,
|
|
268
|
+
type: uiType ?? "ref"
|
|
269
|
+
};
|
|
270
|
+
const tags = kind === "" ? prop.type.tags : void 0;
|
|
271
|
+
let uiTag;
|
|
272
|
+
let isTimestamp = false;
|
|
273
|
+
if (tags) for (const t of tags) {
|
|
274
|
+
if (uiTag === void 0 && UI_TAGS.has(t)) uiTag = t;
|
|
275
|
+
if (t === "timestamp") isTimestamp = true;
|
|
276
|
+
}
|
|
277
|
+
const dt = kind === "" ? prop.type.designType : void 0;
|
|
278
|
+
let numericType;
|
|
279
|
+
if (kind === "") {
|
|
280
|
+
const hasCurrency = require_field_resolver.getFieldMeta(prop, "db.amount.currency") !== void 0 || require_field_resolver.getFieldMeta(prop, "db.amount.currency.ref") !== void 0;
|
|
281
|
+
const isDecimalDesign = dt === "decimal";
|
|
282
|
+
if (hasCurrency) numericType = "decimal";
|
|
283
|
+
else if (isDecimalDesign) numericType = "decimal";
|
|
284
|
+
else if (dt === "number") {
|
|
285
|
+
const hasUnit = require_field_resolver.getFieldMeta(prop, "db.unit") !== void 0 || require_field_resolver.getFieldMeta(prop, "db.unit.ref") !== void 0;
|
|
286
|
+
const hasPrefix = require_field_resolver.getFieldMeta(prop, "ui.form.prefix") !== void 0 || require_field_resolver.getFieldMeta(prop, "ui.form.prefix.ref") !== void 0 || require_field_resolver.getFieldMeta(prop, "ui.form.prefix.icon") !== void 0;
|
|
287
|
+
const hasSuffix = require_field_resolver.getFieldMeta(prop, "ui.form.suffix") !== void 0 || require_field_resolver.getFieldMeta(prop, "ui.form.suffix.ref") !== void 0 || require_field_resolver.getFieldMeta(prop, "ui.form.suffix.icon") !== void 0;
|
|
288
|
+
if (hasUnit || hasPrefix || hasSuffix) numericType = "number";
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const tagType = isTimestamp ? "datetime" : void 0;
|
|
292
|
+
return {
|
|
293
|
+
...base,
|
|
294
|
+
type: uiType ?? uiTag ?? numericType ?? tagType ?? (dt === "number" ? "number" : dt === "boolean" ? "checkbox" : "text"),
|
|
295
|
+
phantom: kind === "" && dt === "phantom"
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
/** Non-`ui.form.*` keys that still address the object itself. */
|
|
299
|
+
const OBJECT_IDENTITY_KEYS = new Set([
|
|
300
|
+
require_annotation_keys.META_LABEL,
|
|
301
|
+
require_annotation_keys.META_DESCRIPTION,
|
|
302
|
+
require_annotation_keys.UI_TYPE
|
|
303
|
+
]);
|
|
304
|
+
/** `ui.form.*` keys that place a field rather than give it an identity. */
|
|
305
|
+
const UI_FORM_PLACEMENT_NS = "ui.form.grid.";
|
|
306
|
+
/**
|
|
307
|
+
* Whether an object prop renders as a field of its own rather than being
|
|
308
|
+
* flattened into its parent.
|
|
309
|
+
*
|
|
310
|
+
* Any `@ui.form.*` annotation (plus `@meta.label` / `@meta.description` /
|
|
311
|
+
* `@ui.type`) means the author addressed the object itself, so it needs a
|
|
312
|
+
* field to carry that instruction to a renderer — a collapsible section, or
|
|
313
|
+
* whatever `@ui.form.component` / `@ui.form.type` names. An object with none
|
|
314
|
+
* of them is a pure namespace: it is dropped and its children render inline
|
|
315
|
+
* in the parent grid, which keeps DB-flattened structs (`addr.street` stored
|
|
316
|
+
* as `addr__street`) from growing chrome nobody asked for. `@db.*`,
|
|
317
|
+
* `@expect.*` and the rest of `@meta.*` stay out for exactly that reason —
|
|
318
|
+
* they are what such a struct carries.
|
|
319
|
+
*
|
|
320
|
+
* Placement keys (`@ui.form.order`, `@ui.form.grid.*`) are excluded: they
|
|
321
|
+
* position a field among its siblings, and a namespace has no position of its
|
|
322
|
+
* own — its children carry their own.
|
|
323
|
+
*
|
|
324
|
+
* A prefix rule rather than a list because the list was wrong the moment it
|
|
325
|
+
* was written. Matching on `@meta.label` and `@ui.form.component` alone left
|
|
326
|
+
* every other key to be read off a prop that never became a field: a
|
|
327
|
+
* `@meta.description` went missing, a `@ui.form.type` component silently
|
|
328
|
+
* never mounted, `@ui.form.pushDown` never pushed down, and — the one that
|
|
329
|
+
* mattered — `@ui.form.hidden` on a struct left every child on screen.
|
|
330
|
+
*/
|
|
331
|
+
function isStructuredObject(prop) {
|
|
332
|
+
for (const key of prop.metadata.keys()) {
|
|
333
|
+
if (OBJECT_IDENTITY_KEYS.has(key)) return true;
|
|
334
|
+
if (!key.startsWith("ui.form.")) continue;
|
|
335
|
+
if (key === "ui.form.order" || key.startsWith(UI_FORM_PLACEMENT_NS)) continue;
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
/** The top-level `@db.column.version` field of a flattened type, if any. */
|
|
341
|
+
function findVersionColumn(flatMap) {
|
|
342
|
+
for (const [path, prop] of flatMap) if (path && !path.includes(".") && prop.metadata.has("db.column.version")) return path;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* An object prop that `flattenAnnotatedType` listed without expanding it: a
|
|
346
|
+
* recursive type reached again. Its children are absent from the flat map.
|
|
347
|
+
*/
|
|
348
|
+
function isUnexpandedObject(path, prop, flatMap) {
|
|
349
|
+
const first = prop.type.props.keys().next();
|
|
350
|
+
return !first.done && !flatMap.has(`${path}.${first.value}`);
|
|
351
|
+
}
|
|
352
|
+
/** Resolves the original annotated type from the type hierarchy by path. */
|
|
353
|
+
function resolveOriginalProp(type, path) {
|
|
354
|
+
const parts = path.split(".");
|
|
355
|
+
let current = type.type;
|
|
356
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
357
|
+
const prop = current.props.get(parts[i]);
|
|
358
|
+
if (!prop || prop.type.kind !== "object") return void 0;
|
|
359
|
+
current = prop.type;
|
|
360
|
+
}
|
|
361
|
+
return current.props.get(parts[parts.length - 1]);
|
|
362
|
+
}
|
|
363
|
+
/** Check if a path is a child of any structured prefix (prefixes are pre-suffixed with "."). */
|
|
364
|
+
function isChildOfStructured(path, prefixes) {
|
|
365
|
+
for (const prefix of prefixes) if (path.startsWith(prefix)) return true;
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Builds union variant definitions from a union annotated type.
|
|
370
|
+
* Iterates top-level items directly — one variant per item.
|
|
371
|
+
*/
|
|
372
|
+
function buildUnionVariants(typeDef) {
|
|
373
|
+
return createUnionVariants(typeDef, false);
|
|
374
|
+
}
|
|
375
|
+
function createUnionVariants(typeDef, edit) {
|
|
376
|
+
const items = typeDef.type.items ?? [typeDef];
|
|
377
|
+
const variants = [];
|
|
378
|
+
for (const item of items) {
|
|
379
|
+
const v = createVariant(item, edit);
|
|
380
|
+
if (items.length > 1) v.label = `${String(variants.length + 1)}. ${v.label}`;
|
|
381
|
+
variants.push(v);
|
|
382
|
+
}
|
|
383
|
+
return variants;
|
|
384
|
+
}
|
|
385
|
+
/** Creates a single union variant from an annotated type item. */
|
|
386
|
+
function createVariant(def, edit) {
|
|
387
|
+
const kind = def.type.kind;
|
|
388
|
+
if (kind === "object") {
|
|
389
|
+
const label = require_field_resolver.getFieldMeta(def, "meta.label") ?? "Object";
|
|
390
|
+
const hasComponent = require_field_resolver.getFieldMeta(def, require_annotation_keys.UI_FORM_COMPONENT) !== void 0;
|
|
391
|
+
return {
|
|
392
|
+
label,
|
|
393
|
+
type: def,
|
|
394
|
+
def: buildFormDef(def, edit),
|
|
395
|
+
itemField: hasComponent ? createFieldDef("", def, edit) : void 0
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
if (kind === "") {
|
|
399
|
+
const dt = def.type.designType;
|
|
400
|
+
return {
|
|
401
|
+
label: capitalize(dt === "phantom" ? "item" : dt),
|
|
402
|
+
type: def,
|
|
403
|
+
itemField: createFieldDef("", def, edit),
|
|
404
|
+
designType: dt
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
return {
|
|
408
|
+
label: capitalize(kind),
|
|
409
|
+
type: def,
|
|
410
|
+
itemField: createFieldDef("", def, edit)
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* An update-stamped field (see `CreateFormDefOptions.mode`). Read off the prop,
|
|
415
|
+
* or — as `@atscript/db` does — off the one value member of `T | null`, unless
|
|
416
|
+
* the prop references another field.
|
|
417
|
+
*/
|
|
418
|
+
function isUpdateStamped(prop) {
|
|
419
|
+
if ((0, _atscript_db_validator.isServerStampedOnUpdate)(prop.metadata)) return true;
|
|
420
|
+
const member = prop.ref?.field ? void 0 : soleValueMember(prop);
|
|
421
|
+
return member !== void 0 && (0, _atscript_db_validator.isServerStampedOnUpdate)(member.metadata);
|
|
422
|
+
}
|
|
423
|
+
/** The one non-`null` / `undefined` member of a (nested) union, if there is exactly one. */
|
|
424
|
+
function soleValueMember(prop) {
|
|
425
|
+
if (prop.type.kind !== "union") return void 0;
|
|
426
|
+
const members = /* @__PURE__ */ new Set();
|
|
427
|
+
const visit = (t) => {
|
|
428
|
+
if (t.type.kind === "union") for (const item of t.type.items) visit(item);
|
|
429
|
+
else if (t.type.kind !== "" || t.type.designType !== "null" && t.type.designType !== "undefined") members.add(t);
|
|
430
|
+
};
|
|
431
|
+
visit(prop);
|
|
432
|
+
return members.size === 1 ? members.values().next().value : void 0;
|
|
433
|
+
}
|
|
434
|
+
function capitalize(s) {
|
|
435
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
436
|
+
}
|
|
437
|
+
//#endregion
|
|
438
|
+
exports.buildUnionVariants = buildUnionVariants;
|
|
439
|
+
exports.createFormDef = createFormDef;
|
|
440
|
+
exports.findVersionColumn = findVersionColumn;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { FormDef, FormUnionVariant } from "./types.cjs";
|
|
2
|
+
import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
|
|
3
|
+
|
|
4
|
+
//#region src/form/create-form-def.d.ts
|
|
5
|
+
/** Options for {@link createFormDef}. */
|
|
6
|
+
interface CreateFormDefOptions {
|
|
7
|
+
/**
|
|
8
|
+
* `meta.versionColumn` — the server-managed OCC version field (its LOGICAL
|
|
9
|
+
* name; a `@db.column` rename changes only the storage column). Excluded
|
|
10
|
+
* from `fields[]`; it stays in `flatMap` so the wire payload is unchanged.
|
|
11
|
+
* Root-table concept; not propagated into nested recursive calls. With
|
|
12
|
+
* `mode: "edit"` it defaults to the top-level `@db.column.version` field.
|
|
13
|
+
*/
|
|
14
|
+
versionColumn?: string;
|
|
15
|
+
/**
|
|
16
|
+
* `meta.fields` of the `/meta` the type came from. A field it marks
|
|
17
|
+
* `computed` (a computed view column, whose `@db.compute` annotation the
|
|
18
|
+
* server does not send) is marked `derived` like a `@db.column.derived`
|
|
19
|
+
* one: read-only, never validated, never sent. Root-table concept; not
|
|
20
|
+
* propagated into nested recursive calls. Since 0.1.147.
|
|
21
|
+
*/
|
|
22
|
+
metaFields?: Record<string, {
|
|
23
|
+
computed?: boolean;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* What the form is for. `"edit"` — the form edits a stored row and is sent
|
|
27
|
+
* as an update: an `@db.onUpdate.now` field (`number.timestamp.updated`) or
|
|
28
|
+
* an `@db.onUpdate.inc` change counter, which the server sets on every
|
|
29
|
+
* update and whose sent value it ignores, is then marked `derived` —
|
|
30
|
+
* read-only, never validated, never sent. Applies at every depth (embedded
|
|
31
|
+
* objects, objects in arrays), except for a reference to another field, an
|
|
32
|
+
* element of an array of numbers, and below a tuple or a union of several
|
|
33
|
+
* types, where the server does not set the field (`@db.onUpdate.inc` is
|
|
34
|
+
* top-level only). `"create"` (default) — the field stays editable: the
|
|
35
|
+
* server only fills it when the value is left out and it has
|
|
36
|
+
* `@db.default.now`, or is an `@db.onUpdate.inc` counter (`0`). Since
|
|
37
|
+
* 0.1.154; `@db.onUpdate.inc` since 0.1.159.
|
|
38
|
+
*/
|
|
39
|
+
mode?: "create" | "edit";
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Converts an ATScript annotated type into a FormDef.
|
|
43
|
+
*
|
|
44
|
+
* - **Object types** (`kind === 'object'`): produces an object root with nested fields.
|
|
45
|
+
* - **Non-object types** (primitive, array, union, etc.): produces a single leaf root field
|
|
46
|
+
* with `path: ''`.
|
|
47
|
+
*
|
|
48
|
+
* A `@db.column.derived` field is marked `derived` straight from its metadata;
|
|
49
|
+
* a computed view column, from `opts.metaFields`; an update-stamped field
|
|
50
|
+
* (see `CreateFormDefOptions.mode`), when `opts.mode` is `"edit"`.
|
|
51
|
+
*
|
|
52
|
+
* A navigation field (`@db.rel.from` / `.to` / `.via`) is left out unless it
|
|
53
|
+
* carries a `@ui.form.*` annotation of its own (see {@link isSkippedNavField}).
|
|
54
|
+
*
|
|
55
|
+
* Recursive types are safe at any depth: a type reached again inside its own
|
|
56
|
+
* build reuses the def being built (the def graph is cyclic, not infinite).
|
|
57
|
+
* A recursive object that would otherwise render inline (no `@ui.form.*`)
|
|
58
|
+
* becomes a nested object field, since inlining it would never end.
|
|
59
|
+
*/
|
|
60
|
+
declare function createFormDef(type: TAtscriptAnnotatedType, opts?: CreateFormDefOptions): FormDef;
|
|
61
|
+
/**
|
|
62
|
+
* Builds union variant definitions from a union annotated type.
|
|
63
|
+
* Iterates top-level items directly — one variant per item.
|
|
64
|
+
*/
|
|
65
|
+
declare function buildUnionVariants(typeDef: TAtscriptAnnotatedType): FormUnionVariant[];
|
|
66
|
+
//#endregion
|
|
67
|
+
export { CreateFormDefOptions, buildUnionVariants, createFormDef };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { FormDef, FormUnionVariant } from "./types.mjs";
|
|
2
|
+
import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
|
|
3
|
+
|
|
4
|
+
//#region src/form/create-form-def.d.ts
|
|
5
|
+
/** Options for {@link createFormDef}. */
|
|
6
|
+
interface CreateFormDefOptions {
|
|
7
|
+
/**
|
|
8
|
+
* `meta.versionColumn` — the server-managed OCC version field (its LOGICAL
|
|
9
|
+
* name; a `@db.column` rename changes only the storage column). Excluded
|
|
10
|
+
* from `fields[]`; it stays in `flatMap` so the wire payload is unchanged.
|
|
11
|
+
* Root-table concept; not propagated into nested recursive calls. With
|
|
12
|
+
* `mode: "edit"` it defaults to the top-level `@db.column.version` field.
|
|
13
|
+
*/
|
|
14
|
+
versionColumn?: string;
|
|
15
|
+
/**
|
|
16
|
+
* `meta.fields` of the `/meta` the type came from. A field it marks
|
|
17
|
+
* `computed` (a computed view column, whose `@db.compute` annotation the
|
|
18
|
+
* server does not send) is marked `derived` like a `@db.column.derived`
|
|
19
|
+
* one: read-only, never validated, never sent. Root-table concept; not
|
|
20
|
+
* propagated into nested recursive calls. Since 0.1.147.
|
|
21
|
+
*/
|
|
22
|
+
metaFields?: Record<string, {
|
|
23
|
+
computed?: boolean;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* What the form is for. `"edit"` — the form edits a stored row and is sent
|
|
27
|
+
* as an update: an `@db.onUpdate.now` field (`number.timestamp.updated`) or
|
|
28
|
+
* an `@db.onUpdate.inc` change counter, which the server sets on every
|
|
29
|
+
* update and whose sent value it ignores, is then marked `derived` —
|
|
30
|
+
* read-only, never validated, never sent. Applies at every depth (embedded
|
|
31
|
+
* objects, objects in arrays), except for a reference to another field, an
|
|
32
|
+
* element of an array of numbers, and below a tuple or a union of several
|
|
33
|
+
* types, where the server does not set the field (`@db.onUpdate.inc` is
|
|
34
|
+
* top-level only). `"create"` (default) — the field stays editable: the
|
|
35
|
+
* server only fills it when the value is left out and it has
|
|
36
|
+
* `@db.default.now`, or is an `@db.onUpdate.inc` counter (`0`). Since
|
|
37
|
+
* 0.1.154; `@db.onUpdate.inc` since 0.1.159.
|
|
38
|
+
*/
|
|
39
|
+
mode?: "create" | "edit";
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Converts an ATScript annotated type into a FormDef.
|
|
43
|
+
*
|
|
44
|
+
* - **Object types** (`kind === 'object'`): produces an object root with nested fields.
|
|
45
|
+
* - **Non-object types** (primitive, array, union, etc.): produces a single leaf root field
|
|
46
|
+
* with `path: ''`.
|
|
47
|
+
*
|
|
48
|
+
* A `@db.column.derived` field is marked `derived` straight from its metadata;
|
|
49
|
+
* a computed view column, from `opts.metaFields`; an update-stamped field
|
|
50
|
+
* (see `CreateFormDefOptions.mode`), when `opts.mode` is `"edit"`.
|
|
51
|
+
*
|
|
52
|
+
* A navigation field (`@db.rel.from` / `.to` / `.via`) is left out unless it
|
|
53
|
+
* carries a `@ui.form.*` annotation of its own (see {@link isSkippedNavField}).
|
|
54
|
+
*
|
|
55
|
+
* Recursive types are safe at any depth: a type reached again inside its own
|
|
56
|
+
* build reuses the def being built (the def graph is cyclic, not infinite).
|
|
57
|
+
* A recursive object that would otherwise render inline (no `@ui.form.*`)
|
|
58
|
+
* becomes a nested object field, since inlining it would never end.
|
|
59
|
+
*/
|
|
60
|
+
declare function createFormDef(type: TAtscriptAnnotatedType, opts?: CreateFormDefOptions): FormDef;
|
|
61
|
+
/**
|
|
62
|
+
* Builds union variant definitions from a union annotated type.
|
|
63
|
+
* Iterates top-level items directly — one variant per item.
|
|
64
|
+
*/
|
|
65
|
+
declare function buildUnionVariants(typeDef: TAtscriptAnnotatedType): FormUnionVariant[];
|
|
66
|
+
//#endregion
|
|
67
|
+
export { CreateFormDefOptions, buildUnionVariants, createFormDef };
|