@platforma-sdk/model 1.88.16 → 1.90.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/dist/columns/columns_collection.cjs +18 -0
- package/dist/columns/columns_collection.cjs.map +1 -1
- package/dist/columns/columns_collection.d.ts +21 -1
- package/dist/columns/columns_collection.d.ts.map +1 -1
- package/dist/columns/columns_collection.js +18 -0
- package/dist/columns/columns_collection.js.map +1 -1
- package/dist/columns/index.d.ts +2 -2
- package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV2.cjs +20 -5
- package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV2.cjs.map +1 -1
- package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV2.d.ts.map +1 -1
- package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV2.js +21 -6
- package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV2.js.map +1 -1
- package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.cjs +28 -23
- package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.cjs.map +1 -1
- package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.d.ts.map +1 -1
- package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.js +29 -23
- package/dist/components/PlDataTable/createPlDataTable/createPlDataTableV3.js.map +1 -1
- package/dist/components/PlDataTable/state-migration.cjs +25 -1
- package/dist/components/PlDataTable/state-migration.cjs.map +1 -1
- package/dist/components/PlDataTable/state-migration.d.ts +31 -6
- package/dist/components/PlDataTable/state-migration.d.ts.map +1 -1
- package/dist/components/PlDataTable/state-migration.js +25 -1
- package/dist/components/PlDataTable/state-migration.js.map +1 -1
- package/dist/components/PlDataTable/tableParams.cjs +156 -0
- package/dist/components/PlDataTable/tableParams.cjs.map +1 -0
- package/dist/components/PlDataTable/tableParams.js +152 -0
- package/dist/components/PlDataTable/tableParams.js.map +1 -0
- package/dist/components/PlDataTable/typesV8.d.ts +12 -9
- package/dist/components/PlDataTable/typesV8.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/package.json +7 -7
- package/src/columns/__test_helpers__/collection_driver.ts +3 -0
- package/src/columns/columns_collection.test.ts +47 -0
- package/src/columns/columns_collection.ts +33 -0
- package/src/components/PlDataTable/createPlDataTable/createPlDataTableV2.ts +35 -6
- package/src/components/PlDataTable/createPlDataTable/createPlDataTableV3.ts +38 -23
- package/src/components/PlDataTable/state-migration.test.ts +26 -26
- package/src/components/PlDataTable/state-migration.ts +75 -11
- package/src/components/PlDataTable/tableParams.test.ts +291 -0
- package/src/components/PlDataTable/tableParams.ts +243 -0
- package/src/components/PlDataTable/typesV8.ts +18 -17
- package/src/components/PlDataTable/createPlDataTable/createPlDataTableV3.test.ts +0 -61
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AxisSpec,
|
|
3
|
+
ColumnUniversalId,
|
|
4
|
+
PColumnSpec,
|
|
5
|
+
PTableColumnId,
|
|
6
|
+
} from "@milaboratories/pl-model-common";
|
|
7
|
+
import { canonicalizeJson, getAxisId } from "@milaboratories/pl-model-common";
|
|
8
|
+
import { describe, expect, test } from "vitest";
|
|
9
|
+
import { upgradePlDataTableStateV2, type PlDataTableStateV2 } from "./state-migration";
|
|
10
|
+
import {
|
|
11
|
+
collectSearchColumns,
|
|
12
|
+
createSearchFilter,
|
|
13
|
+
createSheetFilters,
|
|
14
|
+
joinFilters,
|
|
15
|
+
readPlDataTableUserParams,
|
|
16
|
+
type PlDataTableFilterNode,
|
|
17
|
+
type PlDataTableSearchCandidate,
|
|
18
|
+
} from "./tableParams";
|
|
19
|
+
import type {
|
|
20
|
+
PlDataTableFilterSpecLeaf,
|
|
21
|
+
PlDataTableFiltersWithMeta,
|
|
22
|
+
PlDataTableStateV2CacheEntry,
|
|
23
|
+
PlDataTableStateV2Normalized,
|
|
24
|
+
} from "./typesV8";
|
|
25
|
+
|
|
26
|
+
describe("readPlDataTableUserParams", () => {
|
|
27
|
+
test("reads the entry the stored state points at, not the most recent one", () => {
|
|
28
|
+
const params = readPlDataTableUserParams(
|
|
29
|
+
state("A", [entry("A", { searchString: " needle " }), entry("B", { searchString: "other" })]),
|
|
30
|
+
);
|
|
31
|
+
expect(params.sourceId).toBe("A");
|
|
32
|
+
expect(params.searchString).toBe("needle");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("reports an untouched state when the pointed-at source has no entry", () => {
|
|
36
|
+
expect(readPlDataTableUserParams(state("B", [entry("A", { searchString: "x" })]))).toEqual({
|
|
37
|
+
sourceId: "B",
|
|
38
|
+
sheetsState: [],
|
|
39
|
+
userFilters: null,
|
|
40
|
+
defaultFilters: null,
|
|
41
|
+
searchString: null,
|
|
42
|
+
sorting: null,
|
|
43
|
+
hiddenColIds: null,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("drops suppressed filters and keeps an all-suppressed default group as a group", () => {
|
|
48
|
+
const params = readPlDataTableUserParams(
|
|
49
|
+
state("A", [
|
|
50
|
+
entry("A", {
|
|
51
|
+
filtersState: group([leafMeta("kept"), leafMeta("dropped", { isSuppressed: true })]),
|
|
52
|
+
defaultFiltersState: group([leafMeta("d", { isSuppressed: true })]),
|
|
53
|
+
}),
|
|
54
|
+
]),
|
|
55
|
+
);
|
|
56
|
+
expect(params.userFilters).toEqual({
|
|
57
|
+
type: "and",
|
|
58
|
+
filters: [{ type: "and", filters: [isNA("kept")] }],
|
|
59
|
+
});
|
|
60
|
+
// An empty group, not null: null would bring the block's defaults back.
|
|
61
|
+
expect(params.defaultFilters).toEqual({ type: "and", filters: [] });
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("reads sorting and hidden columns from the entry, never from the grid state", () => {
|
|
65
|
+
const column: PTableColumnId = { type: "column", id: col("c1") };
|
|
66
|
+
const sorting = [{ column, ascending: false, naAndAbsentAreLeastValues: false }];
|
|
67
|
+
const params = readPlDataTableUserParams(
|
|
68
|
+
state("A", [
|
|
69
|
+
entry("A", {
|
|
70
|
+
gridState: { sort: { sortModel: [{ colId: canonicalizeJson(column), sort: "asc" }] } },
|
|
71
|
+
sorting,
|
|
72
|
+
hiddenColIds: [column],
|
|
73
|
+
}),
|
|
74
|
+
]),
|
|
75
|
+
);
|
|
76
|
+
expect(params.sorting).toEqual(sorting);
|
|
77
|
+
expect(params.hiddenColIds).toEqual([column]);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("upgradePlDataTableStateV2", () => {
|
|
82
|
+
const column: PTableColumnId = { type: "column", id: col("c1") };
|
|
83
|
+
const sorting = [{ column, ascending: true, naAndAbsentAreLeastValues: true }];
|
|
84
|
+
|
|
85
|
+
test("moves the sorting and hidden columns earlier versions kept in pTableParams into the shown entry", () => {
|
|
86
|
+
const { sorting: _a, hiddenColIds: _b, ...legacyA } = entry("A");
|
|
87
|
+
const { sorting: _c, hiddenColIds: _d, ...legacyB } = entry("B");
|
|
88
|
+
const upgraded = upgradePlDataTableStateV2(
|
|
89
|
+
stored({
|
|
90
|
+
version: 9,
|
|
91
|
+
stateCache: [legacyA, legacyB],
|
|
92
|
+
pTableParams: { ...nullParams("A"), sorting, hiddenColIds: [column] },
|
|
93
|
+
}),
|
|
94
|
+
);
|
|
95
|
+
expect(upgraded.pTableParams).toEqual(nullParams("A"));
|
|
96
|
+
expect(upgraded.stateCache).toEqual([
|
|
97
|
+
entry("A", { sorting, hiddenColIds: [column] }),
|
|
98
|
+
entry("B"),
|
|
99
|
+
]);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("keeps an entry's own untouched sorting rather than the legacy one", () => {
|
|
103
|
+
const upgraded = upgradePlDataTableStateV2(
|
|
104
|
+
stored({
|
|
105
|
+
version: 9,
|
|
106
|
+
stateCache: [entry("A")],
|
|
107
|
+
pTableParams: { ...nullParams("A"), sorting },
|
|
108
|
+
}),
|
|
109
|
+
);
|
|
110
|
+
expect(upgraded.stateCache[0].sorting).toBeNull();
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe("createSheetFilters", () => {
|
|
115
|
+
test("selects the picked value on each sheet axis", () => {
|
|
116
|
+
const sampleAxis = getAxisId(axis("sample"));
|
|
117
|
+
const roundAxis = getAxisId(axis("round", "Int"));
|
|
118
|
+
expect(
|
|
119
|
+
createSheetFilters([
|
|
120
|
+
{ axisId: sampleAxis, value: "s1" },
|
|
121
|
+
{ axisId: roundAxis, value: 3 },
|
|
122
|
+
]),
|
|
123
|
+
).toEqual([
|
|
124
|
+
{ type: "patternEquals", column: { type: "axis", id: sampleAxis }, value: "s1" },
|
|
125
|
+
{ type: "equal", column: { type: "axis", id: roundAxis }, x: 3 },
|
|
126
|
+
]);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("collectSearchColumns", () => {
|
|
131
|
+
const sample = axis("sample");
|
|
132
|
+
const clone = axis("clone");
|
|
133
|
+
const columns: PlDataTableSearchCandidate[] = [
|
|
134
|
+
candidate("label", { name: "pl7.app/label", axesSpec: [clone], valueType: "String" }),
|
|
135
|
+
candidate("count", { axesSpec: [sample, clone], valueType: "Int" }),
|
|
136
|
+
candidate("extra", { axesSpec: [sample, clone], valueType: "String" }, "optional"),
|
|
137
|
+
candidate("secret", { axesSpec: [sample, clone], valueType: "String" }, "hidden"),
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
test("searches visible columns, skipping sheet axes and optional columns", () => {
|
|
141
|
+
const found = collectSearchColumns({
|
|
142
|
+
columns,
|
|
143
|
+
isAxisHidden: () => false,
|
|
144
|
+
sheetAxes: [getAxisId(sample)],
|
|
145
|
+
hiddenColIds: null,
|
|
146
|
+
}).map((c) => c.id);
|
|
147
|
+
expect(found).toEqual([
|
|
148
|
+
{ type: "column", id: col("label") },
|
|
149
|
+
{ type: "column", id: col("count") },
|
|
150
|
+
]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("searches the label column instead of the axis it names", () => {
|
|
154
|
+
const found = collectSearchColumns({
|
|
155
|
+
columns,
|
|
156
|
+
isAxisHidden: () => false,
|
|
157
|
+
sheetAxes: [],
|
|
158
|
+
hiddenColIds: null,
|
|
159
|
+
}).map((c) => c.id);
|
|
160
|
+
expect(found).toContainEqual({ type: "axis", id: getAxisId(sample) });
|
|
161
|
+
expect(found).toContainEqual({ type: "column", id: col("label") });
|
|
162
|
+
expect(found).not.toContainEqual({ type: "axis", id: getAxisId(clone) });
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("follows the user's hidden list instead of the optional defaults", () => {
|
|
166
|
+
const found = collectSearchColumns({
|
|
167
|
+
columns,
|
|
168
|
+
isAxisHidden: () => false,
|
|
169
|
+
sheetAxes: [],
|
|
170
|
+
hiddenColIds: [{ type: "column", id: col("count") }],
|
|
171
|
+
}).map((c) => c.id);
|
|
172
|
+
expect(found).toEqual([
|
|
173
|
+
{ type: "axis", id: getAxisId(sample) },
|
|
174
|
+
{ type: "column", id: col("label") },
|
|
175
|
+
{ type: "column", id: col("extra") },
|
|
176
|
+
]);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe("createSearchFilter", () => {
|
|
181
|
+
const text = { id: { type: "column", id: col("t") }, valueType: "String" } as const;
|
|
182
|
+
const number = { id: { type: "column", id: col("n") }, valueType: "Int" } as const;
|
|
183
|
+
|
|
184
|
+
test("matches text exactly in text columns, and numbers in numeric ones", () => {
|
|
185
|
+
expect(createSearchFilter("42", [text, number])).toEqual({
|
|
186
|
+
type: "or",
|
|
187
|
+
filters: [
|
|
188
|
+
{ type: "patternEquals", column: text.id, value: "42" },
|
|
189
|
+
{ type: "equal", column: number.id, x: 42 },
|
|
190
|
+
],
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("skips numeric columns for text that is not a number", () => {
|
|
195
|
+
expect(createSearchFilter("abc", [number])).toBeNull();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("is no filter without search text", () => {
|
|
199
|
+
expect(createSearchFilter(null, [text])).toBeNull();
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe("joinFilters", () => {
|
|
204
|
+
test("is no filter when nothing is given", () => {
|
|
205
|
+
expect(joinFilters([null, undefined])).toBeNull();
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("merges AND groups and keeps other nodes as single operands", () => {
|
|
209
|
+
const or: PlDataTableFilterNode = { type: "or", filters: [isNA("o1"), isNA("o2")] };
|
|
210
|
+
const not: PlDataTableFilterNode = { type: "not", filter: isNA("n") };
|
|
211
|
+
expect(
|
|
212
|
+
joinFilters([{ type: "and", filters: [isNA("a1"), isNA("a2")] }, isNA("leaf"), or, not]),
|
|
213
|
+
).toEqual({ type: "and", filters: [isNA("a1"), isNA("a2"), isNA("leaf"), or, not] });
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("an empty AND group contributes nothing", () => {
|
|
217
|
+
expect(joinFilters([{ type: "and", filters: [] }, isNA("x")])).toEqual({
|
|
218
|
+
type: "and",
|
|
219
|
+
filters: [isNA("x")],
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// Fixtures
|
|
225
|
+
|
|
226
|
+
function col(id: string): ColumnUniversalId {
|
|
227
|
+
return id as ColumnUniversalId;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function axis(name: string, type: AxisSpec["type"] = "String"): AxisSpec {
|
|
231
|
+
return { name, type };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function candidate(
|
|
235
|
+
id: string,
|
|
236
|
+
spec: Pick<PColumnSpec, "axesSpec" | "valueType"> & { name?: string },
|
|
237
|
+
visibility: PlDataTableSearchCandidate["visibility"] = "default",
|
|
238
|
+
): PlDataTableSearchCandidate {
|
|
239
|
+
return {
|
|
240
|
+
id: col(id),
|
|
241
|
+
spec: { kind: "PColumn", name: spec.name ?? id, ...spec },
|
|
242
|
+
visibility,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function isNA(id: string): PlDataTableFilterSpecLeaf {
|
|
247
|
+
return { type: "isNA", column: { type: "column", id: col(id) } };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function leafMeta(id: string, meta: { isSuppressed?: boolean } = {}) {
|
|
251
|
+
return { ...isNA(id), id: id.length, ...meta };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function group(
|
|
255
|
+
filters: ReturnType<typeof leafMeta>[],
|
|
256
|
+
): NonNullable<PlDataTableStateV2CacheEntry["filtersState"]> {
|
|
257
|
+
const inner: PlDataTableFiltersWithMeta = { id: 1, type: "and", filters };
|
|
258
|
+
return { id: 0, type: "and", filters: [inner] };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function nullParams(sourceId: null | string): PlDataTableStateV2Normalized["pTableParams"] {
|
|
262
|
+
return { sourceId, hiddenColIds: null, sorting: null, filters: null, defaultFilters: null };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function entry(
|
|
266
|
+
sourceId: string,
|
|
267
|
+
fields: Partial<Omit<PlDataTableStateV2CacheEntry, "sourceId">> = {},
|
|
268
|
+
): PlDataTableStateV2CacheEntry {
|
|
269
|
+
return {
|
|
270
|
+
sourceId,
|
|
271
|
+
gridState: {},
|
|
272
|
+
sorting: null,
|
|
273
|
+
hiddenColIds: null,
|
|
274
|
+
sheetsState: [],
|
|
275
|
+
filtersState: null,
|
|
276
|
+
defaultFiltersState: null,
|
|
277
|
+
...fields,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function state(
|
|
282
|
+
sourceId: null | string,
|
|
283
|
+
stateCache: PlDataTableStateV2CacheEntry[],
|
|
284
|
+
): PlDataTableStateV2Normalized {
|
|
285
|
+
return { version: 9, stateCache, pTableParams: nullParams(sourceId) };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** A state as it comes out of storage, in a shape the current types may no longer describe. */
|
|
289
|
+
function stored(value: unknown): PlDataTableStateV2 {
|
|
290
|
+
return JSON.parse(JSON.stringify(value));
|
|
291
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AxisId,
|
|
3
|
+
AxisSpec,
|
|
4
|
+
CanonicalizedJson,
|
|
5
|
+
ColumnUniversalId,
|
|
6
|
+
FilterSpecNode,
|
|
7
|
+
PColumnSpec,
|
|
8
|
+
PTableColumnId,
|
|
9
|
+
PTableSorting,
|
|
10
|
+
} from "@milaboratories/pl-model-common";
|
|
11
|
+
import {
|
|
12
|
+
canonicalizeJson,
|
|
13
|
+
getAxisId,
|
|
14
|
+
isLabelColumn,
|
|
15
|
+
isLinkerColumn,
|
|
16
|
+
matchAxisId,
|
|
17
|
+
ValueType,
|
|
18
|
+
} from "@milaboratories/pl-model-common";
|
|
19
|
+
import { isNil } from "es-toolkit";
|
|
20
|
+
import { distillFilterSpec } from "../../filters";
|
|
21
|
+
import type {
|
|
22
|
+
PlDataTableFilters,
|
|
23
|
+
PlDataTableFiltersWithMeta,
|
|
24
|
+
PlDataTableFilterSpecLeaf,
|
|
25
|
+
PlDataTableSheetState,
|
|
26
|
+
PlDataTableStateV2CacheEntry,
|
|
27
|
+
PlDataTableStateV2Normalized,
|
|
28
|
+
} from "./typesV8";
|
|
29
|
+
|
|
30
|
+
/** What the user set up for the table of the source the UI shows. */
|
|
31
|
+
export type PlDataTableUserParams = {
|
|
32
|
+
/** Source the UI shows; `null` until the UI has shown one. */
|
|
33
|
+
sourceId: null | string;
|
|
34
|
+
/** The value picked on each sheet. */
|
|
35
|
+
sheetsState: PlDataTableSheetState[];
|
|
36
|
+
/** The user's own filters without the suppressed ones; `null` when there are none. */
|
|
37
|
+
userFilters: null | PlDataTableFilters;
|
|
38
|
+
/**
|
|
39
|
+
* The user's edit of the default filters: `null` while untouched, so the block's
|
|
40
|
+
* defaults apply, and an empty group once every default is suppressed.
|
|
41
|
+
*/
|
|
42
|
+
defaultFilters: null | PlDataTableFilters;
|
|
43
|
+
/** Fast-search text, trimmed; `null` when empty. */
|
|
44
|
+
searchString: null | string;
|
|
45
|
+
/** `null` while untouched, so the block's default sorting applies; `[]` once cleared. */
|
|
46
|
+
sorting: null | PTableSorting[];
|
|
47
|
+
/** Columns the user hid; `null` while untouched, so optional columns stay hidden. */
|
|
48
|
+
hiddenColIds: null | PTableColumnId[];
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** A column the fast search looks into. */
|
|
52
|
+
export type PlDataTableSearchColumn = {
|
|
53
|
+
id: PTableColumnId;
|
|
54
|
+
valueType: ValueType;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** A column of the table as the fast search sees it. */
|
|
58
|
+
export type PlDataTableSearchCandidate = {
|
|
59
|
+
id: ColumnUniversalId;
|
|
60
|
+
spec: PColumnSpec;
|
|
61
|
+
visibility: "default" | "optional" | "hidden";
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** Read the user's settings for the source the stored state points at. */
|
|
65
|
+
export function readPlDataTableUserParams(
|
|
66
|
+
state: PlDataTableStateV2Normalized,
|
|
67
|
+
): PlDataTableUserParams {
|
|
68
|
+
const sourceId = state.pTableParams.sourceId;
|
|
69
|
+
const entry = findEntry(state, sourceId);
|
|
70
|
+
if (isNil(entry)) {
|
|
71
|
+
return {
|
|
72
|
+
sourceId,
|
|
73
|
+
sheetsState: [],
|
|
74
|
+
userFilters: null,
|
|
75
|
+
defaultFilters: null,
|
|
76
|
+
searchString: null,
|
|
77
|
+
sorting: null,
|
|
78
|
+
hiddenColIds: null,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
sourceId,
|
|
84
|
+
sheetsState: entry.sheetsState,
|
|
85
|
+
userFilters: isNil(entry.filtersState)
|
|
86
|
+
? null
|
|
87
|
+
: distillFilterSpec(stripSuppressedFilters(entry.filtersState)),
|
|
88
|
+
defaultFilters: isNil(entry.defaultFiltersState)
|
|
89
|
+
? null
|
|
90
|
+
: (distillFilterSpec(stripSuppressedFilters(entry.defaultFiltersState)) ?? emptyGroup()),
|
|
91
|
+
searchString: entry.searchString?.trim() || null,
|
|
92
|
+
sorting: entry.sorting,
|
|
93
|
+
hiddenColIds: entry.hiddenColIds,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** One filter per sheet, selecting the value picked on it. */
|
|
98
|
+
export function createSheetFilters(sheetsState: PlDataTableSheetState[]): PlDataTableFilterNode[] {
|
|
99
|
+
return sheetsState.map((sheet) => {
|
|
100
|
+
const column: PTableColumnId = { type: "axis", id: sheet.axisId };
|
|
101
|
+
return typeof sheet.value === "number"
|
|
102
|
+
? { type: "equal", column, x: sheet.value }
|
|
103
|
+
: { type: "patternEquals", column, value: sheet.value };
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The columns the fast search looks into: the visible ones, and the visible axes
|
|
109
|
+
* except those a sheet fixes to a single value, where every row would match, and
|
|
110
|
+
* those the table shows through their label column, whose raw values are never
|
|
111
|
+
* on screen. Hidden are columns the block's rules hide, linker columns, and the
|
|
112
|
+
* columns in the user's hidden list, or, while the user has not touched it, the
|
|
113
|
+
* optional ones.
|
|
114
|
+
*/
|
|
115
|
+
export function collectSearchColumns(params: {
|
|
116
|
+
columns: PlDataTableSearchCandidate[];
|
|
117
|
+
isAxisHidden: (axis: AxisSpec) => boolean;
|
|
118
|
+
sheetAxes: AxisId[];
|
|
119
|
+
hiddenColIds: null | PTableColumnId[];
|
|
120
|
+
}): PlDataTableSearchColumn[] {
|
|
121
|
+
const { columns, isAxisHidden, sheetAxes, hiddenColIds } = params;
|
|
122
|
+
const sheetAxisKeys = new Set(sheetAxes.map(axisKey));
|
|
123
|
+
const labeledAxes = columns
|
|
124
|
+
.filter((c) => isLabelColumn(c.spec))
|
|
125
|
+
.flatMap((c) => c.spec.axesSpec.map(getAxisId));
|
|
126
|
+
const shownAxes = uniqueAxes(columns.flatMap((c) => c.spec.axesSpec)).filter((axis) => {
|
|
127
|
+
const id = getAxisId(axis);
|
|
128
|
+
return (
|
|
129
|
+
!sheetAxisKeys.has(axisKey(id)) &&
|
|
130
|
+
!isAxisHidden(axis) &&
|
|
131
|
+
!labeledAxes.some((labeled) => matchAxisId(labeled, id))
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
const shownColumns = columns.filter((c) => c.visibility !== "hidden" && !isLinkerColumn(c.spec));
|
|
135
|
+
|
|
136
|
+
const hidden = new Set(
|
|
137
|
+
(
|
|
138
|
+
hiddenColIds ??
|
|
139
|
+
shownColumns
|
|
140
|
+
.filter((c) => c.visibility === "optional")
|
|
141
|
+
.map((c): PTableColumnId => ({ type: "column", id: c.id }))
|
|
142
|
+
).map((id) => canonicalizeJson<PTableColumnId>(id)),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
return [
|
|
146
|
+
...shownAxes.map((axis): PlDataTableSearchColumn => ({
|
|
147
|
+
id: { type: "axis", id: getAxisId(axis) },
|
|
148
|
+
valueType: axis.type,
|
|
149
|
+
})),
|
|
150
|
+
...shownColumns.map((c): PlDataTableSearchColumn => ({
|
|
151
|
+
id: { type: "column", id: c.id },
|
|
152
|
+
valueType: c.spec.valueType,
|
|
153
|
+
})),
|
|
154
|
+
].filter((c) => !hidden.has(canonicalizeJson<PTableColumnId>(c.id)));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The fast search as a filter: a row matches when any searched column holds the
|
|
159
|
+
* text exactly, or, for a number, holds that number.
|
|
160
|
+
*/
|
|
161
|
+
export function createSearchFilter(
|
|
162
|
+
searchString: null | string,
|
|
163
|
+
columns: PlDataTableSearchColumn[],
|
|
164
|
+
): null | PlDataTableFilters {
|
|
165
|
+
if (isNil(searchString)) return null;
|
|
166
|
+
const number = Number(searchString);
|
|
167
|
+
const isNumber = searchString.length > 0 && Number.isFinite(number);
|
|
168
|
+
|
|
169
|
+
const parts = columns.flatMap((c): PlDataTableFilterNode[] => {
|
|
170
|
+
if (c.valueType === ValueType.String) {
|
|
171
|
+
return [{ type: "patternEquals", column: c.id, value: searchString }];
|
|
172
|
+
}
|
|
173
|
+
if (isNumericValueType(c.valueType) && isNumber) {
|
|
174
|
+
return [{ type: "equal", column: c.id, x: number }];
|
|
175
|
+
}
|
|
176
|
+
return [];
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return parts.length === 0 ? null : { type: "or", filters: parts };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** AND of the given filter trees, `null` when none is left. */
|
|
183
|
+
export function joinFilters(
|
|
184
|
+
filters: (null | undefined | PlDataTableFilterNode)[],
|
|
185
|
+
): null | PlDataTableFilters {
|
|
186
|
+
const operands = filters.flatMap((f): PlDataTableFilterNode[] => {
|
|
187
|
+
if (isNil(f)) return [];
|
|
188
|
+
return f.type === "and" ? f.filters : [f];
|
|
189
|
+
});
|
|
190
|
+
return operands.length === 0 ? null : { type: "and", filters: operands };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export type PlDataTableFilterNode = FilterSpecNode<PlDataTableFilterSpecLeaf>;
|
|
194
|
+
|
|
195
|
+
// Internals
|
|
196
|
+
|
|
197
|
+
function findEntry(
|
|
198
|
+
state: PlDataTableStateV2Normalized,
|
|
199
|
+
sourceId: null | string,
|
|
200
|
+
): undefined | PlDataTableStateV2CacheEntry {
|
|
201
|
+
if (isNil(sourceId)) return undefined;
|
|
202
|
+
return state.stateCache.find((entry) => entry.sourceId === sourceId);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function stripSuppressedFilters(node: PlDataTableFiltersWithMeta): PlDataTableFiltersWithMeta {
|
|
206
|
+
return {
|
|
207
|
+
...node,
|
|
208
|
+
filters: node.filters
|
|
209
|
+
.filter((child) => !("isSuppressed" in child && child.isSuppressed === true))
|
|
210
|
+
.map((child) =>
|
|
211
|
+
(child.type === "and" || child.type === "or") && "filters" in child
|
|
212
|
+
? stripSuppressedFilters(child)
|
|
213
|
+
: child,
|
|
214
|
+
),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function emptyGroup(): PlDataTableFilters {
|
|
219
|
+
return { type: "and", filters: [] };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function isNumericValueType(valueType: ValueType): boolean {
|
|
223
|
+
return (
|
|
224
|
+
valueType === ValueType.Int ||
|
|
225
|
+
valueType === ValueType.Long ||
|
|
226
|
+
valueType === ValueType.Float ||
|
|
227
|
+
valueType === ValueType.Double
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function axisKey(axisId: AxisId): CanonicalizedJson<AxisId> {
|
|
232
|
+
return canonicalizeJson<AxisId>(axisId);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function uniqueAxes(axes: AxisSpec[]): AxisSpec[] {
|
|
236
|
+
const seen = new Set<CanonicalizedJson<AxisId>>();
|
|
237
|
+
return axes.filter((axis) => {
|
|
238
|
+
const key = axisKey(getAxisId(axis));
|
|
239
|
+
if (seen.has(key)) return false;
|
|
240
|
+
seen.add(key);
|
|
241
|
+
return true;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
@@ -73,8 +73,12 @@ export type PlDataTableFiltersWithMeta = RootFilterSpec<
|
|
|
73
73
|
export type PlDataTableStateV2CacheEntry = {
|
|
74
74
|
/** DataSource identifier for state management */
|
|
75
75
|
sourceId: string;
|
|
76
|
-
/**
|
|
76
|
+
/** The grid's own state, kept to restore the grid; opaque to everything but the grid */
|
|
77
77
|
gridState: PlDataTableGridStateCore;
|
|
78
|
+
/** Sorting the user set; `null` while untouched, so the block's default applies, `[]` once cleared */
|
|
79
|
+
sorting: null | PTableSorting[];
|
|
80
|
+
/** Columns the user hid; `null` while untouched, so optional columns stay hidden */
|
|
81
|
+
hiddenColIds: null | PTableColumnId[];
|
|
78
82
|
/** Sheets state */
|
|
79
83
|
sheetsState: PlDataTableSheetState[];
|
|
80
84
|
/** User filters state (tree-based, compatible with PlAdvancedFilter) */
|
|
@@ -85,28 +89,25 @@ export type PlDataTableStateV2CacheEntry = {
|
|
|
85
89
|
searchString?: string;
|
|
86
90
|
};
|
|
87
91
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
filters: null | PlDataTableFilters;
|
|
101
|
-
defaultFilters: null | PlDataTableFilters;
|
|
102
|
-
};
|
|
92
|
+
/**
|
|
93
|
+
* Which source the UI shows. The model builds the table from that source's cache
|
|
94
|
+
* entry; the remaining fields stay `null`, so a block version built against an
|
|
95
|
+
* earlier SDK reads the state as untouched.
|
|
96
|
+
*/
|
|
97
|
+
export type PTableParamsV2 = {
|
|
98
|
+
sourceId: null | string;
|
|
99
|
+
hiddenColIds: null;
|
|
100
|
+
sorting: null;
|
|
101
|
+
filters: null;
|
|
102
|
+
defaultFilters: null;
|
|
103
|
+
};
|
|
103
104
|
|
|
104
105
|
export type PlDataTableStateV2Normalized = {
|
|
105
106
|
/** Version for upgrades. v9 has the v8 shape; it marks column ids rewritten to the current form. */
|
|
106
107
|
version: 9;
|
|
107
108
|
/** Internal states, LRU cache for 5 sourceId-s */
|
|
108
109
|
stateCache: PlDataTableStateV2CacheEntry[];
|
|
109
|
-
/**
|
|
110
|
+
/** Which source the UI shows */
|
|
110
111
|
pTableParams: PTableParamsV2;
|
|
111
112
|
};
|
|
112
113
|
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import { concatFilters } from "./createPlDataTableV3";
|
|
3
|
-
import type { PlDataTableFilters, PlDataTableFilterSpecLeaf } from "../typesV8";
|
|
4
|
-
|
|
5
|
-
// concatFilters only inspects each operand's `.type` (and spreads `.filters` of
|
|
6
|
-
// an "and" node), so the leaf payload is irrelevant — minimal shapes suffice.
|
|
7
|
-
type Node = PlDataTableFilters | PlDataTableFilterSpecLeaf;
|
|
8
|
-
|
|
9
|
-
const leaf = (id: string): PlDataTableFilterSpecLeaf =>
|
|
10
|
-
({ type: "isNA", column: { type: "column", id } }) as unknown as PlDataTableFilterSpecLeaf;
|
|
11
|
-
const and = (...filters: Node[]): PlDataTableFilters =>
|
|
12
|
-
({ type: "and", filters }) as unknown as PlDataTableFilters;
|
|
13
|
-
const or = (...filters: Node[]): PlDataTableFilters =>
|
|
14
|
-
({ type: "or", filters }) as unknown as PlDataTableFilters;
|
|
15
|
-
const not = (filter: Node): PlDataTableFilters =>
|
|
16
|
-
({ type: "not", filter }) as unknown as PlDataTableFilters;
|
|
17
|
-
|
|
18
|
-
describe("concatFilters", () => {
|
|
19
|
-
test("a nil operand returns the other side unchanged", () => {
|
|
20
|
-
const a = and(leaf("x"));
|
|
21
|
-
expect(concatFilters(null, a)).toBe(a);
|
|
22
|
-
expect(concatFilters(a, null)).toBe(a);
|
|
23
|
-
expect(concatFilters(null, null)).toBeNull();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test("two AND groups merge their children", () => {
|
|
27
|
-
expect(concatFilters(and(leaf("u1"), leaf("u2")), and(leaf("d1")))).toEqual(
|
|
28
|
-
and(leaf("u1"), leaf("u2"), leaf("d1")),
|
|
29
|
-
);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("a bare leaf is combined as a single AND operand (no crash)", () => {
|
|
33
|
-
// Regression: a lone sheet-selection leaf used to throw on `[...a.filters]`.
|
|
34
|
-
expect(concatFilters(leaf("sheet"), and(leaf("d1"), leaf("d2")))).toEqual(
|
|
35
|
-
and(leaf("sheet"), leaf("d1"), leaf("d2")),
|
|
36
|
-
);
|
|
37
|
-
// ...and on either side.
|
|
38
|
-
expect(concatFilters(and(leaf("u1")), leaf("d"))).toEqual(and(leaf("u1"), leaf("d")));
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("an OR group is preserved as one operand, not flattened into AND", () => {
|
|
42
|
-
// Regression: old code kept type "or" (or spread the OR's children into the
|
|
43
|
-
// AND), turning "d1 OR d2" into "d1 AND d2".
|
|
44
|
-
expect(concatFilters(or(leaf("u1"), leaf("u2")), and(leaf("d1")))).toEqual(
|
|
45
|
-
and(or(leaf("u1"), leaf("u2")), leaf("d1")),
|
|
46
|
-
);
|
|
47
|
-
expect(concatFilters(and(leaf("u1")), or(leaf("d1"), leaf("d2")))).toEqual(
|
|
48
|
-
and(leaf("u1"), or(leaf("d1"), leaf("d2"))),
|
|
49
|
-
);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test("an empty AND group contributes nothing", () => {
|
|
53
|
-
expect(concatFilters(and(), and(leaf("d1")))).toEqual(and(leaf("d1")));
|
|
54
|
-
expect(concatFilters(and(leaf("u1")), and())).toEqual(and(leaf("u1")));
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test("a NOT node is combined as a single operand (no crash)", () => {
|
|
58
|
-
const n = not(leaf("x"));
|
|
59
|
-
expect(concatFilters(n, and(leaf("d1")))).toEqual(and(n, leaf("d1")));
|
|
60
|
-
});
|
|
61
|
-
});
|