Skip to content

Commit acb844e

Browse files
Merge pull request #606 from lukecotter/bug-group-sorting-not-working
fix: group sorting not working
2 parents f5d36da + 572d9b3 commit acb844e

6 files changed

Lines changed: 115 additions & 101 deletions

File tree

log-viewer/modules/components/analysis-view/AnalysisView.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ export class AnalysisView extends LitElement {
219219
newFindArgs.options.matchCase !== this.findArgs.options?.matchCase;
220220
this.findArgs = newFindArgs;
221221

222-
const clearHighlights =
223-
e.type === 'lv-find-close' || (!isTableVisible && newFindArgs.count === 0);
222+
const clearHighlights = e.type === 'lv-find-close';
224223
if (clearHighlights) {
225224
newFindArgs.text = '';
226225
}
@@ -423,7 +422,7 @@ export class AnalysisView extends LitElement {
423422

424423
_clearSearchHighlights() {
425424
this._find(
426-
new CustomEvent('lv-find', {
425+
new CustomEvent('lv-find-close', {
427426
detail: { text: '', count: 0, options: { matchCase: false } },
428427
}),
429428
);

log-viewer/modules/components/calltree-view/CalltreeView.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,7 @@ export class CalltreeView extends LitElement {
358358
newFindArgs.options.matchCase !== this.findArgs.options?.matchCase;
359359
this.findArgs = newFindArgs;
360360

361-
const clearHighlights =
362-
e.type === 'lv-find-close' ||
363-
(this.canClearSearchHighlights && !isTableVisible && newFindArgs.count === 0);
361+
const clearHighlights = e.type === 'lv-find-close';
364362
if (clearHighlights) {
365363
newFindArgs.text = '';
366364
}
@@ -805,7 +803,7 @@ export class CalltreeView extends LitElement {
805803

806804
private _clearSearchHighlights() {
807805
this._find(
808-
new CustomEvent('lv-find', {
806+
new CustomEvent('lv-find-close', {
809807
detail: { text: '', count: 0, options: { matchCase: false } },
810808
}),
811809
);

log-viewer/modules/components/database-view/DMLView.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class DMLView extends LitElement {
6262
};
6363
findMap: { [key: number]: RowComponent } = {};
6464
totalMatches = 0;
65+
canClearHighlights = false;
6566

6667
constructor() {
6768
super();
@@ -219,6 +220,8 @@ export class DMLView extends LitElement {
219220
return;
220221
}
221222

223+
this.canClearHighlights = true;
224+
222225
const newFindArgs = JSON.parse(JSON.stringify(e.detail));
223226
if (!isTableVisible) {
224227
newFindArgs.text = '';
@@ -229,8 +232,7 @@ export class DMLView extends LitElement {
229232
newFindArgs.options.matchCase !== this.findArgs.options?.matchCase;
230233
this.findArgs = newFindArgs;
231234

232-
const clearHighlights =
233-
e.type === 'lv-find-close' || (!isTableVisible && newFindArgs.count === 0);
235+
const clearHighlights = e.type === 'lv-find-close';
234236
if (clearHighlights) {
235237
newFindArgs.text = '';
236238
}
@@ -248,6 +250,7 @@ export class DMLView extends LitElement {
248250
);
249251
}
250252
}
253+
this.canClearHighlights = false;
251254
}
252255

253256
_renderDMLTable(dmlTableContainer: HTMLElement, dmlLines: DMLBeginLine[]) {
@@ -298,6 +301,7 @@ export class DMLView extends LitElement {
298301
selectableRows: 'highlight',
299302
dataTree: true,
300303
dataTreeBranchElement: false,
304+
dataTreeStartExpanded: true,
301305
columnDefaults: {
302306
title: 'default',
303307
resizable: true,
@@ -377,8 +381,10 @@ export class DMLView extends LitElement {
377381
});
378382

379383
this.dmlTable.on('dataFiltering', () => {
380-
this._resetFindWidget();
381-
this._clearSearchHighlights();
384+
if (this.canClearHighlights) {
385+
this._resetFindWidget();
386+
this._clearSearchHighlights();
387+
}
382388
});
383389

384390
this.dmlTable.on('groupClick', (e: UIEvent, group: GroupComponent) => {
@@ -389,13 +395,6 @@ export class DMLView extends LitElement {
389395

390396
this.dmlTable?.blockRedraw();
391397
group.toggle();
392-
if (!group.isVisible()) {
393-
this.dmlTable?.getRows().forEach((row) => {
394-
if (!row.isTreeExpanded()) {
395-
row.treeExpand();
396-
}
397-
});
398-
}
399398
this.dmlTable?.restoreRedraw();
400399
});
401400

@@ -438,7 +437,7 @@ export class DMLView extends LitElement {
438437

439438
private _clearSearchHighlights() {
440439
this._find(
441-
new CustomEvent('lv-find', {
440+
new CustomEvent('lv-find-close', {
442441
detail: { text: '', count: 0, options: { matchCase: false } },
443442
}),
444443
);

log-viewer/modules/components/database-view/SOQLView.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class SOQLView extends LitElement {
7373
};
7474
findMap: { [key: number]: RowComponent } = {};
7575
totalMatches = 0;
76+
canClearHighlights = false;
7677

7778
get _soqlTableWrapper(): HTMLDivElement | null {
7879
return this.renderRoot?.querySelector('#db-soql-table') ?? null;
@@ -253,14 +254,15 @@ export class SOQLView extends LitElement {
253254
return;
254255
}
255256

257+
this.canClearHighlights = true;
258+
256259
const newFindArgs = JSON.parse(JSON.stringify(e.detail));
257260
const newSearch =
258261
newFindArgs.text !== this.findArgs.text ||
259262
newFindArgs.options.matchCase !== this.findArgs.options?.matchCase;
260263
this.findArgs = newFindArgs;
261264

262-
const clearHighlights =
263-
e.type === 'lv-find-close' || (!isTableVisible && newFindArgs.count === 0);
265+
const clearHighlights = e.type === 'lv-find-close';
264266
if (clearHighlights) {
265267
newFindArgs.text = '';
266268
}
@@ -278,6 +280,8 @@ export class SOQLView extends LitElement {
278280
);
279281
}
280282
}
283+
284+
this.canClearHighlights = false;
281285
}
282286

283287
_renderSOQLTable(soqlTableContainer: HTMLElement, soqlLines: SOQLExecuteBeginLine[]) {
@@ -340,6 +344,7 @@ export class SOQLView extends LitElement {
340344
},
341345
dataTree: true,
342346
dataTreeBranchElement: false,
347+
dataTreeStartExpanded: true,
343348
columnDefaults: {
344349
title: 'default',
345350
resizable: true,
@@ -513,13 +518,6 @@ export class SOQLView extends LitElement {
513518

514519
this.soqlTable?.blockRedraw();
515520
group.toggle();
516-
if (!group.isVisible()) {
517-
this.soqlTable?.getRows().forEach((row) => {
518-
if (!row.isTreeExpanded()) {
519-
row.treeExpand();
520-
}
521-
});
522-
}
523521
this.soqlTable?.restoreRedraw();
524522
});
525523

@@ -540,8 +538,10 @@ export class SOQLView extends LitElement {
540538
});
541539

542540
this.soqlTable.on('dataFiltering', () => {
543-
this._resetFindWidget();
544-
this._clearSearchHighlights();
541+
if (this.canClearHighlights) {
542+
this._resetFindWidget();
543+
this._clearSearchHighlights();
544+
}
545545
});
546546

547547
this.soqlTable.on('renderStarted', () => {
@@ -567,7 +567,7 @@ export class SOQLView extends LitElement {
567567

568568
_clearSearchHighlights() {
569569
this._find(
570-
new CustomEvent('lv-find', {
570+
new CustomEvent('lv-find-close', {
571571
detail: { text: '', count: 0, options: { matchCase: false } },
572572
}),
573573
);

log-viewer/modules/datagrid/groups/GroupCalcs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export class GroupCalcs extends Module {
2727
const arrowClone = rawGroup.arrowElement.cloneNode(true);
2828
rawGroup.arrowElement = document.createElement('span');
2929

30-
const rowFrag = document.createDocumentFragment();
3130
const firstCell = row.cells[0].getElement();
3231
firstCell.insertBefore(arrowClone, firstCell.firstChild);
3332

33+
const rowFrag = document.createDocumentFragment();
3434
row.cells.forEach((cell: { getElement(): HTMLElement }) => {
3535
rowFrag.appendChild(cell.getElement());
3636
});

log-viewer/modules/datagrid/groups/GroupSort.ts

Lines changed: 87 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ export class GroupSort extends Module {
1212
initialize() {
1313
// @ts-expect-error groupSort is a custom propoerty see registerTableOption above
1414
if (this.table.options.groupSort) {
15-
this.table.on('dataSorting', () => {
16-
this.table.blockRedraw();
17-
this._sortGroups();
18-
this.table.restoreRedraw();
19-
});
15+
this.subscribe('sort-changed', this._sortGroups.bind(this));
2016
}
2117
}
2218

@@ -34,89 +30,111 @@ export class GroupSort extends Module {
3430
}
3531
}
3632

37-
_areGroupsEqual(oldGroups: unknown[], newGroups: unknown[]) {
38-
return (
39-
oldGroups &&
40-
newGroups.length === oldGroups.length &&
41-
newGroups.every((value, index) => value === oldGroups[index])
42-
);
43-
}
44-
4533
_sortGroups() {
4634
const grpArray = Array.isArray(this.table.options.groupBy)
4735
? this.table.options.groupBy
4836
: [this.table.options.groupBy];
4937
const { options } = this.table;
50-
options.groupValues = [];
5138

5239
const validGrps = grpArray.filter(Boolean).length > 0;
53-
if (this.table && this.table.options.sortMode !== 'remote' && validGrps) {
54-
const { modules } = this.table;
40+
if (this.table && options.sortMode !== 'remote' && validGrps) {
41+
let groupFunc = grpArray[0];
42+
const grpField = groupFunc as string;
43+
if (typeof groupFunc === 'string') {
44+
groupFunc = function (data) {
45+
return data[grpField];
46+
};
47+
}
5548

56-
const groupRows = modules.groupRows;
57-
const rows = this.table.rowManager.rows;
58-
groupRows.configureGroupSetup();
59-
groupRows.generateGroups(rows);
49+
const groupsByKey: { [key: string]: unknown[] } = {};
50+
if (groupFunc) {
51+
const rows = this.table.rowManager.rows;
52+
rows.forEach((row: InternalColumnTotal) => {
53+
const grpVal = groupFunc(row.data);
54+
let groupRows = groupsByKey[grpVal];
55+
if (!groupRows) {
56+
groupRows = [];
57+
groupsByKey[grpVal] = groupRows;
58+
}
59+
groupRows.push(row);
60+
});
61+
}
6062

61-
const groupTotalsRows: InternalColumnTotal[] = [];
62-
const columnCalcs = modules.columnCalcs;
63+
let groupTotalsRows: InternalColumnTotal[] = [];
64+
const columnCalcs = this.table.modules.columnCalcs;
6365
const field = columnCalcs.botCalcs[0].field;
64-
groupRows.groupList.forEach((group: { key: string; rows: { data: unknown }[] }) => {
65-
const row = columnCalcs.generateBottomRow(group.rows);
66-
row.data[field] = group.key;
67-
row.key = group.key;
68-
row.rows = group.rows;
66+
for (const [key, rows] of Object.entries(groupsByKey)) {
67+
const row = columnCalcs.generateBottomRow(rows);
68+
row.data[field] = key;
69+
row.key = key;
70+
row.rows = rows;
6971
row.generateCells();
7072
groupTotalsRows.push(row);
71-
});
72-
73-
const sortListActual: unknown[] = [];
74-
//build list of valid sorters and trigger column specific callbacks before sort begins
75-
const sorter = modules.sort;
76-
const sortList: InternalSortItem[] = options.sortOrderReverse
77-
? sorter.sortList.slice().reverse()
78-
: sorter.sortList;
79-
sortList.forEach((item) => {
80-
let sortObj;
81-
82-
if (item.column) {
83-
sortObj = item.column.modules.sort;
84-
85-
if (sortObj) {
86-
//if no sorter has been defined, take a guess
87-
if (!sortObj.sorter) {
88-
sortObj.sorter = sorter.findSorter(item.column);
89-
}
90-
91-
item.params =
92-
typeof sortObj.params === 'function'
93-
? sortObj.params(item.column.getComponent(), item.dir)
94-
: sortObj.params;
95-
96-
sortListActual.push(item);
97-
}
98-
}
99-
});
100-
101-
//sort data
102-
if (sortListActual.length) {
103-
sorter._sortItems(groupTotalsRows, sortListActual);
104-
} else {
105-
groupTotalsRows.sort((a, b) => {
106-
const index = b.rows.length - a.rows.length;
107-
if (index === 0) {
108-
return a.key.localeCompare(b.key);
109-
}
110-
return index;
111-
});
11273
}
74+
75+
groupTotalsRows = this._sortGroupTotals(groupTotalsRows);
11376
const groupValues: string[] = [];
11477
groupTotalsRows.forEach((colTotals) => {
11578
groupValues.push(colTotals.data[field] as string);
11679
});
11780

118-
this.table?.setGroupValues([groupValues]);
81+
const originalGroupVals = (options.groupValues ?? [[]])[0] ?? [];
82+
if (!this._areGroupsEqual(groupValues, originalGroupVals)) {
83+
this.table?.setGroupValues([groupValues]);
84+
}
85+
} else {
86+
this.table?.setGroupValues([]);
87+
}
88+
}
89+
90+
_areGroupsEqual(oldGroups: unknown[], newGroups: unknown[]) {
91+
return (
92+
oldGroups &&
93+
newGroups.length === oldGroups.length &&
94+
newGroups.every((value, index) => value === oldGroups[index])
95+
);
96+
}
97+
98+
_sortGroupTotals(groupTotalsRows: InternalColumnTotal[]) {
99+
const sortListActual: unknown[] = [];
100+
const { modules, options } = this.table;
101+
102+
const sorter = modules.sort;
103+
const sortList: InternalSortItem[] = options.sortOrderReverse
104+
? sorter.sortList.slice().reverse()
105+
: sorter.sortList;
106+
sortList.forEach((item) => {
107+
if (item.column) {
108+
const sortObj = item.column.modules.sort;
109+
if (sortObj) {
110+
//if no sorter has been defined, take a guess
111+
if (!sortObj.sorter) {
112+
sortObj.sorter = sorter.findSorter(item.column);
113+
}
114+
115+
item.params =
116+
typeof sortObj.params === 'function'
117+
? sortObj.params(item.column.getComponent(), item.dir)
118+
: sortObj.params;
119+
120+
sortListActual.push(item);
121+
}
122+
}
123+
});
124+
125+
//sort data
126+
if (sortListActual.length) {
127+
sorter._sortItems(groupTotalsRows, sortListActual);
128+
} else {
129+
groupTotalsRows.sort((a, b) => {
130+
const index = b.rows.length - a.rows.length;
131+
if (index === 0) {
132+
return a.key.localeCompare(b.key);
133+
}
134+
return index;
135+
});
119136
}
137+
return groupTotalsRows;
120138
}
121139
}
122140

0 commit comments

Comments
 (0)