Skip to content

Commit c82ffb4

Browse files
authored
refactor: Cherry-pick deephaven#2583 Decouple IrisGrid from Dh.Sort, use SortDescriptor instead (deephaven#2580)
- Decouple IrisGrid from Dh.Sort, delegate sort hydration to model to allow plugins use own sort API - Allow negative column indexes in IrisGrid, so plugins could sort and filter on non-columns, such as ColumnBy sources
1 parent a467feb commit c82ffb4

File tree

14 files changed

+306
-120
lines changed

14 files changed

+306
-120
lines changed

packages/dashboard-core-plugins/src/panels/IrisGridPanel.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
type RowDataMap,
5454
type AdvancedFilterOptions,
5555
type FormattingRule,
56+
type SortDescriptor,
5657
} from '@deephaven/jsapi-utils';
5758
import Log from '@deephaven/log';
5859
import {
@@ -196,7 +197,7 @@ interface IrisGridPanelState {
196197
customColumnFormatMap: Map<string, FormattingRule>;
197198
isFilterBarShown: boolean;
198199
quickFilters: ReadonlyQuickFilterMap;
199-
sorts: readonly dh.Sort[];
200+
sorts: readonly SortDescriptor[];
200201
userColumnWidths: ModelSizeMap;
201202
userRowHeights: ModelSizeMap;
202203
reverse: boolean;

packages/iris-grid/src/CommonTypes.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AdvancedFilterOptions } from '@deephaven/jsapi-utils';
2-
import { ModelIndex } from '@deephaven/grid';
1+
import { type AdvancedFilterOptions } from '@deephaven/jsapi-utils';
2+
import { type ModelIndex } from '@deephaven/grid';
33
import type { dh } from '@deephaven/jsapi-types';
44
import { Shortcut } from '@deephaven/components';
55
import { IconDefinition } from '@deephaven/icons';

packages/iris-grid/src/IrisGrid.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import {
7878
TableColumnFormat,
7979
Settings,
8080
isSortDirection,
81+
type SortDescriptor,
8182
} from '@deephaven/jsapi-utils';
8283
import {
8384
assertNotNull,
@@ -231,7 +232,7 @@ function isEmptyConfig({
231232
rollupConfig?: UIRollupConfig;
232233
searchFilter?: DhType.FilterCondition;
233234
selectDistinctColumns: readonly ColumnName[];
234-
sorts: readonly DhType.Sort[];
235+
sorts: readonly SortDescriptor[];
235236
}): boolean {
236237
return (
237238
advancedFilters.size === 0 &&
@@ -305,7 +306,7 @@ export interface IrisGridProps {
305306
/** @deprecated use `partitionConfig` instead */
306307
partitions?: (string | null)[];
307308
partitionConfig?: PartitionConfig;
308-
sorts: readonly DhType.Sort[];
309+
sorts: readonly SortDescriptor[];
309310

310311
/** @deprecated use `reverse` instead */
311312
reverseType?: ReverseType;
@@ -389,7 +390,7 @@ export interface IrisGridState {
389390
shownAdvancedFilter: number | null;
390391
hoverAdvancedFilter: number | null;
391392

392-
sorts: readonly DhType.Sort[];
393+
sorts: readonly SortDescriptor[];
393394
reverse: boolean;
394395
customColumns: readonly ColumnName[];
395396
selectDistinctColumns: readonly ColumnName[];
@@ -1388,7 +1389,7 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
13881389
loadingScrimProgress: number | null,
13891390
quickFilters: ReadonlyQuickFilterMap,
13901391
advancedFilters: ReadonlyAdvancedFilterMap,
1391-
sorts: readonly DhType.Sort[],
1392+
sorts: readonly SortDescriptor[],
13921393
reverse: boolean,
13931394
rollupConfig: UIRollupConfig | undefined,
13941395
isMenuShown: boolean
@@ -1548,6 +1549,11 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
15481549
return '';
15491550
}
15501551

1552+
/**
1553+
* Get the model column index for the provided visible index
1554+
* @param columnIndex Visible column index
1555+
* @returns Model column index, or null if not found
1556+
*/
15511557
getModelColumn(columnIndex: GridRangeIndex): ModelIndex | null | undefined {
15521558
const { metrics } = this.state;
15531559
assertNotNull(metrics);
@@ -1556,6 +1562,11 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
15561562
return null;
15571563
}
15581564

1565+
if (columnIndex != null && columnIndex < 0) {
1566+
// ColumnBy sources aren't movable, so just return the index directly
1567+
return columnIndex;
1568+
}
1569+
15591570
return columnIndex != null ? modelColumns.get(columnIndex) : null;
15601571
}
15611572

@@ -2767,7 +2778,7 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
27672778
}
27682779
}
27692780

2770-
updateSorts(sorts: readonly DhType.Sort[]): void {
2781+
updateSorts(sorts: readonly SortDescriptor[]): void {
27712782
this.startLoading('Sorting...');
27722783
this.setState({ sorts });
27732784
this.grid?.forceUpdate();

packages/iris-grid/src/IrisGridMetricCalculator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '@deephaven/grid';
1111
import type { GridMetricState } from '@deephaven/grid';
1212
import type { dh } from '@deephaven/jsapi-types';
13+
import { SortDescriptor } from '@deephaven/jsapi-utils';
1314
import { assertNotNull } from '@deephaven/utils';
1415
import type IrisGridModel from './IrisGridModel';
1516
import { IrisGridThemeType } from './IrisGridTheme';
@@ -27,7 +28,7 @@ export interface IrisGridMetricState extends GridMetricState {
2728
isFilterBarShown: boolean;
2829
advancedFilters: ReadonlyAdvancedFilterMap;
2930
quickFilters: ReadonlyQuickFilterMap;
30-
sorts: readonly dh.Sort[];
31+
sorts: readonly SortDescriptor[];
3132
reverse: boolean;
3233
}
3334

packages/iris-grid/src/IrisGridModel.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
VisibleIndex,
1414
} from '@deephaven/grid';
1515
import type { dh as DhType } from '@deephaven/jsapi-types';
16-
import { Formatter } from '@deephaven/jsapi-utils';
16+
import { type Formatter, type SortDescriptor } from '@deephaven/jsapi-utils';
1717
import {
1818
ColumnName,
1919
UITotalsTableConfig,
@@ -262,12 +262,12 @@ abstract class IrisGridModel<
262262
/**
263263
* @returns The sorts used on this model
264264
*/
265-
abstract get sort(): readonly DhType.Sort[];
265+
abstract get sort(): readonly SortDescriptor[];
266266

267267
/**
268268
* @param sort The sorts to use on this model
269269
*/
270-
abstract set sort(sort: readonly DhType.Sort[]);
270+
abstract set sort(sort: readonly SortDescriptor[]);
271271

272272
/**
273273
/**

packages/iris-grid/src/IrisGridModelUpdater.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React, { useEffect, useMemo } from 'react';
44
import type { dh } from '@deephaven/jsapi-types';
55
import { type ModelIndex, type MoveOperation } from '@deephaven/grid';
6-
import { type Formatter } from '@deephaven/jsapi-utils';
6+
import { type SortDescriptor, type Formatter } from '@deephaven/jsapi-utils';
77
import { EMPTY_ARRAY, EMPTY_MAP } from '@deephaven/utils';
88
import IrisGridUtils from './IrisGridUtils';
99
import { ColumnName, UITotalsTableConfig, PendingDataMap } from './CommonTypes';
@@ -24,7 +24,7 @@ interface IrisGridModelUpdaterProps {
2424
left: number | null;
2525
right: number | null;
2626
filter: readonly dh.FilterCondition[];
27-
sorts: readonly dh.Sort[];
27+
sorts: readonly SortDescriptor[];
2828
reverse?: boolean;
2929
customColumns: readonly ColumnName[];
3030
movedColumns: readonly MoveOperation[];

packages/iris-grid/src/IrisGridRenderer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import {
1010
GridUtils,
1111
VisibleIndex,
1212
} from '@deephaven/grid';
13-
import type { dh } from '@deephaven/jsapi-types';
14-
import { TableUtils } from '@deephaven/jsapi-utils';
13+
import { type SortDescriptor, TableUtils } from '@deephaven/jsapi-utils';
1514
import { assertNotNull, getOrThrow } from '@deephaven/utils';
1615
import {
1716
ReadonlyAdvancedFilterMap,
@@ -66,7 +65,7 @@ export class IrisGridRenderer extends GridRenderer {
6665

6766
protected dataBarCellRenderer = new IrisGridDataBarCellRenderer();
6867

69-
getSortIcon(sort: dh.Sort | null, size: number): Path2D | null {
68+
getSortIcon(sort: SortDescriptor | null, size: number): Path2D | null {
7069
if (!sort) {
7170
return null;
7271
}

packages/iris-grid/src/IrisGridTableModelTemplate.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ import {
2121
PromiseUtils,
2222
assertNotNull,
2323
} from '@deephaven/utils';
24-
import { TableUtils, Formatter, FormatterUtils } from '@deephaven/jsapi-utils';
25-
import IrisGridModel, { DisplayColumn } from './IrisGridModel';
24+
import {
25+
TableUtils,
26+
Formatter,
27+
FormatterUtils,
28+
type SortDescriptor,
29+
} from '@deephaven/jsapi-utils';
30+
import IrisGridModel, { type DisplayColumn } from './IrisGridModel';
31+
2632
import AggregationOperation from './sidebar/aggregations/AggregationOperation';
2733
import IrisGridUtils from './IrisGridUtils';
2834
import MissingKeyError from './MissingKeyError';
@@ -1211,13 +1217,13 @@ class IrisGridTableModelTemplate<
12111217
);
12121218
}
12131219

1214-
get sort(): DhType.Sort[] {
1220+
get sort(): readonly SortDescriptor[] {
12151221
return this.table.sort;
12161222
}
12171223

1218-
set sort(sort: DhType.Sort[]) {
1224+
set sort(sort: readonly SortDescriptor[]) {
12191225
this.closeSubscription();
1220-
this.table.applySort(sort);
1226+
this.table.applySort(this.irisGridUtils.hydrateDhSort(this.columns, sort));
12211227
this.applyViewport();
12221228
}
12231229

packages/iris-grid/src/IrisGridTestUtils.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { GridRangeIndex, ModelSizeMap } from '@deephaven/grid';
1+
import { type GridRangeIndex, type ModelSizeMap } from '@deephaven/grid';
22
import type { dh as DhType } from '@deephaven/jsapi-types';
3-
import { Formatter } from '@deephaven/jsapi-utils';
3+
import { Formatter, type SortDescriptor } from '@deephaven/jsapi-utils';
44
import IrisGridProxyModel from './IrisGridProxyModel';
5+
import IrisGridUtils from './IrisGridUtils';
56

67
class IrisGridTestUtils {
78
static DEFAULT_TYPE = 'java.lang.String';
@@ -20,8 +21,11 @@ class IrisGridTestUtils {
2021

2122
private dh: typeof DhType;
2223

24+
private irisGridUtils: IrisGridUtils;
25+
2326
constructor(dh: typeof DhType) {
2427
this.dh = dh;
28+
this.irisGridUtils = new IrisGridUtils(dh);
2529
}
2630

2731
makeColumn(
@@ -72,9 +76,16 @@ class IrisGridTestUtils {
7276
return new (this.dh as any).FilterCondition();
7377
}
7478

75-
makeSort(): DhType.Sort {
79+
makeSort(column: DhType.Column = this.makeColumn()): DhType.Sort {
7680
// eslint-disable-next-line @typescript-eslint/no-explicit-any
77-
return new (this.dh as any).Sort();
81+
return new (this.dh as any).Sort({ column });
82+
}
83+
84+
hydrateSort(
85+
sortDescriptor: readonly SortDescriptor[],
86+
columns: DhType.Column[]
87+
): DhType.Sort[] {
88+
return this.irisGridUtils.hydrateDhSort(columns, sortDescriptor);
7889
}
7990

8091
makeTable({
@@ -85,7 +96,7 @@ class IrisGridTestUtils {
8596
}: {
8697
columns?: DhType.Column[];
8798
size?: number;
88-
sort?: readonly DhType.Sort[];
99+
sort?: readonly SortDescriptor[];
89100
layoutHints?: Partial<DhType.LayoutHints>;
90101
} = {}): DhType.Table {
91102
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages/iris-grid/src/IrisGridUtils.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,16 @@ describe('sort exporting/importing', () => {
185185

186186
expect(importedSort).toEqual([
187187
expect.objectContaining({
188-
column: columns[3],
188+
column: expect.objectContaining({
189+
name: columns[3].name,
190+
}),
189191
isAbs: false,
190192
direction: 'ASC',
191193
}),
192194
expect.objectContaining({
193-
column: columns[7],
195+
column: expect.objectContaining({
196+
name: columns[7].name,
197+
}),
194198
isAbs: true,
195199
direction: 'DESC',
196200
}),
@@ -375,6 +379,15 @@ describe('remove columns in moved columns', () => {
375379
});
376380
});
377381

382+
describe('removeSortsInColumns', () => {
383+
it('removes sort for the given column names', () => {
384+
const table = makeTable();
385+
const sort = [table.columns[2].sort(), table.columns[5].sort().desc()];
386+
const newSort = IrisGridUtils.removeSortsInColumns(sort, ['name_2']);
387+
expect(newSort).toEqual([table.columns[5].sort().desc()]);
388+
});
389+
});
390+
378391
describe('getPrevVisibleColumns', () => {
379392
const columns = irisGridTestUtils.makeColumns(5);
380393
it('returns [] for startIndex < 0', () => {

0 commit comments

Comments
 (0)