Skip to content

Commit 26fa5aa

Browse files
authored
Merge pull request #1708 from ghiscoding/chore/refactor-pagination
chore: refactor Pagination Component in preparation of Custom Comp
2 parents cd6cb96 + b750828 commit 26fa5aa

File tree

5 files changed

+50
-45
lines changed

5 files changed

+50
-45
lines changed

packages/common/src/interfaces/pagination.interface.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import type { BasePubSubService } from '@slickgrid-universal/event-pub-sub';
2+
3+
import type { PaginationService } from '../services/pagination.service';
4+
import type { TranslaterService } from '../services/translater.service';
5+
import type { SlickGrid } from '../core/slickGrid';
6+
17
export interface Pagination {
28
/** Current page number that we are we currently displaying. */
39
pageNumber?: number;
@@ -11,3 +17,16 @@ export interface Pagination {
1117
/** The full total count of items for the entire dataset */
1218
totalItems?: number;
1319
}
20+
21+
export abstract class BasePaginationComponent {
22+
constructor(
23+
_grid: SlickGrid,
24+
_paginationService: PaginationService,
25+
_pubSubService: BasePubSubService,
26+
_translaterService?: TranslaterService | undefined
27+
) { }
28+
29+
dispose(): void { }
30+
31+
render(_containerElm: HTMLElement): void { }
32+
}

packages/pagination-component/src/__tests__/slick-pagination-without-i18n.spec.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2-
import { type GridOption, type Locale, type PaginationService, SharedService, type SlickGrid } from '@slickgrid-universal/common';
2+
import { type GridOption, type Locale, type PaginationService, type SlickGrid } from '@slickgrid-universal/common';
33
import { EventPubSubService } from '@slickgrid-universal/event-pub-sub';
44

55
import { TranslateServiceStub } from '../../../../test/translateServiceStub';
@@ -62,17 +62,14 @@ describe('Slick-Pagination Component', () => {
6262
let component: SlickPaginationComponent;
6363
let div: HTMLDivElement;
6464
let eventPubSubService: EventPubSubService;
65-
let sharedService: SharedService;
6665
let translateService: TranslateServiceStub;
6766

6867
beforeEach(() => {
6968
vi.spyOn(paginationServiceStub, 'getFullPagination').mockReturnValue(mockFullPagination);
7069
div = document.createElement('div');
7170
document.body.appendChild(div);
72-
sharedService = new SharedService();
7371
eventPubSubService = new EventPubSubService();
7472
translateService = new TranslateServiceStub();
75-
sharedService.slickGrid = gridStub;
7673
});
7774

7875
describe('Integration Tests', () => {
@@ -83,24 +80,22 @@ describe('Slick-Pagination Component', () => {
8380

8481
it('should throw an error when "enableTranslate" is set and I18N Service is not provided', () => new Promise((done: any) => {
8582
try {
86-
mockGridOptions.enableTranslate = true;
8783
translateService = undefined as any;
88-
vi.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(mockGridOptions);
84+
vi.spyOn(gridStub, 'getOptions').mockReturnValueOnce({ ...mockGridOptions, enableTranslate: true });
8985

90-
component = new SlickPaginationComponent(paginationServiceStub, eventPubSubService, sharedService, translateService);
91-
component.renderPagination(div);
86+
component = new SlickPaginationComponent(gridStub, paginationServiceStub, eventPubSubService, translateService);
87+
component.render(div);
9288
} catch (e) {
9389
expect(e.toString()).toContain('[Slickgrid-Universal] requires a Translate Service to be installed and configured when the grid option "enableTranslate" is enabled.');
9490
done();
9591
}
9692
}));
9793

9894
it('should have defined locale and expect new text in the UI', () => {
99-
mockGridOptions.locales = mockLocales;
100-
vi.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(mockGridOptions);
95+
vi.spyOn(gridStub, 'getOptions').mockReturnValueOnce({ ...mockGridOptions, locales: mockLocales });
10196

102-
component = new SlickPaginationComponent(paginationServiceStub, eventPubSubService, sharedService, translateService);
103-
component.renderPagination(div);
97+
component = new SlickPaginationComponent(gridStub, paginationServiceStub, eventPubSubService, translateService);
98+
component.render(div);
10499

105100
const pageInfoFromTo = document.querySelector('.page-info-from-to') as HTMLSpanElement;
106101
const pageInfoTotalItems = document.querySelector('.page-info-total-items') as HTMLSpanElement;

packages/pagination-component/src/__tests__/slick-pagination.spec.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterEach, beforeAll, beforeEach, describe, expect, it, test, vi } from 'vitest';
2-
import { type GridOption, type PaginationService, SharedService, type SlickGrid, } from '@slickgrid-universal/common';
2+
import { type GridOption, type PaginationService, type SlickGrid, } from '@slickgrid-universal/common';
33
import { EventPubSubService } from '@slickgrid-universal/event-pub-sub';
44

55
import { TranslateServiceStub } from '../../../../test/translateServiceStub';
@@ -70,7 +70,6 @@ describe('Slick-Pagination Component', () => {
7070
let component: SlickPaginationComponent;
7171
let div: HTMLDivElement;
7272
let eventPubSubService: EventPubSubService;
73-
let sharedService: SharedService;
7473
let translateService: TranslateServiceStub;
7574

7675
describe("Integration Tests", () => {
@@ -94,16 +93,14 @@ describe('Slick-Pagination Component', () => {
9493

9594
beforeEach(() => {
9695
vi.spyOn(paginationServiceStub as PaginationService, 'getFullPagination').mockReturnValue(mockFullPagination);
97-
vi.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(mockGridOptions);
96+
vi.spyOn(gridStub, 'getOptions').mockReturnValueOnce(mockGridOptions);
9897
div = document.createElement('div');
9998
document.body.appendChild(div);
100-
sharedService = new SharedService();
10199
eventPubSubService = new EventPubSubService();
102100
translateService = new TranslateServiceStub();
103-
sharedService.slickGrid = gridStub;
104101

105-
component = new SlickPaginationComponent(paginationServiceStub as PaginationService, eventPubSubService, sharedService, translateService);
106-
component.renderPagination(div);
102+
component = new SlickPaginationComponent(gridStub, paginationServiceStub as PaginationService, eventPubSubService, translateService);
103+
component.render(div);
107104
});
108105

109106
afterEach(() => {
@@ -272,7 +269,7 @@ describe('Slick-Pagination Component', () => {
272269

273270
test(`when "onPaginationSetCursorBased" event is triggered then expect pagination to be recreated`, () => {
274271
const disposeSpy = vi.spyOn(component, 'dispose');
275-
const renderPagSpy = vi.spyOn(component, 'renderPagination');
272+
const renderPagSpy = vi.spyOn(component, 'render');
276273

277274
mockFullPagination.pageNumber = 1;
278275
mockFullPagination.pageCount = 10;
@@ -299,7 +296,6 @@ describe('with different i18n locale', () => {
299296
let component: SlickPaginationComponent;
300297
let div: HTMLDivElement;
301298
let eventPubSubService: EventPubSubService;
302-
let sharedService: SharedService;
303299
let translateService: TranslateServiceStub;
304300
const mockFullPagination2 = {
305301
pageCount: 19,
@@ -317,22 +313,20 @@ describe('with different i18n locale', () => {
317313

318314
beforeEach(() => {
319315
mockGridOptions.enableTranslate = true;
320-
vi.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(mockGridOptions);
316+
vi.spyOn(gridStub, 'getOptions').mockReturnValueOnce(mockGridOptions);
321317
vi.spyOn(paginationServiceStub as PaginationService, 'getFullPagination').mockReturnValue(mockFullPagination2);
322318
div = document.createElement('div');
323319
document.body.appendChild(div);
324-
sharedService = new SharedService();
325320
eventPubSubService = new EventPubSubService();
326321
translateService = new TranslateServiceStub();
327-
sharedService.slickGrid = gridStub;
328322

329-
component = new SlickPaginationComponent(paginationServiceStub, eventPubSubService, sharedService, translateService);
330-
component.renderPagination(div);
323+
component = new SlickPaginationComponent(gridStub, paginationServiceStub, eventPubSubService, translateService);
324+
component.render(div);
331325
});
332326

333327
it('should throw an error when enabling translate without a Translate Service', () => {
334-
mockGridOptions.enableTranslate = true;
335-
expect(() => new SlickPaginationComponent(paginationServiceStub, eventPubSubService, sharedService, null as any))
328+
vi.spyOn(gridStub, 'getOptions').mockReturnValueOnce({ ...mockGridOptions, enableTranslate: true });
329+
expect(() => new SlickPaginationComponent(gridStub, paginationServiceStub, eventPubSubService, null as any))
336330
.toThrow('[Slickgrid-Universal] requires a Translate Service to be installed and configured when the grid option "enableTranslate" is enabled.');
337331
});
338332

packages/pagination-component/src/slick-pagination.component.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import type {
22
GridOption,
33
Locale,
4+
BasePaginationComponent,
45
PaginationService,
56
PubSubService,
67
ServicePagination,
7-
SharedService,
88
SlickGrid,
99
Subscription,
1010
TranslaterService,
1111
} from '@slickgrid-universal/common';
1212
import { Constants, createDomElement, getTranslationPrefix } from '@slickgrid-universal/common';
1313
import { BindingEventService, BindingHelper } from '@slickgrid-universal/binding';
1414

15-
export class SlickPaginationComponent {
15+
export class SlickPaginationComponent implements BasePaginationComponent {
1616
protected _bindingHelper: BindingHelper;
1717
protected _bindingEventService: BindingEventService;
1818
protected _paginationElement!: HTMLDivElement;
1919
protected _enableTranslate = false;
20-
protected _gridParentContainerElm?: HTMLElement;
20+
protected _gridContainerElm?: HTMLElement;
2121
protected _itemPerPageElm!: HTMLSelectElement;
2222
protected _spanInfoFromToElm!: HTMLSpanElement;
2323
protected _seekFirstElm!: HTMLLIElement;
@@ -37,7 +37,7 @@ export class SlickPaginationComponent {
3737
textOf = 'of';
3838
textPage = 'Page';
3939

40-
constructor(protected readonly paginationService: PaginationService, protected readonly pubSubService: PubSubService, protected readonly sharedService: SharedService, protected readonly translaterService?: TranslaterService | undefined) {
40+
constructor(protected readonly grid: SlickGrid, protected readonly paginationService: PaginationService, protected readonly pubSubService: PubSubService, protected readonly translaterService?: TranslaterService | undefined) {
4141
this._bindingHelper = new BindingHelper();
4242
this._bindingEventService = new BindingEventService();
4343
this._bindingHelper.querySelectorPrefix = `.${this.gridUid} `;
@@ -70,7 +70,7 @@ export class SlickPaginationComponent {
7070
}),
7171
this.pubSubService.subscribe('onPaginationSetCursorBased', () => {
7272
this.dispose(); // recreate pagination component, probably only used for GraphQL E2E tests
73-
this.renderPagination(this._gridParentContainerElm!);
73+
this.render(this._gridContainerElm!);
7474
})
7575
);
7676
}
@@ -102,12 +102,9 @@ export class SlickPaginationComponent {
102102
return this.paginationService.pageNumber;
103103
}
104104

105-
get grid(): SlickGrid {
106-
return this.sharedService.slickGrid;
107-
}
108-
105+
/** Getter for the Grid Options pulled through the Grid Object */
109106
get gridOptions(): GridOption {
110-
return this.sharedService.gridOptions;
107+
return this.grid?.getOptions() ?? {};
111108
}
112109

113110
get gridUid(): string {
@@ -140,8 +137,8 @@ export class SlickPaginationComponent {
140137
this._paginationElement.remove();
141138
}
142139

143-
renderPagination(gridParentContainerElm: HTMLElement): void {
144-
this._gridParentContainerElm = gridParentContainerElm;
140+
render(containerElm: HTMLElement): void {
141+
this._gridContainerElm = containerElm;
145142
const paginationElm = this.createPaginationContainer();
146143
const divNavContainerElm = createDomElement('div', { className: 'slick-pagination-nav' });
147144

@@ -175,8 +172,8 @@ export class SlickPaginationComponent {
175172
paginationElm.appendChild(divNavContainerElm);
176173
paginationElm.appendChild(paginationSettingsElm);
177174
this._paginationElement.appendChild(paginationElm);
178-
if (gridParentContainerElm?.appendChild && this._paginationElement) {
179-
gridParentContainerElm.appendChild(this._paginationElement);
175+
if (containerElm?.appendChild && this._paginationElement) {
176+
containerElm.appendChild(this._paginationElement);
180177
}
181178

182179
this.renderPageSizes();

packages/vanilla-bundle/src/components/slick-vanilla-grid-bundle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,9 +1262,9 @@ export class SlickVanillaGridBundle<TData = any> {
12621262
* @param {Boolean} shouldDisposePaginationService - when disposing the Pagination, do we also want to dispose of the Pagination Service? (defaults to True)
12631263
*/
12641264
protected renderPagination(showPagination = true): void {
1265-
if (this._gridOptions?.enablePagination && !this._isPaginationInitialized && showPagination) {
1266-
this.slickPagination = new SlickPaginationComponent(this.paginationService, this._eventPubSubService, this.sharedService, this.translaterService);
1267-
this.slickPagination.renderPagination(this._gridParentContainerElm);
1265+
if (this.slickGrid && this._gridOptions?.enablePagination && !this._isPaginationInitialized && showPagination) {
1266+
this.slickPagination = new SlickPaginationComponent(this.slickGrid, this.paginationService, this._eventPubSubService, this.translaterService);
1267+
this.slickPagination.render(this._gridParentContainerElm);
12681268
this._isPaginationInitialized = true;
12691269
} else if (!showPagination) {
12701270
this.slickPagination?.dispose();

0 commit comments

Comments
 (0)