@sincpro/web-ui 2.10.0 → 2.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -4
- package/dist/components/activity/Messages.d.ts +3 -23
- package/dist/components/activity/Messages.js +3 -17
- package/dist/components/activity/index.d.ts +2 -1
- package/dist/components/activity/index.js +2 -1
- package/dist/components/activity/messageKind.d.ts +23 -0
- package/dist/components/activity/messageKind.js +17 -0
- package/dist/components/assist/assist.d.ts +1 -1
- package/dist/components/assist/assist.js +1 -1
- package/dist/components/attachments/FileValue.d.ts +10 -0
- package/dist/components/attachments/FileValue.js +40 -0
- package/dist/components/attachments/index.d.ts +1 -0
- package/dist/components/attachments/index.js +3 -1
- package/dist/components/frame/model.js +1 -1
- package/dist/components/guide/index.d.ts +2 -1
- package/dist/components/guide/index.js +2 -1
- package/dist/components/guide/selector.d.ts +25 -0
- package/dist/components/guide/selector.js +14 -0
- package/dist/components/guide/target.d.ts +3 -25
- package/dist/components/guide/target.js +2 -14
- package/dist/components/listing/AxisLevels.d.ts +2 -5
- package/dist/components/listing/AxisLevels.js +5 -22
- package/dist/components/listing/ColumnsMenu.d.ts +3 -7
- package/dist/components/listing/ColumnsMenu.js +4 -8
- package/dist/components/listing/collapsed.d.ts +2 -0
- package/dist/components/listing/collapsed.js +8 -0
- package/dist/components/listing/columnSpecs.d.ts +34 -0
- package/dist/components/listing/columnSpecs.js +54 -0
- package/dist/components/listing/columns.d.ts +3 -21
- package/dist/components/listing/columns.js +2 -39
- package/dist/components/listing/index.d.ts +9 -5
- package/dist/components/listing/index.js +13 -6
- package/dist/components/listing/levels.d.ts +9 -0
- package/dist/components/listing/levels.js +27 -0
- package/dist/components/listing/pageSizes.d.ts +9 -0
- package/dist/components/listing/pageSizes.js +11 -0
- package/dist/components/listing/pages.d.ts +2 -9
- package/dist/components/listing/pages.js +5 -9
- package/dist/components/listing/tree.d.ts +2 -1
- package/dist/components/listing/tree.js +4 -5
- package/dist/components/preview/FilePreview.d.ts +3 -3
- package/dist/components/preview/FilePreview.js +3 -15
- package/dist/components/preview/index.d.ts +2 -1
- package/dist/components/preview/index.js +2 -1
- package/dist/components/preview/previewKind.d.ts +3 -0
- package/dist/components/preview/previewKind.js +17 -0
- package/dist/components/search/FilterBuilder.d.ts +5 -139
- package/dist/components/search/FilterBuilder.js +5 -579
- package/dist/components/search/JsonEditor.d.ts +2 -8
- package/dist/components/search/JsonEditor.js +5 -15
- package/dist/components/search/RuleValue.d.ts +2 -2
- package/dist/components/search/RuleValue.js +5 -4
- package/dist/components/search/datePeriods.js +1 -1
- package/dist/components/search/filterTree.d.ts +183 -0
- package/dist/components/search/filterTree.js +570 -0
- package/dist/components/search/formatJson.d.ts +8 -0
- package/dist/components/search/formatJson.js +17 -0
- package/dist/components/search/index.d.ts +5 -3
- package/dist/components/search/index.js +7 -3
- package/dist/components/search/query.js +1 -1
- package/dist/foundations/calendar.js +1 -1
- package/dist/foundations/catalog.d.ts +7 -8
- package/dist/foundations/catalog.js +10 -35
- package/dist/foundations/chart.js +1 -1
- package/dist/foundations/controlName.d.ts +12 -0
- package/dist/foundations/controlName.js +14 -0
- package/dist/foundations/copy.d.ts +3 -1130
- package/dist/foundations/copy.es.d.ts +1 -1
- package/dist/foundations/copy.js +13 -747
- package/dist/foundations/copyStore.d.ts +21 -0
- package/dist/foundations/copyStore.js +47 -0
- package/dist/foundations/density.d.ts +4 -8
- package/dist/foundations/density.js +2 -9
- package/dist/foundations/form.d.ts +2 -12
- package/dist/foundations/form.js +4 -14
- package/dist/foundations/index.d.ts +18 -11
- package/dist/foundations/index.js +21 -11
- package/dist/foundations/keys.d.ts +8 -0
- package/dist/foundations/keys.js +6 -0
- package/dist/foundations/pickBy.d.ts +16 -0
- package/dist/foundations/pickBy.js +17 -0
- package/dist/foundations/pivotSheet.js +0 -0
- package/dist/foundations/shortcut.d.ts +3 -9
- package/dist/foundations/shortcut.js +2 -7
- package/dist/foundations/sticky.d.ts +2 -6
- package/dist/foundations/sticky.js +5 -8
- package/dist/foundations/stickyStyle.d.ts +12 -0
- package/dist/foundations/stickyStyle.js +13 -0
- package/dist/foundations/surface.d.ts +2 -2
- package/dist/foundations/surface.js +4 -2
- package/dist/foundations/theme.d.ts +3 -13
- package/dist/foundations/theme.js +2 -15
- package/dist/foundations/themeChoice.d.ts +19 -0
- package/dist/foundations/themeChoice.js +22 -0
- package/dist/foundations/types.d.ts +3 -0
- package/dist/foundations/uiCopy.d.ts +1135 -0
- package/dist/foundations/uiCopy.js +749 -0
- package/dist/foundations/xlsx.d.ts +4 -10
- package/dist/foundations/xlsx.js +0 -0
- package/dist/foundations/xlsxDownload.d.ts +8 -0
- package/dist/foundations/xlsxDownload.js +28 -0
- package/dist/primitives/button/Button.d.ts +3 -15
- package/dist/primitives/button/Button.js +2 -65
- package/dist/primitives/button/classes.d.ts +21 -0
- package/dist/primitives/button/classes.js +65 -0
- package/dist/primitives/button/index.d.ts +2 -1
- package/dist/primitives/button/index.js +3 -1
- package/dist/primitives/field/Field.d.ts +2 -6
- package/dist/primitives/field/Field.js +4 -10
- package/dist/primitives/field/classes.d.ts +6 -0
- package/dist/primitives/field/classes.js +13 -0
- package/dist/primitives/field/index.d.ts +2 -1
- package/dist/primitives/field/index.js +3 -1
- package/dist/primitives/menu/Menu.d.ts +2 -19
- package/dist/primitives/menu/Menu.js +4 -30
- package/dist/primitives/menu/classes.d.ts +18 -0
- package/dist/primitives/menu/classes.js +33 -0
- package/dist/primitives/menu/index.d.ts +2 -1
- package/dist/primitives/menu/index.js +3 -1
- package/dist/primitives/table/EditableTable.d.ts +3 -37
- package/dist/primitives/table/EditableTable.js +2 -47
- package/dist/primitives/table/Table.d.ts +2 -4
- package/dist/primitives/table/Table.js +4 -11
- package/dist/primitives/table/cellTone.d.ts +5 -0
- package/dist/primitives/table/cellTone.js +13 -0
- package/dist/primitives/table/editRules.d.ts +43 -0
- package/dist/primitives/table/editRules.js +53 -0
- package/dist/primitives/table/index.d.ts +4 -2
- package/dist/primitives/table/index.js +4 -2
- package/dist/primitives/tag/Count.d.ts +2 -3
- package/dist/primitives/tag/Count.js +4 -5
- package/dist/primitives/tag/countText.d.ts +3 -0
- package/dist/primitives/tag/countText.js +7 -0
- package/dist/primitives/tag/index.d.ts +2 -1
- package/dist/primitives/tag/index.js +2 -1
- package/dist/primitives/value/DateValue.d.ts +2 -5
- package/dist/primitives/value/DateValue.js +6 -44
- package/dist/primitives/value/Meter.d.ts +3 -8
- package/dist/primitives/value/Meter.js +2 -9
- package/dist/primitives/value/RichText.d.ts +2 -4
- package/dist/primitives/value/RichText.js +5 -20
- package/dist/primitives/value/Value.d.ts +10 -7
- package/dist/primitives/value/Value.js +7 -24
- package/dist/primitives/value/format.d.ts +16 -0
- package/dist/primitives/value/format.js +81 -0
- package/dist/primitives/value/index.d.ts +7 -4
- package/dist/primitives/value/index.js +9 -4
- package/dist/primitives/value/meterLevel.d.ts +10 -0
- package/dist/primitives/value/meterLevel.js +12 -0
- package/dist/primitives/value/richTextPlain.d.ts +4 -0
- package/dist/primitives/value/richTextPlain.js +23 -0
- package/package.json +1 -1
|
@@ -2,468 +2,20 @@
|
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
3
|
import { useSearchSettings, } from "./context.js";
|
|
4
4
|
import { FieldPath, useFieldPathSteps, useModelCache, } from "./FieldPath.js";
|
|
5
|
+
import { blankCondition, blankGroup, crossedOf, describeFilter, inputType, isRecognizedFilter, liftRecords, MATCHES, namesOf, pathsToLift, reachesOne, recordOps, stringifyFilter, treeFromFilter, treeToFilter, WITH_RANGE, withNames, WITHOUT_VALUE, } from "./filterTree.js";
|
|
5
6
|
import { JoinWord } from "./JoinWord.js";
|
|
6
7
|
import { LazyJsonEditor } from "./LazyJsonEditor.js";
|
|
7
8
|
import { fieldLabel, filterableNames, isFilterable, } from "./model.js";
|
|
8
9
|
import { isRelation, isToMany, } from "./paths.js";
|
|
9
|
-
import { opLabel
|
|
10
|
-
import {
|
|
10
|
+
import { opLabel } from "./query.js";
|
|
11
|
+
import { recordTarget, } from "./records.js";
|
|
11
12
|
import { RuleValue } from "./RuleValue.js";
|
|
12
|
-
import {
|
|
13
|
+
import { fillCopy, useUiCopy } from "../../foundations/copy.js";
|
|
13
14
|
import { useSurface } from "../../foundations/surface.js";
|
|
14
15
|
import { Button } from "../../primitives/button/index.js";
|
|
15
16
|
import { FolderTree, Plus, Trash2 } from "lucide-react";
|
|
16
17
|
import { useEffect, useState } from "react";
|
|
17
|
-
|
|
18
|
-
* A FILTER SOMEBODY BUILDS, FROM WHAT THE MODEL SAYS IT ACCEPTS.
|
|
19
|
-
*
|
|
20
|
-
* The named cuts alongside —«vencidas», «con entregas pendientes»— are the
|
|
21
|
-
* questions somebody already thought of. This is for the rest: choose a field, an
|
|
22
|
-
* operator, write a value — group them with AND or with OR, nest a group
|
|
23
|
-
* inside another as many times as needed, or write the
|
|
24
|
-
* JSON directly to copy or paste a whole criteria.
|
|
25
|
-
*
|
|
26
|
-
* **Nothing here is written by hand.** The fields come from the model
|
|
27
|
-
* definition the server sends, and so do each one's operators: a
|
|
28
|
-
* text field offers «contiene», a numeric one offers «>», one that admits
|
|
29
|
-
* empty offers «está vacío». Adding a column makes it appear here, with
|
|
30
|
-
* its correct operators, without touching this file.
|
|
31
|
-
*
|
|
32
|
-
* **A tree, not a list.** Each group —the root included— has its own
|
|
33
|
-
* «todas / cualquiera» and its own children: a condition, or another group
|
|
34
|
-
* nested inside. The tree icon adds a new group beside the
|
|
35
|
-
* row that was touched; the `+` adds another condition.
|
|
36
|
-
*
|
|
37
|
-
* **One component renders and builds — and there is always a JSON
|
|
38
|
-
* beside it.** The tree and the code editor are two views of the SAME
|
|
39
|
-
* criteria, synchronised in both directions: touching the tree rewrites
|
|
40
|
-
* the JSON, and writing a recognisable JSON rebuilds the tree. The only thing the
|
|
41
|
-
* tree does not know how to build or show is a NOT: if the filter brings one at
|
|
42
|
-
* any level, it is drawn read-only with `ExpressionView` in its
|
|
43
|
-
* place, and the JSON remains the way to touch it.
|
|
44
|
-
* ====================================================================== */
|
|
45
|
-
/* What an operator is called for whoever reads it: `copy.ops`, which is the SAME table the
|
|
46
|
-
bar uses when building the question. Two tables means the same comparison is called two ways
|
|
47
|
-
on the same screen. */
|
|
48
|
-
/** Which operators take no value: asking whether something is empty needs nothing written. */
|
|
49
|
-
const WITHOUT_VALUE = new Set(["is null", "is not null"]);
|
|
50
|
-
/** Which operators compare against TWO values. «Entre el 1 y el 31» is the question everybody
|
|
51
|
-
* asks about dates, and writing it as two loose rules is the way to
|
|
52
|
-
* get one of the two ends wrong. */
|
|
53
|
-
const WITH_RANGE = new Set(["between"]);
|
|
54
|
-
/** What kind of input asks for a value, read from the field's type. */
|
|
55
|
-
function inputType(type) {
|
|
56
|
-
if (type === "integer" || type === "number")
|
|
57
|
-
return "number";
|
|
58
|
-
if (type === "date")
|
|
59
|
-
return "date";
|
|
60
|
-
if (type === "datetime")
|
|
61
|
-
return "datetime-local";
|
|
62
|
-
return "text";
|
|
63
|
-
}
|
|
64
|
-
function isCondition(value) {
|
|
65
|
-
return (typeof value === "object" &&
|
|
66
|
-
value !== null &&
|
|
67
|
-
!Array.isArray(value) &&
|
|
68
|
-
typeof value.field === "string" &&
|
|
69
|
-
typeof value.operator === "string" &&
|
|
70
|
-
"value" in value);
|
|
71
|
-
}
|
|
72
|
-
/** Whether this is one of the shapes the filter recognises — condition, or `all`/`any`/
|
|
73
|
-
* `negate` group. A valid JSON that is none of these —`{}`, `{"foo": 1}`, a loose array—
|
|
74
|
-
* is not a filter this component knows how to draw, and is treated as a shape error instead of
|
|
75
|
-
* trying to show anything at all. */
|
|
76
|
-
export function isRecognizedFilter(value) {
|
|
77
|
-
if (isCondition(value))
|
|
78
|
-
return true;
|
|
79
|
-
if (typeof value !== "object" || value === null)
|
|
80
|
-
return false;
|
|
81
|
-
const record = value;
|
|
82
|
-
if (Array.isArray(record.all))
|
|
83
|
-
return true;
|
|
84
|
-
if (Array.isArray(record.any))
|
|
85
|
-
return true;
|
|
86
|
-
if ("negate" in record)
|
|
87
|
-
return true;
|
|
88
|
-
if (isThrough(record))
|
|
89
|
-
return true;
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
const MATCHES = ["any", "none", "all"];
|
|
93
|
-
function isThrough(value) {
|
|
94
|
-
if (typeof value !== "object" || value === null)
|
|
95
|
-
return false;
|
|
96
|
-
const record = value;
|
|
97
|
-
return typeof record.through === "string" && "selection" in record;
|
|
98
|
-
}
|
|
99
|
-
function isNegate(value) {
|
|
100
|
-
return typeof value === "object" && value !== null && "negate" in value;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* A relation crossed, read off criteria's shape: the quantifier is not a key, it is the
|
|
104
|
-
* sentence the `negate`s around the `through` make.
|
|
105
|
-
*
|
|
106
|
-
* {through, selection: s} «alguno» s
|
|
107
|
-
* {negate: {through, selection: s}} «ninguno» s
|
|
108
|
-
* {negate: {through, selection: {negate: s}}} «todos» s (none that does not)
|
|
109
|
-
*
|
|
110
|
-
* «Ninguno con no-s» and «todos con s» are the same filter; it reads as the second, the
|
|
111
|
-
* shorter sentence, and that is also how a path of two hops comes back: «ninguna línea con
|
|
112
|
-
* ningún impuesto» returns as «todas las líneas con algún impuesto», which filters the same.
|
|
113
|
-
* Anything else is not a relation crossed: `null`.
|
|
114
|
-
*/
|
|
115
|
-
function crossedOf(value) {
|
|
116
|
-
if (isThrough(value)) {
|
|
117
|
-
return { through: value.through, match: "any", selection: value.selection };
|
|
118
|
-
}
|
|
119
|
-
if (!isNegate(value) || !isThrough(value.negate))
|
|
120
|
-
return null;
|
|
121
|
-
const { through, selection } = value.negate;
|
|
122
|
-
return isNegate(selection)
|
|
123
|
-
? { through, match: "all", selection: selection.negate }
|
|
124
|
-
: { through, match: "none", selection };
|
|
125
|
-
}
|
|
126
|
-
/** The other way: a relation and its quantifier, written the way criteria reads them. */
|
|
127
|
-
function crossing(through, match, inner) {
|
|
128
|
-
if (match === "none")
|
|
129
|
-
return { negate: { through, selection: inner } };
|
|
130
|
-
if (match === "all")
|
|
131
|
-
return { negate: { through, selection: { negate: inner } } };
|
|
132
|
-
return { through, selection: inner };
|
|
133
|
-
}
|
|
134
|
-
/** Whether a «some» crossing reaches ONE record: a to-one relation, as the model says. Then
|
|
135
|
-
* it is said as a path («Cliente > País»), since «alguno de» would be noise. Without the
|
|
136
|
-
* model it is said as what `{through, selection}` means — «alguno de» —, never wrong. */
|
|
137
|
-
function reachesOne(model, crossed) {
|
|
138
|
-
const field = model?.fields[crossed.through];
|
|
139
|
-
return crossed.match === "any" && field !== undefined && !isToMany(field);
|
|
140
|
-
}
|
|
141
|
-
export function blankCondition(model, first) {
|
|
142
|
-
const field = first ?? "";
|
|
143
|
-
return {
|
|
144
|
-
kind: "condition",
|
|
145
|
-
field,
|
|
146
|
-
operator: model.fields[field]?.ops[0] ?? "=",
|
|
147
|
-
value: "",
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
export function blankGroup(model, first) {
|
|
151
|
-
return { kind: "group", join: "all", children: [blankCondition(model, first)] };
|
|
152
|
-
}
|
|
153
|
-
/** The tree as the filter that travels.
|
|
154
|
-
*
|
|
155
|
-
* in a lone condition
|
|
156
|
-
* out that condition, unwrapped
|
|
157
|
-
*
|
|
158
|
-
* in group with two children, join «any»
|
|
159
|
-
* out {any: [ … , … ]}
|
|
160
|
-
*
|
|
161
|
-
* A group with a single child is not wrapped —it is that child—, and a group with no children disappears: neither
|
|
162
|
-
* an empty «todas» nor «cualquiera» is a filter anybody asked for. The rule holds at any
|
|
163
|
-
* level, not only at the root, so nesting a single-condition group is the same as not
|
|
164
|
-
* having nested it. */
|
|
165
|
-
export function treeToFilter(node) {
|
|
166
|
-
if (node.kind === "condition") {
|
|
167
|
-
const leaf = conditionToFilter(node);
|
|
168
|
-
return leaf === undefined ? undefined : wrapThrough(node.through ?? [], leaf);
|
|
169
|
-
}
|
|
170
|
-
return groupToFilter(node);
|
|
171
|
-
}
|
|
172
|
-
/** The relations of a path, as nested `through` nodes around the condition: the first hop
|
|
173
|
-
* outside, the last one next to the condition — the order the path reads in. */
|
|
174
|
-
function wrapThrough(hops, leaf) {
|
|
175
|
-
return hops.reduceRight((inner, hop) => crossing(hop.field, hop.match, inner), leaf);
|
|
176
|
-
}
|
|
177
|
-
function conditionToFilter(node) {
|
|
178
|
-
if (node.field === "")
|
|
179
|
-
return undefined;
|
|
180
|
-
/* A rule about records: by key, the way the relation is compared — directly by its column
|
|
181
|
-
(a many2one) or crossing it (a to-many). «Está vacío» asks the column. */
|
|
182
|
-
if (node.target !== undefined) {
|
|
183
|
-
if (WITHOUT_VALUE.has(node.operator)) {
|
|
184
|
-
return node.target.column === undefined
|
|
185
|
-
? undefined
|
|
186
|
-
: { field: node.target.column, operator: node.operator, value: true };
|
|
187
|
-
}
|
|
188
|
-
const keys = (node.values ?? []).map((one) => one.id);
|
|
189
|
-
if (keys.length === 0)
|
|
190
|
-
return undefined;
|
|
191
|
-
return recordsFilter(node.target, keys, node.operator === "!=" || node.operator === "not in");
|
|
192
|
-
}
|
|
193
|
-
if (WITHOUT_VALUE.has(node.operator)) {
|
|
194
|
-
return { field: node.field, operator: node.operator, value: true };
|
|
195
|
-
}
|
|
196
|
-
if (WITH_RANGE.has(node.operator)) {
|
|
197
|
-
/* A half-written range is not half a range: while one end is missing, the rule does not
|
|
198
|
-
travel. */
|
|
199
|
-
if (node.value === "" || (node.upper ?? "") === "")
|
|
200
|
-
return undefined;
|
|
201
|
-
return { field: node.field, operator: node.operator, value: [node.value, node.upper] };
|
|
202
|
-
}
|
|
203
|
-
/* Chosen values travel as what they are —a list for «está en», the one for «es»—, by key. */
|
|
204
|
-
if (node.values !== undefined && node.values.length > 0) {
|
|
205
|
-
const keys = node.values.map((one) => one.id);
|
|
206
|
-
return {
|
|
207
|
-
field: node.field,
|
|
208
|
-
operator: node.operator,
|
|
209
|
-
value: node.operator === "in" || node.operator === "not in" ? keys : keys[0],
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
if (node.raw !== undefined) {
|
|
213
|
-
return { field: node.field, operator: node.operator, value: node.raw };
|
|
214
|
-
}
|
|
215
|
-
/* **A rule with no value does not filter.** It used to travel with `value: ""`, and that is not «not
|
|
216
|
-
written anything yet»: against an `=` it is «the field is the empty string», which empties the list
|
|
217
|
-
while somebody is still typing, and against a `like` it is «contains nothing», which leaves it
|
|
218
|
-
the same. To ask for what is empty there is the «está vacío» operator, which the
|
|
219
|
-
model publishes on the fields that admit it — explicit, and not by omission. */
|
|
220
|
-
if (node.value === "")
|
|
221
|
-
return undefined;
|
|
222
|
-
return { field: node.field, operator: node.operator, value: node.value };
|
|
223
|
-
}
|
|
224
|
-
function groupToFilter(node) {
|
|
225
|
-
const kept = node.children
|
|
226
|
-
.map(treeToFilter)
|
|
227
|
-
.filter((one) => one !== undefined);
|
|
228
|
-
if (kept.length === 0)
|
|
229
|
-
return undefined;
|
|
230
|
-
/* «Ninguna» negates the WHOLE group and not each child on its own: «ni vencida ni
|
|
231
|
-
grande» is `not (vencida or grande)`. And a group of one is not dismantled
|
|
232
|
-
here either —dismantling it would lose the negation—, which is what the bar does. */
|
|
233
|
-
if (node.join === "not") {
|
|
234
|
-
return { negate: kept.length === 1 ? kept[0] : { any: kept } };
|
|
235
|
-
}
|
|
236
|
-
if (kept.length === 1)
|
|
237
|
-
return kept[0];
|
|
238
|
-
return node.join === "any" ? { any: kept } : { all: kept };
|
|
239
|
-
}
|
|
240
|
-
/** A condition back to an editable row: the value goes back to text, which is what an
|
|
241
|
-
* `<input>` shows — and stays as it came (`raw`), so the rule goes back out the same. A list
|
|
242
|
-
* —an `in`'s value— is written separated by commas, and kept as chosen values. */
|
|
243
|
-
function conditionToTree(condition) {
|
|
244
|
-
if (WITH_RANGE.has(condition.operator) && Array.isArray(condition.value)) {
|
|
245
|
-
const [from, to] = condition.value;
|
|
246
|
-
return {
|
|
247
|
-
kind: "condition",
|
|
248
|
-
field: condition.field,
|
|
249
|
-
operator: condition.operator,
|
|
250
|
-
value: String(from ?? ""),
|
|
251
|
-
upper: String(to ?? ""),
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
if (WITHOUT_VALUE.has(condition.operator)) {
|
|
255
|
-
return {
|
|
256
|
-
kind: "condition",
|
|
257
|
-
field: condition.field,
|
|
258
|
-
operator: condition.operator,
|
|
259
|
-
value: "",
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
const value = condition.value;
|
|
263
|
-
return {
|
|
264
|
-
kind: "condition",
|
|
265
|
-
field: condition.field,
|
|
266
|
-
operator: condition.operator,
|
|
267
|
-
value: Array.isArray(value) ? value.join(",") : String(value ?? ""),
|
|
268
|
-
...(Array.isArray(value)
|
|
269
|
-
? { values: value.map((id) => ({ id: id, name: "" })) }
|
|
270
|
-
: value === undefined
|
|
271
|
-
? {}
|
|
272
|
-
: { raw: value }),
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
/** The filter read back into a tree.
|
|
276
|
-
*
|
|
277
|
-
* in {field: "name", operator: "=", value: "labs"}
|
|
278
|
-
* out TreeCondition
|
|
279
|
-
*
|
|
280
|
-
* in {any: [{all: [cond1, cond2]}, cond3]} group inside a group
|
|
281
|
-
* out TreeGroup(any) with a TreeGroup(all) and a condition as children
|
|
282
|
-
*
|
|
283
|
-
* in {negate: {any: [cond1, cond2]}}
|
|
284
|
-
* out TreeGroup(not) with the two conditions as children
|
|
285
|
-
*
|
|
286
|
-
* An `any`'s `negate` opens into the «ninguna» group and does not stay as a group
|
|
287
|
-
* inside another: it is what `treeToFilter` writes back, so there and
|
|
288
|
-
* back give the same. What can NOT be read —an object that is neither a
|
|
289
|
-
* condition nor any of the three shapes— returns `null`, and there the component
|
|
290
|
-
* shows it read-only while still letting you touch it as JSON.
|
|
291
|
-
*/
|
|
292
|
-
export function treeFromFilter(filter) {
|
|
293
|
-
if (isCondition(filter))
|
|
294
|
-
return conditionToTree(filter);
|
|
295
|
-
/* A chain of relations ending in ONE condition is one row with its path. A chain that ends
|
|
296
|
-
in a group («alguna línea con impuesto > 10 Y cantidad > 2», both about the SAME line)
|
|
297
|
-
cannot be a row: the tree shows it read-only and the JSON stays the way to edit it.
|
|
298
|
-
Read before `negate`: «ninguna línea con…» is a `negate` around a `through`, and it is
|
|
299
|
-
one row with its quantifier, not a group. */
|
|
300
|
-
const hops = [];
|
|
301
|
-
let node = filter;
|
|
302
|
-
for (let crossed = crossedOf(node); crossed !== null; crossed = crossedOf(node)) {
|
|
303
|
-
hops.push({ field: crossed.through, match: crossed.match });
|
|
304
|
-
node = crossed.selection;
|
|
305
|
-
}
|
|
306
|
-
if (hops.length > 0) {
|
|
307
|
-
return isCondition(node) ? { ...conditionToTree(node), through: hops } : null;
|
|
308
|
-
}
|
|
309
|
-
if (typeof filter !== "object" || filter === null)
|
|
310
|
-
return null;
|
|
311
|
-
const record = filter;
|
|
312
|
-
if ("negate" in record) {
|
|
313
|
-
const inside = record.negate;
|
|
314
|
-
/* An `any`'s `negate` is the negated group's children; anything else
|
|
315
|
-
is a single child. */
|
|
316
|
-
const raws = typeof inside === "object" &&
|
|
317
|
-
inside !== null &&
|
|
318
|
-
Array.isArray(inside.any)
|
|
319
|
-
? inside.any
|
|
320
|
-
: [inside];
|
|
321
|
-
const children = readChildren(raws);
|
|
322
|
-
return children === null ? null : { kind: "group", join: "not", children };
|
|
323
|
-
}
|
|
324
|
-
const join = Array.isArray(record.all)
|
|
325
|
-
? "all"
|
|
326
|
-
: Array.isArray(record.any)
|
|
327
|
-
? "any"
|
|
328
|
-
: null;
|
|
329
|
-
if (join === null)
|
|
330
|
-
return null;
|
|
331
|
-
const children = readChildren(record[join]);
|
|
332
|
-
return children === null ? null : { kind: "group", join, children };
|
|
333
|
-
}
|
|
334
|
-
/** A group's children, or `null` if any cannot be read: half a tree is
|
|
335
|
-
* worse than none, because it draws something that is not what filters. */
|
|
336
|
-
function readChildren(raws) {
|
|
337
|
-
const children = [];
|
|
338
|
-
for (const raw of raws) {
|
|
339
|
-
const child = treeFromFilter(raw);
|
|
340
|
-
if (child === null)
|
|
341
|
-
return null;
|
|
342
|
-
children.push(child);
|
|
343
|
-
}
|
|
344
|
-
return children;
|
|
345
|
-
}
|
|
346
|
-
/** The operators a rule about records offers: one record or several, in or out — and, through
|
|
347
|
-
* a column that admits it, «está vacío». A to-many relation is asked by some of its records
|
|
348
|
-
* or none of them. */
|
|
349
|
-
export function recordOps(target) {
|
|
350
|
-
if (target.many)
|
|
351
|
-
return ["in", "not in"];
|
|
352
|
-
const empty = ["is null", "is not null"].filter((op) => target.column !== undefined && target.ops.includes(op));
|
|
353
|
-
return ["=", "!=", "in", "not in", ...empty];
|
|
354
|
-
}
|
|
355
|
-
/** The keys of a list read back, as chosen values not named yet. */
|
|
356
|
-
function unnamed(keys) {
|
|
357
|
-
return keys.map((id) => ({ id, name: "" }));
|
|
358
|
-
}
|
|
359
|
-
/**
|
|
360
|
-
* The rules about RECORDS of a relation, recognised again in a tree read back.
|
|
361
|
-
*
|
|
362
|
-
* A filter carries keys, never which relation they were chosen from: `customer_id in [7, 9]`
|
|
363
|
-
* is, for the tree, a column compared to two numbers. The model says that `customer_id` is
|
|
364
|
-
* how «Cliente» is compared, so the row is drawn as «Cliente es alguno de [Andrés Pérez,
|
|
365
|
-
* Andrés Gutiérrez]» — the names asked of `searchRecords` by key. The same for a to-many
|
|
366
|
-
* crossed to its key: `{through: "tags", selection: {field: "id", operator: "in", …}}` is
|
|
367
|
-
* «Etiquetas es alguno de …».
|
|
368
|
-
*
|
|
369
|
-
* in {field: "customer_id", operator: "in", value: [7, 9]} (customer_id names Cliente)
|
|
370
|
-
* out {field: "customer", target: Cliente, operator: "in", values: [#7, #9]}
|
|
371
|
-
*
|
|
372
|
-
* Only on the record's own fields: a relation of a related record needs its model, and a row
|
|
373
|
-
* that stays a plain condition still filters the same.
|
|
374
|
-
*/
|
|
375
|
-
export function liftRecords(node, model,
|
|
376
|
-
/** The model at the end of a path of relations, when it already arrived: with it, a rule
|
|
377
|
-
* about records past a relation —«Cliente > País es Bolivia»— is recognised too. */
|
|
378
|
-
modelAt) {
|
|
379
|
-
if (node.kind === "group") {
|
|
380
|
-
return {
|
|
381
|
-
...node,
|
|
382
|
-
children: node.children.map((child) => liftRecords(child, model, modelAt)),
|
|
383
|
-
};
|
|
384
|
-
}
|
|
385
|
-
if (node.target !== undefined)
|
|
386
|
-
return node;
|
|
387
|
-
const hops = node.through ?? [];
|
|
388
|
-
const at = (path) => path.length === 0 ? model : modelAt?.(path.map((hop) => hop.field));
|
|
389
|
-
const keys = () => node.values !== undefined
|
|
390
|
-
? node.values.map((one) => one.id)
|
|
391
|
-
: typeof node.raw === "string" || typeof node.raw === "number"
|
|
392
|
-
? [node.raw]
|
|
393
|
-
: [];
|
|
394
|
-
/* A to-one relation compared by its column, in the model the rule's field lives in. */
|
|
395
|
-
const here = at(hops);
|
|
396
|
-
if (here !== undefined) {
|
|
397
|
-
for (const name of Object.keys(here.fields)) {
|
|
398
|
-
const target = recordTarget(here, name);
|
|
399
|
-
if (target === undefined || target.many || target.column !== node.field)
|
|
400
|
-
continue;
|
|
401
|
-
if (!recordOps(target).includes(node.operator))
|
|
402
|
-
return node;
|
|
403
|
-
return {
|
|
404
|
-
kind: "condition",
|
|
405
|
-
...(hops.length === 0 ? {} : { through: hops }),
|
|
406
|
-
field: name,
|
|
407
|
-
operator: node.operator,
|
|
408
|
-
value: "",
|
|
409
|
-
target,
|
|
410
|
-
...(WITHOUT_VALUE.has(node.operator) ? {} : { values: unnamed(keys()) }),
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
/* A to-many relation crossed to its key: the LAST hop is the relation. */
|
|
415
|
-
const last = hops.at(-1);
|
|
416
|
-
if (last === undefined || last.match === "all")
|
|
417
|
-
return node;
|
|
418
|
-
const before = hops.slice(0, -1);
|
|
419
|
-
const owner = at(before);
|
|
420
|
-
const target = owner === undefined ? undefined : recordTarget(owner, last.field);
|
|
421
|
-
if (target === undefined || !target.many || node.field !== target.key)
|
|
422
|
-
return node;
|
|
423
|
-
if (node.operator !== "in" && node.operator !== "=")
|
|
424
|
-
return node;
|
|
425
|
-
return {
|
|
426
|
-
kind: "condition",
|
|
427
|
-
...(before.length === 0 ? {} : { through: before }),
|
|
428
|
-
field: last.field,
|
|
429
|
-
operator: last.match === "none" ? "not in" : "in",
|
|
430
|
-
value: "",
|
|
431
|
-
target,
|
|
432
|
-
values: unnamed(keys()),
|
|
433
|
-
};
|
|
434
|
-
}
|
|
435
|
-
/** The names of the records a tree has chosen, by relation and key: what says them in a
|
|
436
|
-
* sentence (`describeFilter`) once the tree is applied. */
|
|
437
|
-
export function namesOf(node) {
|
|
438
|
-
const known = new Map();
|
|
439
|
-
const walk = (one) => {
|
|
440
|
-
if (one.kind === "group")
|
|
441
|
-
return one.children.forEach(walk);
|
|
442
|
-
if (one.target === undefined)
|
|
443
|
-
return;
|
|
444
|
-
for (const value of one.values ?? []) {
|
|
445
|
-
if (value.name !== "")
|
|
446
|
-
known.set(`${one.target.name}\u0000${value.id}`, value.name);
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
walk(node);
|
|
450
|
-
return (relation, key) => known.get(`${relation}\u0000${key}`);
|
|
451
|
-
}
|
|
452
|
-
/** The tree with the names that were missing, where they are known. */
|
|
453
|
-
function withNames(node, names) {
|
|
454
|
-
if (node.kind === "group") {
|
|
455
|
-
return { ...node, children: node.children.map((child) => withNames(child, names)) };
|
|
456
|
-
}
|
|
457
|
-
if (node.target === undefined || node.values === undefined)
|
|
458
|
-
return node;
|
|
459
|
-
const target = node.target;
|
|
460
|
-
return {
|
|
461
|
-
...node,
|
|
462
|
-
values: node.values.map((one) => one.name !== ""
|
|
463
|
-
? one
|
|
464
|
-
: { ...one, name: names.get(`${target.name}\u0000${one.id}`) ?? `#${one.id}` }),
|
|
465
|
-
};
|
|
466
|
-
}
|
|
18
|
+
export { blankCondition, blankGroup, describeFilter, isRecognizedFilter, liftRecords, namesOf, recordOps, treeFromFilter, treeToFilter, };
|
|
467
19
|
/**
|
|
468
20
|
* Names the records a tree read back carries only by key — ONE question per relation, for the
|
|
469
21
|
* whole tree. Per row, five rows answered at five moments and each wrote its answer over a tree
|
|
@@ -510,132 +62,6 @@ export function useRecordNames(tree, searchRecords, apply) {
|
|
|
510
62
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
511
63
|
}, [key]);
|
|
512
64
|
}
|
|
513
|
-
/** The paths whose models a tree needs to recognise its rules about records past a relation:
|
|
514
|
-
* every prefix of the hops of a rule that is not one yet. */
|
|
515
|
-
function pathsToLift(node) {
|
|
516
|
-
const out = new Map();
|
|
517
|
-
const walk = (one) => {
|
|
518
|
-
if (one.kind === "group")
|
|
519
|
-
return one.children.forEach(walk);
|
|
520
|
-
if (one.target !== undefined)
|
|
521
|
-
return;
|
|
522
|
-
const fields = (one.through ?? []).map((hop) => hop.field);
|
|
523
|
-
for (let end = 1; end <= fields.length; end++) {
|
|
524
|
-
const path = fields.slice(0, end);
|
|
525
|
-
out.set(path.join("."), path);
|
|
526
|
-
}
|
|
527
|
-
};
|
|
528
|
-
walk(node);
|
|
529
|
-
return [...out.values()];
|
|
530
|
-
}
|
|
531
|
-
function stringifyFilter(filter) {
|
|
532
|
-
return filter === undefined ? "" : JSON.stringify(filter, null, 2);
|
|
533
|
-
}
|
|
534
|
-
/** A condition value, legible: a list is joined with commas, and what has nothing is
|
|
535
|
-
* said in words instead of showing a bare `true`/`undefined`. */
|
|
536
|
-
function describeValue(value, operator) {
|
|
537
|
-
const copy = currentCopy();
|
|
538
|
-
if (Array.isArray(value)) {
|
|
539
|
-
return operator !== undefined && WITH_RANGE.has(operator)
|
|
540
|
-
? value.join(` ${copy.search.and} `)
|
|
541
|
-
: value.join(", ");
|
|
542
|
-
}
|
|
543
|
-
/* The same two words the table puts in a boolean cell: a `sí` that
|
|
544
|
-
is not the `sí` next door is a second way of saying the same thing. */
|
|
545
|
-
if (typeof value === "boolean")
|
|
546
|
-
return value ? copy.table.yes : copy.table.no;
|
|
547
|
-
return String(value ?? "");
|
|
548
|
-
}
|
|
549
|
-
/** The filter said in one line, for a chip or a label.
|
|
550
|
-
*
|
|
551
|
-
* in {field: "row_count", operator: ">", value: 1000}, with the model
|
|
552
|
-
* out 'filas mayor que 1000'
|
|
553
|
-
*
|
|
554
|
-
* in {all: [cond1, cond2]}
|
|
555
|
-
* out 'el nombre contiene labs y filas mayor que 1000'
|
|
556
|
-
*
|
|
557
|
-
* in {any: [cond1, cond2]}
|
|
558
|
-
* out 'el nombre es a o el nombre es b'
|
|
559
|
-
*
|
|
560
|
-
* Each condition is said by `sayCondition`, which is the same one the bar uses: without
|
|
561
|
-
* that, what is written in the bar and what is built in the dialog read differently even though
|
|
562
|
-
* they filter the same, and nobody knows which of the two phrases is the one in charge.
|
|
563
|
-
*
|
|
564
|
-
* **The model is the one that knows what a field is called.** Without it the raw
|
|
565
|
-
* name remains —`row_count`—, which is the best that can be said without asking
|
|
566
|
-
* anybody.
|
|
567
|
-
*
|
|
568
|
-
* A nested group is enclosed in parentheses, which is what keeps «a y (b o c)» from being
|
|
569
|
-
* read as «(a y b) o c». Past three conditions it is cut: a chip that does not fit in
|
|
570
|
-
* the bar says nothing — that is what opening the builder and seeing it whole is for. */
|
|
571
|
-
export function describeFilter(node, model,
|
|
572
|
-
/** The model at the end of a path of relations, when it already arrived: it names the
|
|
573
|
-
* fields of a `through`. Without it they keep their raw names. */
|
|
574
|
-
modelAt,
|
|
575
|
-
/** The names of the records a filter asks for, by relation and key: «Cliente es Andrés
|
|
576
|
-
* Pérez» instead of «customer_id es 7». The builder hands its own (`namesOf`). */
|
|
577
|
-
names) {
|
|
578
|
-
return say(node, model, 0, [], modelAt, names);
|
|
579
|
-
}
|
|
580
|
-
/** A filter that asks for RECORDS of one of the model's relations, said as such: «Cliente es
|
|
581
|
-
* alguno de Andrés Pérez, Andrés Gutiérrez». `null` when it is not one. */
|
|
582
|
-
function sayRecords(node, model, names) {
|
|
583
|
-
if (model === undefined)
|
|
584
|
-
return null;
|
|
585
|
-
for (const name of Object.keys(model.fields)) {
|
|
586
|
-
const target = recordTarget(model, name);
|
|
587
|
-
if (target === undefined)
|
|
588
|
-
continue;
|
|
589
|
-
const asked = recordsOf(target, node);
|
|
590
|
-
if (asked === null)
|
|
591
|
-
continue;
|
|
592
|
-
const one = asked.keys.length === 1 && !target.many;
|
|
593
|
-
const op = asked.negate ? (one ? "!=" : "not in") : one ? "=" : "in";
|
|
594
|
-
const said = asked.keys.map((key) => names?.(name, key) ?? String(key)).join(", ");
|
|
595
|
-
return sayCondition(fieldLabel(model, name, currentCopy().locale), op, said);
|
|
596
|
-
}
|
|
597
|
-
return null;
|
|
598
|
-
}
|
|
599
|
-
function say(node, model, depth, at, modelAt, names) {
|
|
600
|
-
const copy = currentCopy();
|
|
601
|
-
const records = sayRecords(node, model, names);
|
|
602
|
-
if (records !== null)
|
|
603
|
-
return records;
|
|
604
|
-
const crossed = crossedOf(node);
|
|
605
|
-
if (crossed !== null) {
|
|
606
|
-
/* «alguno de Líneas con alguno de Impuestos con Monto mayor que 10». The quantifier
|
|
607
|
-
is a separate word and not «algún impuesto» because the model publishes the label,
|
|
608
|
-
not its gender or its singular: «algún Impuestos» would be worse than neutral. */
|
|
609
|
-
const relation = model === undefined ? crossed.through : fieldLabel(model, crossed.through, copy.locale);
|
|
610
|
-
const next = [...at, crossed.through];
|
|
611
|
-
const inner = say(crossed.selection, modelAt?.(next), depth + 1, next, modelAt, names);
|
|
612
|
-
const template = reachesOne(model, crossed)
|
|
613
|
-
? copy.fieldPath.say.one
|
|
614
|
-
: copy.fieldPath.say[crossed.match];
|
|
615
|
-
return fillCopy(template, { relation, inner });
|
|
616
|
-
}
|
|
617
|
-
if ("all" in node || "any" in node) {
|
|
618
|
-
const items = "all" in node ? node.all : node.any;
|
|
619
|
-
const glue = "all" in node ? copy.search.and : copy.search.or;
|
|
620
|
-
const said = items
|
|
621
|
-
.slice(0, 3)
|
|
622
|
-
.map((child) => say(child, model, depth + 1, at, modelAt, names));
|
|
623
|
-
if (items.length > 3) {
|
|
624
|
-
said.push(fillCopy(copy.search.andMore, { count: items.length - 3 }));
|
|
625
|
-
}
|
|
626
|
-
const joined = said.join(` ${glue} `);
|
|
627
|
-
return depth === 0 || said.length === 1 ? joined : `(${joined})`;
|
|
628
|
-
}
|
|
629
|
-
if ("negate" in node) {
|
|
630
|
-
return `${copy.search.notWord} ${say(node.negate, model, depth + 1, at, modelAt, names)}`;
|
|
631
|
-
}
|
|
632
|
-
/* Every `through` was said above; anything still not a condition is not a shape of the
|
|
633
|
-
grammar, and it is shown as it came rather than as a sentence it is not. */
|
|
634
|
-
if (!isCondition(node))
|
|
635
|
-
return JSON.stringify(node);
|
|
636
|
-
const label = model === undefined ? node.field : fieldLabel(model, node.field, copy.locale);
|
|
637
|
-
return sayCondition(label, node.operator, describeValue(node.value, node.operator));
|
|
638
|
-
}
|
|
639
65
|
/** The filter drawn as it is, recursive: a condition is a line, `all`/`any` are a
|
|
640
66
|
* group with its label and its children inside, `negate` is «no» wrapping the one it negates.
|
|
641
67
|
*
|
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* in '{"all":[{"field":"a","operator":"=","value":1}]}'
|
|
4
|
-
* out the same, indented by two spaces and one field per line
|
|
5
|
-
*
|
|
6
|
-
* Separated from the component so it can be tested without drawing anything, which is the only thing
|
|
7
|
-
* with logic in here. */
|
|
8
|
-
export declare function formatJson(text: string): string;
|
|
1
|
+
import { formatJson } from "./formatJson.js";
|
|
2
|
+
export { formatJson };
|
|
9
3
|
export declare function JsonEditor({ value, onChange, error, height, label, }: {
|
|
10
4
|
value: string;
|
|
11
5
|
onChange: (next: string) => void;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { json, jsonParseLinter } from "@codemirror/lang-json";
|
|
4
4
|
import { linter, lintGutter } from "@codemirror/lint";
|
|
5
|
+
import { formatJson } from "./formatJson.js";
|
|
5
6
|
import { useUiCopy } from "../../foundations/copy.js";
|
|
6
7
|
import { useSurface } from "../../foundations/surface.js";
|
|
7
8
|
import { useTheme } from "../../foundations/theme.js";
|
|
@@ -23,21 +24,10 @@ import { useId } from "react";
|
|
|
23
24
|
* as you type. The outer notice —`error`— still exists because it
|
|
24
25
|
* says something else: that the JSON is well written but is not a filter.
|
|
25
26
|
* ====================================================================== */
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*
|
|
31
|
-
* Separated from the component so it can be tested without drawing anything, which is the only thing
|
|
32
|
-
* with logic in here. */
|
|
33
|
-
export function formatJson(text) {
|
|
34
|
-
try {
|
|
35
|
-
return JSON.stringify(JSON.parse(text), null, 2);
|
|
36
|
-
}
|
|
37
|
-
catch {
|
|
38
|
-
return text;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
27
|
+
/* `formatJson` is plain (`formatJson.ts`), without the directive — and without CodeMirror —
|
|
28
|
+
so a server component reads it; re-exported so every path that read it here keeps reading
|
|
29
|
+
it. */
|
|
30
|
+
export { formatJson };
|
|
41
31
|
/* The built-in `light`/`dark` themes pick the SYNTAX colours; this layer paints the box from
|
|
42
32
|
the system's tokens, so the editor sits on the same surface as the fields around it in
|
|
43
33
|
either theme instead of on a hard-coded one. */
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ValueEditorProps } from "./context.js";
|
|
2
|
+
import { takesMany } from "./filterTree.js";
|
|
2
3
|
import type { BrowseRecords, RecordTarget, SearchRecords } from "./records.js";
|
|
3
|
-
|
|
4
|
-
export declare function takesMany(operator: string): boolean;
|
|
4
|
+
export { takesMany };
|
|
5
5
|
/**
|
|
6
6
|
* The builder's own value control for a rule — what is drawn when the application's
|
|
7
7
|
* `valueEditor` does not answer for the field.
|