-
Notifications
You must be signed in to change notification settings - Fork 614
Expand file tree
/
Copy pathresultGrid.tsx
More file actions
411 lines (379 loc) · 16.1 KB
/
Copy pathresultGrid.tsx
File metadata and controls
411 lines (379 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import $ from "jquery";
import { forwardRef, useContext, useEffect, useImperativeHandle, useRef } from "react";
import "../../media/slickgrid.css";
import { ACTIONBAR_WIDTH_PX, range, Table } from "./table/table";
import { defaultTableStyles } from "./table/interfaces";
import { RowNumberColumn } from "./table/plugins/rowNumberColumn.plugin";
import { VirtualizedCollection } from "./table/asyncDataView";
import { HybridDataProvider } from "./table/hybridDataProvider";
import { hyperLinkFormatter, textFormatter, DBCellValue, escape } from "./table/formatters";
import { DbCellValue, ResultSetSummary } from "../../../sharedInterfaces/queryResult";
import * as DOM from "./table/dom";
import { locConstants } from "../../common/locConstants";
import { QueryResultCommandsContext } from "./queryResultStateProvider";
import { LogCallback } from "../../../sharedInterfaces/webview";
import { useQueryResultSelector } from "./queryResultSelector";
import { useVscodeWebview2 } from "../../common/vscodeWebviewProvider2";
window.jQuery = $ as any;
require("slickgrid/lib/jquery.event.drag-2.3.0.js");
require("slickgrid/lib/jquery-1.11.2.min.js");
require("slickgrid/slick.core.js");
require("slickgrid/slick.grid.js");
require("slickgrid/plugins/slick.cellrangedecorator.js");
declare global {
interface Window {
$: any;
jQuery: any;
}
}
export interface ResultGridProps {
loadFunc: (offset: number, count: number) => Thenable<any[]>;
resultSetSummary?: ResultSetSummary;
divId?: string;
uri?: string;
gridParentRef?: React.RefObject<HTMLDivElement>;
linkHandler: (fileContent: string, fileType: string) => void;
gridId: string;
}
export interface ResultGridHandle {
refreshGrid: () => void;
resizeGrid: (width: number, height: number) => void;
hideGrid: () => void;
showGrid: () => void;
}
const ResultGrid = forwardRef<ResultGridHandle, ResultGridProps>((props: ResultGridProps, ref) => {
const tableRef = useRef<Table<any> | null>(null);
const gridContainerRef = useRef<HTMLDivElement>(null);
const context = useContext(QueryResultCommandsContext);
if (!context) {
return undefined;
}
// Helper to get selected columns from the grid selection
const getSelectedColumns = () => {
if (!tableRef.current) return [];
const ranges = tableRef.current.getSelectedRanges();
const columns: Set<number> = new Set();
ranges.forEach((range) => {
for (let c = range.fromCell; c <= range.toCell; c++) {
columns.add(c);
}
});
// Skip row selector column (usually index 0)
return Array.from(columns).filter((idx) => idx > 0);
};
// Resize selected columns by delta px
const resizeSelectedColumns = (delta: number) => {
if (!tableRef.current) return;
const grid = tableRef.current.grid;
const columns = grid.getColumns();
const selectedCols = getSelectedColumns();
let changed = false;
selectedCols.forEach((colIdx) => {
const col = columns[colIdx];
if (col && col.width) {
col.width = Math.max(40, col.width + delta);
changed = true;
}
});
if (changed) {
grid.setColumns(columns);
grid.render();
}
};
// Wheel event handler for CTRL + scroll
useEffect(() => {
const gridDiv = gridContainerRef.current;
if (!gridDiv) return;
const wheelHandler = (event: WheelEvent) => {
if (event.ctrlKey && getSelectedColumns().length > 0) {
event.preventDefault();
if (event.deltaY < 0) {
resizeSelectedColumns(20); // Expand
} else if (event.deltaY > 0) {
resizeSelectedColumns(-20); // Contract
}
}
};
gridDiv.addEventListener("wheel", wheelHandler, { passive: false });
return () => gridDiv.removeEventListener("wheel", wheelHandler);
}, [tableRef.current]);
const inMemoryDataProcessingThreshold = useQueryResultSelector<number | undefined>(
(state) => state.inMemoryDataProcessingThreshold,
);
const fontSettings = useQueryResultSelector((state) => state.fontSettings);
const autoSizeColumns = useQueryResultSelector((state) => state.autoSizeColumns);
const { themeKind } = useVscodeWebview2();
const isTableCreated = useRef<boolean>(false);
if (!props.gridParentRef) {
return undefined;
}
const refreshGrid = () => {
if (gridContainerRef.current) {
while (gridContainerRef.current.firstChild) {
gridContainerRef.current.removeChild(gridContainerRef.current.firstChild);
}
}
isTableCreated.current = false;
tableRef.current = null;
};
const resizeGrid = (width: number, height: number) => {
if (!tableRef.current) {
context.log("resizeGrid - table is not initialized, creating table");
createTableIfNeeded();
return;
}
let gridParent: HTMLElement | null;
if (!props.resultSetSummary) {
return;
}
gridParent = document.getElementById(
`grid-parent-${props.resultSetSummary.batchId}-${props.resultSetSummary.id}`,
);
if (gridParent) {
gridParent.style.height = `${height}px`;
}
const dimension = new DOM.Dimension(width, height);
tableRef.current?.layout(dimension);
};
const hideGrid = () => {
let gridParent: HTMLElement | null;
if (!props.resultSetSummary) {
return;
}
gridParent = document.getElementById(
`grid-parent-${props.resultSetSummary.batchId}-${props.resultSetSummary.id}`,
);
if (gridParent) {
gridParent.style.display = "none";
}
};
const showGrid = () => {
let gridParent: HTMLElement | null;
if (!props.resultSetSummary) {
return;
}
gridParent = document.getElementById(
`grid-parent-${props.resultSetSummary.batchId}-${props.resultSetSummary.id}`,
);
if (gridParent) {
gridParent.style.display = "";
}
};
const updateRowCountOnly = () => {
if (tableRef.current && props.resultSetSummary) {
// Update the data provider with new row count
const dataProvider = tableRef.current.getData() as HybridDataProvider<any>;
if (dataProvider && "length" in dataProvider) {
dataProvider.length = props.resultSetSummary.rowCount;
}
tableRef.current.updateRowCount();
}
};
const createTableIfNeeded = () => {
if (isTableCreated.current && tableRef.current) {
// Table already exists, just update row count
updateRowCountOnly();
return;
}
const setupState = async () => {
if (!tableRef.current) return;
await tableRef.current.setupFilterState();
await tableRef.current.restoreColumnWidths();
await tableRef.current.setupScrollPosition();
tableRef.current.headerFilter.enabled =
tableRef.current.grid.getDataLength() < inMemoryDataProcessingThreshold!;
tableRef.current.rerenderGrid();
};
const DEFAULT_FONT_SIZE = 12;
const ROW_HEIGHT = fontSettings.fontSize! + 12; // 12 px is the padding
const COLUMN_WIDTH = Math.max((fontSettings.fontSize! / DEFAULT_FONT_SIZE) * 120, 120); // Scale width with font size, but keep a minimum of 120px
if (!props.resultSetSummary || !props.linkHandler) {
return;
}
let columns: Slick.Column<Slick.SlickData>[] = props.resultSetSummary.columnInfo.map(
(c, i) => {
return {
id: i.toString(),
name:
c.columnName === "Microsoft SQL Server 2005 XML Showplan"
? locConstants.queryResult.showplanXML
: escape(c.columnName),
field: i.toString(),
formatter:
c.isXml || c.isJson
? hyperLinkFormatter
: (
row: number | undefined,
cell: any | undefined,
value: DbCellValue,
columnDef: any | undefined,
dataContext: any | undefined,
):
| string
| {
text: string;
addClasses: string;
} => {
if (isXmlCell(value, context?.log) && props.resultSetSummary) {
props.resultSetSummary.columnInfo[i].isXml = true;
return hyperLinkFormatter(
row,
cell,
value,
columnDef,
dataContext,
);
} else if (isJsonCell(value) && props.resultSetSummary) {
//TODO use showJsonAsLink config
props.resultSetSummary.columnInfo[i].isJson = true;
return hyperLinkFormatter(
row,
cell,
value,
columnDef,
dataContext,
);
} else {
return textFormatter(
row,
cell,
value,
columnDef,
dataContext,
DBCellValue.isDBCellValue(value) && value.isNull
? NULL_CELL_CSS_CLASS
: undefined,
);
}
},
};
},
);
let div = document.createElement("div");
div.id = "grid";
div.className = "grid-panel";
div.style.display = "inline-block";
let tableOptions: Slick.GridOptions<Slick.SlickData> = {
rowHeight: ROW_HEIGHT,
showRowNumber: true,
forceFitColumns: false,
defaultColumnWidth: COLUMN_WIDTH,
};
let rowNumberColumn = new RowNumberColumn<Slick.SlickData>({
autoCellSelection: false,
});
columns.unshift(rowNumberColumn.getColumnDefinition());
let collection = new VirtualizedCollection<any>(
50,
(_index) => {},
props.resultSetSummary?.rowCount ?? 0,
props.loadFunc,
);
let dataProvider = new HybridDataProvider(
collection,
(_startIndex, _count) => {
if (props.resultSetSummary?.rowCount && props.resultSetSummary?.rowCount > 0) {
return props.loadFunc(_startIndex, _count);
} else {
console.info(`No rows to load: start index: ${_startIndex}, count: ${_count}`);
return Promise.resolve([]);
}
},
(data: DbCellValue) => {
if (!data || data.isNull) {
return undefined;
}
// If the string only contains whitespaces, it will be treated as empty string to make the filtering easier.
// Note: this is the display string and does not impact the export/copy features.
return data.displayValue.trim() === "" ? "" : data.displayValue;
},
{
inMemoryDataProcessing: true,
inMemoryDataCountThreshold: inMemoryDataProcessingThreshold,
},
context,
undefined,
undefined,
);
tableRef.current = new Table(
div,
defaultTableStyles,
props.uri!,
props.resultSetSummary!,
context,
props.linkHandler!,
props.gridId,
{ dataProvider: dataProvider, columns: columns },
tableOptions,
props.gridParentRef,
autoSizeColumns,
themeKind,
);
void setupState();
collection.setCollectionChangedCallback((startIndex, count) => {
let refreshedRows = range(startIndex, startIndex + count);
tableRef.current?.invalidateRows(refreshedRows, true);
});
tableRef.current.updateRowCount();
gridContainerRef.current?.appendChild(div);
if (
props.gridParentRef &&
props.gridParentRef.current &&
props.gridParentRef.current.clientWidth
) {
tableRef.current.layout(
new DOM.Dimension(
props.gridParentRef.current.clientWidth - ACTIONBAR_WIDTH_PX,
props.gridParentRef.current.clientHeight,
),
);
}
isTableCreated.current = true;
};
useImperativeHandle(ref, () => ({
refreshGrid,
resizeGrid,
hideGrid,
showGrid,
}));
useEffect(() => {
createTableIfNeeded();
}, [props.resultSetSummary?.rowCount]);
return <div id="gridContainter" ref={gridContainerRef}></div>;
});
function isJsonCell(value: DbCellValue): boolean {
return !!(value && !value.isNull && value.displayValue?.match(IsJsonRegex));
}
function isXmlCell(value: DBCellValue, log?: LogCallback): boolean {
let isXML = false;
try {
if (value && !value.isNull && value.displayValue.trim() !== "") {
var parser = new DOMParser();
// Script elements if any are not evaluated during parsing
var doc = parser.parseFromString(value.displayValue, "text/xml");
// For non-xmls, parsererror element is present in body element.
var parserErrors = doc.body?.getElementsByTagName("parsererror") ?? [];
isXML = parserErrors?.length === 0;
}
} catch (e) {
// Ignore errors when parsing cell content, log and continue
log && log(`An error occurred when parsing data as XML: ${e}`); // only call if callback is defined
}
return isXML;
}
// The regex to check whether a string is a valid JSON string. It is used to determine:
// 1. whether the cell should be rendered as a hyperlink.
// 2. when user clicks a cell, whether the cell content should be displayed in a new text editor as json.
// Based on the requirements, the solution doesn't need to be very accurate, a simple regex is enough since it is more
// performant than trying to parse the string to object.
// Regex explaination: after removing the trailing whitespaces and line breaks, the string must start with '[' (to support arrays)
// or '{', and there must be a '}' or ']' to close it.
const IsJsonRegex = /^\s*[\{|\[][\S\s]*[\}\]]\s*$/g;
// The css class for null cell
const NULL_CELL_CSS_CLASS = "cell-null";
ResultGrid.displayName = "ResultGrid";
export default ResultGrid;