@atscript/ui 0.1.163 → 0.1.165
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 +58 -0
- package/dist/client-factory.d.mts +58 -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 +462 -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 +460 -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 +13 -0
- package/dist/form/labels.d.cts +11 -0
- package/dist/form/labels.d.mts +11 -0
- package/dist/form/labels.mjs +13 -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 +147 -0
- package/dist/form/types.d.mts +147 -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 +186 -0
- package/dist/form/validate.d.cts +81 -0
- package/dist/form/validate.d.mts +81 -0
- package/dist/form/validate.mjs +182 -0
- package/dist/i18n/ui-texts.cjs +135 -0
- package/dist/i18n/ui-texts.d.cts +81 -0
- package/dist/i18n/ui-texts.d.mts +81 -0
- package/dist/i18n/ui-texts.mjs +132 -0
- package/dist/index.cjs +223 -3360
- package/dist/index.d.cts +39 -1693
- package/dist/index.d.mts +39 -1693
- package/dist/index.mjs +36 -3179
- package/dist/messages.cjs +301 -0
- package/dist/messages.d.cts +302 -0
- package/dist/messages.d.mts +302 -0
- package/dist/messages.mjs +300 -0
- 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 +592 -0
- package/dist/plugin/annotations.mjs +592 -0
- package/dist/plugin/primitives.cjs +30 -0
- package/dist/plugin/primitives.mjs +30 -0
- package/dist/plugin/value-help-validation.cjs +429 -0
- package/dist/plugin/value-help-validation.mjs +423 -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 +282 -0
- package/dist/shared/meta-cache.d.cts +130 -0
- package/dist/shared/meta-cache.d.mts +130 -0
- package/dist/shared/meta-cache.mjs +275 -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 +242 -0
- package/dist/table/create-table-def.d.cts +19 -0
- package/dist/table/create-table-def.d.mts +19 -0
- package/dist/table/create-table-def.mjs +242 -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 +290 -0
- package/dist/table/types.d.mts +290 -0
- package/dist/table/value-kind.cjs +20 -0
- package/dist/table/value-kind.d.cts +16 -0
- package/dist/table/value-kind.d.mts +16 -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 +115 -0
- package/dist/value-help/extract-literals.d.cts +44 -0
- package/dist/value-help/extract-literals.d.mts +44 -0
- package/dist/value-help/extract-literals.mjs +111 -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 +68 -0
- package/dist/value-help/resolve-options.d.cts +40 -0
- package/dist/value-help/resolve-options.d.mts +40 -0
- package/dist/value-help/resolve-options.mjs +64 -0
- package/dist/value-help/resolve.cjs +66 -0
- package/dist/value-help/resolve.d.cts +36 -0
- package/dist/value-help/resolve.d.mts +36 -0
- package/dist/value-help/resolve.mjs +65 -0
- package/dist/value-help/types.d.cts +41 -0
- package/dist/value-help/types.d.mts +41 -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 +15 -10
|
@@ -0,0 +1,242 @@
|
|
|
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_str = require("../shared/str.cjs");
|
|
6
|
+
const require_measurement = require("../form/measurement.cjs");
|
|
7
|
+
let _atscript_typescript_utils = require("@atscript/typescript/utils");
|
|
8
|
+
//#region src/table/create-table-def.ts
|
|
9
|
+
/**
|
|
10
|
+
* Builds a TableDef from a moost-db MetaResponse.
|
|
11
|
+
*
|
|
12
|
+
* 1. Deserializes `meta.type` into a live TAtscriptAnnotatedType
|
|
13
|
+
* 2. Flattens to discover all field paths
|
|
14
|
+
* 3. Builds ColumnDef per field using annotations + meta.fields capabilities
|
|
15
|
+
* 4. Sorts by @ui.table.order
|
|
16
|
+
*
|
|
17
|
+
* `t` words the default boolean option labels (`UI_YES` / `UI_NO`); the
|
|
18
|
+
* active UI texts when omitted.
|
|
19
|
+
*/
|
|
20
|
+
function createTableDef(meta, preDeserializedType, t) {
|
|
21
|
+
const type = preDeserializedType ?? (0, _atscript_typescript_utils.deserializeAnnotatedType)(meta.type);
|
|
22
|
+
const flatMap = type.type.kind === "object" ? (0, _atscript_typescript_utils.flattenAnnotatedType)(type, { excludePhantomTypes: true }) : /* @__PURE__ */ new Map();
|
|
23
|
+
const columns = [];
|
|
24
|
+
for (const [path, prop] of flatMap.entries()) {
|
|
25
|
+
if (path === "") continue;
|
|
26
|
+
if (path === meta.versionColumn) continue;
|
|
27
|
+
const fieldMeta = meta.fields[path];
|
|
28
|
+
if (!isReadable(fieldMeta)) continue;
|
|
29
|
+
if (require_field_resolver.getFieldMeta(prop, "ui.table.exclude") !== void 0) continue;
|
|
30
|
+
const options = require_extract_literals.extractFieldLiteralOptions(prop, t);
|
|
31
|
+
const valueHelpInfo = require_extract_ref.extractValueHelp(prop);
|
|
32
|
+
let distinct;
|
|
33
|
+
if (!valueHelpInfo && !options && fieldMeta.groupable && fieldMeta.filterable) {
|
|
34
|
+
const url = type.metadata.get(require_annotation_keys.DB_HTTP_PATH);
|
|
35
|
+
if (url && require_field_resolver.getFieldMeta(prop, "ui.valueHelp.distinct") !== void 0) distinct = {
|
|
36
|
+
url,
|
|
37
|
+
field: path
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
columns.push(buildColumn(path, prop, fieldMeta, {
|
|
41
|
+
options,
|
|
42
|
+
valueHelpInfo,
|
|
43
|
+
distinct
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
if (meta.decorations) {
|
|
47
|
+
const decoType = (0, _atscript_typescript_utils.deserializeAnnotatedType)(meta.decorations);
|
|
48
|
+
if (decoType.type.kind === "object") for (const [path, prop] of decoType.type.props) {
|
|
49
|
+
const fieldMeta = meta.fields[path];
|
|
50
|
+
if (!fieldMeta?.decoration || flatMap.has(path)) continue;
|
|
51
|
+
if (require_field_resolver.getFieldMeta(prop, "ui.table.exclude") !== void 0) continue;
|
|
52
|
+
flatMap.set(path, prop);
|
|
53
|
+
columns.push(buildColumn(path, prop, fieldMeta, { options: require_extract_literals.extractFieldLiteralOptions(prop, t) }));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
columns.sort((a, b) => a.order - b.order);
|
|
57
|
+
const actions = groupActions(meta.actions ?? []);
|
|
58
|
+
const crud = meta.crud ?? {};
|
|
59
|
+
const preferredId = meta.preferredId ?? meta.primaryKeys;
|
|
60
|
+
return {
|
|
61
|
+
type,
|
|
62
|
+
columns,
|
|
63
|
+
flatMap,
|
|
64
|
+
fetchableFields: new Set(Object.keys(meta.fields).filter((p) => isReadable(meta.fields[p]))),
|
|
65
|
+
primaryKeys: meta.primaryKeys,
|
|
66
|
+
preferredId,
|
|
67
|
+
identifierFields: identifierFieldsOf(preferredId, meta.actions ?? []),
|
|
68
|
+
versionColumn: meta.versionColumn,
|
|
69
|
+
crud,
|
|
70
|
+
canRemove: "remove" in crud,
|
|
71
|
+
actions,
|
|
72
|
+
searchable: meta.searchable,
|
|
73
|
+
vectorSearchable: meta.vectorSearchable,
|
|
74
|
+
searchIndexes: meta.searchIndexes,
|
|
75
|
+
relations: meta.relations
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* One column. A server-declared display-only column (`fieldMeta.decoration`)
|
|
80
|
+
* is never sortable, filterable or groupable and always nullable (a decoration
|
|
81
|
+
* can be absent); an ordinary one takes its capabilities from `/meta`.
|
|
82
|
+
*/
|
|
83
|
+
function buildColumn(path, prop, fieldMeta, resolved) {
|
|
84
|
+
const { options, valueHelpInfo, distinct } = resolved;
|
|
85
|
+
const maxLengthMeta = require_field_resolver.getFieldMeta(prop, require_annotation_keys.EXPECT_MAX_LENGTH);
|
|
86
|
+
const tableType = require_field_resolver.getFieldMeta(prop, require_annotation_keys.UI_TABLE_TYPE);
|
|
87
|
+
const sharedType = require_field_resolver.getFieldMeta(prop, require_annotation_keys.UI_TYPE);
|
|
88
|
+
const tableComponent = require_field_resolver.getFieldMeta(prop, require_annotation_keys.UI_TABLE_COMPONENT);
|
|
89
|
+
const decoration = fieldMeta.decoration === true;
|
|
90
|
+
const filterOps = decoration ? void 0 : fieldMeta.filterOps;
|
|
91
|
+
return {
|
|
92
|
+
path,
|
|
93
|
+
label: require_field_resolver.getFieldMeta(prop, "meta.label") ?? require_str.humanizePath(path),
|
|
94
|
+
type: tableType ?? sharedType ?? (valueHelpInfo ? "ref" : inferDisplayType(prop, options)),
|
|
95
|
+
valueKind: inferValueKind(prop),
|
|
96
|
+
component: tableComponent,
|
|
97
|
+
selectWith: require_field_resolver.getFieldMeta(prop, require_annotation_keys.UI_TABLE_SELECT_WITH),
|
|
98
|
+
sortable: !decoration && (fieldMeta.sortable ?? false),
|
|
99
|
+
filterable: !decoration && (fieldMeta.filterable ?? false),
|
|
100
|
+
...filterOps && { filterOps: [...filterOps] },
|
|
101
|
+
nullable: decoration || prop.optional === true || hasEmptyMember(prop),
|
|
102
|
+
width: require_field_resolver.getFieldMeta(prop, require_annotation_keys.UI_TABLE_WIDTH),
|
|
103
|
+
maxLen: maxLengthMeta?.length,
|
|
104
|
+
order: require_field_resolver.getFieldMeta(prop, "ui.table.order") ?? Infinity,
|
|
105
|
+
options,
|
|
106
|
+
valueHelpInfo,
|
|
107
|
+
...distinct && { distinct },
|
|
108
|
+
...require_measurement.extractMeasurement(prop)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* `preferredId` ∪ every delegated action's `idMap` paths, in that order —
|
|
113
|
+
* the same array when no action carries an `idMap`.
|
|
114
|
+
*/
|
|
115
|
+
function identifierFieldsOf(preferredId, actions) {
|
|
116
|
+
let out;
|
|
117
|
+
for (const a of actions) {
|
|
118
|
+
if (!a.idMap) continue;
|
|
119
|
+
for (const path of Object.values(a.idMap)) {
|
|
120
|
+
if (preferredId.includes(path) || out?.includes(path)) continue;
|
|
121
|
+
(out ??= [...preferredId]).push(path);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return out ?? preferredId;
|
|
125
|
+
}
|
|
126
|
+
/** A field the caller may read: listed in `meta.fields` and not write-only. */
|
|
127
|
+
function isReadable(field) {
|
|
128
|
+
return field !== void 0 && field.writeOnly !== true;
|
|
129
|
+
}
|
|
130
|
+
/** Sort by (order ?? 0). `toSorted` is stable per spec, so ties preserve declaration order. */
|
|
131
|
+
function byOrder(xs) {
|
|
132
|
+
return xs.toSorted((x, y) => (x.order ?? 0) - (y.order ?? 0));
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Partition actions by `level`, sort each group by `(order ?? 0)` then
|
|
136
|
+
* declaration order, and pick the first `default: true` entry per level.
|
|
137
|
+
* The synthesised `__remove` UI action lives outside this set and is never
|
|
138
|
+
* a candidate for `default.row`.
|
|
139
|
+
*/
|
|
140
|
+
function groupActions(actions) {
|
|
141
|
+
const table = [];
|
|
142
|
+
const row = [];
|
|
143
|
+
const rows = [];
|
|
144
|
+
for (const a of actions) if (a.level === "table") table.push(a);
|
|
145
|
+
else if (a.level === "row") row.push(a);
|
|
146
|
+
else if (a.level === "rows") rows.push(a);
|
|
147
|
+
const tableSorted = byOrder(table);
|
|
148
|
+
const rowSorted = byOrder(row);
|
|
149
|
+
const rowsSorted = byOrder(rows);
|
|
150
|
+
return {
|
|
151
|
+
table: tableSorted,
|
|
152
|
+
row: rowSorted,
|
|
153
|
+
rows: rowsSorted,
|
|
154
|
+
default: {
|
|
155
|
+
table: tableSorted.find((a) => a.default === true),
|
|
156
|
+
row: rowSorted.find((a) => a.default === true),
|
|
157
|
+
rows: rowsSorted.find((a) => a.default === true)
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/** Infers a display type string from the annotated type's kind and designType. */
|
|
162
|
+
function inferDisplayType(prop, literalOpts) {
|
|
163
|
+
const kind = prop.type.kind;
|
|
164
|
+
if (kind === "array") return "array";
|
|
165
|
+
if (kind === "object") return "object";
|
|
166
|
+
if (kind === "union") {
|
|
167
|
+
if (literalOpts === void 0) return "union";
|
|
168
|
+
return literalOpts.every((o) => typeof o.value === "boolean") ? "boolean" : "enum";
|
|
169
|
+
}
|
|
170
|
+
if (kind === "") {
|
|
171
|
+
const final = prop.type;
|
|
172
|
+
const dt = final.designType;
|
|
173
|
+
if (dt === "number") return final.tags?.has("timestamp") ? "datetime" : "number";
|
|
174
|
+
if (dt === "decimal") return "number";
|
|
175
|
+
if (dt === "boolean") return "boolean";
|
|
176
|
+
return "text";
|
|
177
|
+
}
|
|
178
|
+
return "text";
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Number metadata that makes the server treat a plain `number` column as an
|
|
182
|
+
* integer (mirrors the db layer's `valueTypeOf`), beside the `int` / `expect.int` tags.
|
|
183
|
+
*/
|
|
184
|
+
const INTEGER_NUMBER_META = [
|
|
185
|
+
"expect.int",
|
|
186
|
+
"db.default.increment",
|
|
187
|
+
"db.default.now",
|
|
188
|
+
"db.agg.count",
|
|
189
|
+
"db.agg.countDistinct"
|
|
190
|
+
];
|
|
191
|
+
/** The kind of one scalar leaf, or `undefined` for a non-scalar / unknown design type. */
|
|
192
|
+
function scalarValueKind(prop) {
|
|
193
|
+
const final = prop.type;
|
|
194
|
+
const tags = final.tags;
|
|
195
|
+
switch (final.designType) {
|
|
196
|
+
case "string": return tags?.has("isoDate") ? "isoDate" : tags?.has("date") ? "date" : "string";
|
|
197
|
+
case "number":
|
|
198
|
+
if (tags?.has("timestamp")) return "timestamp";
|
|
199
|
+
if (tags?.has("int") || INTEGER_NUMBER_META.some((k) => prop.metadata.has(k))) return "integer";
|
|
200
|
+
return "number";
|
|
201
|
+
case "decimal": return "decimal";
|
|
202
|
+
case "boolean": return "boolean";
|
|
203
|
+
default: return;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/** Whether a union has a `null` / `undefined` member (`T | null`): its value can be empty. */
|
|
207
|
+
function hasEmptyMember(prop) {
|
|
208
|
+
if (prop.type.kind !== "union") return false;
|
|
209
|
+
return prop.type.items.some((item) => require_extract_literals.isEmptyMember(item) || hasEmptyMember(item));
|
|
210
|
+
}
|
|
211
|
+
/** Kinds of every non-null leaf of a (possibly nested) union; `null` when one is not a scalar. */
|
|
212
|
+
function collectUnionKinds(items, out) {
|
|
213
|
+
for (const item of items) {
|
|
214
|
+
if (item.type.kind === "union") {
|
|
215
|
+
collectUnionKinds(item.type.items, out);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (item.type.kind === "") {
|
|
219
|
+
if (!require_extract_literals.isEmptyMember(item)) out.add(scalarValueKind(item));
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
out.add(void 0);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Scalar storage kind of the column's value — what the server's filter guard
|
|
227
|
+
* checks. A scalar maps to its kind; a union that agrees on one kind across
|
|
228
|
+
* its non-null members (`'a' | 'b'`, `1 | 2`, `number.timestamp | null`)
|
|
229
|
+
* takes it; arrays, objects, JSON and mixed unions have none (`undefined`),
|
|
230
|
+
* which means no value coercion.
|
|
231
|
+
*/
|
|
232
|
+
function inferValueKind(prop) {
|
|
233
|
+
const kind = prop.type.kind;
|
|
234
|
+
if (kind === "") return scalarValueKind(prop);
|
|
235
|
+
if (kind !== "union") return void 0;
|
|
236
|
+
const kinds = /* @__PURE__ */ new Set();
|
|
237
|
+
collectUnionKinds(prop.type.items, kinds);
|
|
238
|
+
if (kinds.size !== 1) return void 0;
|
|
239
|
+
return [...kinds][0];
|
|
240
|
+
}
|
|
241
|
+
//#endregion
|
|
242
|
+
exports.createTableDef = createTableDef;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MetaResponse, TableDef } from "./types.cjs";
|
|
2
|
+
import { UiT } from "../i18n/ui-texts.cjs";
|
|
3
|
+
import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
|
|
4
|
+
|
|
5
|
+
//#region src/table/create-table-def.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Builds a TableDef from a moost-db MetaResponse.
|
|
8
|
+
*
|
|
9
|
+
* 1. Deserializes `meta.type` into a live TAtscriptAnnotatedType
|
|
10
|
+
* 2. Flattens to discover all field paths
|
|
11
|
+
* 3. Builds ColumnDef per field using annotations + meta.fields capabilities
|
|
12
|
+
* 4. Sorts by @ui.table.order
|
|
13
|
+
*
|
|
14
|
+
* `t` words the default boolean option labels (`UI_YES` / `UI_NO`); the
|
|
15
|
+
* active UI texts when omitted.
|
|
16
|
+
*/
|
|
17
|
+
declare function createTableDef(meta: MetaResponse, preDeserializedType?: TAtscriptAnnotatedType, t?: UiT): TableDef;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { createTableDef };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MetaResponse, TableDef } from "./types.mjs";
|
|
2
|
+
import { UiT } from "../i18n/ui-texts.mjs";
|
|
3
|
+
import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
|
|
4
|
+
|
|
5
|
+
//#region src/table/create-table-def.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Builds a TableDef from a moost-db MetaResponse.
|
|
8
|
+
*
|
|
9
|
+
* 1. Deserializes `meta.type` into a live TAtscriptAnnotatedType
|
|
10
|
+
* 2. Flattens to discover all field paths
|
|
11
|
+
* 3. Builds ColumnDef per field using annotations + meta.fields capabilities
|
|
12
|
+
* 4. Sorts by @ui.table.order
|
|
13
|
+
*
|
|
14
|
+
* `t` words the default boolean option labels (`UI_YES` / `UI_NO`); the
|
|
15
|
+
* active UI texts when omitted.
|
|
16
|
+
*/
|
|
17
|
+
declare function createTableDef(meta: MetaResponse, preDeserializedType?: TAtscriptAnnotatedType, t?: UiT): TableDef;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { createTableDef };
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { DB_HTTP_PATH, EXPECT_MAX_LENGTH, UI_TABLE_COMPONENT, UI_TABLE_SELECT_WITH, UI_TABLE_TYPE, UI_TABLE_WIDTH, UI_TYPE } from "../shared/annotation-keys.mjs";
|
|
2
|
+
import { getFieldMeta } from "../shared/field-resolver.mjs";
|
|
3
|
+
import { extractFieldLiteralOptions, isEmptyMember } from "../value-help/extract-literals.mjs";
|
|
4
|
+
import { extractValueHelp } from "../value-help/extract-ref.mjs";
|
|
5
|
+
import { humanizePath } from "../shared/str.mjs";
|
|
6
|
+
import { extractMeasurement } from "../form/measurement.mjs";
|
|
7
|
+
import { deserializeAnnotatedType, flattenAnnotatedType } from "@atscript/typescript/utils";
|
|
8
|
+
//#region src/table/create-table-def.ts
|
|
9
|
+
/**
|
|
10
|
+
* Builds a TableDef from a moost-db MetaResponse.
|
|
11
|
+
*
|
|
12
|
+
* 1. Deserializes `meta.type` into a live TAtscriptAnnotatedType
|
|
13
|
+
* 2. Flattens to discover all field paths
|
|
14
|
+
* 3. Builds ColumnDef per field using annotations + meta.fields capabilities
|
|
15
|
+
* 4. Sorts by @ui.table.order
|
|
16
|
+
*
|
|
17
|
+
* `t` words the default boolean option labels (`UI_YES` / `UI_NO`); the
|
|
18
|
+
* active UI texts when omitted.
|
|
19
|
+
*/
|
|
20
|
+
function createTableDef(meta, preDeserializedType, t) {
|
|
21
|
+
const type = preDeserializedType ?? deserializeAnnotatedType(meta.type);
|
|
22
|
+
const flatMap = type.type.kind === "object" ? flattenAnnotatedType(type, { excludePhantomTypes: true }) : /* @__PURE__ */ new Map();
|
|
23
|
+
const columns = [];
|
|
24
|
+
for (const [path, prop] of flatMap.entries()) {
|
|
25
|
+
if (path === "") continue;
|
|
26
|
+
if (path === meta.versionColumn) continue;
|
|
27
|
+
const fieldMeta = meta.fields[path];
|
|
28
|
+
if (!isReadable(fieldMeta)) continue;
|
|
29
|
+
if (getFieldMeta(prop, "ui.table.exclude") !== void 0) continue;
|
|
30
|
+
const options = extractFieldLiteralOptions(prop, t);
|
|
31
|
+
const valueHelpInfo = extractValueHelp(prop);
|
|
32
|
+
let distinct;
|
|
33
|
+
if (!valueHelpInfo && !options && fieldMeta.groupable && fieldMeta.filterable) {
|
|
34
|
+
const url = type.metadata.get(DB_HTTP_PATH);
|
|
35
|
+
if (url && getFieldMeta(prop, "ui.valueHelp.distinct") !== void 0) distinct = {
|
|
36
|
+
url,
|
|
37
|
+
field: path
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
columns.push(buildColumn(path, prop, fieldMeta, {
|
|
41
|
+
options,
|
|
42
|
+
valueHelpInfo,
|
|
43
|
+
distinct
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
if (meta.decorations) {
|
|
47
|
+
const decoType = deserializeAnnotatedType(meta.decorations);
|
|
48
|
+
if (decoType.type.kind === "object") for (const [path, prop] of decoType.type.props) {
|
|
49
|
+
const fieldMeta = meta.fields[path];
|
|
50
|
+
if (!fieldMeta?.decoration || flatMap.has(path)) continue;
|
|
51
|
+
if (getFieldMeta(prop, "ui.table.exclude") !== void 0) continue;
|
|
52
|
+
flatMap.set(path, prop);
|
|
53
|
+
columns.push(buildColumn(path, prop, fieldMeta, { options: extractFieldLiteralOptions(prop, t) }));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
columns.sort((a, b) => a.order - b.order);
|
|
57
|
+
const actions = groupActions(meta.actions ?? []);
|
|
58
|
+
const crud = meta.crud ?? {};
|
|
59
|
+
const preferredId = meta.preferredId ?? meta.primaryKeys;
|
|
60
|
+
return {
|
|
61
|
+
type,
|
|
62
|
+
columns,
|
|
63
|
+
flatMap,
|
|
64
|
+
fetchableFields: new Set(Object.keys(meta.fields).filter((p) => isReadable(meta.fields[p]))),
|
|
65
|
+
primaryKeys: meta.primaryKeys,
|
|
66
|
+
preferredId,
|
|
67
|
+
identifierFields: identifierFieldsOf(preferredId, meta.actions ?? []),
|
|
68
|
+
versionColumn: meta.versionColumn,
|
|
69
|
+
crud,
|
|
70
|
+
canRemove: "remove" in crud,
|
|
71
|
+
actions,
|
|
72
|
+
searchable: meta.searchable,
|
|
73
|
+
vectorSearchable: meta.vectorSearchable,
|
|
74
|
+
searchIndexes: meta.searchIndexes,
|
|
75
|
+
relations: meta.relations
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* One column. A server-declared display-only column (`fieldMeta.decoration`)
|
|
80
|
+
* is never sortable, filterable or groupable and always nullable (a decoration
|
|
81
|
+
* can be absent); an ordinary one takes its capabilities from `/meta`.
|
|
82
|
+
*/
|
|
83
|
+
function buildColumn(path, prop, fieldMeta, resolved) {
|
|
84
|
+
const { options, valueHelpInfo, distinct } = resolved;
|
|
85
|
+
const maxLengthMeta = getFieldMeta(prop, EXPECT_MAX_LENGTH);
|
|
86
|
+
const tableType = getFieldMeta(prop, UI_TABLE_TYPE);
|
|
87
|
+
const sharedType = getFieldMeta(prop, UI_TYPE);
|
|
88
|
+
const tableComponent = getFieldMeta(prop, UI_TABLE_COMPONENT);
|
|
89
|
+
const decoration = fieldMeta.decoration === true;
|
|
90
|
+
const filterOps = decoration ? void 0 : fieldMeta.filterOps;
|
|
91
|
+
return {
|
|
92
|
+
path,
|
|
93
|
+
label: getFieldMeta(prop, "meta.label") ?? humanizePath(path),
|
|
94
|
+
type: tableType ?? sharedType ?? (valueHelpInfo ? "ref" : inferDisplayType(prop, options)),
|
|
95
|
+
valueKind: inferValueKind(prop),
|
|
96
|
+
component: tableComponent,
|
|
97
|
+
selectWith: getFieldMeta(prop, UI_TABLE_SELECT_WITH),
|
|
98
|
+
sortable: !decoration && (fieldMeta.sortable ?? false),
|
|
99
|
+
filterable: !decoration && (fieldMeta.filterable ?? false),
|
|
100
|
+
...filterOps && { filterOps: [...filterOps] },
|
|
101
|
+
nullable: decoration || prop.optional === true || hasEmptyMember(prop),
|
|
102
|
+
width: getFieldMeta(prop, UI_TABLE_WIDTH),
|
|
103
|
+
maxLen: maxLengthMeta?.length,
|
|
104
|
+
order: getFieldMeta(prop, "ui.table.order") ?? Infinity,
|
|
105
|
+
options,
|
|
106
|
+
valueHelpInfo,
|
|
107
|
+
...distinct && { distinct },
|
|
108
|
+
...extractMeasurement(prop)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* `preferredId` ∪ every delegated action's `idMap` paths, in that order —
|
|
113
|
+
* the same array when no action carries an `idMap`.
|
|
114
|
+
*/
|
|
115
|
+
function identifierFieldsOf(preferredId, actions) {
|
|
116
|
+
let out;
|
|
117
|
+
for (const a of actions) {
|
|
118
|
+
if (!a.idMap) continue;
|
|
119
|
+
for (const path of Object.values(a.idMap)) {
|
|
120
|
+
if (preferredId.includes(path) || out?.includes(path)) continue;
|
|
121
|
+
(out ??= [...preferredId]).push(path);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return out ?? preferredId;
|
|
125
|
+
}
|
|
126
|
+
/** A field the caller may read: listed in `meta.fields` and not write-only. */
|
|
127
|
+
function isReadable(field) {
|
|
128
|
+
return field !== void 0 && field.writeOnly !== true;
|
|
129
|
+
}
|
|
130
|
+
/** Sort by (order ?? 0). `toSorted` is stable per spec, so ties preserve declaration order. */
|
|
131
|
+
function byOrder(xs) {
|
|
132
|
+
return xs.toSorted((x, y) => (x.order ?? 0) - (y.order ?? 0));
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Partition actions by `level`, sort each group by `(order ?? 0)` then
|
|
136
|
+
* declaration order, and pick the first `default: true` entry per level.
|
|
137
|
+
* The synthesised `__remove` UI action lives outside this set and is never
|
|
138
|
+
* a candidate for `default.row`.
|
|
139
|
+
*/
|
|
140
|
+
function groupActions(actions) {
|
|
141
|
+
const table = [];
|
|
142
|
+
const row = [];
|
|
143
|
+
const rows = [];
|
|
144
|
+
for (const a of actions) if (a.level === "table") table.push(a);
|
|
145
|
+
else if (a.level === "row") row.push(a);
|
|
146
|
+
else if (a.level === "rows") rows.push(a);
|
|
147
|
+
const tableSorted = byOrder(table);
|
|
148
|
+
const rowSorted = byOrder(row);
|
|
149
|
+
const rowsSorted = byOrder(rows);
|
|
150
|
+
return {
|
|
151
|
+
table: tableSorted,
|
|
152
|
+
row: rowSorted,
|
|
153
|
+
rows: rowsSorted,
|
|
154
|
+
default: {
|
|
155
|
+
table: tableSorted.find((a) => a.default === true),
|
|
156
|
+
row: rowSorted.find((a) => a.default === true),
|
|
157
|
+
rows: rowsSorted.find((a) => a.default === true)
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/** Infers a display type string from the annotated type's kind and designType. */
|
|
162
|
+
function inferDisplayType(prop, literalOpts) {
|
|
163
|
+
const kind = prop.type.kind;
|
|
164
|
+
if (kind === "array") return "array";
|
|
165
|
+
if (kind === "object") return "object";
|
|
166
|
+
if (kind === "union") {
|
|
167
|
+
if (literalOpts === void 0) return "union";
|
|
168
|
+
return literalOpts.every((o) => typeof o.value === "boolean") ? "boolean" : "enum";
|
|
169
|
+
}
|
|
170
|
+
if (kind === "") {
|
|
171
|
+
const final = prop.type;
|
|
172
|
+
const dt = final.designType;
|
|
173
|
+
if (dt === "number") return final.tags?.has("timestamp") ? "datetime" : "number";
|
|
174
|
+
if (dt === "decimal") return "number";
|
|
175
|
+
if (dt === "boolean") return "boolean";
|
|
176
|
+
return "text";
|
|
177
|
+
}
|
|
178
|
+
return "text";
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Number metadata that makes the server treat a plain `number` column as an
|
|
182
|
+
* integer (mirrors the db layer's `valueTypeOf`), beside the `int` / `expect.int` tags.
|
|
183
|
+
*/
|
|
184
|
+
const INTEGER_NUMBER_META = [
|
|
185
|
+
"expect.int",
|
|
186
|
+
"db.default.increment",
|
|
187
|
+
"db.default.now",
|
|
188
|
+
"db.agg.count",
|
|
189
|
+
"db.agg.countDistinct"
|
|
190
|
+
];
|
|
191
|
+
/** The kind of one scalar leaf, or `undefined` for a non-scalar / unknown design type. */
|
|
192
|
+
function scalarValueKind(prop) {
|
|
193
|
+
const final = prop.type;
|
|
194
|
+
const tags = final.tags;
|
|
195
|
+
switch (final.designType) {
|
|
196
|
+
case "string": return tags?.has("isoDate") ? "isoDate" : tags?.has("date") ? "date" : "string";
|
|
197
|
+
case "number":
|
|
198
|
+
if (tags?.has("timestamp")) return "timestamp";
|
|
199
|
+
if (tags?.has("int") || INTEGER_NUMBER_META.some((k) => prop.metadata.has(k))) return "integer";
|
|
200
|
+
return "number";
|
|
201
|
+
case "decimal": return "decimal";
|
|
202
|
+
case "boolean": return "boolean";
|
|
203
|
+
default: return;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/** Whether a union has a `null` / `undefined` member (`T | null`): its value can be empty. */
|
|
207
|
+
function hasEmptyMember(prop) {
|
|
208
|
+
if (prop.type.kind !== "union") return false;
|
|
209
|
+
return prop.type.items.some((item) => isEmptyMember(item) || hasEmptyMember(item));
|
|
210
|
+
}
|
|
211
|
+
/** Kinds of every non-null leaf of a (possibly nested) union; `null` when one is not a scalar. */
|
|
212
|
+
function collectUnionKinds(items, out) {
|
|
213
|
+
for (const item of items) {
|
|
214
|
+
if (item.type.kind === "union") {
|
|
215
|
+
collectUnionKinds(item.type.items, out);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (item.type.kind === "") {
|
|
219
|
+
if (!isEmptyMember(item)) out.add(scalarValueKind(item));
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
out.add(void 0);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Scalar storage kind of the column's value — what the server's filter guard
|
|
227
|
+
* checks. A scalar maps to its kind; a union that agrees on one kind across
|
|
228
|
+
* its non-null members (`'a' | 'b'`, `1 | 2`, `number.timestamp | null`)
|
|
229
|
+
* takes it; arrays, objects, JSON and mixed unions have none (`undefined`),
|
|
230
|
+
* which means no value coercion.
|
|
231
|
+
*/
|
|
232
|
+
function inferValueKind(prop) {
|
|
233
|
+
const kind = prop.type.kind;
|
|
234
|
+
if (kind === "") return scalarValueKind(prop);
|
|
235
|
+
if (kind !== "union") return void 0;
|
|
236
|
+
const kinds = /* @__PURE__ */ new Set();
|
|
237
|
+
collectUnionKinds(prop.type.items, kinds);
|
|
238
|
+
if (kinds.size !== 1) return void 0;
|
|
239
|
+
return [...kinds][0];
|
|
240
|
+
}
|
|
241
|
+
//#endregion
|
|
242
|
+
export { createTableDef };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
let _atscript_db_client = require("@atscript/db-client");
|
|
2
|
+
//#region src/table/navigate-href.ts
|
|
3
|
+
/**
|
|
4
|
+
* Compute the href a `processor: 'navigate'` action would open — synchronously,
|
|
5
|
+
* at render time. Mirrors the interpolation `Client` performs when the action
|
|
6
|
+
* is invoked (`action.value` with every `$1` replaced by the URL-encoded,
|
|
7
|
+
* `/`-joined `preferredId` fields — see `encodeNavigateId`), so tables can
|
|
8
|
+
* render navigate actions as real `<a href>` anchors and native link behaviour
|
|
9
|
+
* (middle-click → new tab, copy link, hover preview) just works.
|
|
10
|
+
*
|
|
11
|
+
* Returns `undefined` when no link is possible — the caller should fall back to
|
|
12
|
+
* a button and let `Client` handle the invoke:
|
|
13
|
+
*
|
|
14
|
+
* - `action.processor !== 'navigate'` — backend/custom actions stay buttons;
|
|
15
|
+
* - row-level action with `id === undefined` — the row is not identifiable
|
|
16
|
+
* (note this deliberately differs from `Client`, which navigates to the raw
|
|
17
|
+
* template in that case; a raw-template href would be a broken link);
|
|
18
|
+
* - row-level action with an empty `preferredId` — `$1` has nothing to encode.
|
|
19
|
+
*
|
|
20
|
+
* Table/rows-level navigate actions carry no `$1` placeholder and return
|
|
21
|
+
* `action.value` verbatim.
|
|
22
|
+
*
|
|
23
|
+
* A delegated action (`action.idMap`, since 0.1.147) reads `id` as one of THIS
|
|
24
|
+
* table's rows (or identifiers) and encodes the owner's identification it maps
|
|
25
|
+
* to, in `Object.keys(idMap)` order — the same as `Client`. A row the map
|
|
26
|
+
* cannot address (a missing path) has no link.
|
|
27
|
+
*/
|
|
28
|
+
function navigateHrefFor(action, id, preferredId) {
|
|
29
|
+
if (action.processor !== "navigate") return void 0;
|
|
30
|
+
if (action.level !== "row") return action.value;
|
|
31
|
+
if (id === void 0) return void 0;
|
|
32
|
+
if (action.idMap) {
|
|
33
|
+
let mapped;
|
|
34
|
+
try {
|
|
35
|
+
mapped = (0, _atscript_db_client.actionIdentifier)(action, id, []);
|
|
36
|
+
} catch {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
return action.value.replace(/\$1/g, (0, _atscript_db_client.encodeNavigateId)(mapped, Object.keys(action.idMap)));
|
|
40
|
+
}
|
|
41
|
+
if (preferredId.length === 0) return void 0;
|
|
42
|
+
return action.value.replace(/\$1/g, (0, _atscript_db_client.encodeNavigateId)(id, preferredId));
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
exports.navigateHrefFor = navigateHrefFor;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { TDbActionInfo } from "@atscript/db-client";
|
|
2
|
+
|
|
3
|
+
//#region src/table/navigate-href.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Compute the href a `processor: 'navigate'` action would open — synchronously,
|
|
6
|
+
* at render time. Mirrors the interpolation `Client` performs when the action
|
|
7
|
+
* is invoked (`action.value` with every `$1` replaced by the URL-encoded,
|
|
8
|
+
* `/`-joined `preferredId` fields — see `encodeNavigateId`), so tables can
|
|
9
|
+
* render navigate actions as real `<a href>` anchors and native link behaviour
|
|
10
|
+
* (middle-click → new tab, copy link, hover preview) just works.
|
|
11
|
+
*
|
|
12
|
+
* Returns `undefined` when no link is possible — the caller should fall back to
|
|
13
|
+
* a button and let `Client` handle the invoke:
|
|
14
|
+
*
|
|
15
|
+
* - `action.processor !== 'navigate'` — backend/custom actions stay buttons;
|
|
16
|
+
* - row-level action with `id === undefined` — the row is not identifiable
|
|
17
|
+
* (note this deliberately differs from `Client`, which navigates to the raw
|
|
18
|
+
* template in that case; a raw-template href would be a broken link);
|
|
19
|
+
* - row-level action with an empty `preferredId` — `$1` has nothing to encode.
|
|
20
|
+
*
|
|
21
|
+
* Table/rows-level navigate actions carry no `$1` placeholder and return
|
|
22
|
+
* `action.value` verbatim.
|
|
23
|
+
*
|
|
24
|
+
* A delegated action (`action.idMap`, since 0.1.147) reads `id` as one of THIS
|
|
25
|
+
* table's rows (or identifiers) and encodes the owner's identification it maps
|
|
26
|
+
* to, in `Object.keys(idMap)` order — the same as `Client`. A row the map
|
|
27
|
+
* cannot address (a missing path) has no link.
|
|
28
|
+
*/
|
|
29
|
+
declare function navigateHrefFor(action: TDbActionInfo, id: Record<string, unknown> | undefined, preferredId: readonly string[]): string | undefined;
|
|
30
|
+
//#endregion
|
|
31
|
+
export { navigateHrefFor };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { TDbActionInfo } from "@atscript/db-client";
|
|
2
|
+
|
|
3
|
+
//#region src/table/navigate-href.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Compute the href a `processor: 'navigate'` action would open — synchronously,
|
|
6
|
+
* at render time. Mirrors the interpolation `Client` performs when the action
|
|
7
|
+
* is invoked (`action.value` with every `$1` replaced by the URL-encoded,
|
|
8
|
+
* `/`-joined `preferredId` fields — see `encodeNavigateId`), so tables can
|
|
9
|
+
* render navigate actions as real `<a href>` anchors and native link behaviour
|
|
10
|
+
* (middle-click → new tab, copy link, hover preview) just works.
|
|
11
|
+
*
|
|
12
|
+
* Returns `undefined` when no link is possible — the caller should fall back to
|
|
13
|
+
* a button and let `Client` handle the invoke:
|
|
14
|
+
*
|
|
15
|
+
* - `action.processor !== 'navigate'` — backend/custom actions stay buttons;
|
|
16
|
+
* - row-level action with `id === undefined` — the row is not identifiable
|
|
17
|
+
* (note this deliberately differs from `Client`, which navigates to the raw
|
|
18
|
+
* template in that case; a raw-template href would be a broken link);
|
|
19
|
+
* - row-level action with an empty `preferredId` — `$1` has nothing to encode.
|
|
20
|
+
*
|
|
21
|
+
* Table/rows-level navigate actions carry no `$1` placeholder and return
|
|
22
|
+
* `action.value` verbatim.
|
|
23
|
+
*
|
|
24
|
+
* A delegated action (`action.idMap`, since 0.1.147) reads `id` as one of THIS
|
|
25
|
+
* table's rows (or identifiers) and encodes the owner's identification it maps
|
|
26
|
+
* to, in `Object.keys(idMap)` order — the same as `Client`. A row the map
|
|
27
|
+
* cannot address (a missing path) has no link.
|
|
28
|
+
*/
|
|
29
|
+
declare function navigateHrefFor(action: TDbActionInfo, id: Record<string, unknown> | undefined, preferredId: readonly string[]): string | undefined;
|
|
30
|
+
//#endregion
|
|
31
|
+
export { navigateHrefFor };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { actionIdentifier, encodeNavigateId } from "@atscript/db-client";
|
|
2
|
+
//#region src/table/navigate-href.ts
|
|
3
|
+
/**
|
|
4
|
+
* Compute the href a `processor: 'navigate'` action would open — synchronously,
|
|
5
|
+
* at render time. Mirrors the interpolation `Client` performs when the action
|
|
6
|
+
* is invoked (`action.value` with every `$1` replaced by the URL-encoded,
|
|
7
|
+
* `/`-joined `preferredId` fields — see `encodeNavigateId`), so tables can
|
|
8
|
+
* render navigate actions as real `<a href>` anchors and native link behaviour
|
|
9
|
+
* (middle-click → new tab, copy link, hover preview) just works.
|
|
10
|
+
*
|
|
11
|
+
* Returns `undefined` when no link is possible — the caller should fall back to
|
|
12
|
+
* a button and let `Client` handle the invoke:
|
|
13
|
+
*
|
|
14
|
+
* - `action.processor !== 'navigate'` — backend/custom actions stay buttons;
|
|
15
|
+
* - row-level action with `id === undefined` — the row is not identifiable
|
|
16
|
+
* (note this deliberately differs from `Client`, which navigates to the raw
|
|
17
|
+
* template in that case; a raw-template href would be a broken link);
|
|
18
|
+
* - row-level action with an empty `preferredId` — `$1` has nothing to encode.
|
|
19
|
+
*
|
|
20
|
+
* Table/rows-level navigate actions carry no `$1` placeholder and return
|
|
21
|
+
* `action.value` verbatim.
|
|
22
|
+
*
|
|
23
|
+
* A delegated action (`action.idMap`, since 0.1.147) reads `id` as one of THIS
|
|
24
|
+
* table's rows (or identifiers) and encodes the owner's identification it maps
|
|
25
|
+
* to, in `Object.keys(idMap)` order — the same as `Client`. A row the map
|
|
26
|
+
* cannot address (a missing path) has no link.
|
|
27
|
+
*/
|
|
28
|
+
function navigateHrefFor(action, id, preferredId) {
|
|
29
|
+
if (action.processor !== "navigate") return void 0;
|
|
30
|
+
if (action.level !== "row") return action.value;
|
|
31
|
+
if (id === void 0) return void 0;
|
|
32
|
+
if (action.idMap) {
|
|
33
|
+
let mapped;
|
|
34
|
+
try {
|
|
35
|
+
mapped = actionIdentifier(action, id, []);
|
|
36
|
+
} catch {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
return action.value.replace(/\$1/g, encodeNavigateId(mapped, Object.keys(action.idMap)));
|
|
40
|
+
}
|
|
41
|
+
if (preferredId.length === 0) return void 0;
|
|
42
|
+
return action.value.replace(/\$1/g, encodeNavigateId(id, preferredId));
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { navigateHrefFor };
|