Skip to content

Commit 5d430a6

Browse files
committed
refactor: split pagination service responsibilities
1 parent 8fcc7ed commit 5d430a6

File tree

7 files changed

+35
-22
lines changed

7 files changed

+35
-22
lines changed

packages/pluggableWidgets/datagrid-web/src/Datagrid.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@
301301
<enumerationValue key="both">Both</enumerationValue>
302302
</enumerationValues>
303303
</property>
304+
<property key="dynamicPageSize" type="attribute" required="false">
305+
<caption>Dynamic page size attribute</caption>
306+
<description>Optional attribute to set the page size dynamically. The attribute requires Integer type.</description>
307+
<attributeTypes>
308+
<attributeType name="Integer" />
309+
</attributeTypes>
310+
</property>
304311
<property key="loadMoreButtonCaption" type="textTemplate" required="false">
305312
<caption>Load more caption</caption>
306313
<description />

packages/pluggableWidgets/datagrid-web/src/model/configs/Datagrid.config.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export interface DatagridConfig {
2828
columnsResizable: boolean;
2929
columnsSortable: boolean;
3030
isInteractive: boolean;
31+
dynamicPageSizeEnabled: boolean;
32+
requestTotalCount: boolean;
33+
constPageSize: number;
3134
}
3235

3336
export function datagridConfig(props: DatagridContainerProps): DatagridConfig {
@@ -56,7 +59,10 @@ export function datagridConfig(props: DatagridContainerProps): DatagridConfig {
5659
columnsFilterable: props.columnsFilterable,
5760
columnsResizable: props.columnsResizable,
5861
columnsSortable: props.columnsSortable,
59-
isInteractive: isInteractive(props)
62+
isInteractive: isInteractive(props),
63+
dynamicPageSizeEnabled: dynamicPageSizeEnabled(props),
64+
requestTotalCount: requestTotalCount(props),
65+
constPageSize: props.pageSize
6066
};
6167

6268
return Object.freeze(config);
@@ -93,3 +99,11 @@ function selectionType(props: DatagridContainerProps): SelectionType {
9399
function selectionMethod(props: DatagridContainerProps): SelectionMethod {
94100
return props.itemSelection ? props.itemSelectionMethod : "none";
95101
}
102+
103+
function dynamicPageSizeEnabled(props: DatagridContainerProps): boolean {
104+
return props.dynamicPageSize !== undefined;
105+
}
106+
107+
function requestTotalCount(props: DatagridContainerProps): boolean {
108+
return props.pagination === "buttons" || props.showNumberOfRows;
109+
}

packages/pluggableWidgets/datagrid-web/src/model/containers/Datagrid.container.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ import { gridStyleAtom } from "../models/grid.model";
2929
import { rowClassProvider } from "../models/rows.model";
3030
import { DatasourceParamsController } from "../services/DatasourceParamsController";
3131
import { DerivedLoaderController } from "../services/DerivedLoaderController";
32-
import { PaginationController } from "../services/PaginationController";
32+
import { PaginationService } from "../services/Pagination.service";
3333
import { SelectionGate } from "../services/SelectionGate.service";
3434
import { CORE_TOKENS as CORE, DG_TOKENS as DG, SA_TOKENS } from "../tokens";
3535

3636
// base
3737
injected(ColumnGroupStore, CORE.setupService, CORE.mainGate, CORE.config, DG.filterHost);
3838
injected(DatasourceParamsController, CORE.setupService, DG.query, DG.combinedFilter, CORE.columnsStore);
3939
injected(DatasourceService, CORE.setupService, DG.queryGate, DG.refreshInterval.optional);
40-
injected(PaginationController, CORE.setupService, DG.paginationConfig, DG.query);
40+
injected(PaginationService, DG.paginationConfig, DG.query);
4141
injected(GridBasicData, CORE.mainGate);
4242
injected(WidgetRootViewModel, CORE.mainGate, CORE.config, DG.exportProgressService, SA_TOKENS.selectionDialogVM);
4343

@@ -97,7 +97,7 @@ export class DatagridContainer extends Container {
9797
// Query service
9898
this.bind(DG.query).toInstance(DatasourceService).inSingletonScope();
9999
// Pagination service
100-
this.bind(DG.paginationService).toInstance(PaginationController).inSingletonScope();
100+
this.bind(DG.paginationService).toInstance(PaginationService).inSingletonScope();
101101
// Datasource params service
102102
this.bind(DG.paramsService).toInstance(DatasourceParamsController).inSingletonScope();
103103
// FilterAPI
@@ -213,7 +213,10 @@ export class DatagridContainer extends Container {
213213
private postInit(props: MainGateProps, config: DatagridConfig): void {
214214
// Make sure essential services are created upfront
215215
this.get(DG.paramsService);
216-
this.get(DG.paginationService);
216+
217+
const query = this.get(DG.query);
218+
query.requestTotalCount(config.requestTotalCount);
219+
query.setBaseLimit(config.constPageSize);
217220

218221
if (config.settingsStorageEnabled) {
219222
this.get(DG.personalizationService);

packages/pluggableWidgets/datagrid-web/src/model/services/PaginationController.ts renamed to packages/pluggableWidgets/datagrid-web/src/model/services/Pagination.service.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { QueryService } from "@mendix/widget-plugin-grid/main";
2-
import { SetupComponent, SetupComponentHost } from "@mendix/widget-plugin-mobx-kit/main";
32
import { PaginationEnum, ShowPagingButtonsEnum } from "../../../typings/DatagridProps";
43

54
export interface PaginationConfig {
@@ -11,21 +10,18 @@ export interface PaginationConfig {
1110

1211
type PaginationKind = `${PaginationEnum}.${ShowPagingButtonsEnum}`;
1312

14-
export class PaginationController implements SetupComponent {
13+
export class PaginationService {
1514
readonly pagination: PaginationEnum;
1615
readonly paginationKind: PaginationKind;
1716
readonly showPagingButtons: ShowPagingButtonsEnum;
1817

1918
constructor(
20-
host: SetupComponentHost,
2119
private config: PaginationConfig,
2220
private query: QueryService
2321
) {
24-
host.add(this);
2522
this.pagination = config.pagination;
2623
this.paginationKind = `${this.pagination}.${config.showPagingButtons}`;
2724
this.showPagingButtons = config.showPagingButtons;
28-
this.setInitParams();
2925
}
3026

3127
get isLimitBased(): boolean {
@@ -65,16 +61,6 @@ export class PaginationController implements SetupComponent {
6561
return this.query.totalCount;
6662
}
6763

68-
private setInitParams(): void {
69-
if (this.pagination === "buttons" || this.config.showNumberOfRows) {
70-
this.query.requestTotalCount(true);
71-
}
72-
73-
this.query.setBaseLimit(this.pageSize);
74-
}
75-
76-
setup(): void {}
77-
7864
setPage = (computePage: ((prevPage: number) => number) | number): void => {
7965
const newPage = typeof computePage === "function" ? computePage(this.currentPage) : computePage;
8066
if (this.isLimitBased) {

packages/pluggableWidgets/datagrid-web/src/model/tokens.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { DatagridConfig } from "./configs/Datagrid.config";
3939
import { RowClassProvider } from "./models/rows.model";
4040
import { DatagridSetupService } from "./services/DatagridSetup.service";
4141
import { DerivedLoaderController, DerivedLoaderControllerConfig } from "./services/DerivedLoaderController";
42-
import { PaginationConfig, PaginationController } from "./services/PaginationController";
42+
import { PaginationConfig, PaginationService } from "./services/Pagination.service";
4343
import { TextsService } from "./services/Texts.service";
4444
import { PageSizeStore } from "./stores/PageSize.store";
4545

@@ -102,7 +102,7 @@ export const DG_TOKENS = {
102102
loaderVM: token<DerivedLoaderController>("DatagridLoaderViewModel"),
103103

104104
paginationConfig: token<PaginationConfig>("PaginationConfig"),
105-
paginationService: token<PaginationController>("PaginationService"),
105+
paginationService: token<PaginationService>("PaginationService"),
106106

107107
parentChannelName: token<string>("parentChannelName"),
108108
refreshInterval: token<number>("refreshInterval"),

packages/pluggableWidgets/datagrid-web/typings/DatagridProps.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export interface DatagridContainerProps {
120120
showPagingButtons: ShowPagingButtonsEnum;
121121
showNumberOfRows: boolean;
122122
pagingPosition: PagingPositionEnum;
123+
dynamicPageSize?: EditableValue<Big>;
123124
loadMoreButtonCaption?: DynamicValue<string>;
124125
showEmptyPlaceholder: ShowEmptyPlaceholderEnum;
125126
emptyPlaceholder?: ReactNode;
@@ -179,6 +180,7 @@ export interface DatagridPreviewProps {
179180
showPagingButtons: ShowPagingButtonsEnum;
180181
showNumberOfRows: boolean;
181182
pagingPosition: PagingPositionEnum;
183+
dynamicPageSize: string;
182184
loadMoreButtonCaption: string;
183185
showEmptyPlaceholder: ShowEmptyPlaceholderEnum;
184186
emptyPlaceholder: { widgetCount: number; renderer: ComponentType<{ children: ReactNode; caption?: string }> };

packages/pluggableWidgets/datagrid-web/typings/MainGateProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ export type MainGateProps = Pick<
3737
| "showPagingButtons"
3838
| "storeFiltersInPersonalization"
3939
| "style"
40+
| "dynamicPageSize"
4041
>;

0 commit comments

Comments
 (0)