@mdbase-dev/ui 0.2.1 → 0.2.2
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 +119 -0
- package/css/controls.css +37 -0
- package/css/date-picker.css +176 -0
- package/css/filter-builder.css +336 -0
- package/css/properties.css +398 -0
- package/dist/AnchoredPopover.d.ts +20 -0
- package/dist/AnchoredPopover.js +100 -0
- package/dist/Combo.d.ts +41 -0
- package/dist/Combo.js +82 -0
- package/dist/DatePicker.d.ts +69 -0
- package/dist/DatePicker.js +317 -0
- package/dist/Menu.d.ts +3 -1
- package/dist/Menu.js +3 -2
- package/dist/QuickCreate.js +1 -16
- package/dist/date-picker-model.d.ts +31 -0
- package/dist/date-picker-model.js +97 -0
- package/dist/filter-builder/AddFilter.d.ts +13 -0
- package/dist/filter-builder/AddFilter.js +295 -0
- package/dist/filter-builder/FilterBuilder.d.ts +36 -0
- package/dist/filter-builder/FilterBuilder.js +225 -0
- package/dist/filter-builder/FilterPill.d.ts +17 -0
- package/dist/filter-builder/FilterPill.js +494 -0
- package/dist/filter-builder/context.d.ts +40 -0
- package/dist/filter-builder/context.js +67 -0
- package/dist/filter-builder/editors.d.ts +31 -0
- package/dist/filter-builder/editors.js +294 -0
- package/dist/filter-builder/filter-ast.d.ts +13 -0
- package/dist/filter-builder/filter-ast.js +382 -0
- package/dist/filter-builder/labels.d.ts +99 -0
- package/dist/filter-builder/labels.js +555 -0
- package/dist/filter-builder/menu.d.ts +21 -0
- package/dist/filter-builder/menu.js +60 -0
- package/dist/filter-builder/operators.d.ts +30 -0
- package/dist/filter-builder/operators.js +192 -0
- package/dist/filter-builder/tree.d.ts +12 -0
- package/dist/filter-builder/tree.js +61 -0
- package/dist/filter-builder/types.d.ts +69 -0
- package/dist/filter-builder/types.js +48 -0
- package/dist/filter-builder.d.ts +13 -0
- package/dist/filter-builder.js +10 -0
- package/dist/glyphs.d.ts +32 -0
- package/dist/glyphs.js +143 -0
- package/dist/locales/de/messages.json +1 -1
- package/dist/locales/en/messages.json +1 -1
- package/dist/locales/es/messages.json +1 -1
- package/dist/locales/fr/messages.json +1 -1
- package/dist/locales/it/messages.json +1 -1
- package/dist/locales/ja/messages.json +1 -1
- package/dist/locales/ko/messages.json +1 -1
- package/dist/locales/pt/messages.json +1 -1
- package/dist/locales/ru/messages.json +1 -1
- package/dist/locales/zh/messages.json +1 -1
- package/dist/popover.d.ts +5 -0
- package/dist/popover.js +15 -0
- package/dist/properties/PropertyList.d.ts +31 -0
- package/dist/properties/PropertyList.js +233 -0
- package/dist/properties/PropertyValue.d.ts +42 -0
- package/dist/properties/PropertyValue.js +304 -0
- package/dist/properties/fields.d.ts +20 -0
- package/dist/properties/fields.js +81 -0
- package/dist/properties/labels.d.ts +29 -0
- package/dist/properties/labels.js +127 -0
- package/dist/properties/model.d.ts +52 -0
- package/dist/properties/model.js +195 -0
- package/dist/properties.d.ts +13 -0
- package/dist/properties.js +9 -0
- package/package.json +25 -2
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useRef, useState } from "react";
|
|
3
|
+
import { Calendar } from "../DatePicker.js";
|
|
4
|
+
import { Glyph } from "../glyphs.js";
|
|
5
|
+
import { EXPRESSION_EXAMPLE, useBar } from "./context.js";
|
|
6
|
+
import { tryExpressionToRule } from "./filter-ast.js";
|
|
7
|
+
import { isNegated, MULTI, selection, valueKindOf, varsFor, withSelection, withValue } from "./operators.js";
|
|
8
|
+
/** Keeps focus where it is, so a click inside a popover does not blur (and commit) the field being typed in. */
|
|
9
|
+
const keepFocus = event => event.preventDefault();
|
|
10
|
+
function parseLiteral(text, kind) {
|
|
11
|
+
const trimmed = text.trim();
|
|
12
|
+
if (kind === "number") return trimmed && Number.isFinite(Number(trimmed)) ? Number(trimmed) : undefined;
|
|
13
|
+
if (kind === "date") return /^\d{4}-\d{2}-\d{2}/.test(trimmed) ? trimmed : undefined;
|
|
14
|
+
if (kind === "formula" && trimmed && Number.isFinite(Number(trimmed))) return Number(trimmed);
|
|
15
|
+
return text === "" ? undefined : text;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A typed literal; Enter commits it, as does its button, and Escape leaves it. A date is picked
|
|
19
|
+
* from a calendar instead, or typed above it.
|
|
20
|
+
*/
|
|
21
|
+
function LiteralInput({
|
|
22
|
+
kind,
|
|
23
|
+
initial,
|
|
24
|
+
placeholder,
|
|
25
|
+
label,
|
|
26
|
+
commitOnBlur,
|
|
27
|
+
onCommit,
|
|
28
|
+
action,
|
|
29
|
+
children
|
|
30
|
+
}) {
|
|
31
|
+
const {
|
|
32
|
+
labels
|
|
33
|
+
} = useBar();
|
|
34
|
+
const [draft, setDraft] = useState(initial === undefined ? "" : String(initial));
|
|
35
|
+
const cancelled = useRef(false);
|
|
36
|
+
const value = parseLiteral(draft, kind);
|
|
37
|
+
const commit = viaEnter => {
|
|
38
|
+
if (value !== undefined && (viaEnter || value !== initial)) onCommit(value, viaEnter);
|
|
39
|
+
};
|
|
40
|
+
if (kind === "date") {
|
|
41
|
+
return _jsxs(_Fragment, {
|
|
42
|
+
children: [_jsx(Calendar, {
|
|
43
|
+
value: typeof initial === "string" ? initial.slice(0, 10) : null,
|
|
44
|
+
labels: labels.date,
|
|
45
|
+
onPick: picked => onCommit(picked, true)
|
|
46
|
+
}), children]
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return _jsxs(_Fragment, {
|
|
50
|
+
children: [_jsxs("div", {
|
|
51
|
+
className: "mdbase-filter-input-row",
|
|
52
|
+
children: [_jsx("input", {
|
|
53
|
+
className: "mdbase-field",
|
|
54
|
+
"data-autofocus": true,
|
|
55
|
+
"aria-label": label,
|
|
56
|
+
spellCheck: false,
|
|
57
|
+
autoComplete: "off",
|
|
58
|
+
value: draft,
|
|
59
|
+
type: "text",
|
|
60
|
+
inputMode: kind === "number" ? "decimal" : undefined,
|
|
61
|
+
placeholder: placeholder,
|
|
62
|
+
onChange: event => setDraft(event.target.value),
|
|
63
|
+
onBlur: () => {
|
|
64
|
+
if (commitOnBlur && !cancelled.current) commit(false);
|
|
65
|
+
},
|
|
66
|
+
onKeyDown: event => {
|
|
67
|
+
if (event.key === "Enter") {
|
|
68
|
+
event.preventDefault();
|
|
69
|
+
commit(true);
|
|
70
|
+
}
|
|
71
|
+
if (event.key === "Escape") cancelled.current = true;
|
|
72
|
+
}
|
|
73
|
+
}), !action && _jsx("span", {
|
|
74
|
+
className: "mdbase-filter-enter",
|
|
75
|
+
children: _jsx(Glyph, {
|
|
76
|
+
name: "enter"
|
|
77
|
+
})
|
|
78
|
+
})]
|
|
79
|
+
}), children, action && _jsxs("div", {
|
|
80
|
+
className: "mdbase-filter-foot",
|
|
81
|
+
children: [_jsx("span", {
|
|
82
|
+
className: "mdbase-filter-hint",
|
|
83
|
+
children: draft.trim() && value === undefined ? kind === "number" ? labels.notANumber : "" : labels.enterHint
|
|
84
|
+
}), _jsx("button", {
|
|
85
|
+
type: "button",
|
|
86
|
+
className: "mdbase-button is-primary",
|
|
87
|
+
disabled: value === undefined,
|
|
88
|
+
onMouseDown: keepFocus,
|
|
89
|
+
onClick: () => commit(true),
|
|
90
|
+
children: action
|
|
91
|
+
})]
|
|
92
|
+
})]
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Picks a rule's value: a checklist for enums and `in` rules (one value is
|
|
97
|
+
* `eq`, several are `in`), True/False for booleans, otherwise a typed literal,
|
|
98
|
+
* plus one-click `this.*` bindings.
|
|
99
|
+
*/
|
|
100
|
+
export function ValuePanel({
|
|
101
|
+
rule,
|
|
102
|
+
field,
|
|
103
|
+
contextVars,
|
|
104
|
+
commitOnBlur,
|
|
105
|
+
onChange,
|
|
106
|
+
onDone,
|
|
107
|
+
action
|
|
108
|
+
}) {
|
|
109
|
+
const {
|
|
110
|
+
labels
|
|
111
|
+
} = useBar();
|
|
112
|
+
const kind = valueKindOf(field.kind, rule.op);
|
|
113
|
+
const vars = varsFor(contextVars, kind);
|
|
114
|
+
const context = rule.valueKind === "context";
|
|
115
|
+
const checklist = MULTI.has(rule.op) || kind === "enum" && !context && (rule.op === "eq" || rule.op === "neq");
|
|
116
|
+
const chips = vars.length > 0 && !MULTI.has(rule.op) && _jsx("div", {
|
|
117
|
+
className: "mdbase-filter-vars",
|
|
118
|
+
role: "group",
|
|
119
|
+
"aria-label": labels.contextValues,
|
|
120
|
+
children: vars.map(item => _jsxs("button", {
|
|
121
|
+
type: "button",
|
|
122
|
+
className: "mdbase-filter-var",
|
|
123
|
+
"aria-pressed": context && rule.value === item.expr,
|
|
124
|
+
title: labels.compareWith(item.expr),
|
|
125
|
+
onMouseDown: keepFocus,
|
|
126
|
+
onClick: () => {
|
|
127
|
+
onChange(withValue(rule, item.expr, "context"));
|
|
128
|
+
onDone();
|
|
129
|
+
},
|
|
130
|
+
children: [_jsx("code", {
|
|
131
|
+
children: item.expr
|
|
132
|
+
}), item.preview !== undefined && _jsxs("span", {
|
|
133
|
+
children: [" \u00B7 ", item.preview]
|
|
134
|
+
})]
|
|
135
|
+
}, item.expr))
|
|
136
|
+
});
|
|
137
|
+
if (checklist) {
|
|
138
|
+
const selected = selection(rule);
|
|
139
|
+
const options = [...(field.enumValues ?? []), ...selected.filter(value => !field.enumValues?.includes(value))];
|
|
140
|
+
const toggle = value => {
|
|
141
|
+
const next = selected.includes(value) ? selected.filter(item => item !== value) : [...selected, value];
|
|
142
|
+
onChange(withSelection(rule, options.filter(option => next.includes(option)), isNegated(rule)));
|
|
143
|
+
};
|
|
144
|
+
return _jsxs("div", {
|
|
145
|
+
className: "mdbase-filter-panel",
|
|
146
|
+
children: [_jsxs("div", {
|
|
147
|
+
className: "mdbase-filter-checklist",
|
|
148
|
+
role: "group",
|
|
149
|
+
"aria-label": labels.values,
|
|
150
|
+
children: [options.map((option, index) => _jsxs("label", {
|
|
151
|
+
className: "mdbase-filter-check",
|
|
152
|
+
children: [_jsx("input", {
|
|
153
|
+
type: "checkbox",
|
|
154
|
+
className: "mdbase-checkbox",
|
|
155
|
+
"data-autofocus": index === 0 || undefined,
|
|
156
|
+
checked: selected.includes(option),
|
|
157
|
+
onChange: () => toggle(option),
|
|
158
|
+
onKeyDown: event => {
|
|
159
|
+
if (event.key === "Enter") {
|
|
160
|
+
event.preventDefault();
|
|
161
|
+
onDone();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}), _jsx("span", {
|
|
165
|
+
title: String(option),
|
|
166
|
+
children: option === "" ? '""' : String(option)
|
|
167
|
+
})]
|
|
168
|
+
}, JSON.stringify(option))), !options.length && _jsx("p", {
|
|
169
|
+
className: "mdbase-filter-hint",
|
|
170
|
+
children: labels.noValues
|
|
171
|
+
})]
|
|
172
|
+
}), field.kind !== "enum" && kind !== "date" && _jsx(LiteralInput, {
|
|
173
|
+
kind: kind,
|
|
174
|
+
label: labels.addValue,
|
|
175
|
+
placeholder: labels.addValuePlaceholder,
|
|
176
|
+
commitOnBlur: false,
|
|
177
|
+
onCommit: value => {
|
|
178
|
+
if (!selected.includes(value)) onChange(withSelection(rule, [...selected, value], isNegated(rule)));
|
|
179
|
+
}
|
|
180
|
+
}), chips, _jsxs("div", {
|
|
181
|
+
className: "mdbase-filter-foot",
|
|
182
|
+
children: [_jsx("span", {
|
|
183
|
+
className: "mdbase-filter-hint",
|
|
184
|
+
children: selected.length > 1 ? labels.selected(selected.length) : labels.pickOneOrMore
|
|
185
|
+
}), _jsx("button", {
|
|
186
|
+
type: "button",
|
|
187
|
+
className: "mdbase-button is-primary",
|
|
188
|
+
onClick: onDone,
|
|
189
|
+
children: labels.done
|
|
190
|
+
})]
|
|
191
|
+
})]
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
if (kind === "boolean" && !context) {
|
|
195
|
+
return _jsxs("div", {
|
|
196
|
+
className: "mdbase-filter-panel",
|
|
197
|
+
children: [_jsx("div", {
|
|
198
|
+
className: "mdbase-filter-choices",
|
|
199
|
+
role: "group",
|
|
200
|
+
"aria-label": labels.value,
|
|
201
|
+
children: [true, false].map(option => _jsx("button", {
|
|
202
|
+
type: "button",
|
|
203
|
+
className: "mdbase-filter-choice",
|
|
204
|
+
"data-autofocus": rule.value === option || option && rule.value === undefined || undefined,
|
|
205
|
+
"aria-pressed": rule.value === option,
|
|
206
|
+
onClick: () => {
|
|
207
|
+
onChange(withValue(rule, option));
|
|
208
|
+
onDone();
|
|
209
|
+
},
|
|
210
|
+
children: option ? labels.true : labels.false
|
|
211
|
+
}, String(option)))
|
|
212
|
+
}), chips]
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return _jsx("div", {
|
|
216
|
+
className: "mdbase-filter-panel",
|
|
217
|
+
children: _jsx(LiteralInput, {
|
|
218
|
+
kind: kind,
|
|
219
|
+
initial: context ? undefined : rule.value,
|
|
220
|
+
label: labels.value,
|
|
221
|
+
commitOnBlur: commitOnBlur,
|
|
222
|
+
action: action,
|
|
223
|
+
placeholder: rule.op === "year_eq" ? labels.yearPlaceholder : kind === "number" ? labels.numberPlaceholder : labels.valuePlaceholder,
|
|
224
|
+
onCommit: (value, viaEnter) => {
|
|
225
|
+
onChange(withValue(rule, value));
|
|
226
|
+
if (viaEnter) onDone();
|
|
227
|
+
},
|
|
228
|
+
children: chips
|
|
229
|
+
}, `${rule.op}:${String(context)}`)
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
/** A CEL editor: Enter applies, Shift+Enter breaks the line. */
|
|
233
|
+
export function ExprEditor({
|
|
234
|
+
expression,
|
|
235
|
+
id,
|
|
236
|
+
onApply,
|
|
237
|
+
onConvert,
|
|
238
|
+
commitOnBlur
|
|
239
|
+
}) {
|
|
240
|
+
const {
|
|
241
|
+
labels
|
|
242
|
+
} = useBar();
|
|
243
|
+
const [draft, setDraft] = useState(expression);
|
|
244
|
+
const cancelled = useRef(false);
|
|
245
|
+
const rule = tryExpressionToRule(draft, id);
|
|
246
|
+
const apply = () => {
|
|
247
|
+
if (draft.trim()) onApply(draft.trim());
|
|
248
|
+
};
|
|
249
|
+
return _jsxs("div", {
|
|
250
|
+
className: "mdbase-filter-panel",
|
|
251
|
+
children: [_jsx("textarea", {
|
|
252
|
+
className: "mdbase-field mdbase-filter-code",
|
|
253
|
+
"data-autofocus": true,
|
|
254
|
+
"aria-label": labels.celExpression,
|
|
255
|
+
rows: 2,
|
|
256
|
+
spellCheck: false,
|
|
257
|
+
autoComplete: "off",
|
|
258
|
+
value: draft,
|
|
259
|
+
placeholder: EXPRESSION_EXAMPLE,
|
|
260
|
+
onChange: event => setDraft(event.target.value),
|
|
261
|
+
onBlur: () => {
|
|
262
|
+
if (commitOnBlur && !cancelled.current && draft.trim() && draft.trim() !== expression.trim()) onApply(draft.trim());
|
|
263
|
+
},
|
|
264
|
+
onKeyDown: event => {
|
|
265
|
+
if (event.key === "Enter" && !event.shiftKey) {
|
|
266
|
+
event.preventDefault();
|
|
267
|
+
apply();
|
|
268
|
+
}
|
|
269
|
+
if (event.key === "Escape") cancelled.current = true;
|
|
270
|
+
}
|
|
271
|
+
}), _jsxs("div", {
|
|
272
|
+
className: "mdbase-filter-foot",
|
|
273
|
+
children: [rule ? _jsxs("button", {
|
|
274
|
+
type: "button",
|
|
275
|
+
className: "mdbase-button is-tertiary mdbase-filter-convert",
|
|
276
|
+
onMouseDown: keepFocus,
|
|
277
|
+
onClick: () => onConvert(rule),
|
|
278
|
+
children: [_jsx(Glyph, {
|
|
279
|
+
name: "convert"
|
|
280
|
+
}), labels.convertToPill]
|
|
281
|
+
}) : _jsx("span", {
|
|
282
|
+
className: "mdbase-filter-hint",
|
|
283
|
+
children: labels.celHint
|
|
284
|
+
}), _jsx("button", {
|
|
285
|
+
type: "button",
|
|
286
|
+
className: "mdbase-button is-primary",
|
|
287
|
+
disabled: !draft.trim(),
|
|
288
|
+
onMouseDown: keepFocus,
|
|
289
|
+
onClick: apply,
|
|
290
|
+
children: labels.apply
|
|
291
|
+
})]
|
|
292
|
+
})]
|
|
293
|
+
});
|
|
294
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FilterGroup, FilterNode, FilterRule } from "./types.js";
|
|
2
|
+
/** Parses a `where` expression into a root group. Empty text is an empty `all` group. */
|
|
3
|
+
export declare function parseWhere(where: string): FilterGroup;
|
|
4
|
+
/** The rule `expression` spells, or null when it is custom CEL. */
|
|
5
|
+
export declare function tryExpressionToRule(expression: string, id?: string): FilterRule | null;
|
|
6
|
+
/** True when a rule has everything it needs to compile. */
|
|
7
|
+
export declare const isComplete: (rule: FilterRule) => boolean;
|
|
8
|
+
/** The CEL for one rule, or "" for a draft without a value. */
|
|
9
|
+
export declare function ruleToExpression({ field, op, value, valueKind, values }: FilterRule): string;
|
|
10
|
+
/** Emits the canonical `where` expression for a root group. Drafts and empty groups compile to nothing. */
|
|
11
|
+
export declare function compileWhere(root: FilterGroup): string;
|
|
12
|
+
/** Rules and expressions that currently contribute to the compiled query. */
|
|
13
|
+
export declare function countActive(node: FilterNode): number;
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import { MULTI, UNARY } from "./operators.js";
|
|
2
|
+
const OPERATORS = ["&&", "||", "==", "!=", ">=", "<=", ">", "<", "!", "(", ")", "[", "]", "{", "}", ",", ".", "?", ":", "+", "-", "*", "/", "%"];
|
|
3
|
+
const IDENT = /^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*/;
|
|
4
|
+
const NUMBER = /^\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/;
|
|
5
|
+
const ESCAPES = {
|
|
6
|
+
n: "\n",
|
|
7
|
+
t: "\t",
|
|
8
|
+
r: "\r",
|
|
9
|
+
b: "\b",
|
|
10
|
+
f: "\f",
|
|
11
|
+
"\\": "\\",
|
|
12
|
+
'"': '"',
|
|
13
|
+
"'": "'"
|
|
14
|
+
};
|
|
15
|
+
const CONTEXT = /^this(?:\.[A-Za-z_][A-Za-z0-9_]*)+$/;
|
|
16
|
+
const OPEN = new Set(["(", "[", "{"]);
|
|
17
|
+
const CLOSE = new Set([")", "]", "}"]);
|
|
18
|
+
/** Tokenizes enough CEL to find group structure. Malformed text yields `bad` tokens, which no rule matches. */
|
|
19
|
+
function tokenize(source) {
|
|
20
|
+
const tokens = [];
|
|
21
|
+
let i = 0;
|
|
22
|
+
while (i < source.length) {
|
|
23
|
+
const rest = source.slice(i);
|
|
24
|
+
const space = /^\s+/.exec(rest);
|
|
25
|
+
if (space) {
|
|
26
|
+
i += space[0].length;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const quote = rest[0];
|
|
30
|
+
if (quote === '"' || quote === "'") {
|
|
31
|
+
let value = "";
|
|
32
|
+
let bad = false;
|
|
33
|
+
let j = 1;
|
|
34
|
+
for (; j < rest.length && rest[j] !== quote; j++) {
|
|
35
|
+
if (rest[j] !== "\\") {
|
|
36
|
+
value += rest[j];
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const next = rest[++j];
|
|
40
|
+
if (next === "u" && /^[0-9a-fA-F]{4}$/.test(rest.slice(j + 1, j + 5))) {
|
|
41
|
+
value += String.fromCharCode(parseInt(rest.slice(j + 1, j + 5), 16));
|
|
42
|
+
j += 4;
|
|
43
|
+
} else if (next !== undefined && next in ESCAPES) value += ESCAPES[next];else bad = true;
|
|
44
|
+
}
|
|
45
|
+
const end = Math.min(rest.length, j + 1);
|
|
46
|
+
tokens.push({
|
|
47
|
+
t: bad || j >= rest.length ? "bad" : "str",
|
|
48
|
+
v: value,
|
|
49
|
+
start: i,
|
|
50
|
+
end: i + end
|
|
51
|
+
});
|
|
52
|
+
i += end;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
// A leading minus is part of a number only where an operand may start.
|
|
56
|
+
const previous = tokens.at(-1);
|
|
57
|
+
const negative = rest[0] === "-" && (!previous || previous.t === "op" && !CLOSE.has(String(previous.v)));
|
|
58
|
+
const number = NUMBER.exec(negative ? rest.slice(1) : rest);
|
|
59
|
+
if (number) {
|
|
60
|
+
const length = number[0].length + (negative ? 1 : 0);
|
|
61
|
+
tokens.push({
|
|
62
|
+
t: "num",
|
|
63
|
+
v: Number(number[0]) * (negative ? -1 : 1),
|
|
64
|
+
start: i,
|
|
65
|
+
end: i + length
|
|
66
|
+
});
|
|
67
|
+
i += length;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const ident = IDENT.exec(rest);
|
|
71
|
+
if (ident) {
|
|
72
|
+
tokens.push({
|
|
73
|
+
t: "ident",
|
|
74
|
+
v: ident[0],
|
|
75
|
+
start: i,
|
|
76
|
+
end: i + ident[0].length
|
|
77
|
+
});
|
|
78
|
+
i += ident[0].length;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const op = OPERATORS.find(candidate => rest.startsWith(candidate));
|
|
82
|
+
tokens.push({
|
|
83
|
+
t: op ? "op" : "bad",
|
|
84
|
+
v: op ?? rest.charAt(0),
|
|
85
|
+
start: i,
|
|
86
|
+
end: i + (op?.length ?? 1)
|
|
87
|
+
});
|
|
88
|
+
i += op?.length ?? 1;
|
|
89
|
+
}
|
|
90
|
+
return tokens;
|
|
91
|
+
}
|
|
92
|
+
const isOp = (token, value) => token?.t === "op" && token.v === value;
|
|
93
|
+
const isIdent = (token, value) => token?.t === "ident" && token.v === value;
|
|
94
|
+
/** Index of the bracket closing the one at `open`, or -1. */
|
|
95
|
+
function closing(tokens, open) {
|
|
96
|
+
let depth = 0;
|
|
97
|
+
for (const [offset, token] of tokens.slice(open).entries()) {
|
|
98
|
+
if (token.t !== "op") continue;
|
|
99
|
+
if (OPEN.has(String(token.v))) depth++;else if (CLOSE.has(String(token.v)) && --depth === 0) return open + offset;
|
|
100
|
+
}
|
|
101
|
+
return -1;
|
|
102
|
+
}
|
|
103
|
+
function unwrap(tokens) {
|
|
104
|
+
while (tokens.length > 2 && isOp(tokens[0], "(") && closing(tokens, 0) === tokens.length - 1) tokens = tokens.slice(1, -1);
|
|
105
|
+
return tokens;
|
|
106
|
+
}
|
|
107
|
+
/** Splits on a top-level operator; null when brackets are unbalanced or a part is empty. */
|
|
108
|
+
function split(tokens, op) {
|
|
109
|
+
const parts = [];
|
|
110
|
+
let depth = 0;
|
|
111
|
+
let start = 0;
|
|
112
|
+
for (const [i, token] of tokens.entries()) {
|
|
113
|
+
if (token.t === "op" && OPEN.has(String(token.v))) depth++;else if (token.t === "op" && CLOSE.has(String(token.v)) && --depth < 0) return null;else if (depth === 0 && isOp(token, op)) {
|
|
114
|
+
parts.push(tokens.slice(start, i));
|
|
115
|
+
start = i + 1;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
parts.push(tokens.slice(start));
|
|
119
|
+
return depth === 0 && parts.every(part => part.length) ? parts : null;
|
|
120
|
+
}
|
|
121
|
+
const COMPARISONS = {
|
|
122
|
+
"==": "eq",
|
|
123
|
+
"!=": "neq",
|
|
124
|
+
">": "gt",
|
|
125
|
+
">=": "gte",
|
|
126
|
+
"<": "lt",
|
|
127
|
+
"<=": "lte"
|
|
128
|
+
};
|
|
129
|
+
const TEXT_CALLS = {
|
|
130
|
+
contains: "contains",
|
|
131
|
+
startsWith: "starts_with",
|
|
132
|
+
endsWith: "ends_with"
|
|
133
|
+
};
|
|
134
|
+
function field(token) {
|
|
135
|
+
if (token?.t !== "ident") return null;
|
|
136
|
+
const name = String(token.v);
|
|
137
|
+
return name === "this" || name.startsWith("this.") || ["true", "false", "null"].includes(name) ? null : name;
|
|
138
|
+
}
|
|
139
|
+
function operand(token) {
|
|
140
|
+
if (token?.t === "str" || token?.t === "num") return {
|
|
141
|
+
value: token.v,
|
|
142
|
+
valueKind: "literal"
|
|
143
|
+
};
|
|
144
|
+
if (isIdent(token, "true") || isIdent(token, "false")) return {
|
|
145
|
+
value: token.v === "true",
|
|
146
|
+
valueKind: "literal"
|
|
147
|
+
};
|
|
148
|
+
if (token?.t === "ident" && CONTEXT.test(String(token.v))) return {
|
|
149
|
+
value: String(token.v),
|
|
150
|
+
valueKind: "context"
|
|
151
|
+
};
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
const textOperand = token => token?.t === "num" || isIdent(token, "true") || isIdent(token, "false") ? null : operand(token);
|
|
155
|
+
function yearOperand(token) {
|
|
156
|
+
const value = operand(token);
|
|
157
|
+
return value && (value.valueKind === "context" || typeof value.value === "number" && Number.isInteger(value.value)) ? value : null;
|
|
158
|
+
}
|
|
159
|
+
function rule(id, name, op, value) {
|
|
160
|
+
if (name === null || op === undefined || value === null) return null;
|
|
161
|
+
return {
|
|
162
|
+
kind: "rule",
|
|
163
|
+
id,
|
|
164
|
+
field: name,
|
|
165
|
+
op,
|
|
166
|
+
...value
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/** `field.method` → [field, method] when `method` is one of `methods`. */
|
|
170
|
+
function method(token, methods) {
|
|
171
|
+
if (token?.t !== "ident") return null;
|
|
172
|
+
const name = String(token.v);
|
|
173
|
+
const dot = name.lastIndexOf(".");
|
|
174
|
+
if (dot < 0 || !methods.includes(name.slice(dot + 1))) return null;
|
|
175
|
+
return [field({
|
|
176
|
+
...token,
|
|
177
|
+
v: name.slice(0, dot)
|
|
178
|
+
}), name.slice(dot + 1)];
|
|
179
|
+
}
|
|
180
|
+
/** Matches the exact token shapes the compiler emits (plus their alternate call forms). */
|
|
181
|
+
function matchRule(tokens, id) {
|
|
182
|
+
const [a, b, c, d, e, f, g] = tokens;
|
|
183
|
+
switch (tokens.length) {
|
|
184
|
+
case 3:
|
|
185
|
+
// field <cmp> operand
|
|
186
|
+
return b?.t === "op" && String(b.v) in COMPARISONS ? rule(id, field(a), COMPARISONS[String(b.v)], operand(c)) : null;
|
|
187
|
+
case 4:
|
|
188
|
+
{
|
|
189
|
+
// field.contains("text")
|
|
190
|
+
const call = method(a, Object.keys(TEXT_CALLS));
|
|
191
|
+
return call && isOp(b, "(") && isOp(d, ")") ? rule(id, call[0], TEXT_CALLS[call[1]], textOperand(c)) : null;
|
|
192
|
+
}
|
|
193
|
+
case 5:
|
|
194
|
+
{
|
|
195
|
+
// !field.contains("text") | field.year() == operand
|
|
196
|
+
const negated = isOp(a, "!") ? method(b, ["contains"]) : null;
|
|
197
|
+
if (negated && isOp(c, "(") && isOp(e, ")")) return rule(id, negated[0], "not_contains", textOperand(d));
|
|
198
|
+
const year = method(a, ["year"]);
|
|
199
|
+
return year && isOp(b, "(") && isOp(c, ")") && isOp(d, "==") ? rule(id, year[0], "year_eq", yearOperand(e)) : null;
|
|
200
|
+
}
|
|
201
|
+
case 6:
|
|
202
|
+
// contains(field, "text") | year(field) == operand
|
|
203
|
+
if (a?.t === "ident" && String(a.v) in TEXT_CALLS && isOp(b, "(") && isOp(d, ",") && isOp(f, ")")) return rule(id, field(c), TEXT_CALLS[String(a.v)], textOperand(e));
|
|
204
|
+
return isIdent(a, "year") && isOp(b, "(") && isOp(d, ")") && isOp(e, "==") ? rule(id, field(c), "year_eq", yearOperand(f)) : null;
|
|
205
|
+
case 7:
|
|
206
|
+
{
|
|
207
|
+
// !contains(field, "text") | (field == null || field == "")
|
|
208
|
+
if (isOp(a, "!") && isIdent(b, "contains") && isOp(c, "(") && isOp(e, ",") && isOp(g, ")")) return rule(id, field(d), "not_contains", textOperand(f));
|
|
209
|
+
const name = field(a);
|
|
210
|
+
const empty = isOp(b, "==") && isOp(d, "||") && isOp(f, "==");
|
|
211
|
+
const filled = isOp(b, "!=") && isOp(d, "&&") && isOp(f, "!=");
|
|
212
|
+
if (!name || !(empty || filled) || !isIdent(c, "null") || e?.t !== "ident" || e.v !== name || g?.t !== "str" || g.v !== "") return null;
|
|
213
|
+
return rule(id, name, empty ? "is_empty" : "is_not_empty", undefined);
|
|
214
|
+
}
|
|
215
|
+
default:
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/** `a == 1 || a == 2` as `a in [1, 2]`, or null when the children are not literal equalities on one field. */
|
|
220
|
+
function collapse(id, children) {
|
|
221
|
+
const [first] = children;
|
|
222
|
+
if (children.length < 2 || first?.kind !== "rule") return null;
|
|
223
|
+
const same = children.every(child => child.kind === "rule" && child.op === "eq" && child.valueKind === "literal" && child.field === first.field);
|
|
224
|
+
return same ? {
|
|
225
|
+
kind: "rule",
|
|
226
|
+
id,
|
|
227
|
+
field: first.field,
|
|
228
|
+
op: "in",
|
|
229
|
+
values: children.map(child => child.value)
|
|
230
|
+
} : null;
|
|
231
|
+
}
|
|
232
|
+
function parseTokens(source, tokens, ids) {
|
|
233
|
+
tokens = unwrap(tokens);
|
|
234
|
+
const id = ids();
|
|
235
|
+
const leaf = matchRule(tokens, id);
|
|
236
|
+
if (leaf) return leaf;
|
|
237
|
+
const expr = {
|
|
238
|
+
kind: "expr",
|
|
239
|
+
id,
|
|
240
|
+
expression: source.slice(tokens[0].start, tokens.at(-1).end)
|
|
241
|
+
};
|
|
242
|
+
// A top-level ternary binds looser than `||`; splitting would cut it apart.
|
|
243
|
+
if (tokens.some(token => token.t === "bad") || split(tokens, "?")?.length !== 1) return expr;
|
|
244
|
+
for (const [op, combinator] of [["||", "any"], ["&&", "all"]]) {
|
|
245
|
+
const parts = split(tokens, op);
|
|
246
|
+
if (!parts || parts.length < 2) continue;
|
|
247
|
+
const children = parts.map(part => parseTokens(source, part, ids));
|
|
248
|
+
return combinator === "any" && collapse(id, children) || {
|
|
249
|
+
kind: "group",
|
|
250
|
+
id,
|
|
251
|
+
combinator,
|
|
252
|
+
children
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
if (isOp(tokens[0], "!") && isOp(tokens[1], "(") && closing(tokens, 1) === tokens.length - 1) {
|
|
256
|
+
const inner = parseTokens(source, tokens.slice(2, -1), ids);
|
|
257
|
+
if (inner.kind === "rule" && inner.op === "in") return {
|
|
258
|
+
...inner,
|
|
259
|
+
id,
|
|
260
|
+
op: "not_in"
|
|
261
|
+
};
|
|
262
|
+
return {
|
|
263
|
+
kind: "group",
|
|
264
|
+
id,
|
|
265
|
+
combinator: "none",
|
|
266
|
+
children: inner.kind === "group" && inner.combinator === "any" ? inner.children : [inner]
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
return expr;
|
|
270
|
+
}
|
|
271
|
+
/** Parses a `where` expression into a root group. Empty text is an empty `all` group. */
|
|
272
|
+
export function parseWhere(where) {
|
|
273
|
+
let next = 0;
|
|
274
|
+
const ids = () => `n${next++}`;
|
|
275
|
+
const tokens = tokenize(where);
|
|
276
|
+
if (!tokens.length) return {
|
|
277
|
+
kind: "group",
|
|
278
|
+
id: ids(),
|
|
279
|
+
combinator: "all",
|
|
280
|
+
children: []
|
|
281
|
+
};
|
|
282
|
+
const node = parseTokens(where, tokens, ids);
|
|
283
|
+
return node.kind === "group" ? node : {
|
|
284
|
+
kind: "group",
|
|
285
|
+
id: ids(),
|
|
286
|
+
combinator: "all",
|
|
287
|
+
children: [node]
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
/** The rule `expression` spells, or null when it is custom CEL. */
|
|
291
|
+
export function tryExpressionToRule(expression, id = "rule") {
|
|
292
|
+
const tokens = tokenize(expression);
|
|
293
|
+
if (!tokens.length) return null;
|
|
294
|
+
const node = parseTokens(expression, tokens, () => id);
|
|
295
|
+
return node.kind === "rule" ? node : null;
|
|
296
|
+
}
|
|
297
|
+
const SYMBOLS = {
|
|
298
|
+
eq: "==",
|
|
299
|
+
neq: "!=",
|
|
300
|
+
gt: ">",
|
|
301
|
+
gte: ">=",
|
|
302
|
+
lt: "<",
|
|
303
|
+
lte: "<="
|
|
304
|
+
};
|
|
305
|
+
const CALLS = {
|
|
306
|
+
contains: "contains",
|
|
307
|
+
not_contains: "!contains",
|
|
308
|
+
starts_with: "startsWith",
|
|
309
|
+
ends_with: "endsWith"
|
|
310
|
+
};
|
|
311
|
+
function literal(value) {
|
|
312
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
313
|
+
if (typeof value === "number" && !Number.isFinite(value)) throw new Error(`Cannot compile non-finite number ${value}`);
|
|
314
|
+
return String(value);
|
|
315
|
+
}
|
|
316
|
+
/** True when a rule has everything it needs to compile. */
|
|
317
|
+
export const isComplete = rule => UNARY.has(rule.op) || (MULTI.has(rule.op) ? !!rule.values?.length : rule.value !== undefined);
|
|
318
|
+
/** The CEL for one rule, or "" for a draft without a value. */
|
|
319
|
+
export function ruleToExpression({
|
|
320
|
+
field,
|
|
321
|
+
op,
|
|
322
|
+
value,
|
|
323
|
+
valueKind,
|
|
324
|
+
values
|
|
325
|
+
}) {
|
|
326
|
+
if (op === "is_empty") return `(${field} == null || ${field} == "")`;
|
|
327
|
+
if (op === "is_not_empty") return `(${field} != null && ${field} != "")`;
|
|
328
|
+
if (MULTI.has(op)) {
|
|
329
|
+
if (!values?.length) return "";
|
|
330
|
+
if (values.length === 1) return `${field} ${op === "in" ? "==" : "!="} ${literal(values[0])}`;
|
|
331
|
+
const any = values.map(item => `${field} == ${literal(item)}`).join(" || ");
|
|
332
|
+
return op === "in" ? any : `!(${any})`;
|
|
333
|
+
}
|
|
334
|
+
if (value === undefined) return "";
|
|
335
|
+
if (valueKind === "context" && (typeof value !== "string" || !CONTEXT.test(value))) throw new Error(`Invalid context expression ${String(value)}`);
|
|
336
|
+
const right = valueKind === "context" ? value : literal(value);
|
|
337
|
+
if (op === "year_eq") return `${field}.year() == ${right}`;
|
|
338
|
+
const call = CALLS[op];
|
|
339
|
+
return call ? `${call}(${field}, ${right})` : `${field} ${SYMBOLS[op]} ${right}`;
|
|
340
|
+
}
|
|
341
|
+
/** Whether CEL text must be parenthesised to stand as one operand of `&&` or `||`. */
|
|
342
|
+
function loose(expression) {
|
|
343
|
+
const tokens = tokenize(expression);
|
|
344
|
+
if (tokens.some(token => token.t === "bad")) return true;
|
|
345
|
+
return ["||", "&&", "?"].some(op => split(tokens, op)?.length !== 1);
|
|
346
|
+
}
|
|
347
|
+
const JOIN = {
|
|
348
|
+
all: " && ",
|
|
349
|
+
any: " || ",
|
|
350
|
+
none: " || "
|
|
351
|
+
};
|
|
352
|
+
/** The CEL for a node, parenthesised when it is one of several operands and would otherwise bind loosely. */
|
|
353
|
+
function compileNode(node, operand) {
|
|
354
|
+
switch (node.kind) {
|
|
355
|
+
case "rule":
|
|
356
|
+
case "expr":
|
|
357
|
+
{
|
|
358
|
+
const text = node.kind === "rule" ? ruleToExpression(node) : node.expression.trim();
|
|
359
|
+
return operand && text && loose(text) ? `(${text})` : text;
|
|
360
|
+
}
|
|
361
|
+
case "group":
|
|
362
|
+
{
|
|
363
|
+
const text = compileGroup(node);
|
|
364
|
+
return operand && text && loose(text) ? `(${text})` : text;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
function compileGroup(group) {
|
|
369
|
+
const active = group.children.filter(child => countActive(child) > 0);
|
|
370
|
+
const parts = active.map(child => compileNode(child, active.length > 1));
|
|
371
|
+
if (!parts.length) return "";
|
|
372
|
+
return group.combinator === "none" ? `!(${parts.join(JOIN.none)})` : parts.join(JOIN[group.combinator]);
|
|
373
|
+
}
|
|
374
|
+
/** Emits the canonical `where` expression for a root group. Drafts and empty groups compile to nothing. */
|
|
375
|
+
export function compileWhere(root) {
|
|
376
|
+
return compileGroup(root);
|
|
377
|
+
}
|
|
378
|
+
/** Rules and expressions that currently contribute to the compiled query. */
|
|
379
|
+
export function countActive(node) {
|
|
380
|
+
if (node.kind === "group") return node.children.reduce((total, child) => total + countActive(child), 0);
|
|
381
|
+
return compileNode(node, false) ? 1 : 0;
|
|
382
|
+
}
|