@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,41 @@
|
|
|
1
|
+
import { FilterExpr } from "@atscript/db-client";
|
|
2
|
+
|
|
3
|
+
//#region src/value-help/types.d.ts
|
|
4
|
+
/** An option for select/radio fields — either a plain string or a `{ key, label }` pair. */
|
|
5
|
+
/**
|
|
6
|
+
* A form option: a plain string, or `{ key, label }` — plus, for options derived from a
|
|
7
|
+
* literal union or a boolean, the typed `value` (`true`, `3`) the field stores (since 0.1.165).
|
|
8
|
+
*/
|
|
9
|
+
type TFormEntryOptions = {
|
|
10
|
+
key: string;
|
|
11
|
+
label: string;
|
|
12
|
+
value?: string | number | boolean;
|
|
13
|
+
} | string;
|
|
14
|
+
/**
|
|
15
|
+
* Minimal sync probe output for a value-help–eligible prop.
|
|
16
|
+
*
|
|
17
|
+
* Emitted by `extractValueHelp(prop)` at def-construction time. Consumers use
|
|
18
|
+
* `url` to key the lazy resolver (`resolveValueHelp(url)`) that fetches the
|
|
19
|
+
* target's own `/meta` endpoint and returns the field-role details.
|
|
20
|
+
*/
|
|
21
|
+
interface ValueHelpInfo {
|
|
22
|
+
/** HTTP path to the value-help target (from the target's `@db.http.path`). */
|
|
23
|
+
url: string;
|
|
24
|
+
/**
|
|
25
|
+
* Field on the target that this FK references (from `prop.ref.field`, e.g. "id").
|
|
26
|
+
* This is the value committed to the FK field when the user picks a row.
|
|
27
|
+
*/
|
|
28
|
+
targetField: string;
|
|
29
|
+
/**
|
|
30
|
+
* Static dictionary scope from `@ui.valueHelp`'s filter — AND'd into every
|
|
31
|
+
* picker query. Since 0.1.148.
|
|
32
|
+
*/
|
|
33
|
+
filter?: FilterExpr;
|
|
34
|
+
/**
|
|
35
|
+
* Target fields the filter pins with `=` — constant in the picker, hidden
|
|
36
|
+
* from its columns. Since 0.1.148.
|
|
37
|
+
*/
|
|
38
|
+
pinned?: string[];
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
export { TFormEntryOptions, ValueHelpInfo };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { FilterExpr } from "@atscript/db-client";
|
|
2
|
+
|
|
3
|
+
//#region src/value-help/types.d.ts
|
|
4
|
+
/** An option for select/radio fields — either a plain string or a `{ key, label }` pair. */
|
|
5
|
+
/**
|
|
6
|
+
* A form option: a plain string, or `{ key, label }` — plus, for options derived from a
|
|
7
|
+
* literal union or a boolean, the typed `value` (`true`, `3`) the field stores (since 0.1.165).
|
|
8
|
+
*/
|
|
9
|
+
type TFormEntryOptions = {
|
|
10
|
+
key: string;
|
|
11
|
+
label: string;
|
|
12
|
+
value?: string | number | boolean;
|
|
13
|
+
} | string;
|
|
14
|
+
/**
|
|
15
|
+
* Minimal sync probe output for a value-help–eligible prop.
|
|
16
|
+
*
|
|
17
|
+
* Emitted by `extractValueHelp(prop)` at def-construction time. Consumers use
|
|
18
|
+
* `url` to key the lazy resolver (`resolveValueHelp(url)`) that fetches the
|
|
19
|
+
* target's own `/meta` endpoint and returns the field-role details.
|
|
20
|
+
*/
|
|
21
|
+
interface ValueHelpInfo {
|
|
22
|
+
/** HTTP path to the value-help target (from the target's `@db.http.path`). */
|
|
23
|
+
url: string;
|
|
24
|
+
/**
|
|
25
|
+
* Field on the target that this FK references (from `prop.ref.field`, e.g. "id").
|
|
26
|
+
* This is the value committed to the FK field when the user picks a row.
|
|
27
|
+
*/
|
|
28
|
+
targetField: string;
|
|
29
|
+
/**
|
|
30
|
+
* Static dictionary scope from `@ui.valueHelp`'s filter — AND'd into every
|
|
31
|
+
* picker query. Since 0.1.148.
|
|
32
|
+
*/
|
|
33
|
+
filter?: FilterExpr;
|
|
34
|
+
/**
|
|
35
|
+
* Target fields the filter pins with `=` — constant in the picker, hidden
|
|
36
|
+
* from its columns. Since 0.1.148.
|
|
37
|
+
*/
|
|
38
|
+
pinned?: string[];
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
export { TFormEntryOptions, ValueHelpInfo };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
const require_str = require("../shared/str.cjs");
|
|
2
|
+
//#region src/value-help/value-help-client.ts
|
|
3
|
+
let cacheTtl = 5e3;
|
|
4
|
+
let searches = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
/**
|
|
6
|
+
* How long (ms) a settled value-help search is reused for an identical
|
|
7
|
+
* search on the same client. Default 5000. `0` shares only searches still in
|
|
8
|
+
* flight; a negative value turns sharing off. Server-side rendering never
|
|
9
|
+
* shares (one process serves many viewers).
|
|
10
|
+
*/
|
|
11
|
+
function setValueHelpCacheTtl(ms) {
|
|
12
|
+
cacheTtl = ms;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Drop shared value-help search results — for one `client`, or for every
|
|
16
|
+
* client when omitted. Call after writing to a dictionary outside the
|
|
17
|
+
* built-in table actions to make pickers refetch at once.
|
|
18
|
+
*/
|
|
19
|
+
function invalidateValueHelpCache(client) {
|
|
20
|
+
if (client) searches.get(client)?.clear();
|
|
21
|
+
else searches = /* @__PURE__ */ new WeakMap();
|
|
22
|
+
}
|
|
23
|
+
function now() {
|
|
24
|
+
return typeof performance === "undefined" ? Date.now() : performance.now();
|
|
25
|
+
}
|
|
26
|
+
/** Cache key of a plain-JSON request; `undefined` when it holds anything else (never shared). */
|
|
27
|
+
function requestKey(request) {
|
|
28
|
+
let plain = true;
|
|
29
|
+
try {
|
|
30
|
+
const key = JSON.stringify(request, (_k, v) => {
|
|
31
|
+
if (typeof v === "function" || typeof v === "symbol") plain = false;
|
|
32
|
+
else if (v !== null && typeof v === "object" && !Array.isArray(v)) {
|
|
33
|
+
const proto = Object.getPrototypeOf(v);
|
|
34
|
+
if (proto !== Object.prototype && proto !== null) plain = false;
|
|
35
|
+
}
|
|
36
|
+
return v;
|
|
37
|
+
});
|
|
38
|
+
return plain ? key : void 0;
|
|
39
|
+
} catch {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function sharedQuery(client, request) {
|
|
44
|
+
const run = () => client.query(request);
|
|
45
|
+
if (cacheTtl < 0 || typeof window === "undefined") return run();
|
|
46
|
+
const key = requestKey(request);
|
|
47
|
+
if (key === void 0) return run();
|
|
48
|
+
let byKey = searches.get(client);
|
|
49
|
+
if (!byKey) searches.set(client, byKey = /* @__PURE__ */ new Map());
|
|
50
|
+
const hit = byKey.get(key);
|
|
51
|
+
if (hit && (hit.settledAt === void 0 || now() - hit.settledAt < cacheTtl)) return hit.promise;
|
|
52
|
+
const entry = { promise: run().then((items) => {
|
|
53
|
+
entry.settledAt = now();
|
|
54
|
+
return items;
|
|
55
|
+
}, (err) => {
|
|
56
|
+
if (byKey.get(key) === entry) byKey.delete(key);
|
|
57
|
+
throw err;
|
|
58
|
+
}) };
|
|
59
|
+
byKey.set(key, entry);
|
|
60
|
+
return entry.promise;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Value-help query client. Wraps a `Client` from `@atscript/db-client`
|
|
64
|
+
* with FK-specific search logic (regex fallback for non-searchable tables,
|
|
65
|
+
* $select scoping).
|
|
66
|
+
*
|
|
67
|
+
* Consumers resolve the target's metadata once via `resolveValueHelp(url)`
|
|
68
|
+
* and pass the resulting `ResolvedValueHelp` to `search()`. Label resolution
|
|
69
|
+
* for cells is deliberately unsupported — cells always display raw ids.
|
|
70
|
+
*
|
|
71
|
+
* Identical searches on the same `Client` are shared (see
|
|
72
|
+
* {@link setValueHelpCacheTtl}).
|
|
73
|
+
*/
|
|
74
|
+
var ValueHelpClient = class {
|
|
75
|
+
_client;
|
|
76
|
+
constructor(client) {
|
|
77
|
+
this._client = client;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Search the target with value-help semantics.
|
|
81
|
+
*
|
|
82
|
+
* - If target is searchable → sends `$search` (server full-text)
|
|
83
|
+
* - If not searchable → sends `$or` regex across select fields + exact PK match
|
|
84
|
+
*/
|
|
85
|
+
async search(resolved, opts) {
|
|
86
|
+
const mode = opts?.mode ?? "form";
|
|
87
|
+
const limit = opts?.limit ?? 20;
|
|
88
|
+
const valueField = opts?.valueField;
|
|
89
|
+
const selectFields = opts?.select ?? computeSelectFields(resolved, mode, valueField);
|
|
90
|
+
const text = opts?.text;
|
|
91
|
+
const scope = opts?.filter;
|
|
92
|
+
const controls = {
|
|
93
|
+
$select: selectFields,
|
|
94
|
+
$limit: limit
|
|
95
|
+
};
|
|
96
|
+
let filter = scope;
|
|
97
|
+
if (text) if (resolved.searchable) controls.$search = text;
|
|
98
|
+
else {
|
|
99
|
+
const search = buildOrFilter(text, selectFields, valueField ?? resolved.primaryKeys[0] ?? resolved.labelField);
|
|
100
|
+
filter = scope ? { $and: [scope, search] } : search;
|
|
101
|
+
}
|
|
102
|
+
const items = await sharedQuery(this._client, {
|
|
103
|
+
...filter && { filter },
|
|
104
|
+
controls
|
|
105
|
+
});
|
|
106
|
+
return { items: Array.isArray(items) ? items.slice() : items };
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Compute the $select fields for a value-help query — always including the
|
|
111
|
+
* `valueField` the picker commits (a unique non-PK field is otherwise never fetched).
|
|
112
|
+
*/
|
|
113
|
+
function computeSelectFields(resolved, mode, valueField) {
|
|
114
|
+
const fields = [...resolved.primaryKeys, resolved.labelField];
|
|
115
|
+
if (valueField) fields.push(valueField);
|
|
116
|
+
if (resolved.descrField) fields.push(resolved.descrField);
|
|
117
|
+
if (mode === "filter") fields.push(...resolved.attrFields);
|
|
118
|
+
return [...new Set(fields)];
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Build a Uniquery `$or` filter for value-help search across multiple fields.
|
|
122
|
+
* Uses regex startsWith (case-insensitive) on text fields + exact match on PK.
|
|
123
|
+
*/
|
|
124
|
+
function buildOrFilter(text, fields, pkField) {
|
|
125
|
+
const escaped = require_str.escapeRegex(text);
|
|
126
|
+
const conditions = [];
|
|
127
|
+
for (const field of fields) if (field !== pkField) conditions.push({ [field]: { $regex: `/^${escaped}/i` } });
|
|
128
|
+
const asNum = Number(text);
|
|
129
|
+
if (!Number.isNaN(asNum)) conditions.push({ [pkField]: asNum });
|
|
130
|
+
else conditions.push({ [pkField]: text });
|
|
131
|
+
return { $or: conditions };
|
|
132
|
+
}
|
|
133
|
+
//#endregion
|
|
134
|
+
exports.ValueHelpClient = ValueHelpClient;
|
|
135
|
+
exports.invalidateValueHelpCache = invalidateValueHelpCache;
|
|
136
|
+
exports.setValueHelpCacheTtl = setValueHelpCacheTtl;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ResolvedValueHelp } from "./resolve.cjs";
|
|
2
|
+
import { Client, FilterExpr } from "@atscript/db-client";
|
|
3
|
+
|
|
4
|
+
//#region src/value-help/value-help-client.d.ts
|
|
5
|
+
interface ValueHelpSearchOptions {
|
|
6
|
+
/** Search term. Empty or undefined returns all records. */
|
|
7
|
+
text?: string;
|
|
8
|
+
/** "form" = PK + label + descr; "filter" = all dict fields including attrs. Default: "form". */
|
|
9
|
+
mode?: "form" | "filter";
|
|
10
|
+
/** Max results. Default: 20. */
|
|
11
|
+
limit?: number;
|
|
12
|
+
/** Override the computed select fields. */
|
|
13
|
+
select?: string[];
|
|
14
|
+
/**
|
|
15
|
+
* Static dictionary scope (`ValueHelpInfo.filter`) — AND'd with the search
|
|
16
|
+
* filter and with `$search`. Since 0.1.148.
|
|
17
|
+
*/
|
|
18
|
+
filter?: FilterExpr;
|
|
19
|
+
/**
|
|
20
|
+
* The target field the picker commits (`ValueHelpInfo.targetField`). It is
|
|
21
|
+
* selected, and it is the exact-match key of a non-searchable search — a
|
|
22
|
+
* composite-key dictionary's `primaryKeys[0]` is not the value. Default:
|
|
23
|
+
* `primaryKeys[0]`. Since 0.1.148.
|
|
24
|
+
*/
|
|
25
|
+
valueField?: string;
|
|
26
|
+
}
|
|
27
|
+
interface ValueHelpResult {
|
|
28
|
+
items: Record<string, unknown>[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* How long (ms) a settled value-help search is reused for an identical
|
|
32
|
+
* search on the same client. Default 5000. `0` shares only searches still in
|
|
33
|
+
* flight; a negative value turns sharing off. Server-side rendering never
|
|
34
|
+
* shares (one process serves many viewers).
|
|
35
|
+
*/
|
|
36
|
+
declare function setValueHelpCacheTtl(ms: number): void;
|
|
37
|
+
/**
|
|
38
|
+
* Drop shared value-help search results — for one `client`, or for every
|
|
39
|
+
* client when omitted. Call after writing to a dictionary outside the
|
|
40
|
+
* built-in table actions to make pickers refetch at once.
|
|
41
|
+
*/
|
|
42
|
+
declare function invalidateValueHelpCache(client?: Client): void;
|
|
43
|
+
/**
|
|
44
|
+
* Value-help query client. Wraps a `Client` from `@atscript/db-client`
|
|
45
|
+
* with FK-specific search logic (regex fallback for non-searchable tables,
|
|
46
|
+
* $select scoping).
|
|
47
|
+
*
|
|
48
|
+
* Consumers resolve the target's metadata once via `resolveValueHelp(url)`
|
|
49
|
+
* and pass the resulting `ResolvedValueHelp` to `search()`. Label resolution
|
|
50
|
+
* for cells is deliberately unsupported — cells always display raw ids.
|
|
51
|
+
*
|
|
52
|
+
* Identical searches on the same `Client` are shared (see
|
|
53
|
+
* {@link setValueHelpCacheTtl}).
|
|
54
|
+
*/
|
|
55
|
+
declare class ValueHelpClient {
|
|
56
|
+
private readonly _client;
|
|
57
|
+
constructor(client: Client);
|
|
58
|
+
/**
|
|
59
|
+
* Search the target with value-help semantics.
|
|
60
|
+
*
|
|
61
|
+
* - If target is searchable → sends `$search` (server full-text)
|
|
62
|
+
* - If not searchable → sends `$or` regex across select fields + exact PK match
|
|
63
|
+
*/
|
|
64
|
+
search(resolved: ResolvedValueHelp, opts?: ValueHelpSearchOptions): Promise<ValueHelpResult>;
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { ValueHelpClient, ValueHelpResult, ValueHelpSearchOptions, invalidateValueHelpCache, setValueHelpCacheTtl };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ResolvedValueHelp } from "./resolve.mjs";
|
|
2
|
+
import { Client, FilterExpr } from "@atscript/db-client";
|
|
3
|
+
|
|
4
|
+
//#region src/value-help/value-help-client.d.ts
|
|
5
|
+
interface ValueHelpSearchOptions {
|
|
6
|
+
/** Search term. Empty or undefined returns all records. */
|
|
7
|
+
text?: string;
|
|
8
|
+
/** "form" = PK + label + descr; "filter" = all dict fields including attrs. Default: "form". */
|
|
9
|
+
mode?: "form" | "filter";
|
|
10
|
+
/** Max results. Default: 20. */
|
|
11
|
+
limit?: number;
|
|
12
|
+
/** Override the computed select fields. */
|
|
13
|
+
select?: string[];
|
|
14
|
+
/**
|
|
15
|
+
* Static dictionary scope (`ValueHelpInfo.filter`) — AND'd with the search
|
|
16
|
+
* filter and with `$search`. Since 0.1.148.
|
|
17
|
+
*/
|
|
18
|
+
filter?: FilterExpr;
|
|
19
|
+
/**
|
|
20
|
+
* The target field the picker commits (`ValueHelpInfo.targetField`). It is
|
|
21
|
+
* selected, and it is the exact-match key of a non-searchable search — a
|
|
22
|
+
* composite-key dictionary's `primaryKeys[0]` is not the value. Default:
|
|
23
|
+
* `primaryKeys[0]`. Since 0.1.148.
|
|
24
|
+
*/
|
|
25
|
+
valueField?: string;
|
|
26
|
+
}
|
|
27
|
+
interface ValueHelpResult {
|
|
28
|
+
items: Record<string, unknown>[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* How long (ms) a settled value-help search is reused for an identical
|
|
32
|
+
* search on the same client. Default 5000. `0` shares only searches still in
|
|
33
|
+
* flight; a negative value turns sharing off. Server-side rendering never
|
|
34
|
+
* shares (one process serves many viewers).
|
|
35
|
+
*/
|
|
36
|
+
declare function setValueHelpCacheTtl(ms: number): void;
|
|
37
|
+
/**
|
|
38
|
+
* Drop shared value-help search results — for one `client`, or for every
|
|
39
|
+
* client when omitted. Call after writing to a dictionary outside the
|
|
40
|
+
* built-in table actions to make pickers refetch at once.
|
|
41
|
+
*/
|
|
42
|
+
declare function invalidateValueHelpCache(client?: Client): void;
|
|
43
|
+
/**
|
|
44
|
+
* Value-help query client. Wraps a `Client` from `@atscript/db-client`
|
|
45
|
+
* with FK-specific search logic (regex fallback for non-searchable tables,
|
|
46
|
+
* $select scoping).
|
|
47
|
+
*
|
|
48
|
+
* Consumers resolve the target's metadata once via `resolveValueHelp(url)`
|
|
49
|
+
* and pass the resulting `ResolvedValueHelp` to `search()`. Label resolution
|
|
50
|
+
* for cells is deliberately unsupported — cells always display raw ids.
|
|
51
|
+
*
|
|
52
|
+
* Identical searches on the same `Client` are shared (see
|
|
53
|
+
* {@link setValueHelpCacheTtl}).
|
|
54
|
+
*/
|
|
55
|
+
declare class ValueHelpClient {
|
|
56
|
+
private readonly _client;
|
|
57
|
+
constructor(client: Client);
|
|
58
|
+
/**
|
|
59
|
+
* Search the target with value-help semantics.
|
|
60
|
+
*
|
|
61
|
+
* - If target is searchable → sends `$search` (server full-text)
|
|
62
|
+
* - If not searchable → sends `$or` regex across select fields + exact PK match
|
|
63
|
+
*/
|
|
64
|
+
search(resolved: ResolvedValueHelp, opts?: ValueHelpSearchOptions): Promise<ValueHelpResult>;
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { ValueHelpClient, ValueHelpResult, ValueHelpSearchOptions, invalidateValueHelpCache, setValueHelpCacheTtl };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { escapeRegex } from "../shared/str.mjs";
|
|
2
|
+
//#region src/value-help/value-help-client.ts
|
|
3
|
+
let cacheTtl = 5e3;
|
|
4
|
+
let searches = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
/**
|
|
6
|
+
* How long (ms) a settled value-help search is reused for an identical
|
|
7
|
+
* search on the same client. Default 5000. `0` shares only searches still in
|
|
8
|
+
* flight; a negative value turns sharing off. Server-side rendering never
|
|
9
|
+
* shares (one process serves many viewers).
|
|
10
|
+
*/
|
|
11
|
+
function setValueHelpCacheTtl(ms) {
|
|
12
|
+
cacheTtl = ms;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Drop shared value-help search results — for one `client`, or for every
|
|
16
|
+
* client when omitted. Call after writing to a dictionary outside the
|
|
17
|
+
* built-in table actions to make pickers refetch at once.
|
|
18
|
+
*/
|
|
19
|
+
function invalidateValueHelpCache(client) {
|
|
20
|
+
if (client) searches.get(client)?.clear();
|
|
21
|
+
else searches = /* @__PURE__ */ new WeakMap();
|
|
22
|
+
}
|
|
23
|
+
function now() {
|
|
24
|
+
return typeof performance === "undefined" ? Date.now() : performance.now();
|
|
25
|
+
}
|
|
26
|
+
/** Cache key of a plain-JSON request; `undefined` when it holds anything else (never shared). */
|
|
27
|
+
function requestKey(request) {
|
|
28
|
+
let plain = true;
|
|
29
|
+
try {
|
|
30
|
+
const key = JSON.stringify(request, (_k, v) => {
|
|
31
|
+
if (typeof v === "function" || typeof v === "symbol") plain = false;
|
|
32
|
+
else if (v !== null && typeof v === "object" && !Array.isArray(v)) {
|
|
33
|
+
const proto = Object.getPrototypeOf(v);
|
|
34
|
+
if (proto !== Object.prototype && proto !== null) plain = false;
|
|
35
|
+
}
|
|
36
|
+
return v;
|
|
37
|
+
});
|
|
38
|
+
return plain ? key : void 0;
|
|
39
|
+
} catch {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function sharedQuery(client, request) {
|
|
44
|
+
const run = () => client.query(request);
|
|
45
|
+
if (cacheTtl < 0 || typeof window === "undefined") return run();
|
|
46
|
+
const key = requestKey(request);
|
|
47
|
+
if (key === void 0) return run();
|
|
48
|
+
let byKey = searches.get(client);
|
|
49
|
+
if (!byKey) searches.set(client, byKey = /* @__PURE__ */ new Map());
|
|
50
|
+
const hit = byKey.get(key);
|
|
51
|
+
if (hit && (hit.settledAt === void 0 || now() - hit.settledAt < cacheTtl)) return hit.promise;
|
|
52
|
+
const entry = { promise: run().then((items) => {
|
|
53
|
+
entry.settledAt = now();
|
|
54
|
+
return items;
|
|
55
|
+
}, (err) => {
|
|
56
|
+
if (byKey.get(key) === entry) byKey.delete(key);
|
|
57
|
+
throw err;
|
|
58
|
+
}) };
|
|
59
|
+
byKey.set(key, entry);
|
|
60
|
+
return entry.promise;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Value-help query client. Wraps a `Client` from `@atscript/db-client`
|
|
64
|
+
* with FK-specific search logic (regex fallback for non-searchable tables,
|
|
65
|
+
* $select scoping).
|
|
66
|
+
*
|
|
67
|
+
* Consumers resolve the target's metadata once via `resolveValueHelp(url)`
|
|
68
|
+
* and pass the resulting `ResolvedValueHelp` to `search()`. Label resolution
|
|
69
|
+
* for cells is deliberately unsupported — cells always display raw ids.
|
|
70
|
+
*
|
|
71
|
+
* Identical searches on the same `Client` are shared (see
|
|
72
|
+
* {@link setValueHelpCacheTtl}).
|
|
73
|
+
*/
|
|
74
|
+
var ValueHelpClient = class {
|
|
75
|
+
_client;
|
|
76
|
+
constructor(client) {
|
|
77
|
+
this._client = client;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Search the target with value-help semantics.
|
|
81
|
+
*
|
|
82
|
+
* - If target is searchable → sends `$search` (server full-text)
|
|
83
|
+
* - If not searchable → sends `$or` regex across select fields + exact PK match
|
|
84
|
+
*/
|
|
85
|
+
async search(resolved, opts) {
|
|
86
|
+
const mode = opts?.mode ?? "form";
|
|
87
|
+
const limit = opts?.limit ?? 20;
|
|
88
|
+
const valueField = opts?.valueField;
|
|
89
|
+
const selectFields = opts?.select ?? computeSelectFields(resolved, mode, valueField);
|
|
90
|
+
const text = opts?.text;
|
|
91
|
+
const scope = opts?.filter;
|
|
92
|
+
const controls = {
|
|
93
|
+
$select: selectFields,
|
|
94
|
+
$limit: limit
|
|
95
|
+
};
|
|
96
|
+
let filter = scope;
|
|
97
|
+
if (text) if (resolved.searchable) controls.$search = text;
|
|
98
|
+
else {
|
|
99
|
+
const search = buildOrFilter(text, selectFields, valueField ?? resolved.primaryKeys[0] ?? resolved.labelField);
|
|
100
|
+
filter = scope ? { $and: [scope, search] } : search;
|
|
101
|
+
}
|
|
102
|
+
const items = await sharedQuery(this._client, {
|
|
103
|
+
...filter && { filter },
|
|
104
|
+
controls
|
|
105
|
+
});
|
|
106
|
+
return { items: Array.isArray(items) ? items.slice() : items };
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Compute the $select fields for a value-help query — always including the
|
|
111
|
+
* `valueField` the picker commits (a unique non-PK field is otherwise never fetched).
|
|
112
|
+
*/
|
|
113
|
+
function computeSelectFields(resolved, mode, valueField) {
|
|
114
|
+
const fields = [...resolved.primaryKeys, resolved.labelField];
|
|
115
|
+
if (valueField) fields.push(valueField);
|
|
116
|
+
if (resolved.descrField) fields.push(resolved.descrField);
|
|
117
|
+
if (mode === "filter") fields.push(...resolved.attrFields);
|
|
118
|
+
return [...new Set(fields)];
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Build a Uniquery `$or` filter for value-help search across multiple fields.
|
|
122
|
+
* Uses regex startsWith (case-insensitive) on text fields + exact match on PK.
|
|
123
|
+
*/
|
|
124
|
+
function buildOrFilter(text, fields, pkField) {
|
|
125
|
+
const escaped = escapeRegex(text);
|
|
126
|
+
const conditions = [];
|
|
127
|
+
for (const field of fields) if (field !== pkField) conditions.push({ [field]: { $regex: `/^${escaped}/i` } });
|
|
128
|
+
const asNum = Number(text);
|
|
129
|
+
if (!Number.isNaN(asNum)) conditions.push({ [pkField]: asNum });
|
|
130
|
+
else conditions.push({ [pkField]: text });
|
|
131
|
+
return { $or: conditions };
|
|
132
|
+
}
|
|
133
|
+
//#endregion
|
|
134
|
+
export { ValueHelpClient, invalidateValueHelpCache, setValueHelpCacheTtl };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/ui",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.165",
|
|
4
4
|
"description": "Framework-agnostic runtime for form and table definitions from atscript annotated types",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"annotations",
|
|
@@ -41,26 +41,31 @@
|
|
|
41
41
|
"import": "./dist/plugin.mjs",
|
|
42
42
|
"require": "./dist/plugin.cjs"
|
|
43
43
|
},
|
|
44
|
+
"./messages": {
|
|
45
|
+
"types": "./dist/messages.d.mts",
|
|
46
|
+
"import": "./dist/messages.mjs",
|
|
47
|
+
"require": "./dist/messages.cjs"
|
|
48
|
+
},
|
|
44
49
|
"./package.json": "./package.json"
|
|
45
50
|
},
|
|
46
51
|
"publishConfig": {
|
|
47
52
|
"access": "public"
|
|
48
53
|
},
|
|
49
54
|
"dependencies": {
|
|
50
|
-
"@atscript/db-client": "^0.1.
|
|
55
|
+
"@atscript/db-client": "^0.1.170"
|
|
51
56
|
},
|
|
52
57
|
"devDependencies": {
|
|
53
|
-
"@atscript/core": "^0.2.
|
|
54
|
-
"@atscript/db": "^0.1.
|
|
55
|
-
"@atscript/typescript": "^0.2.
|
|
56
|
-
"@uniqu/core": "^0.1
|
|
57
|
-
"unplugin-atscript": "^0.2.
|
|
58
|
+
"@atscript/core": "^0.2.7",
|
|
59
|
+
"@atscript/db": "^0.1.170",
|
|
60
|
+
"@atscript/typescript": "^0.2.7",
|
|
61
|
+
"@uniqu/core": "^0.2.1",
|
|
62
|
+
"unplugin-atscript": "^0.2.7",
|
|
58
63
|
"vitest": "npm:@voidzero-dev/vite-plus-test@0.1.14"
|
|
59
64
|
},
|
|
60
65
|
"peerDependencies": {
|
|
61
|
-
"@atscript/core": "^0.2.
|
|
62
|
-
"@atscript/db": "^0.1.
|
|
63
|
-
"@atscript/typescript": "^0.2.
|
|
66
|
+
"@atscript/core": "^0.2.7",
|
|
67
|
+
"@atscript/db": "^0.1.170",
|
|
68
|
+
"@atscript/typescript": "^0.2.7"
|
|
64
69
|
},
|
|
65
70
|
"scripts": {
|
|
66
71
|
"build": "vp pack",
|