Skip to content

Commit 4858794

Browse files
fix(gridMenu): command "Toggle Filter Row" header row (#466)
previous PR #448 caused a new and small issue, were the header row is being shown when changing locale even when there are no Filters. Co-authored-by: Ghislain Beaulac <ghislain.beaulac@se.com>
1 parent 528abf5 commit 4858794

File tree

5 files changed

+62
-33
lines changed

5 files changed

+62
-33
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ One of the best javascript datagrid [SlickGrid](https://github.com/mleibman/Slic
1919
[MIT License](LICENSE)
2020

2121
### Like it? :star: it
22-
You like and use this great library `Angular-Slickgrid`? Be sure to upvote :star: and feel free to contribute. :smile:
22+
You like and use this great library `Angular-Slickgrid`? Be sure to upvote :star: and feel free to contribute. :construction_worker:
2323

2424
### Like my work?
2525
If you like my work, you can also support me with caffeine :coffee:

src/app/modules/angular-slickgrid/components/__tests__/angular-slickgrid-constructor.spec.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export class ExtensionUtilityStub {
7676
const groupingAndColspanServiceStub = {
7777
init: jest.fn(),
7878
dispose: jest.fn(),
79+
translateGroupingAndColSpan: jest.fn(),
7980
} as unknown as GroupingAndColspanService;
8081

8182
const mockGraphqlService = {
@@ -1152,13 +1153,46 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
11521153
const transContextMenuSpy = jest.spyOn(extensionServiceStub, 'translateContextMenu');
11531154
const transGridMenuSpy = jest.spyOn(extensionServiceStub, 'translateGridMenu');
11541155
const transHeaderMenuSpy = jest.spyOn(extensionServiceStub, 'translateHeaderMenu');
1156+
const transGroupingColSpanSpy = jest.spyOn(groupingAndColspanServiceStub, 'translateGroupingAndColSpan');
1157+
const setHeaderRowSpy = jest.spyOn(mockGrid, 'setHeaderRowVisibility');
11551158

1156-
component.gridOptions = { enableTranslate: true } as GridOption;
1159+
component.gridOptions = { enableTranslate: true, createPreHeaderPanel: false, enableDraggableGrouping: false } as GridOption;
1160+
component.ngAfterViewInit();
1161+
1162+
translate.use('fr');
1163+
1164+
setTimeout(() => {
1165+
expect(setHeaderRowSpy).not.toHaveBeenCalled();
1166+
expect(transGroupingColSpanSpy).not.toHaveBeenCalled();
1167+
expect(transCellMenuSpy).toHaveBeenCalled();
1168+
expect(transColHeaderSpy).toHaveBeenCalled();
1169+
expect(transColPickerSpy).toHaveBeenCalled();
1170+
expect(transContextMenuSpy).toHaveBeenCalled();
1171+
expect(transGridMenuSpy).toHaveBeenCalled();
1172+
expect(transHeaderMenuSpy).toHaveBeenCalled();
1173+
done();
1174+
});
1175+
});
1176+
1177+
it('should call "setHeaderRowVisibility", "translateGroupingAndColSpan" and other methods when locale changes', (done) => {
1178+
component.columnDefinitions = [{ id: 'firstName', field: 'firstName', filterable: true }];
1179+
const transCellMenuSpy = jest.spyOn(extensionServiceStub, 'translateCellMenu');
1180+
const transColHeaderSpy = jest.spyOn(extensionServiceStub, 'translateColumnHeaders');
1181+
const transColPickerSpy = jest.spyOn(extensionServiceStub, 'translateColumnPicker');
1182+
const transContextMenuSpy = jest.spyOn(extensionServiceStub, 'translateContextMenu');
1183+
const transGridMenuSpy = jest.spyOn(extensionServiceStub, 'translateGridMenu');
1184+
const transHeaderMenuSpy = jest.spyOn(extensionServiceStub, 'translateHeaderMenu');
1185+
const transGroupingColSpanSpy = jest.spyOn(groupingAndColspanServiceStub, 'translateGroupingAndColSpan');
1186+
const setHeaderRowSpy = jest.spyOn(mockGrid, 'setHeaderRowVisibility');
1187+
1188+
component.gridOptions = { enableTranslate: true, createPreHeaderPanel: true, enableDraggableGrouping: false } as GridOption;
11571189
component.ngAfterViewInit();
11581190

11591191
translate.use('fr');
11601192

11611193
setTimeout(() => {
1194+
expect(setHeaderRowSpy).toHaveBeenCalledWith(true);
1195+
expect(transGroupingColSpanSpy).toHaveBeenCalled();
11621196
expect(transCellMenuSpy).toHaveBeenCalled();
11631197
expect(transColHeaderSpy).toHaveBeenCalled();
11641198
expect(transColPickerSpy).toHaveBeenCalled();

src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
125125
private _fixedWidth: number | null;
126126
private _hideHeaderRowAfterPageLoad = false;
127127
private _isGridInitialized = false;
128+
private _isGridHavingFilters = false;
128129
private _isDatasetInitialized = false;
129130
private _isPaginationInitialized = false;
130131
private _isLocalGrid = true;
@@ -239,6 +240,9 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
239240

240241
ngAfterViewInit() {
241242
this.initialization();
243+
if (this.columnDefinitions.findIndex((col) => col.filterable) > -1) {
244+
this._isGridHavingFilters = true;
245+
}
242246
this._isGridInitialized = true;
243247

244248
// user must provide a "gridHeight" or use "autoResize: true" in the grid options
@@ -466,7 +470,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
466470
this.subscriptions.push(
467471
this.translate.onLangChange.subscribe(() => {
468472
if (gridOptions.enableTranslate) {
469-
if (!this._hideHeaderRowAfterPageLoad) {
473+
if (!this._hideHeaderRowAfterPageLoad && this._isGridHavingFilters) {
470474
// before translating, make sure the filter row is visible to avoid having other problems,
471475
// because if it's not shown prior to translating then the filters won't be recreated after translating
472476
this.grid.setHeaderRowVisibility(true);
@@ -480,6 +484,9 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
480484
this.translateCustomFooterTexts();
481485
this.translateColumnHeaderTitleKeys();
482486
this.translateColumnGroupKeys();
487+
if (gridOptions.createPreHeaderPanel && !gridOptions.enableDraggableGrouping) {
488+
this.groupingAndColspanService.translateGroupingAndColSpan();
489+
}
483490
}
484491
})
485492
);

src/app/modules/angular-slickgrid/services/__tests__/groupingAndColspan.service.spec.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Subject } from 'rxjs';
55
import { GroupingAndColspanService } from '../groupingAndColspan.service';
66
import { GridOption, SlickEventHandler, Column } from '../../models';
77
import { ResizerService, GridDimension } from '../resizer.service';
8+
import { ExtensionUtility } from '../../extensions/extensionUtility';
89
import { SharedService } from '../shared.service';
910

1011
declare var Slick: any;
@@ -24,6 +25,11 @@ const dataViewStub = {
2425
reSort: jest.fn(),
2526
};
2627

28+
const mockExtensionUtility = {
29+
loadExtensionDynamically: jest.fn(),
30+
translateItems: jest.fn(),
31+
} as unknown as ExtensionUtility;
32+
2733
const gridStub = {
2834
autosizeColumns: jest.fn(),
2935
getColumnIndex: jest.fn(),
@@ -83,6 +89,7 @@ describe('GroupingAndColspanService', () => {
8389
providers: [
8490
GroupingAndColspanService,
8591
SharedService,
92+
{ provide: ExtensionUtility, useValue: mockExtensionUtility },
8693
{ provide: ResizerService, useValue: resizerServiceStub },
8794
],
8895
imports: [TranslateModule.forRoot()]
@@ -221,29 +228,22 @@ describe('GroupingAndColspanService', () => {
221228
// expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 75);
222229
});
223230

224-
it('should call the "renderPreHeaderRowGroupingTitles" after triggering a translate language change', () => {
231+
it('should call the "renderPreHeaderRowGroupingTitles" after calling the "translateGroupingAndColSpan" method', () => {
225232
gridOptionMock.enableTranslate = true;
226233
const renderSpy = jest.spyOn(service, 'renderPreHeaderRowGroupingTitles');
227-
const translateSpy = jest.spyOn(service, 'translateItems');
234+
const translateSpy = jest.spyOn(mockExtensionUtility, 'translateItems');
228235
const getColSpy = jest.spyOn(gridStub, 'getColumns');
229236
const setColSpy = jest.spyOn(gridStub, 'setColumns');
230237

231238
service.init(gridStub, dataViewStub);
232-
translate.use('fr');
239+
service.translateGroupingAndColSpan();
233240

234241
expect(getColSpy).toHaveBeenCalled();
235242
expect(setColSpy).toHaveBeenCalled();
236243
expect(translateSpy).toHaveBeenCalled();
237244
expect(renderSpy).toHaveBeenCalled();
238245
});
239246

240-
it('should translate the items when "translateItems" is called', () => {
241-
translate.use('fr');
242-
service.translateItems(mockColumns, 'nameKey', 'name');
243-
244-
expect(mockColumns[3]).toEqual({ id: 'start', name: 'Début', nameKey: 'START', field: 'start' });
245-
});
246-
247247
it('should render the pre-header row grouping title DOM element', () => {
248248
const spy = jest.spyOn(service, 'renderPreHeaderRowGroupingTitles');
249249
const divHeaderColumns = document.getElementsByClassName('slick-header-columns');

src/app/modules/angular-slickgrid/services/groupingAndColspan.service.ts

+8-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Injectable, Optional } from '@angular/core';
22
import { TranslateService } from '@ngx-translate/core';
33

44
import { Column, GridOption, SlickEventHandler } from './../models/index';
5+
import { ExtensionUtility } from '../extensions/extensionUtility';
56
import { ResizerService } from './resizer.service';
67

78
// using external non-typed js libraries
@@ -15,7 +16,7 @@ export class GroupingAndColspanService {
1516
private _eventHandler: SlickEventHandler;
1617
private _grid: any;
1718

18-
constructor(private resizerService: ResizerService, @Optional() private translate: TranslateService) {
19+
constructor(private extensionUtility: ExtensionUtility, private resizerService: ResizerService, @Optional() private translate: TranslateService) {
1920
this._eventHandler = new Slick.EventHandler();
2021
}
2122

@@ -48,16 +49,6 @@ export class GroupingAndColspanService {
4849
this._eventHandler.subscribe(dataView.onRowCountChanged, () => this.renderPreHeaderRowGroupingTitles());
4950
this.resizerService.onGridAfterResize.subscribe(() => this.renderPreHeaderRowGroupingTitles());
5051

51-
// if we use Translation, we need to re-translate the keys after a language change
52-
if (this._gridOptions.enableTranslate) {
53-
this.translate.onLangChange.subscribe(() => {
54-
const currentColumnDefinitions = this._grid.getColumns();
55-
this.translateItems(currentColumnDefinitions, 'columnGroupKey', 'columnGroup');
56-
this._grid.setColumns(currentColumnDefinitions);
57-
this.renderPreHeaderRowGroupingTitles();
58-
});
59-
}
60-
6152
// also not sure why at this point, but it seems that I need to call the 1st create in a delayed execution
6253
// probably some kind of timing issues and delaying it until the grid is fully ready does help
6354
setTimeout(() => this.renderPreHeaderRowGroupingTitles(), 50);
@@ -121,14 +112,11 @@ export class GroupingAndColspanService {
121112
}
122113
}
123114

124-
/** Translate the an array of items from an input key and assign to the output key */
125-
translateItems(items: any[], inputKey: string, outputKey: string) {
126-
if (Array.isArray(items)) {
127-
for (const item of items) {
128-
if (item[inputKey]) {
129-
item[outputKey] = this.translate && this.translate && this.translate.instant && this.translate.instant(item[inputKey]);
130-
}
131-
}
132-
}
115+
/** Translate Column Group texts and re-render them afterward. */
116+
translateGroupingAndColSpan() {
117+
const currentColumnDefinitions = this._grid.getColumns();
118+
this.extensionUtility.translateItems(currentColumnDefinitions, 'columnGroupKey', 'columnGroup');
119+
this._grid.setColumns(currentColumnDefinitions);
120+
this.renderPreHeaderRowGroupingTitles();
133121
}
134122
}

0 commit comments

Comments
 (0)