viewkitjs 0.27.0 → 0.28.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/CHANGELOG.md +71 -0
- package/MIGRATION.md +40 -3
- package/package.json +1 -1
- package/views/dist/cells/cell-format.d.ts +3 -1
- package/views/dist/cells/cell-format.js +2 -2
- package/views/dist/cells/use-cell-format.d.ts +8 -1
- package/views/dist/cells/use-cell-format.js +13 -2
- package/views/dist/contract.d.ts +70 -3
- package/views/dist/contract.js +76 -0
- package/views/dist/controls/alert/alert.d.ts +46 -0
- package/views/dist/controls/alert/alert.js +11 -0
- package/views/dist/controls/alert/looks.d.ts +4 -0
- package/views/dist/controls/alert/looks.js +19 -0
- package/views/dist/controls/alert/tokens.d.ts +92 -0
- package/views/dist/controls/alert/tokens.js +25 -0
- package/views/dist/controls/alert-dialog/alert-dialog.d.ts +50 -0
- package/views/dist/controls/alert-dialog/alert-dialog.js +67 -0
- package/views/dist/controls/alert-dialog/confirm.d.ts +13 -0
- package/views/dist/controls/alert-dialog/confirm.js +11 -0
- package/views/dist/controls/alert-dialog/looks.d.ts +4 -0
- package/views/dist/controls/alert-dialog/looks.js +17 -0
- package/views/dist/controls/alert-dialog/tokens.d.ts +119 -0
- package/views/dist/controls/alert-dialog/tokens.js +32 -0
- package/views/dist/controls/alert-dialog.css +95 -0
- package/views/dist/controls/alert.css +51 -0
- package/views/dist/controls/collapsible/collapsible.d.ts +39 -0
- package/views/dist/controls/collapsible/collapsible.js +18 -0
- package/views/dist/controls/collapsible/looks.d.ts +4 -0
- package/views/dist/controls/collapsible/looks.js +5 -0
- package/views/dist/controls/collapsible/map-collapse.d.ts +2 -0
- package/views/dist/controls/collapsible/map-collapse.js +6 -0
- package/views/dist/controls/collapsible/tokens.d.ts +68 -0
- package/views/dist/controls/collapsible/tokens.js +19 -0
- package/views/dist/controls/collapsible.css +83 -0
- package/views/dist/controls/compound-editor/place-parts.js +5 -1
- package/views/dist/controls/controls.d.ts +9 -0
- package/views/dist/controls/controls.js +9 -0
- package/views/dist/controls/date-editor/date-editor.d.ts +2 -1
- package/views/dist/controls/date-editor/map-calendar.js +3 -1
- package/views/dist/controls/filter-editor/filter-editor.d.ts +56 -0
- package/views/dist/controls/filter-editor/filter-editor.js +208 -0
- package/views/dist/controls/filter-editor/how.d.ts +23 -0
- package/views/dist/controls/filter-editor/how.js +13 -0
- package/views/dist/controls/filter-editor/map-filter-body.d.ts +18 -0
- package/views/dist/controls/filter-editor/map-filter-body.js +164 -0
- package/views/dist/controls/filter-editor/map-rules.d.ts +85 -0
- package/views/dist/controls/filter-editor/map-rules.js +273 -0
- package/views/dist/controls/filter-editor/parts.d.ts +46 -0
- package/views/dist/controls/filter-editor/parts.js +75 -0
- package/views/dist/controls/filter-editor/rules.d.ts +104 -0
- package/views/dist/controls/filter-editor/rules.js +148 -0
- package/views/dist/controls/filter-editor/tokens.d.ts +83 -0
- package/views/dist/controls/filter-editor/tokens.js +23 -0
- package/views/dist/controls/filter-editor/words.d.ts +59 -0
- package/views/dist/controls/filter-editor/words.js +40 -0
- package/views/dist/controls/filter-editor.css +124 -0
- package/views/dist/controls/group-editor/group-editor.d.ts +56 -0
- package/views/dist/controls/group-editor/group-editor.js +126 -0
- package/views/dist/controls/group-editor/map-group-body.d.ts +19 -0
- package/views/dist/controls/group-editor/map-group-body.js +106 -0
- package/views/dist/controls/group-editor/rules.d.ts +48 -0
- package/views/dist/controls/group-editor/rules.js +41 -0
- package/views/dist/controls/group-editor/tokens.d.ts +15 -0
- package/views/dist/controls/group-editor/tokens.js +6 -0
- package/views/dist/controls/group-editor/words.d.ts +39 -0
- package/views/dist/controls/group-editor/words.js +21 -0
- package/views/dist/controls/group-editor.css +20 -0
- package/views/dist/controls/index.d.ts +19 -1
- package/views/dist/controls/index.js +19 -1
- package/views/dist/controls/internal/editor-parts.d.ts +8 -1
- package/views/dist/controls/internal/editor-parts.js +5 -0
- package/views/dist/controls/internal/step-return.d.ts +9 -0
- package/views/dist/controls/internal/step-return.js +27 -0
- package/views/dist/controls/links-editor/links-editor.d.ts +5 -3
- package/views/dist/controls/links-editor/links-editor.js +38 -17
- package/views/dist/controls/links-editor/map-links-body.js +4 -1
- package/views/dist/controls/links-editor.css +1 -1
- package/views/dist/controls/menu-list/menu-list.d.ts +17 -2
- package/views/dist/controls/menu-list/menu-list.js +25 -16
- package/views/dist/controls/modal/looks.d.ts +4 -0
- package/views/dist/controls/modal/looks.js +25 -0
- package/views/dist/controls/modal/modal.d.ts +69 -0
- package/views/dist/controls/modal/modal.js +62 -0
- package/views/dist/controls/modal/tokens.d.ts +192 -0
- package/views/dist/controls/modal/tokens.js +50 -0
- package/views/dist/controls/modal.css +130 -0
- package/views/dist/controls/notice/notice.d.ts +49 -0
- package/views/dist/controls/notice/notice.js +35 -0
- package/views/dist/controls/notice/tokens.d.ts +99 -0
- package/views/dist/controls/notice/tokens.js +27 -0
- package/views/dist/controls/notice.css +100 -0
- package/views/dist/controls/number-editor/number-editor.js +1 -1
- package/views/dist/controls/pane/looks.d.ts +4 -0
- package/views/dist/controls/pane/looks.js +11 -0
- package/views/dist/controls/pane/pane.d.ts +63 -0
- package/views/dist/controls/pane/pane.js +98 -0
- package/views/dist/controls/pane/state.d.ts +14 -0
- package/views/dist/controls/pane/state.js +7 -0
- package/views/dist/controls/pane/tokens.d.ts +79 -0
- package/views/dist/controls/pane/tokens.js +22 -0
- package/views/dist/controls/pane.css +104 -0
- package/views/dist/controls/picker/rows.d.ts +4 -2
- package/views/dist/controls/picker/rows.js +4 -3
- package/views/dist/controls/sort-editor/map-sort-body.d.ts +9 -0
- package/views/dist/controls/sort-editor/map-sort-body.js +81 -0
- package/views/dist/controls/sort-editor/rules.d.ts +48 -0
- package/views/dist/controls/sort-editor/rules.js +24 -0
- package/views/dist/controls/sort-editor/sort-editor.d.ts +58 -0
- package/views/dist/controls/sort-editor/sort-editor.js +180 -0
- package/views/dist/controls/sort-editor/tokens.d.ts +79 -0
- package/views/dist/controls/sort-editor/tokens.js +22 -0
- package/views/dist/controls/sort-editor/words.d.ts +48 -0
- package/views/dist/controls/sort-editor/words.js +25 -0
- package/views/dist/controls/sort-editor.css +96 -0
- package/views/dist/controls/toast/toast.d.ts +75 -0
- package/views/dist/controls/toast/toast.js +99 -0
- package/views/dist/controls/toast/tokens.d.ts +100 -0
- package/views/dist/controls/toast/tokens.js +27 -0
- package/views/dist/controls/toast.css +93 -0
- package/views/dist/controls/tokens.js +20 -0
- package/views/dist/controls/values-editor/values-editor.js +7 -1
- package/views/dist/display/date.js +2 -2
- package/views/dist/display/format.js +10 -2
- package/views/dist/display/options.d.ts +3 -1
- package/views/dist/display/use-display-options.d.ts +3 -0
- package/views/dist/display/use-display-options.js +10 -2
- package/views/dist/display/viewer.d.ts +1 -1
- package/views/dist/display/viewer.js +1 -1
- package/views/dist/editors/DateEditor.js +11 -9
- package/views/dist/editors/relation-panel.js +7 -1
- package/views/dist/editors/relation-picker.d.ts +2 -1
- package/views/dist/editors/relation-picker.js +4 -3
- package/views/dist/editors/relation-source.d.ts +4 -1
- package/views/dist/editors/relation-source.js +5 -2
- package/views/dist/field-row/editors/RowNumberEditor.js +2 -2
- package/views/dist/fields/number-edit.d.ts +6 -0
- package/views/dist/fields/number-edit.js +8 -1
- package/views/dist/grid/chrome/GridCellEditors.js +2 -2
- package/views/dist/grid/chrome/GridColumnDateSection.d.ts +4 -3
- package/views/dist/grid/chrome/GridColumnDateSection.js +4 -4
- package/views/dist/grid/chrome/GridColumnDurationSection.js +2 -2
- package/views/dist/grid/chrome/GridColumnMenu.js +6 -4
- package/views/dist/grid/chrome/GridFooter.js +2 -2
- package/views/dist/grid/chrome/grid-column-menu-items.js +4 -2
- package/views/dist/grid/chrome/useGridBandFigures.js +2 -2
- package/views/dist/grid/chrome/useGridFittedWidths.d.ts +3 -0
- package/views/dist/grid/chrome/useGridFittedWidths.js +9 -3
- package/views/dist/grid/chrome/useGridKeyboard.d.ts +4 -1
- package/views/dist/grid/chrome/useGridKeyboard.js +5 -2
- package/views/dist/grid/chrome/useGridTableInteractionModel.js +2 -1
- package/views/dist/grid/core/commit-model.d.ts +7 -0
- package/views/dist/grid/core/commit-model.js +5 -1
- package/views/dist/grid/core/editing-controller.js +10 -2
- package/views/dist/grid/core/editing-model.js +3 -7
- package/views/dist/grid/core/undo-owner.d.ts +6 -0
- package/views/dist/grid/core/undo-owner.js +13 -0
- package/views/dist/grid/core/useGridEditing.d.ts +2 -0
- package/views/dist/grid/core/useGridEditing.js +4 -0
- package/views/dist/looks/index.d.ts +5 -0
- package/views/dist/looks/index.js +5 -0
- package/views/dist/looks/pieces.js +10 -0
- package/views/dist/panels/model.d.ts +6 -1
- package/views/dist/panels/react/customize-body.d.ts +4 -2
- package/views/dist/panels/react/customize-body.js +3 -3
- package/views/dist/panels/react/customize.js +4 -2
- package/views/dist/panels/react/filter/FilterEditor.js +2 -2
- package/views/dist/panels/react/filter/PhoneFilterEditor.d.ts +8 -2
- package/views/dist/panels/react/filter/PhoneFilterEditor.js +9 -6
- package/views/dist/panels/react/filter/filter-body.d.ts +8 -5
- package/views/dist/panels/react/filter/filter-body.js +17 -6
- package/views/dist/panels/react/filter/filter-part-body.d.ts +4 -1
- package/views/dist/panels/react/filter/filter-part-body.js +3 -1
- package/views/dist/panels/react/panel-sheet.d.ts +5 -1
- package/views/dist/panels/react/panel-sheet.js +6 -2
- package/views/dist/panels/react/panel-step.d.ts +12 -0
- package/views/dist/panels/react/panel-step.js +26 -0
- package/views/dist/panels/react/sort/SortEditor.d.ts +4 -0
- package/views/dist/panels/react/sort/SortEditor.js +5 -2
- package/views/dist/panels/react/toolbar/ReadyToolbar.d.ts +8 -0
- package/views/dist/panels/react/toolbar/ReadyToolbar.js +84 -2
- package/views/dist/panels/react/toolbar/ToolbarBarFilter.js +2 -2
- package/views/dist/panels/react/toolbar/ToolbarFilterAdd.d.ts +16 -0
- package/views/dist/panels/react/toolbar/ToolbarFilterAdd.js +49 -0
- package/views/dist/panels/react/toolbar/ToolbarFilterSheet.js +3 -1
- package/views/dist/panels/react/toolbar/ToolbarFilters.d.ts +4 -1
- package/views/dist/panels/react/toolbar/ToolbarFilters.js +36 -21
- package/views/dist/panels/react/toolbar/ToolbarQuickFilters.d.ts +4 -1
- package/views/dist/panels/react/toolbar/ToolbarQuickFilters.js +2 -2
- package/views/dist/panels/react/toolbar/ToolbarSortPill.d.ts +5 -0
- package/views/dist/panels/react/toolbar/ToolbarSortPill.js +15 -6
- package/views/dist/panels/react/toolbar/panel-part.d.ts +5 -1
- package/views/dist/panels/react/toolbar/panel-part.js +2 -2
- package/views/dist/panels/react/toolbar/use-filter-add.d.ts +16 -0
- package/views/dist/panels/react/toolbar/use-filter-add.js +32 -0
- package/views/dist/panels/react.d.ts +1 -0
- package/views/dist/panels/react.js +1 -0
- package/views/dist/panels/settings/filter-groups.js +35 -1
- package/views/dist/panels/settings/group-buckets.js +1 -0
- package/views/dist/provider/ViewsProvider.d.ts +7 -1
- package/views/dist/provider/ViewsProvider.js +23 -3
- package/views/dist/provider/components.d.ts +30 -4
- package/views/dist/provider/moment.d.ts +6 -0
- package/views/dist/provider/moment.js +10 -0
- package/views/dist/styles.css +847 -52
- package/views/dist/styles.unlayered.css +848 -52
- package/views/docs/contract.md +15 -9
- package/views/docs/controls/alert-dialog.md +37 -0
- package/views/docs/controls/alert.md +30 -0
- package/views/docs/controls/collapsible.md +22 -0
- package/views/docs/controls/filter-editor.md +28 -0
- package/views/docs/controls/group-editor.md +11 -0
- package/views/docs/controls/modal.md +55 -0
- package/views/docs/controls/notice.md +32 -0
- package/views/docs/controls/pane.md +25 -0
- package/views/docs/controls/sort-editor.md +27 -0
- package/views/docs/controls/toast.md +32 -0
- package/views/docs/guides/cells.md +1 -1
- package/views/docs/guides/display.md +1 -1
- package/views/docs/guides/the-grid.md +1 -1
- package/views/docs/guides/the-provider.md +19 -0
- package/views/docs/reference/cells.md +2 -2
- package/views/docs/reference/controls.md +213 -7
- package/views/docs/reference/display.md +2 -2
- package/views/docs/reference/editors-relation.md +1 -1
- package/views/docs/reference/looks.md +10 -0
- package/views/docs/reference/panels-react.md +7 -6
- package/views/docs/reference/panels.md +1 -1
- package/views/docs/reference/react.md +7 -7
- package/views/docs/reference/views.md +1 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { findItem } from "../picker/rows.js";
|
|
2
|
+
import { CHECKED } from "./how.js";
|
|
3
|
+
import { isOwnWay, isPair, OWN_WAY, rulesOf } from "./rules.js";
|
|
4
|
+
const TYPES = {
|
|
5
|
+
status: "status", select: "select", multiselect: "select", user: "person", relation: "record",
|
|
6
|
+
number: "number", currency: "number", percent: "number",
|
|
7
|
+
text: "text", email: "text", url: "text", domain: "text", phone: "text", name: "text",
|
|
8
|
+
date: "date", datetime: "date", boolean: "checkbox", range: "range",
|
|
9
|
+
};
|
|
10
|
+
export const CHOICE = new Set(["status", "select", "person", "record"]);
|
|
11
|
+
export const kitTypeOf = (type) => TYPES[type] ?? null;
|
|
12
|
+
export function kitPartTypeOf(type, part) {
|
|
13
|
+
if (part === undefined)
|
|
14
|
+
return kitTypeOf(type);
|
|
15
|
+
if (type === "location")
|
|
16
|
+
return part === "city" || part === "region" || part === "postalCode" ? "text" : null;
|
|
17
|
+
if (type === "range")
|
|
18
|
+
return "number";
|
|
19
|
+
return kitTypeOf(type);
|
|
20
|
+
}
|
|
21
|
+
export const RELATED = "Related records";
|
|
22
|
+
const SEP = "\u0000";
|
|
23
|
+
export const pathOf = (relationKey, farKey) => `${relationKey}${SEP}${farKey}`;
|
|
24
|
+
export const pathParts = (value) => {
|
|
25
|
+
const at = value.indexOf(SEP);
|
|
26
|
+
return at < 0 ? [value] : [value.slice(0, at), value.slice(at + 1)];
|
|
27
|
+
};
|
|
28
|
+
const placeOf = (a) => [a.relation ?? "", a.fieldKey, a.part ?? ""].join(SEP);
|
|
29
|
+
export function kitFieldsOf(fields, choices, relation) {
|
|
30
|
+
const at = new Map();
|
|
31
|
+
const field = (f, value, place, from) => {
|
|
32
|
+
const type = kitPartTypeOf(f.type, f.part);
|
|
33
|
+
if (!type) {
|
|
34
|
+
if (f.part !== undefined)
|
|
35
|
+
at.set(value, place);
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
const values = type === "checkbox" ? CHECKED : CHOICE.has(type) && f.part === undefined ? choices(f.fieldKey, from) : undefined;
|
|
39
|
+
at.set(value, place);
|
|
40
|
+
return [{ value, label: f.label, type, ...(values ? { choices: values } : {}) }];
|
|
41
|
+
};
|
|
42
|
+
const own = (f, from) => field(f, f.key, { fieldKey: f.fieldKey, ...(f.part !== undefined ? { part: f.part } : {}) }, from);
|
|
43
|
+
if (relation !== undefined)
|
|
44
|
+
return { fields: (fields.find((f) => f.far && f.fieldKey === relation)?.far ?? []).flatMap((f) => own(f, relation)), at };
|
|
45
|
+
const list = fields.filter((f) => !f.far).flatMap((f) => own(f));
|
|
46
|
+
const related = fields.filter((f) => f.far).flatMap((r) => {
|
|
47
|
+
const level = (r.far ?? []).flatMap((f) => field(f, pathOf(r.key, f.key), { fieldKey: f.fieldKey, ...(f.part !== undefined ? { part: f.part } : {}), relation: r.fieldKey }, r.fieldKey));
|
|
48
|
+
return level.length ? [{ value: r.key, label: r.label, type: "record", group: RELATED, level }] : [];
|
|
49
|
+
});
|
|
50
|
+
return { fields: [...list, ...related], at };
|
|
51
|
+
}
|
|
52
|
+
const none = { write: () => null, read: () => null };
|
|
53
|
+
const list = (v) => (Array.isArray(v) ? v.map(String) : typeof v === "string" ? [v] : []);
|
|
54
|
+
const one = { write: (v) => (typeof v === "string" ? [v] : []), read: (v) => (Array.isArray(v) && typeof v[0] === "string" ? v[0] : null) };
|
|
55
|
+
const several = { write: list, read: list };
|
|
56
|
+
const words = { write: (v) => (typeof v === "string" ? v : ""), read: (v) => (typeof v === "string" && v ? v : null) };
|
|
57
|
+
const figure = { write: (v) => (typeof v === "number" ? v : null), read: (v) => (typeof v === "number" ? v : null) };
|
|
58
|
+
const day = { write: (v) => (typeof v === "string" && v ? v : null), read: (v) => (typeof v === "string" && v ? v : null) };
|
|
59
|
+
const end = (n) => (typeof n === "number" ? n : null);
|
|
60
|
+
const pair = {
|
|
61
|
+
write: (v) => (isPair(v) ? [v[0], v[1]] : [null, null]),
|
|
62
|
+
read: (v) => { if (!Array.isArray(v))
|
|
63
|
+
return null; const p = [end(v[0]), end(v[1])]; return p[0] === null && p[1] === null ? null : p; },
|
|
64
|
+
};
|
|
65
|
+
const yesNo = { write: (v) => (v === "true" ? true : v === "false" ? false : undefined), read: (v) => (typeof v === "boolean" ? String(v) : null) };
|
|
66
|
+
const EMPTINESS = [{ how: "empty", operator: "is_empty", ...none }, { how: "not-empty", operator: "is_not_empty", ...none }];
|
|
67
|
+
const WAYS = {
|
|
68
|
+
...Object.fromEntries(["status", "select", "person", "record"].map((t) => [t, [
|
|
69
|
+
{ how: "is-any-of", operator: "is_any_of", ...several }, { how: "is-none-of", operator: "is_none_of", ...several },
|
|
70
|
+
{ how: "is", operator: "is_any_of", ...one }, { how: "is-not", operator: "is_none_of", ...one }, ...EMPTINESS
|
|
71
|
+
]])),
|
|
72
|
+
number: [{ how: "is", operator: "eq", ...figure }, { how: "is-not", operator: "neq", ...figure }, { how: "less-than", operator: "lt", ...figure },
|
|
73
|
+
{ how: "greater-than", operator: "gt", ...figure }, ...EMPTINESS],
|
|
74
|
+
text: [{ how: "contains", operator: "contains", ...words }, { how: "does-not-contain", operator: "not_contains", ...words },
|
|
75
|
+
{ how: "starts-with", operator: "starts_with", ...words }, { how: "ends-with", operator: "ends_with", ...words },
|
|
76
|
+
{ how: "is", operator: "is", ...words }, { how: "is-not", operator: "is_not", ...words }, ...EMPTINESS],
|
|
77
|
+
date: [{ how: "is", operator: "is", ...day }, { how: "is-not", operator: "is_not", ...day }, { how: "before", operator: "is_before", ...day },
|
|
78
|
+
{ how: "after", operator: "is_after", ...day }, ...EMPTINESS],
|
|
79
|
+
checkbox: [{ how: "is", operator: "is", ...yesNo }],
|
|
80
|
+
range: [{ how: "any-part-between", operator: "overlaps", ...pair }, { how: "entirely-within", operator: "within", ...pair }, { how: "includes", operator: "includes", ...figure },
|
|
81
|
+
{ how: "at-least", operator: "at_least", ...figure }, { how: "at-most", operator: "at_most", ...figure }, ...EMPTINESS],
|
|
82
|
+
};
|
|
83
|
+
const ASIDE_FIELD = "aside:";
|
|
84
|
+
const typeOf = (f, key) => findItem(f.fields, key)?.item.type;
|
|
85
|
+
const isGroup = (n) => "op" in n;
|
|
86
|
+
const isKitGroup = (x) => "rules" in x;
|
|
87
|
+
const isRelation = (n) => "quantifier" in n;
|
|
88
|
+
const plainRule = (n) => !isGroup(n) && !isRelation(n) && !n.setAside;
|
|
89
|
+
const groupIds = (g) => [g.id, ...g.rules.flatMap((r) => (isKitGroup(r) ? groupIds(r) : []))];
|
|
90
|
+
const holds = (g, id) => g.rules.some((r) => r.id === id || (isKitGroup(r) && holds(r, id)));
|
|
91
|
+
const itemFor = (g, id) => g.rules.find((r) => r.id === id || (isKitGroup(r) && holds(r, id)));
|
|
92
|
+
function anchorIn(g, order, after) {
|
|
93
|
+
if (after === null)
|
|
94
|
+
return null;
|
|
95
|
+
const here = itemFor(g, after);
|
|
96
|
+
if (here)
|
|
97
|
+
return here.id;
|
|
98
|
+
for (let i = order.indexOf(after) - 1; i >= 0; i--) {
|
|
99
|
+
const earlier = itemFor(g, order[i]);
|
|
100
|
+
if (earlier)
|
|
101
|
+
return earlier.id;
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
export function viewKitFilterOf(filter, fields, kept, raw = new Map()) {
|
|
106
|
+
const here = new Set(groupIds(filter)), orphans = {};
|
|
107
|
+
for (const [id, k] of Object.entries(kept)) {
|
|
108
|
+
if (here.has(id) || !k.nodes.length)
|
|
109
|
+
continue;
|
|
110
|
+
let to = k.parent;
|
|
111
|
+
while (to && !here.has(to))
|
|
112
|
+
to = kept[to]?.parent ?? null;
|
|
113
|
+
(orphans[to ?? filter.id] ??= []).push({ op: k.op, items: k.nodes.map((n) => n.node) });
|
|
114
|
+
}
|
|
115
|
+
const top = kept[filter.id]?.op === "not" ? "not" : filter.join;
|
|
116
|
+
return { ...levelOf(filter, fields, kept, raw, orphans), op: top };
|
|
117
|
+
}
|
|
118
|
+
function withoutAside(n) {
|
|
119
|
+
const { setAside: _gone, ...rest } = n;
|
|
120
|
+
return isRelation(n) ? { ...rest, where: { ...n.where, items: n.where.items.map((i) => (isGroup(i) || isRelation(i) ? i : withoutAside(i))) } } : rest;
|
|
121
|
+
}
|
|
122
|
+
function ruleNodes(x, fields, raw) {
|
|
123
|
+
const was = raw.get(x.id);
|
|
124
|
+
if (was && x.aside)
|
|
125
|
+
return [was];
|
|
126
|
+
if (was && isOwnWay(x.how))
|
|
127
|
+
return [withoutAside(was)];
|
|
128
|
+
const type = typeOf(fields, x.field), place = fields.at.get(x.field);
|
|
129
|
+
const way = type && WAYS[type].find((w) => w.how === x.how);
|
|
130
|
+
const value = way && way.write(x.value);
|
|
131
|
+
if (!way || !place || value === undefined)
|
|
132
|
+
return [];
|
|
133
|
+
const rule = { fieldKey: place.fieldKey, ...(place.part !== undefined ? { part: place.part } : {}), operator: way.operator, value };
|
|
134
|
+
return [place.relation !== undefined ? { fieldKey: place.relation, quantifier: "any", where: { op: "and", items: [rule] } } : rule];
|
|
135
|
+
}
|
|
136
|
+
function levelOf(filter, fields, kept, raw, orphans) {
|
|
137
|
+
const k = kept[filter.id];
|
|
138
|
+
const after = new Map();
|
|
139
|
+
for (const n of k?.nodes ?? []) {
|
|
140
|
+
const at = anchorIn(filter, k?.order ?? [], n.after);
|
|
141
|
+
after.set(at, [...(after.get(at) ?? []), n.node]);
|
|
142
|
+
}
|
|
143
|
+
const items = [...(after.get(null) ?? [])];
|
|
144
|
+
for (const x of filter.rules) {
|
|
145
|
+
items.push(...(isKitGroup(x) ? [levelOf(x, fields, kept, raw, orphans)] : ruleNodes(x, fields, raw)), ...(after.get(x.id) ?? []));
|
|
146
|
+
}
|
|
147
|
+
return { op: filter.join, items: [...items, ...(orphans[filter.id] ?? [])] };
|
|
148
|
+
}
|
|
149
|
+
export function kitFilterOf(level, fields, asideWords) {
|
|
150
|
+
const kept = {}, raw = new Map();
|
|
151
|
+
let groups = 0, rules = 0;
|
|
152
|
+
const back = new Map([...fields.at].map(([value, place]) => [placeOf(place), value]));
|
|
153
|
+
const valueOf = (n, relation) => back.get(placeOf({ fieldKey: n.fieldKey, ...(n.part !== undefined ? { part: n.part } : {}), ...(relation !== undefined ? { relation } : {}) }));
|
|
154
|
+
const read = (n, relation, how) => {
|
|
155
|
+
const value = valueOf(n, relation), type = value && typeOf(fields, value), way = type && WAYS[type].find(how ?? ((w) => w.operator === n.operator));
|
|
156
|
+
return value && way ? { id: `r${++rules}`, field: value, how: way.how, value: way.read(n.value) } : null;
|
|
157
|
+
};
|
|
158
|
+
const readOwn = (n, node, relation) => {
|
|
159
|
+
const value = valueOf(n, relation);
|
|
160
|
+
if (!value)
|
|
161
|
+
return null;
|
|
162
|
+
const id = `r${++rules}`;
|
|
163
|
+
raw.set(id, node);
|
|
164
|
+
return { id, field: value, how: `${OWN_WAY}${n.operator}`, value: null };
|
|
165
|
+
};
|
|
166
|
+
const readAside = (n, relation, node = n) => {
|
|
167
|
+
const value = valueOf(n, relation), type = value && typeOf(fields, value), way = type && WAYS[type].find((w) => w.operator === n.operator);
|
|
168
|
+
const id = `r${++rules}`;
|
|
169
|
+
raw.set(id, node);
|
|
170
|
+
return {
|
|
171
|
+
id, field: value ?? `${ASIDE_FIELD}${n.fieldKey}${n.part !== undefined ? `~${n.part}` : ""}`,
|
|
172
|
+
how: way ? way.how : `${OWN_WAY}${n.operator}`, value: way ? way.read(n.value) : null,
|
|
173
|
+
aside: asideWords?.(n, relation) ?? { label: n.fieldKey, why: "", putBack: false },
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
const dateIsNot = (n) => {
|
|
177
|
+
const [only] = n.items;
|
|
178
|
+
if (n.op !== "not" || n.items.length !== 1 || !only || !plainRule(only) || only.operator !== "is")
|
|
179
|
+
return null;
|
|
180
|
+
const value = valueOf(only);
|
|
181
|
+
return value && typeOf(fields, value) === "date" ? read(only, undefined, (w) => w.how === "is-not") : null;
|
|
182
|
+
};
|
|
183
|
+
const walk = (g, id, parent) => {
|
|
184
|
+
const out = [];
|
|
185
|
+
const place = () => (kept[id] ??= { parent, op: g.op, nodes: [], order: [] });
|
|
186
|
+
const keep = (n) => { place().nodes.push({ node: n, after: out.at(-1)?.id ?? null }); };
|
|
187
|
+
if (g.op === "not")
|
|
188
|
+
place();
|
|
189
|
+
for (const n of g.items) {
|
|
190
|
+
if (isGroup(n)) {
|
|
191
|
+
const not = dateIsNot(n);
|
|
192
|
+
if (not) {
|
|
193
|
+
out.push(not);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (n.op === "not") {
|
|
197
|
+
keep(n);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const inner = walk(n, `v${++groups}`, id);
|
|
201
|
+
if (inner.rules.length)
|
|
202
|
+
out.push(inner);
|
|
203
|
+
else {
|
|
204
|
+
delete kept[inner.id];
|
|
205
|
+
keep(n);
|
|
206
|
+
}
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (isRelation(n)) {
|
|
210
|
+
const [only] = n.where.items;
|
|
211
|
+
const one = n.quantifier === "any" && n.where.items.length === 1 && !!only && !isGroup(only) && !isRelation(only) && only.part === undefined;
|
|
212
|
+
const aside = one && (n.setAside || only.setAside) ? readAside({ ...only, ...(only.setAside ?? n.setAside ? { setAside: only.setAside ?? n.setAside } : {}) }, n.fieldKey, n) : null;
|
|
213
|
+
const drawn = aside ?? (one && plainRule(only) ? read(only, n.fieldKey) ?? readOwn(only, n, n.fieldKey) : null);
|
|
214
|
+
if (drawn)
|
|
215
|
+
out.push(drawn);
|
|
216
|
+
else
|
|
217
|
+
keep(n);
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const drawn = n.setAside ? readAside(n) : read(n) ?? readOwn(n, n);
|
|
221
|
+
if (drawn)
|
|
222
|
+
out.push(drawn);
|
|
223
|
+
else
|
|
224
|
+
keep(n);
|
|
225
|
+
}
|
|
226
|
+
if (kept[id])
|
|
227
|
+
kept[id].order = out.map((x) => x.id);
|
|
228
|
+
return { id, join: g.op === "or" ? "or" : "and", rules: out };
|
|
229
|
+
};
|
|
230
|
+
return { filter: walk(level, "root", null), kept, raw };
|
|
231
|
+
}
|
|
232
|
+
function settled(filter, kept) {
|
|
233
|
+
const here = new Set(groupIds(filter)), out = {}, landing = {};
|
|
234
|
+
for (const [id, k] of Object.entries(kept)) {
|
|
235
|
+
if (here.has(id) || !k.nodes.length)
|
|
236
|
+
continue;
|
|
237
|
+
let to = k.parent;
|
|
238
|
+
while (to && !here.has(to))
|
|
239
|
+
to = kept[to]?.parent ?? null;
|
|
240
|
+
(landing[to ?? filter.id] ??= []).push({ op: k.op, items: k.nodes.map((n) => n.node) });
|
|
241
|
+
}
|
|
242
|
+
const walk = (g, parent) => {
|
|
243
|
+
const k = kept[g.id], last = g.rules.at(-1)?.id ?? null;
|
|
244
|
+
const nodes = [...(k?.nodes ?? []).map((n) => ({ node: n.node, after: anchorIn(g, k?.order ?? [], n.after) })), ...(landing[g.id] ?? []).map((node) => ({ node, after: last }))];
|
|
245
|
+
if (k || nodes.length)
|
|
246
|
+
out[g.id] = { parent, op: k?.op ?? g.join, nodes, order: g.rules.map((r) => r.id) };
|
|
247
|
+
for (const r of g.rules)
|
|
248
|
+
if (isKitGroup(r))
|
|
249
|
+
walk(r, g.id);
|
|
250
|
+
};
|
|
251
|
+
walk(filter, null);
|
|
252
|
+
return out;
|
|
253
|
+
}
|
|
254
|
+
const WRITTEN = new Map();
|
|
255
|
+
const WRITTEN_MAX = 50;
|
|
256
|
+
function writesNothing(filter, fields) {
|
|
257
|
+
return rulesOf(filter).some((r) => { const type = typeOf(fields, r.field), way = type && WAYS[type].find((w) => w.how === r.how); return !!way && fields.at.has(r.field) && way.write(r.value) === undefined; });
|
|
258
|
+
}
|
|
259
|
+
export function rulesFor(level, fields, own, asideWords) {
|
|
260
|
+
const key = JSON.stringify(level);
|
|
261
|
+
return own?.get(key) ?? WRITTEN.get(key) ?? kitFilterOf(level, fields, asideWords);
|
|
262
|
+
}
|
|
263
|
+
export function writeRules(filter, fields, kept, own, raw = new Map()) {
|
|
264
|
+
const level = viewKitFilterOf(filter, fields, kept, raw), key = JSON.stringify(level), at = writesNothing(filter, fields) ? own : WRITTEN;
|
|
265
|
+
own?.clear();
|
|
266
|
+
if (!at)
|
|
267
|
+
return level;
|
|
268
|
+
at.delete(key);
|
|
269
|
+
at.set(key, { filter, kept: settled(filter, kept), raw });
|
|
270
|
+
if (at.size > WRITTEN_MAX)
|
|
271
|
+
at.delete(at.keys().next().value);
|
|
272
|
+
return level;
|
|
273
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { FilterOwn } from "./filter-editor.js";
|
|
3
|
+
import { type FilterField } from "./how.js";
|
|
4
|
+
import { type FilterGroup, type FilterRule } from "./rules.js";
|
|
5
|
+
import { type FilterEditorWords } from "./words.js";
|
|
6
|
+
/** Which part of a filter was pressed: its field, how it compares, or its value. */
|
|
7
|
+
export type FilterPart = "field" | "how" | "value";
|
|
8
|
+
export interface FilterPartProps {
|
|
9
|
+
/** The part pressed. */
|
|
10
|
+
part: FilterPart;
|
|
11
|
+
/** The filter's rule, by its id; none for Filter and +, which add one. */
|
|
12
|
+
rule?: string;
|
|
13
|
+
/** What it is, to a screen reader ("Filter"). */
|
|
14
|
+
label: string;
|
|
15
|
+
/** The fields a list filters by, each typed, a choice's values with it. */
|
|
16
|
+
fields: FilterField[];
|
|
17
|
+
/** The view's filter. */
|
|
18
|
+
filter: FilterGroup;
|
|
19
|
+
/** Each change, as it is made: a filter added, its field, its way or its value changed. A rule's change comes as a change to the
|
|
20
|
+
* filter as it stands when it lands, so a value typed and kept as its editor leaves the page changes nothing where its rule is
|
|
21
|
+
* gone and never brings an earlier filter back. */
|
|
22
|
+
onFilterChange: (filter: FilterGroup | ((now: FilterGroup) => FilterGroup)) => void;
|
|
23
|
+
/** The job is done and the Popover closes: a field or a way picked, one value picked, Enter in a typed value. */
|
|
24
|
+
onDone: () => void;
|
|
25
|
+
/** ViewKit's own words and value body for a rule the kit has no box for; null where the kit draws it. */
|
|
26
|
+
own?: (rule: FilterRule) => FilterOwn | null;
|
|
27
|
+
/** Which form its lists take; the computer's unless said. */
|
|
28
|
+
form?: "computer" | "phone";
|
|
29
|
+
/** Its words; English unless handed. */
|
|
30
|
+
words?: Partial<FilterEditorWords>;
|
|
31
|
+
}
|
|
32
|
+
/** What the fields' search says. */
|
|
33
|
+
export declare const FILTER_SEARCH: string;
|
|
34
|
+
/** What a filter's value says on the bar while it has none, by its field's type. */
|
|
35
|
+
export declare function valuePlaceholder(field: FilterField, w?: FilterEditorWords): string;
|
|
36
|
+
/** The body a Popover holds under the part of a filter pressed: the fields, its type's ways, or its value in its type's own. */
|
|
37
|
+
export declare function FilterPartBody({ part, rule: ruleId, label, fields, filter, onFilterChange, onDone, own, form, words: given }: FilterPartProps): import("react/jsx-runtime").JSX.Element | null;
|
|
38
|
+
/** A range's From and To: two Number fields, an en dash between, each named by its end; either may be left empty. */
|
|
39
|
+
export declare function PairValue({ name, value, onValue, size, onKeyDown, words: given }: {
|
|
40
|
+
name: string;
|
|
41
|
+
value: [number | null, number | null];
|
|
42
|
+
onValue: (v: [number | null, number | null]) => void;
|
|
43
|
+
size?: "sm" | "lg";
|
|
44
|
+
onKeyDown?: (e: React.KeyboardEvent) => void;
|
|
45
|
+
words?: Partial<FilterEditorWords>;
|
|
46
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { DateEditor } from "../date-editor/date-editor.js";
|
|
4
|
+
import { Input } from "../input/input.js";
|
|
5
|
+
import { NumberField } from "../number-field/number-field.js";
|
|
6
|
+
import { Picker } from "../picker/picker.js";
|
|
7
|
+
import { itemLabel } from "../picker/rows.js";
|
|
8
|
+
import { HOW, wayOf } from "./how.js";
|
|
9
|
+
import { addRule, changeRule, fieldOf, isOwnWay, isPair, rulesOf } from "./rules.js";
|
|
10
|
+
import { filterWords, FILTER_EDITOR_WORDS } from "./words.js";
|
|
11
|
+
export const FILTER_SEARCH = FILTER_EDITOR_WORDS.search;
|
|
12
|
+
export function valuePlaceholder(field, w = FILTER_EDITOR_WORDS) {
|
|
13
|
+
if (field.choices)
|
|
14
|
+
return w.choose;
|
|
15
|
+
return field.type === "number" || field.type === "range" ? w.typeNumber : field.type === "date" ? w.chooseDate : w.typeValue;
|
|
16
|
+
}
|
|
17
|
+
export function FilterPartBody({ part, rule: ruleId, label, fields, filter, onFilterChange, onDone, own, form = "computer", words: given }) {
|
|
18
|
+
const w = filterWords(given), words = w.picker ? { words: w.picker } : {};
|
|
19
|
+
const r = ruleId ? rulesOf(filter).find((x) => x.id === ruleId) : undefined;
|
|
20
|
+
const field = r && fieldOf(fields, r.field), name = r ? itemLabel(fields, r.field) : label;
|
|
21
|
+
const set = (change) => { if (r)
|
|
22
|
+
onFilterChange((now) => (rulesOf(now).some((x) => x.id === r.id) ? changeRule(now, r.id, change, fields) : now)); };
|
|
23
|
+
if (part === "field")
|
|
24
|
+
return (_jsx(Picker, { label: r ? name : label, items: fields, value: r?.field ?? null, search: w.search, searchShown: true, form: form, onDone: onDone, ...words, onValueChange: (v) => {
|
|
25
|
+
if (typeof v !== "string")
|
|
26
|
+
return;
|
|
27
|
+
if (!r) {
|
|
28
|
+
const added = addRule(filter, v, fields);
|
|
29
|
+
if (added.id)
|
|
30
|
+
onFilterChange(added.filter);
|
|
31
|
+
}
|
|
32
|
+
else if (v !== r.field)
|
|
33
|
+
set({ field: v });
|
|
34
|
+
} }));
|
|
35
|
+
if (!r)
|
|
36
|
+
return null;
|
|
37
|
+
const theirs = part === "value" ? own?.(r) : null;
|
|
38
|
+
if (theirs)
|
|
39
|
+
return _jsx(_Fragment, { children: theirs.body });
|
|
40
|
+
if (!field)
|
|
41
|
+
return null;
|
|
42
|
+
const takes = wayOf(field.type, r.how)?.takes;
|
|
43
|
+
if (part === "how") {
|
|
44
|
+
const ways = HOW[field.type].map((x) => ({ value: x.value, label: w.way(x.value), divider: x.value === "empty" }));
|
|
45
|
+
return _jsx(Picker, { label: w.howOf(name), items: ways, value: isOwnWay(r.how) ? null : r.how, searchShown: false, form: form, onDone: onDone, ...words, onValueChange: (v) => { if (typeof v === "string" && v !== r.how)
|
|
46
|
+
set({ how: v }); } });
|
|
47
|
+
}
|
|
48
|
+
if (!takes || takes === "none")
|
|
49
|
+
return null;
|
|
50
|
+
if (field.choices) {
|
|
51
|
+
const several = takes === "several", value = several ? (Array.isArray(r.value) && !isPair(r.value) ? r.value : []) : typeof r.value === "string" ? r.value : null;
|
|
52
|
+
return _jsx(Picker, { label: name, items: field.choices, multiple: several, value: value, form: form, onDone: onDone, ...words, onValueChange: (v) => set({ value: several ? (Array.isArray(v) ? v : []) : typeof v === "string" ? v : null }) });
|
|
53
|
+
}
|
|
54
|
+
if (takes === "two")
|
|
55
|
+
return _jsx(PairValue, { name: name, value: isPair(r.value) ? r.value : [null, null], onValue: (v) => set({ value: v }), words: w, onKeyDown: (e) => { if (e.key === "Enter") {
|
|
56
|
+
e.preventDefault();
|
|
57
|
+
onDone();
|
|
58
|
+
} } });
|
|
59
|
+
if (field.type === "date")
|
|
60
|
+
return _jsx(DateEditor, { label: name, value: typeof r.value === "string" ? r.value : null, onValueChange: (v) => set({ value: v }), onDone: onDone, form: form });
|
|
61
|
+
return _jsx(TypedValue, { rule: r, field: field, name: name, set: set, onDone: onDone, w: w });
|
|
62
|
+
}
|
|
63
|
+
export function PairValue({ name, value, onValue, size, onKeyDown, words: given }) {
|
|
64
|
+
const w = filterWords(given), [from, to] = value;
|
|
65
|
+
return (_jsxs("div", { className: "o-filter-editor-pair", children: [_jsx(NumberField, { size: size, steppers: false, "aria-label": w.fromOf(name), placeholder: w.from, value: from, onValueChange: (v) => onValue([v ?? null, to]), onKeyDown: onKeyDown }), _jsx("span", { className: "o-filter-editor-dash", "aria-hidden": "true", children: "\u2013" }), _jsx(NumberField, { size: size, steppers: false, "aria-label": w.toOf(name), placeholder: w.to, value: to, onValueChange: (v) => onValue([from, v ?? null]), onKeyDown: onKeyDown })] }));
|
|
66
|
+
}
|
|
67
|
+
function TypedValue({ rule: r, field, name, set, onDone, w }) {
|
|
68
|
+
const enter = (e) => { if (e.key === "Enter") {
|
|
69
|
+
e.preventDefault();
|
|
70
|
+
onDone();
|
|
71
|
+
} };
|
|
72
|
+
return field.type === "number" || field.type === "range"
|
|
73
|
+
? _jsx(NumberField, { "aria-label": name, placeholder: valuePlaceholder(field, w), value: typeof r.value === "number" ? r.value : null, onValueChange: (v) => set({ value: v ?? null }), onKeyDown: enter })
|
|
74
|
+
: _jsx(Input, { "aria-label": name, placeholder: valuePlaceholder(field, w), value: typeof r.value === "string" ? r.value : "", onChange: (e) => set({ value: e.target.value }), onKeyDown: enter });
|
|
75
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { type FilterField } from "./how.js";
|
|
2
|
+
/** One rule: the field it reads (one level in, its path's value: company.region), how it compares, its value: a choice's value
|
|
3
|
+
* or several, a number, words, a day ("2026-12-31"), a range's From and To (either may be none); none while it has none, or for a
|
|
4
|
+
* way that takes none. */
|
|
5
|
+
export interface FilterRule {
|
|
6
|
+
id: string;
|
|
7
|
+
field: string;
|
|
8
|
+
how: string;
|
|
9
|
+
value: string | string[] | number | [number | null, number | null] | null;
|
|
10
|
+
/** Set aside by ViewKit: its field's name and why, in words, and whether Put back fits. Drawn set aside, whatever its field. */
|
|
11
|
+
aside?: FilterAside;
|
|
12
|
+
}
|
|
13
|
+
/** A rule ViewKit set aside, in words: its field's name, why, and whether it can be put back. */
|
|
14
|
+
export interface FilterAside {
|
|
15
|
+
label: string;
|
|
16
|
+
why: string;
|
|
17
|
+
putBack: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** The prefix of a way only ViewKit words: `vk:<operator>`. */
|
|
20
|
+
export declare const OWN_WAY = "vk:";
|
|
21
|
+
/** Whether a rule's way is one only ViewKit words: the kit has no word for it. */
|
|
22
|
+
export declare const isOwnWay: (how: string) => boolean;
|
|
23
|
+
/** A level of rules, joined by its one join; the filter itself is one. */
|
|
24
|
+
export interface FilterGroup {
|
|
25
|
+
id: string;
|
|
26
|
+
join: "and" | "or";
|
|
27
|
+
rules: (FilterRule | FilterGroup)[];
|
|
28
|
+
}
|
|
29
|
+
export type FilterItem = FilterRule | FilterGroup;
|
|
30
|
+
/** How many levels deep rules stand at most, the filter itself the first: a rule three deep can't become a group. */
|
|
31
|
+
export declare const FILTER_DEPTH = 3;
|
|
32
|
+
/** The filter with nothing in it. */
|
|
33
|
+
export declare const NO_FILTER: FilterGroup;
|
|
34
|
+
export declare const isGroup: (x: FilterItem) => x is FilterGroup;
|
|
35
|
+
/** Whether a value is a range's From and To: two ends, each a number or none. */
|
|
36
|
+
export declare const isPair: (v: FilterRule["value"]) => v is [number | null, number | null];
|
|
37
|
+
/** The field a rule reads, at the top or one level in; nothing once it is gone. */
|
|
38
|
+
export declare const fieldOf: (fields: FilterField[], field: string) => FilterField | undefined;
|
|
39
|
+
/** Every id in the filter, its own, each group's and each rule's. */
|
|
40
|
+
export declare const idsOf: (g: FilterGroup) => string[];
|
|
41
|
+
/** Every rule, in order, groups opened. */
|
|
42
|
+
export declare const rulesOf: (f: FilterGroup) => FilterRule[];
|
|
43
|
+
/** The rule or group by its id, wherever it stands. */
|
|
44
|
+
export declare function itemOf(f: FilterGroup, id: string): FilterItem | undefined;
|
|
45
|
+
/** A rule on `field`, with its type's first way and no value, added at the end of the group `into` (the filter's own unless said);
|
|
46
|
+
* its id with it, never one in the filter or in `taken`. A field it can't find, or a group that isn't there, adds nothing. */
|
|
47
|
+
export declare function addRule(f: FilterGroup, field: string, fields: FilterField[], into?: string, taken?: readonly string[]): {
|
|
48
|
+
filter: FilterGroup;
|
|
49
|
+
id: string;
|
|
50
|
+
};
|
|
51
|
+
/** The filter with the rule or group `id` taken away, and any group it leaves with no rules with it; the filter's own last rule
|
|
52
|
+
* leaves it empty. */
|
|
53
|
+
export declare function removeRule(f: FilterGroup, id: string): FilterGroup;
|
|
54
|
+
/** The group `groupId`'s join, which is every join at its level. */
|
|
55
|
+
export declare const setJoin: (f: FilterGroup, groupId: string, join: FilterGroup["join"]) => FilterGroup;
|
|
56
|
+
/** How many levels hold `id`: the filter itself 0, a rule or group in it 1, one in a group in it 2; -1 if it isn't there. */
|
|
57
|
+
export declare function depthOf(f: FilterGroup, id: string, at?: number): number;
|
|
58
|
+
/** Whether the rule `id` can become a group: a rule standing less than `levels` deep (three unless said; an advanced filter's group,
|
|
59
|
+
* itself a level of the view, two). */
|
|
60
|
+
export declare const canGroup: (f: FilterGroup, id: string, levels?: number) => boolean;
|
|
61
|
+
/** The rule `ruleId` turned into a group of its own, joined by And, holding it, its id never one in the filter or in `taken`; a rule
|
|
62
|
+
* `levels` deep, or a group, stays as it is. */
|
|
63
|
+
export declare function toGroup(f: FilterGroup, ruleId: string, taken?: readonly string[], levels?: number): FilterGroup;
|
|
64
|
+
/** A set-aside rule put back: its `aside` dropped, so it is written as a rule again; anything else stays. */
|
|
65
|
+
export declare function putBack(f: FilterGroup, id: string): FilterGroup;
|
|
66
|
+
/** The filter with nothing in it, its id and join kept. */
|
|
67
|
+
export declare const clearAll: (f: FilterGroup) => FilterGroup;
|
|
68
|
+
/** The rule `id` changed: a new field keeps its way where the new type has it (else the type's first) and starts with no value;
|
|
69
|
+
* a new way keeps the value it can (one of several, several of one, From of a range's two, two from one) and none for a way that
|
|
70
|
+
* takes none; a value as given. */
|
|
71
|
+
export declare function changeRule(f: FilterGroup, id: string, change: Partial<Pick<FilterRule, "field" | "how" | "value">>, fields: FilterField[]): FilterGroup;
|
|
72
|
+
/** What a rule's way takes, by its field's type; nothing for a rule set aside. */
|
|
73
|
+
export declare const takesOf: (rule: FilterRule, fields: FilterField[]) => import("./how.js").Takes | undefined;
|
|
74
|
+
/** Whether a rule narrows the list: its field there, its way its type's, and its value whole for what the way takes. */
|
|
75
|
+
export declare function isWhole(rule: FilterRule, fields: FilterField[]): boolean;
|
|
76
|
+
/** Whether the filter is advanced: an Or between its rules, or a group in it. */
|
|
77
|
+
export declare const isAdvanced: (f: FilterGroup) => boolean;
|
|
78
|
+
/** A filter as the filter bar shows it: a plain one, its rule, or an advanced one, its group and how many rules it holds. */
|
|
79
|
+
export type BarFilter = {
|
|
80
|
+
kind: "plain";
|
|
81
|
+
rule: FilterRule;
|
|
82
|
+
} | {
|
|
83
|
+
kind: "advanced";
|
|
84
|
+
group: FilterGroup;
|
|
85
|
+
count: number;
|
|
86
|
+
};
|
|
87
|
+
/** The filter as the bar reads it, in order: each rule at the filter's own level a plain filter, each group there an advanced one,
|
|
88
|
+
* counted by every rule in it. */
|
|
89
|
+
export declare const barOf: (f: FilterGroup) => BarFilter[];
|
|
90
|
+
/** The rule `ruleId` at the filter's own level turned advanced: a group of its own at its place, joined by And, holding it.
|
|
91
|
+
* Anything else stays as it is. */
|
|
92
|
+
export declare function toAdvanced(f: FilterGroup, ruleId: string): FilterGroup;
|
|
93
|
+
/** The group of `group`'s id at the filter's own level replaced by it, as the filter editor changed it; taken away once it holds
|
|
94
|
+
* no rules. */
|
|
95
|
+
export declare function setGroup(f: FilterGroup, group: FilterGroup): FilterGroup;
|
|
96
|
+
/** Whether a rule is set aside: ViewKit set it aside, or its field is no longer the list's (a rule on a way only ViewKit words, on
|
|
97
|
+
* a field the kit doesn't list, is ViewKit's to word, never set aside). */
|
|
98
|
+
export declare const isSetAside: (rule: FilterRule, fields: FilterField[]) => boolean;
|
|
99
|
+
/** A rule's value in words: a choice's name, several joined, a number with its thousands, a range's From – To (an end with none
|
|
100
|
+
* "…", as ViewKit says it), a day as a cell shows it, words as typed. */
|
|
101
|
+
export declare function valueText(rule: FilterRule, fields: FilterField[]): string;
|
|
102
|
+
/** A rule in words, its field named by its path: "Stage is any of Qualified, Proposal", "Company › Region is EMEA"; a rule set
|
|
103
|
+
* aside keeps what it has. */
|
|
104
|
+
export declare function ruleText(rule: FilterRule, fields: FilterField[]): string;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { cellText } from "../date-editor/calendar.js";
|
|
2
|
+
import { findItem, itemLabel } from "../picker/rows.js";
|
|
3
|
+
import { HOW, wayOf } from "./how.js";
|
|
4
|
+
export const OWN_WAY = "vk:";
|
|
5
|
+
export const isOwnWay = (how) => how.startsWith(OWN_WAY);
|
|
6
|
+
export const FILTER_DEPTH = 3;
|
|
7
|
+
export const NO_FILTER = { id: "root", join: "and", rules: [] };
|
|
8
|
+
export const isGroup = (x) => "rules" in x;
|
|
9
|
+
export const isPair = (v) => Array.isArray(v) && v.length === 2 && v.every((n) => n === null || typeof n === "number");
|
|
10
|
+
export const fieldOf = (fields, field) => findItem(fields, field)?.item;
|
|
11
|
+
export const idsOf = (g) => [g.id, ...g.rules.flatMap((r) => (isGroup(r) ? idsOf(r) : [r.id]))];
|
|
12
|
+
const nextId = (f, prefix, taken = []) => {
|
|
13
|
+
const all = new Set([...idsOf(f), ...taken]);
|
|
14
|
+
let n = 1;
|
|
15
|
+
while (all.has(`${prefix}${n}`))
|
|
16
|
+
n++;
|
|
17
|
+
return `${prefix}${n}`;
|
|
18
|
+
};
|
|
19
|
+
const inGroup = (g, id, fn) => g.id === id ? fn(g) : { ...g, rules: g.rules.map((r) => (isGroup(r) ? inGroup(r, id, fn) : r)) };
|
|
20
|
+
export const rulesOf = (f) => f.rules.flatMap((r) => (isGroup(r) ? rulesOf(r) : [r]));
|
|
21
|
+
export function itemOf(f, id) {
|
|
22
|
+
if (f.id === id)
|
|
23
|
+
return f;
|
|
24
|
+
for (const r of f.rules) {
|
|
25
|
+
if (r.id === id)
|
|
26
|
+
return r;
|
|
27
|
+
if (isGroup(r)) {
|
|
28
|
+
const x = itemOf(r, id);
|
|
29
|
+
if (x)
|
|
30
|
+
return x;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
export function addRule(f, field, fields, into = f.id, taken = []) {
|
|
36
|
+
const type = fieldOf(fields, field)?.type, target = itemOf(f, into);
|
|
37
|
+
if (!type || !target || !isGroup(target))
|
|
38
|
+
return { filter: f, id: "" };
|
|
39
|
+
const id = nextId(f, "r", taken), rule = { id, field, how: HOW[type][0].value, value: null };
|
|
40
|
+
return { filter: inGroup(f, into, (g) => ({ ...g, rules: [...g.rules, rule] })), id };
|
|
41
|
+
}
|
|
42
|
+
export function removeRule(f, id) {
|
|
43
|
+
if (f.id === id)
|
|
44
|
+
return clearAll(f);
|
|
45
|
+
const without = (g) => ({ ...g, rules: g.rules.filter((r) => r.id !== id).map((r) => (isGroup(r) ? without(r) : r)).filter((r) => !isGroup(r) || r.rules.length > 0) });
|
|
46
|
+
return without(f);
|
|
47
|
+
}
|
|
48
|
+
export const setJoin = (f, groupId, join) => inGroup(f, groupId, (g) => ({ ...g, join }));
|
|
49
|
+
export function depthOf(f, id, at = 0) {
|
|
50
|
+
if (f.id === id)
|
|
51
|
+
return at;
|
|
52
|
+
for (const r of f.rules) {
|
|
53
|
+
if (r.id === id)
|
|
54
|
+
return at + 1;
|
|
55
|
+
if (isGroup(r)) {
|
|
56
|
+
const d = depthOf(r, id, at + 1);
|
|
57
|
+
if (d >= 0)
|
|
58
|
+
return d;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return -1;
|
|
62
|
+
}
|
|
63
|
+
export const canGroup = (f, id, levels = FILTER_DEPTH) => { const x = itemOf(f, id), d = depthOf(f, id); return !!x && !isGroup(x) && d > 0 && d < levels; };
|
|
64
|
+
export function toGroup(f, ruleId, taken = [], levels = FILTER_DEPTH) {
|
|
65
|
+
if (!canGroup(f, ruleId, levels))
|
|
66
|
+
return f;
|
|
67
|
+
const id = nextId(f, "g", taken);
|
|
68
|
+
const wrap = (g) => ({ ...g, rules: g.rules.map((r) => (r.id === ruleId ? { id, join: "and", rules: [r] } : isGroup(r) ? wrap(r) : r)) });
|
|
69
|
+
return wrap(f);
|
|
70
|
+
}
|
|
71
|
+
export function putBack(f, id) {
|
|
72
|
+
const walk = (g) => ({ ...g, rules: g.rules.map((r) => (isGroup(r) ? walk(r) : r.id === id && r.aside ? (({ aside: _gone, ...rest }) => rest)(r) : r)) });
|
|
73
|
+
return walk(f);
|
|
74
|
+
}
|
|
75
|
+
export const clearAll = (f) => ({ ...f, rules: [] });
|
|
76
|
+
export function changeRule(f, id, change, fields) {
|
|
77
|
+
const one = (r) => {
|
|
78
|
+
if (change.field !== undefined && change.field !== r.field) {
|
|
79
|
+
const type = fieldOf(fields, change.field)?.type;
|
|
80
|
+
if (!type)
|
|
81
|
+
return r;
|
|
82
|
+
return { ...r, field: change.field, how: wayOf(type, r.how) ? r.how : HOW[type][0].value, value: null };
|
|
83
|
+
}
|
|
84
|
+
if (change.how !== undefined && change.how !== r.how) {
|
|
85
|
+
const type = fieldOf(fields, r.field)?.type, was = type && wayOf(type, r.how)?.takes, next = type && wayOf(type, change.how)?.takes;
|
|
86
|
+
if (!next)
|
|
87
|
+
return r;
|
|
88
|
+
const v = r.value;
|
|
89
|
+
const value = next === "none" ? null : next === was ? v
|
|
90
|
+
: next === "two" ? (isPair(v) ? v : typeof v === "number" ? [v, null] : null)
|
|
91
|
+
: isPair(v) ? v[0]
|
|
92
|
+
: next === "several" ? (typeof v === "string" ? [v] : Array.isArray(v) ? v : null)
|
|
93
|
+
: Array.isArray(v) ? v[0] ?? null : v;
|
|
94
|
+
return { ...r, how: change.how, value };
|
|
95
|
+
}
|
|
96
|
+
return "value" in change ? { ...r, value: change.value ?? null } : r;
|
|
97
|
+
};
|
|
98
|
+
const walk = (g) => ({ ...g, rules: g.rules.map((r) => (isGroup(r) ? walk(r) : r.id === id ? one(r) : r)) });
|
|
99
|
+
return walk(f);
|
|
100
|
+
}
|
|
101
|
+
export const takesOf = (rule, fields) => { const t = fieldOf(fields, rule.field)?.type; return t ? wayOf(t, rule.how)?.takes : undefined; };
|
|
102
|
+
export function isWhole(rule, fields) {
|
|
103
|
+
if (rule.aside || isOwnWay(rule.how))
|
|
104
|
+
return true;
|
|
105
|
+
const type = fieldOf(fields, rule.field)?.type, takes = type && wayOf(type, rule.how)?.takes, v = rule.value;
|
|
106
|
+
if (!type || !takes)
|
|
107
|
+
return false;
|
|
108
|
+
if (takes === "none")
|
|
109
|
+
return true;
|
|
110
|
+
if (takes === "several")
|
|
111
|
+
return Array.isArray(v) && v.length > 0;
|
|
112
|
+
if (takes === "two")
|
|
113
|
+
return isPair(v) && v.some((n) => typeof n === "number" && Number.isFinite(n));
|
|
114
|
+
if (type === "number" || type === "range")
|
|
115
|
+
return typeof v === "number" && Number.isFinite(v);
|
|
116
|
+
return typeof v === "string" && v.trim() !== "";
|
|
117
|
+
}
|
|
118
|
+
export const isAdvanced = (f) => f.rules.some(isGroup) || (f.join === "or" && f.rules.length > 1);
|
|
119
|
+
export const barOf = (f) => f.rules.map((r) => (isGroup(r) ? { kind: "advanced", group: r, count: rulesOf(r).length } : { kind: "plain", rule: r }));
|
|
120
|
+
export function toAdvanced(f, ruleId) {
|
|
121
|
+
if (!f.rules.some((r) => !isGroup(r) && r.id === ruleId))
|
|
122
|
+
return f;
|
|
123
|
+
const id = nextId(f, "g");
|
|
124
|
+
return { ...f, rules: f.rules.map((r) => (r.id === ruleId ? { id, join: "and", rules: [r] } : r)) };
|
|
125
|
+
}
|
|
126
|
+
export function setGroup(f, group) {
|
|
127
|
+
const empty = rulesOf(group).length === 0;
|
|
128
|
+
return { ...f, rules: f.rules.flatMap((r) => (isGroup(r) && r.id === group.id ? (empty ? [] : [group]) : [r])) };
|
|
129
|
+
}
|
|
130
|
+
export const isSetAside = (rule, fields) => !!rule.aside || (!fieldOf(fields, rule.field) && !isOwnWay(rule.how));
|
|
131
|
+
const NUMBER = new Intl.NumberFormat("en-GB");
|
|
132
|
+
export function valueText(rule, fields) {
|
|
133
|
+
const field = fieldOf(fields, rule.field), v = rule.value, name = (x) => field?.choices?.find((c) => c.value === x)?.label ?? x;
|
|
134
|
+
if (v == null || v === "")
|
|
135
|
+
return "";
|
|
136
|
+
if (isPair(v))
|
|
137
|
+
return v.map((n) => (n === null ? "…" : NUMBER.format(n))).join(" – ");
|
|
138
|
+
if (Array.isArray(v))
|
|
139
|
+
return v.map(name).join(", ");
|
|
140
|
+
if (typeof v === "number")
|
|
141
|
+
return NUMBER.format(v);
|
|
142
|
+
return field?.type === "date" ? cellText(v, v) : field?.choices ? name(v) : v;
|
|
143
|
+
}
|
|
144
|
+
export function ruleText(rule, fields) {
|
|
145
|
+
const type = fieldOf(fields, rule.field)?.type;
|
|
146
|
+
const how = (type && wayOf(type, rule.how)?.label) ?? rule.how.replace(/-/g, " ");
|
|
147
|
+
return [itemLabel(fields, rule.field), how, valueText(rule, fields)].filter(Boolean).join(" ");
|
|
148
|
+
}
|