@platforma-sdk/ui-vue 1.88.15 → 1.89.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.
Files changed (40) hide show
  1. package/.turbo/turbo-build.log +14 -14
  2. package/.turbo/turbo-formatter$colon$check.log +1 -1
  3. package/.turbo/turbo-linter$colon$check.log +1 -1
  4. package/.turbo/turbo-types$colon$check.log +1 -1
  5. package/CHANGELOG.md +26 -0
  6. package/dist/components/PlAdvancedFilter/utils.js +5 -13
  7. package/dist/components/PlAdvancedFilter/utils.js.map +1 -1
  8. package/dist/components/PlAgDataTable/PlAgDataTableSheets.js.map +1 -1
  9. package/dist/components/PlAgDataTable/PlAgDataTableSheets.style.js.map +1 -1
  10. package/dist/components/PlAgDataTable/PlAgDataTableSheets.vue.d.ts +7 -12
  11. package/dist/components/PlAgDataTable/PlAgDataTableSheets.vue.d.ts.map +1 -1
  12. package/dist/components/PlAgDataTable/PlAgDataTableSheets.vue2.js +28 -47
  13. package/dist/components/PlAgDataTable/PlAgDataTableSheets.vue2.js.map +1 -1
  14. package/dist/components/PlAgDataTable/PlAgDataTableV2.js.map +1 -1
  15. package/dist/components/PlAgDataTable/PlAgDataTableV2.style.js.map +1 -1
  16. package/dist/components/PlAgDataTable/PlAgDataTableV2.vue.d.ts.map +1 -1
  17. package/dist/components/PlAgDataTable/PlAgDataTableV2.vue2.js +169 -184
  18. package/dist/components/PlAgDataTable/PlAgDataTableV2.vue2.js.map +1 -1
  19. package/dist/components/PlAgDataTable/sources/grid-state.d.ts +25 -1
  20. package/dist/components/PlAgDataTable/sources/grid-state.d.ts.map +1 -1
  21. package/dist/components/PlAgDataTable/sources/grid-state.js +55 -16
  22. package/dist/components/PlAgDataTable/sources/grid-state.js.map +1 -1
  23. package/dist/components/PlAgDataTable/sources/table-state-v2.d.ts +44 -15
  24. package/dist/components/PlAgDataTable/sources/table-state-v2.d.ts.map +1 -1
  25. package/dist/components/PlAgDataTable/sources/table-state-v2.js +130 -178
  26. package/dist/components/PlAgDataTable/sources/table-state-v2.js.map +1 -1
  27. package/dist/components/PlAgDataTable/types.d.ts +3 -9
  28. package/dist/components/PlAgDataTable/types.d.ts.map +1 -1
  29. package/dist/components/PlAgDataTable/types.js +8 -4
  30. package/dist/components/PlAgDataTable/types.js.map +1 -1
  31. package/dist/components/PlAnnotations/components/PlAnnotations.vue2.js.map +1 -1
  32. package/package.json +4 -4
  33. package/src/components/PlAgDataTable/PlAgDataTableSheets.vue +23 -67
  34. package/src/components/PlAgDataTable/PlAgDataTableV2.vue +91 -120
  35. package/src/components/PlAgDataTable/__tests__/PlAgDataTableV2.reload.jsdomtest.ts +93 -0
  36. package/src/components/PlAgDataTable/sources/grid-state.test.ts +97 -2
  37. package/src/components/PlAgDataTable/sources/grid-state.ts +95 -1
  38. package/src/components/PlAgDataTable/sources/table-state-v2.test.ts +192 -34
  39. package/src/components/PlAgDataTable/sources/table-state-v2.ts +197 -269
  40. package/src/components/PlAgDataTable/types.ts +6 -10
@@ -1,6 +1,15 @@
1
1
  import { describe, expect, it } from "vitest";
2
- import type { PlDataTableGridStateCore, PlTableColumnIdJson } from "@platforma-sdk/model";
3
- import { isStoredStateApplied } from "./grid-state";
2
+ import type {
3
+ ColumnUniversalId,
4
+ PlDataTableGridStateCore,
5
+ PlTableColumnIdJson,
6
+ } from "@platforma-sdk/model";
7
+ import {
8
+ convertAgSortingToPTableSorting,
9
+ createGridRestoreState,
10
+ isStoredStateApplied,
11
+ readGridReport,
12
+ } from "./grid-state";
4
13
 
5
14
  const colA = '{"id":"a","type":"column"}' as PlTableColumnIdJson;
6
15
  const colB = '{"id":"b","type":"column"}' as PlTableColumnIdJson;
@@ -92,3 +101,89 @@ describe("isStoredStateApplied", () => {
92
101
  });
93
102
  });
94
103
  });
104
+
105
+ describe("readGridReport", () => {
106
+ const a = { type: "column", id: "a" as ColumnUniversalId } as const;
107
+ const untouched = { sorting: null, hiddenColIds: null };
108
+ const noDefaults = () => [];
109
+
110
+ it("keeps the report as it came, to restore the grid from", () => {
111
+ const reported = order(colB, colA);
112
+ expect(readGridReport(reported, untouched, noDefaults).gridState).toBe(reported);
113
+ });
114
+
115
+ it("reads a missing sort as untouched until the user has sorted, and as cleared after", () => {
116
+ expect(readGridReport({}, untouched, noDefaults).sorting).toBeNull();
117
+ expect(readGridReport({}, { ...untouched, sorting: [] }, noDefaults).sorting).toEqual([]);
118
+ });
119
+
120
+ it("reads a missing visibility as every column shown once the user has hidden some", () => {
121
+ expect(
122
+ readGridReport({}, { ...untouched, hiddenColIds: [a] }, noDefaults).hiddenColIds,
123
+ ).toEqual([]);
124
+ });
125
+
126
+ it("reads a missing visibility of an untouched table as the default hidden columns", () => {
127
+ expect(readGridReport({}, untouched, () => [colA]).hiddenColIds).toEqual([a]);
128
+ expect(readGridReport({}, untouched, noDefaults).hiddenColIds).toBeNull();
129
+ });
130
+
131
+ it("takes reported sorting and visibility as they are", () => {
132
+ const reported: PlDataTableGridStateCore = {
133
+ sort: { sortModel: [{ colId: colA, sort: "desc" }] },
134
+ columnVisibility: { hiddenColIds: [colA] },
135
+ };
136
+ expect(readGridReport(reported, untouched, noDefaults)).toEqual({
137
+ gridState: reported,
138
+ sorting: [{ column: a, ascending: false, naAndAbsentAreLeastValues: false }],
139
+ hiddenColIds: [a],
140
+ });
141
+ });
142
+ });
143
+
144
+ describe("createGridRestoreState", () => {
145
+ const a = { type: "column", id: "a" as ColumnUniversalId } as const;
146
+
147
+ it("spells out the sorting and hidden columns the report leaves out", () => {
148
+ expect(
149
+ createGridRestoreState({ gridState: order(colA), sorting: [], hiddenColIds: [] }),
150
+ ).toEqual({ ...order(colA), sort: { sortModel: [] }, columnVisibility: { hiddenColIds: [] } });
151
+ expect(
152
+ createGridRestoreState({
153
+ gridState: {},
154
+ sorting: [{ column: a, ascending: false, naAndAbsentAreLeastValues: false }],
155
+ hiddenColIds: [a],
156
+ }),
157
+ ).toEqual({ ...hidden(colA), sort: { sortModel: [{ colId: colA, sort: "desc" }] } });
158
+ });
159
+
160
+ it("keeps the report's own parts and adds nothing for untouched settings", () => {
161
+ const reported = { ...order(colA), ...sorted(colB) };
162
+ const sorting = [{ column: a, ascending: true, naAndAbsentAreLeastValues: true }];
163
+ expect(createGridRestoreState({ gridState: reported, sorting, hiddenColIds: null })).toEqual(
164
+ reported,
165
+ );
166
+ });
167
+
168
+ it("makes a grid still sorted elsewhere drop the sorting the user cleared", () => {
169
+ const restore = createGridRestoreState({ gridState: {}, sorting: [], hiddenColIds: null });
170
+ expect(isStoredStateApplied(restore, sorted(colA), grid)).toBe(false);
171
+ });
172
+ });
173
+
174
+ describe("convertAgSortingToPTableSorting", () => {
175
+ it("takes NA and absent values as the least ones only when ascending", () => {
176
+ const column = { type: "column", id: "a" as ColumnUniversalId } as const;
177
+ expect(
178
+ convertAgSortingToPTableSorting({
179
+ sortModel: [
180
+ { colId: colA, sort: "asc" },
181
+ { colId: colA, sort: "desc" },
182
+ ],
183
+ }),
184
+ ).toEqual([
185
+ { column, ascending: true, naAndAbsentAreLeastValues: true },
186
+ { column, ascending: false, naAndAbsentAreLeastValues: false },
187
+ ]);
188
+ });
189
+ });
@@ -1,5 +1,90 @@
1
1
  import { isJsonEqual } from "@milaboratories/helpers";
2
- import type { PlDataTableGridStateCore, PlTableColumnIdJson } from "@platforma-sdk/model";
2
+ import {
3
+ canonicalizeJson,
4
+ parseJson,
5
+ type PlDataTableGridStateCore,
6
+ type PlDataTableStateV2CacheEntry,
7
+ type PlTableColumnIdJson,
8
+ type PTableColumnId,
9
+ type PTableSorting,
10
+ } from "@platforma-sdk/model";
11
+ import { isNil } from "es-toolkit";
12
+
13
+ /** The fields of a cache entry that the grid's state report determines. */
14
+ export type PlDataTableGridFields = Pick<
15
+ PlDataTableStateV2CacheEntry,
16
+ "gridState" | "sorting" | "hiddenColIds"
17
+ >;
18
+
19
+ /**
20
+ * Read what a grid state report means for the entry of the source the grid
21
+ * shows. The report itself is kept as it came, to restore the grid from; the
22
+ * sorting and hidden columns the table is built with are taken out of it.
23
+ *
24
+ * AG Grid leaves out what carries no information — no `sort` while nothing is
25
+ * sorted, no `columnVisibility` while every column is shown — so a missing part
26
+ * means "cleared" once the user has set it before, and "untouched" otherwise.
27
+ * Untouched sorting keeps the block's default; untouched visibility hides the
28
+ * optional columns, which the grid does hide, so an untouched report names them.
29
+ */
30
+ export function readGridReport(
31
+ reported: PlDataTableGridStateCore,
32
+ previous: undefined | Pick<PlDataTableStateV2CacheEntry, "sorting" | "hiddenColIds">,
33
+ defaultHiddenColIds: () => PlTableColumnIdJson[],
34
+ ): PlDataTableGridFields {
35
+ return {
36
+ gridState: reported,
37
+ sorting: readSorting(),
38
+ hiddenColIds: readHiddenColIds(),
39
+ };
40
+
41
+ function readSorting(): null | PTableSorting[] {
42
+ if (!isNil(reported.sort)) return convertAgSortingToPTableSorting(reported.sort);
43
+ return isNil(previous?.sorting) ? null : [];
44
+ }
45
+
46
+ function readHiddenColIds(): null | PTableColumnId[] {
47
+ if (!isNil(reported.columnVisibility)) {
48
+ return reported.columnVisibility.hiddenColIds.map((id) => parseJson<PTableColumnId>(id));
49
+ }
50
+ if (!isNil(previous?.hiddenColIds)) return [];
51
+ const defaults = defaultHiddenColIds();
52
+ return defaults.length > 0 ? defaults.map((id) => parseJson<PTableColumnId>(id)) : null;
53
+ }
54
+ }
55
+
56
+ /**
57
+ * The state to restore the grid from: the stored report, with the user's
58
+ * choices that a report leaves out spelled out. AG Grid omits a cleared sorting
59
+ * and an all-shown column set from its reports; without them, a grid still
60
+ * showing an older choice, such as one open in another view, would keep it and
61
+ * report it back.
62
+ */
63
+ export function createGridRestoreState(fields: PlDataTableGridFields): PlDataTableGridStateCore {
64
+ const { gridState, sorting, hiddenColIds } = fields;
65
+ return {
66
+ ...gridState,
67
+ ...(isNil(gridState.sort) &&
68
+ !isNil(sorting) && { sort: { sortModel: sorting.map(convertPTableSortingToAgSortItem) } }),
69
+ ...(isNil(gridState.columnVisibility) &&
70
+ !isNil(hiddenColIds) && {
71
+ columnVisibility: {
72
+ hiddenColIds: hiddenColIds.map((id) => canonicalizeJson<PTableColumnId>(id)),
73
+ },
74
+ }),
75
+ };
76
+ }
77
+
78
+ /** AG Grid's sort model as table sorting; the least values are the NA ones when ascending. */
79
+ export function convertAgSortingToPTableSorting(
80
+ sort: NonNullable<PlDataTableGridStateCore["sort"]>,
81
+ ): PTableSorting[] {
82
+ return sort.sortModel.map((item) => ({
83
+ column: parseJson<PTableColumnId>(item.colId),
84
+ ascending: item.sort === "asc",
85
+ naAndAbsentAreLeastValues: item.sort === "asc",
86
+ }));
87
+ }
3
88
 
4
89
  /**
5
90
  * Whether the grid already shows everything the stored state asks for that it
@@ -108,3 +193,12 @@ function keepColumnsInGrid<T>(
108
193
  function sortIds(ids: PlTableColumnIdJson[]): PlTableColumnIdJson[] {
109
194
  return [...ids].sort();
110
195
  }
196
+
197
+ function convertPTableSortingToAgSortItem(
198
+ sorting: PTableSorting,
199
+ ): NonNullable<PlDataTableGridStateCore["sort"]>["sortModel"][number] {
200
+ return {
201
+ colId: canonicalizeJson<PTableColumnId>(sorting.column),
202
+ sort: sorting.ascending ? "asc" : "desc",
203
+ };
204
+ }
@@ -1,37 +1,195 @@
1
- // @vitest-environment jsdom
2
- import { describe, expect, it, vi } from "vitest";
3
- import { computed } from "vue";
4
-
5
- // The module pulls in uikit for `computedCached`; stub it so the test does not
6
- // drag the whole component library (and its DOM-time side effects) in.
7
- vi.mock("@milaboratories/uikit", () => ({
8
- computedCached: computed,
9
- }));
10
-
11
- const { convertAgSortingToPTableSorting } = await import("./table-state-v2");
12
-
13
- describe("convertAgSortingToPTableSorting", () => {
14
- it("reports an absent sort state as untouched, so the model keeps its default sorting", () => {
15
- expect(convertAgSortingToPTableSorting(undefined)).toBeNull();
16
- });
17
-
18
- it("reports an empty sort model as explicitly cleared, which suppresses the default", () => {
19
- expect(convertAgSortingToPTableSorting({ sortModel: [] })).toEqual([]);
20
- });
21
-
22
- it("converts a sort model, taking NA/absent as least values only when ascending", () => {
23
- const column = { type: "column", id: "colId" };
24
- const colId = JSON.stringify(column) as never;
25
- expect(
26
- convertAgSortingToPTableSorting({
27
- sortModel: [
28
- { colId, sort: "asc" },
29
- { colId, sort: "desc" },
30
- ],
31
- }),
32
- ).toEqual([
33
- { column, ascending: true, naAndAbsentAreLeastValues: true },
34
- { column, ascending: false, naAndAbsentAreLeastValues: false },
1
+ import {
2
+ createPlDataTableStateV2,
3
+ getAxisId,
4
+ type AxisSpec,
5
+ type PlDataTableSheet,
6
+ type PlDataTableStateV2,
7
+ type PlDataTableStateV2CacheEntry,
8
+ type PlDataTableStateV2Normalized,
9
+ upgradePlDataTableStateV2,
10
+ } from "@platforma-sdk/model";
11
+ import { describe, expect, it } from "vitest";
12
+ import { effectScope, nextTick, shallowRef } from "vue";
13
+ import type { PlDataTableSettingsV2 } from "../types";
14
+ import { reduceTableState, resolveSheetsState, useTableState } from "./table-state-v2";
15
+
16
+ describe("reduceTableState", () => {
17
+ it("points the stored state at the source on screen, even with nothing edited", () => {
18
+ const stored = state("A", [entry("A", { searchString: "a" })]);
19
+ const next = reduceTableState(stored, target("B"), []);
20
+ expect(next.pTableParams.sourceId).toBe("B");
21
+ expect(next.stateCache.map((e) => e.sourceId)).toEqual(["A", "B"]);
22
+ expect(next.stateCache[0].searchString).toBe("a");
23
+ });
24
+
25
+ it("leaves a stored state that already describes the source on screen as it is", () => {
26
+ const stored = reduceTableState(state("A", []), target("A"), []);
27
+ expect(reduceTableState(stored, target("A"), [])).toEqual(stored);
28
+ });
29
+
30
+ it("moves a source the user returns to to the recent end of the cache", () => {
31
+ const stored = state("B", [entry("A"), entry("B")]);
32
+ expect(reduceTableState(stored, target("A"), []).stateCache.map((e) => e.sourceId)).toEqual([
33
+ "B",
34
+ "A",
35
+ ]);
36
+ });
37
+
38
+ it("applies an edit to the source it was made on, not to the one now on screen", () => {
39
+ const stored = state("A", [entry("A")]);
40
+ const next = reduceTableState(stored, target("B"), [
41
+ { sourceId: "A", patch: { searchString: "late" } },
42
+ ]);
43
+ expect(next.pTableParams.sourceId).toBe("B");
44
+ expect(next.stateCache.find((e) => e.sourceId === "A")?.searchString).toBe("late");
45
+ expect(next.stateCache.find((e) => e.sourceId === "B")?.searchString).toBeUndefined();
46
+ });
47
+
48
+ it("keeps five sources", () => {
49
+ const stored = state(
50
+ "E",
51
+ ["A", "B", "C", "D", "E"].map((id) => entry(id)),
52
+ );
53
+ expect(reduceTableState(stored, target("F"), []).stateCache.map((e) => e.sourceId)).toEqual([
54
+ "B",
55
+ "C",
56
+ "D",
57
+ "E",
58
+ "F",
35
59
  ]);
36
60
  });
61
+
62
+ it("does not touch the stored state while no source is on screen", () => {
63
+ const stored = state("A", [entry("A")]);
64
+ expect(reduceTableState(stored, null, [])).toBe(stored);
65
+ });
37
66
  });
67
+
68
+ describe("resolveSheetsState", () => {
69
+ const sheet: PlDataTableSheet = {
70
+ axis: axis("sample"),
71
+ options: [
72
+ { label: "S1", value: "s1" },
73
+ { label: "S2", value: "s2" },
74
+ ],
75
+ defaultValue: "s2",
76
+ };
77
+ const axisId = getAxisId(axis("sample"));
78
+
79
+ it("keeps a saved value the sheet still offers", () => {
80
+ expect(resolveSheetsState([sheet], [{ axisId, value: "s1" }])).toEqual([
81
+ { axisId, value: "s1" },
82
+ ]);
83
+ });
84
+
85
+ it("falls back to the sheet's default, then to its first option", () => {
86
+ expect(resolveSheetsState([sheet], [{ axisId, value: "gone" }])).toEqual([
87
+ { axisId, value: "s2" },
88
+ ]);
89
+ expect(resolveSheetsState([{ ...sheet, defaultValue: "gone" }], [])).toEqual([
90
+ { axisId, value: "s1" },
91
+ ]);
92
+ });
93
+
94
+ it("gives a sheet without options no entry", () => {
95
+ expect(resolveSheetsState([{ ...sheet, options: [] }], [])).toEqual([]);
96
+ });
97
+ });
98
+
99
+ describe("useTableState", () => {
100
+ it("writes the source on screen when mounted on one the stored state does not point at", async () => {
101
+ const { tableState, stop } = setup(state("A", [entry("A")]), settings("B"));
102
+ await nextTick();
103
+ expect(normalized(tableState.value).pTableParams.sourceId).toBe("B");
104
+ stop();
105
+ });
106
+
107
+ it("keeps an edit while the platform pushes back the state from before it", async () => {
108
+ const before = reduceTableState(state("A", [entry("A")]), target("A"), []);
109
+ const { tableState, table, stop } = setup(before, settings("A"));
110
+ await nextTick();
111
+
112
+ table.searchString.value = "needle";
113
+ expect(normalized(tableState.value).stateCache[0].searchString).toBe("needle");
114
+
115
+ // The platform re-pushes the project state, which does not have the edit yet.
116
+ tableState.value = before;
117
+ await nextTick();
118
+ expect(table.searchString.value).toBe("needle");
119
+ expect(normalized(tableState.value).stateCache[0].searchString).toBe("needle");
120
+ stop();
121
+ });
122
+
123
+ it("stores a grid report under the source the grid was built for", async () => {
124
+ const { tableState, table, stop } = setup(state("A", [entry("A")]), settings("B"));
125
+ await nextTick();
126
+ table.updateGrid("A", {
127
+ gridState: { sort: { sortModel: [] } },
128
+ sorting: [],
129
+ hiddenColIds: null,
130
+ });
131
+ const stored = normalized(tableState.value);
132
+ expect(stored.stateCache.find((e) => e.sourceId === "A")).toMatchObject({
133
+ gridState: { sort: { sortModel: [] } },
134
+ sorting: [],
135
+ });
136
+ expect(stored.stateCache.find((e) => e.sourceId === "B")).toMatchObject({
137
+ gridState: {},
138
+ sorting: null,
139
+ });
140
+ stop();
141
+ });
142
+ });
143
+
144
+ // Fixtures
145
+
146
+ function axis(name: string): AxisSpec {
147
+ return { name, type: "String" };
148
+ }
149
+
150
+ function target(sourceId: string) {
151
+ return { sourceId, sheets: [] };
152
+ }
153
+
154
+ function settings(sourceId: string): PlDataTableSettingsV2 {
155
+ return { sourceId, sheets: [], model: undefined, pending: false };
156
+ }
157
+
158
+ function entry(
159
+ sourceId: string,
160
+ fields: Partial<Omit<PlDataTableStateV2CacheEntry, "sourceId">> = {},
161
+ ): PlDataTableStateV2CacheEntry {
162
+ return {
163
+ sourceId,
164
+ gridState: {},
165
+ sorting: null,
166
+ hiddenColIds: null,
167
+ sheetsState: [],
168
+ filtersState: null,
169
+ defaultFiltersState: null,
170
+ ...fields,
171
+ };
172
+ }
173
+
174
+ function state(
175
+ sourceId: null | string,
176
+ stateCache: PlDataTableStateV2CacheEntry[],
177
+ ): PlDataTableStateV2Normalized {
178
+ const empty = createPlDataTableStateV2();
179
+ return { ...empty, stateCache, pTableParams: { ...empty.pTableParams, sourceId } };
180
+ }
181
+
182
+ function normalized(value: PlDataTableStateV2): PlDataTableStateV2Normalized {
183
+ if (!("version" in value) || value.version !== 9) throw new Error("expected a v9 state");
184
+ return upgradePlDataTableStateV2(value);
185
+ }
186
+
187
+ function setup(initial: PlDataTableStateV2Normalized, current: PlDataTableSettingsV2) {
188
+ const tableState = shallowRef<PlDataTableStateV2>(initial);
189
+ const scope = effectScope();
190
+ const table = scope.run(() =>
191
+ useTableState(tableState, shallowRef(current), shallowRef(undefined)),
192
+ );
193
+ if (table === undefined) throw new Error("effect scope did not run");
194
+ return { tableState, table, stop: () => scope.stop() };
195
+ }