Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class AIAssistantIntegrationController extends Controller {
paging: {
pageIndex: this.dataController.pageIndex(),
pageSize: this.dataController.pageSize(),
totalCount: this.dataController.totalCount(),
totalCount: this.dataSourceController.totalCount(),
visibleRowCount: this.dataController
.getVisibleRows()
.filter((row) => row.rowType === 'data')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ export const pageIndexCommand = defineGridCommand({
schema: pageIndexCommandSchema,
execute: (component, { success, failure }) => async (args): Promise<CommandResult> => {
const paging = component.option('paging');
const dataController = component.getController('data');
const dataSourceController = component.getController('dataSource');
const defaultMessage = `Switch the view to page number ${args.pageIndex + 1}.`;

const isIndexValid = args.pageIndex < dataController.pageCount();
const isIndexValid = args.pageIndex < dataSourceController.pageCount();

if (paging?.enabled === false || !isIndexValid) {
return failure(defaultMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,10 @@ export class DataController extends modules.Controller {
'getKeyByRowIndex',
'getRowIndexByKey',
'getVisibleRows',
'pageCount',
'pageIndex',
'pageSize',
'refresh',
'repaintRows',
'totalCount',
];
}

Expand Down Expand Up @@ -1342,10 +1340,6 @@ export class DataController extends modules.Controller {
return !this.items().length;
}

public pageCount(): number {
return this._dataSource ? this._dataSource.pageCount() : 1;
}

public loadAllItems(
data?: RawItemData[],
skipFilter = false,
Expand Down Expand Up @@ -1612,7 +1606,7 @@ export class DataController extends modules.Controller {
*/
public isLastPageLoaded(): boolean {
const pageIndex = this.pageIndex();
const pageCount = this.pageCount();
const pageCount = this.dataSourceController.pageCount();
return pageIndex === (pageCount - 1);
}

Expand All @@ -1632,29 +1626,13 @@ export class DataController extends modules.Controller {
this._dataSource?.push(changes, fromStore);
}

private itemsCount(): number {
return (this._dataSource ? this._dataSource.itemsCount() : 0);
}

public totalItemsCount(): number {
return (this._dataSource ? this._dataSource.totalItemsCount() : 0);
}

public hasKnownLastPage(): boolean {
return (this._dataSource ? this._dataSource.hasKnownLastPage() : true);
}

/**
* @extended: state_storing
*/
public isLoaded(): boolean {
return (this._dataSource ? this._dataSource.isLoaded() : true);
}

public totalCount(): number {
return (this._dataSource ? this._dataSource.totalCount() : 0);
}

public hasLoadOperation(): boolean {
const operationTypes = this._dataSource?.operationTypes() ?? {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ describe('dataSource module registration', () => {
.toBe(instance.getController('dataSource').keyOf(DATA[1]));
});

it('owns the totalCount widget method', async () => {
const { instance } = await createDataGrid({ dataSource: DATA });

expect(instance.totalCount()).toBe(DATA.length);
expect(instance.totalCount())
.toBe(instance.getController('dataSource').totalCount());
});

it('owns the pageCount widget method', async () => {
const { instance } = await createDataGrid({ dataSource: DATA, paging: { pageSize: 1 } });

expect(instance.pageCount()).toBe(DATA.length);
expect(instance.pageCount())
.toBe(instance.getController('dataSource').pageCount());
});

it('sits at the bottom of the controller order', async () => {
const { instance } = await createDataGrid({ dataSource: DATA });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface AdapterStub {
key: jest.Mock<() => StoreKey | undefined>;
remoteOperations: jest.Mock<() => RemoteOperationsOptions>;
getDataIndexGetter: jest.Mock<() => (data: RawItemData) => number>;
hasKnownLastPage: jest.Mock<() => boolean>;
totalItemsCount: jest.Mock<() => number>;
totalCount: jest.Mock<() => number>;
pageCount: jest.Mock<() => number>;
dispose: jest.Mock<(isShared?: boolean) => void>;
init: jest.Mock<(dataSource: DataSource) => void>;
}
Expand All @@ -38,6 +42,10 @@ const createAdapterStub = (marker: string): AdapterStub => ({
key: jest.fn(() => marker as StoreKey),
remoteOperations: jest.fn(() => ({ filtering: true } as RemoteOperationsOptions)),
getDataIndexGetter: jest.fn(() => (): number => 0),
hasKnownLastPage: jest.fn(() => false),
totalItemsCount: jest.fn(() => 42),
totalCount: jest.fn(() => 99),
pageCount: jest.fn(() => 7),
dispose: jest.fn(),
init: jest.fn(),
});
Expand Down Expand Up @@ -164,6 +172,19 @@ describe('DataSourceController', () => {
expect(createController().getDataIndexGetter()).toBeUndefined();
});

it('reports the last page as known', () => {
expect(createController().hasKnownLastPage()).toBe(true);
});

it('counts no items', () => {
expect(createController().totalItemsCount()).toBe(0);
expect(createController().totalCount()).toBe(0);
});

it('reports a single page', () => {
expect(createController().pageCount()).toBe(1);
});

it('returns an empty object from remoteOperations, so callers can enumerate it', () => {
const controller = createController();

Expand Down Expand Up @@ -209,6 +230,34 @@ describe('DataSourceController', () => {
expect(adapter.getDataIndexGetter).toHaveBeenCalledTimes(1);
});

it('delegates hasKnownLastPage to the adapter', () => {
const { controller, adapter } = withAdapter();

expect(controller.hasKnownLastPage()).toBe(false);
expect(adapter.hasKnownLastPage).toHaveBeenCalledTimes(1);
});

it('delegates totalItemsCount to the adapter', () => {
const { controller, adapter } = withAdapter();

expect(controller.totalItemsCount()).toBe(42);
expect(adapter.totalItemsCount).toHaveBeenCalledTimes(1);
});

it('delegates totalCount to the adapter', () => {
const { controller, adapter } = withAdapter();

expect(controller.totalCount()).toBe(99);
expect(adapter.totalCount).toHaveBeenCalledTimes(1);
});

it('delegates pageCount to the adapter', () => {
const { controller, adapter } = withAdapter();

expect(controller.pageCount()).toBe(7);
expect(adapter.pageCount).toHaveBeenCalledTimes(1);
});

it('returns the inner DataSource from getDataSource, not the adapter', () => {
const { controller, adapter } = withAdapter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class DataSourceController<
}

public publicMethods(): string[] {
return ['getDataSource', 'keyOf'];
return ['getDataSource', 'keyOf', 'pageCount', 'totalCount'];
}

/**
Expand Down Expand Up @@ -147,4 +147,20 @@ export class DataSourceController<
public getCachedStoreData(): RawItemData[] | undefined {
return this.adapter?.getCachedStoreData();
}

public hasKnownLastPage(): boolean {
return this.adapter ? this.adapter.hasKnownLastPage() : true;
}

public totalItemsCount(): number {
return this.adapter ? this.adapter.totalItemsCount() : 0;
}

public totalCount(): number {
return this.adapter ? this.adapter.totalCount() : 0;
}

public pageCount(): number {
return this.adapter ? this.adapter.pageCount() : 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ class EditingControllerImpl extends modules.ViewController {
const newRowPosition: any = this._getNewRowPosition();
const dataController = this._dataController;
const pageIndex = dataController.pageIndex();
const lastPageIndex = dataController.pageCount() - 1;
const lastPageIndex = this.dataSourceController.pageCount() - 1;

if (newRowPosition === FIRST_NEW_ROW_POSITION && pageIndex !== 0) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export class FocusController extends core.ViewController {
const offset = rowsScrollController.getItemOffset(focusedRowIndex);

const triggerUpdateFocusedRow = () => {
if (this.getDataController().totalCount() && !this.getDataController().items().length) {
if (this.getDataSourceController().totalCount() && !this.getDataController().items().length) {
return;
}
this.component.off('contentReady', triggerUpdateFocusedRow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ export class KeyboardNavigationController extends KeyboardNavigationControllerCo

private _pageUpDownKeyHandler(eventArgs) {
const pageIndex = this._dataController.pageIndex();
const pageCount = this._dataController.pageCount();
const pageCount = this.dataSourceController.pageCount();
const pagingEnabled = this.option('paging.enabled');
const isPageUp = eventArgs.keyName === 'pageUp';
const pageStep = isPageUp ? -1 : 1;
Expand Down Expand Up @@ -1463,7 +1463,7 @@ export class KeyboardNavigationController extends KeyboardNavigationControllerCo

private getFirstOrLastRowIndex(needFirstRow: boolean): number {
const rowCount = this._isVirtualScrolling()
? this._dataController.totalItemsCount()
? this.dataSourceController.totalItemsCount()
: this._dataController.items(true)?.length;

return needFirstRow ? 0 : rowCount - 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
afterEach, beforeEach, describe, expect, it,
} from '@jest/globals';

import type { DataGridInstance } from '../../__tests__/__mock__/helpers/utils';
import {
afterTest,
beforeTest,
createDataGrid,
} from '../../__tests__/__mock__/helpers/utils';

const ITEMS = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }];

interface Pagination {
option: (name: string) => unknown;
}

const getPagination = (instance: DataGridInstance): Pagination => (
instance.getView('pagerView') as unknown as { getPager: () => Pagination }
).getPager();

const isPagerVisible = (instance: DataGridInstance): boolean => instance
.getView('pagerView')
.isVisible();

describe('PagerView', () => {
beforeEach(beforeTest);
afterEach(afterTest);

describe('without a data source', () => {
it('reports one page and no items', async () => {
const { instance } = await createDataGrid({
pager: { visible: true },
});

expect(getPagination(instance).option('pageCount')).toBe(1);
expect(getPagination(instance).option('itemCount')).toBe(0);
expect(getPagination(instance).option('hasKnownLastPage')).toBe(true);
});

it('is hidden in auto mode', async () => {
const { instance } = await createDataGrid({
pager: { visible: 'auto' },
});

expect(isPagerVisible(instance)).toBe(false);
});
});

describe('with a data source', () => {
it('reports the page count and the item count', async () => {
const { instance } = await createDataGrid({
dataSource: ITEMS,
paging: { pageSize: 2 },
pager: { visible: true },
});

expect(getPagination(instance).option('pageCount')).toBe(3);
expect(getPagination(instance).option('itemCount')).toBe(5);
expect(getPagination(instance).option('hasKnownLastPage')).toBe(true);
});

it('is visible in auto mode when there is more than one page', async () => {
const { instance } = await createDataGrid({
dataSource: ITEMS,
paging: { pageSize: 2 },
pager: { visible: 'auto' },
});

expect(isPagerVisible(instance)).toBe(true);
});

it('is hidden in auto mode when a single page holds every item', async () => {
const { instance } = await createDataGrid({
dataSource: ITEMS,
paging: { pageSize: 10 },
pager: { visible: 'auto' },
});

expect(isPagerVisible(instance)).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import messageLocalization from '@js/common/core/localization/message';
import { isDefined } from '@js/core/utils/type';
import { hasWindow } from '@js/core/utils/window';
import type { DataSourceController } from '@ts/grids/grid_core/data_source/data_source_controller';
import Pagination from '@ts/pagination/wrappers/pagination';

import modules from '../m_modules';
Expand All @@ -19,19 +20,23 @@ export class PagerView extends modules.View {

private _pageSizes: any;

private dataSourceController!: DataSourceController;
Comment thread
anna-shakhova marked this conversation as resolved.

public init() {
const dataController = this.getController('data');

this.dataSourceController = this.getController('dataSource');
Comment thread
anna-shakhova marked this conversation as resolved.

dataController.changed.add((e) => {
if (e && e.repaintChangesOnly) {
const pager = this._pager;
if (pager) {
pager.option({
pageIndex: getPageIndex(dataController),
pageSize: dataController.pageSize(),
pageCount: dataController.pageCount(),
itemCount: dataController.totalCount(),
hasKnownLastPage: dataController.hasKnownLastPage(),
pageCount: this.dataSourceController.pageCount(),
itemCount: this.dataSourceController.totalCount(),
hasKnownLastPage: this.dataSourceController.hasKnownLastPage(),
Comment thread
anna-shakhova marked this conversation as resolved.
});
} else {
this.render();
Expand Down Expand Up @@ -86,7 +91,7 @@ export class PagerView extends modules.View {
const options: any = {
maxPagesCount: MAX_PAGES_COUNT,
pageIndex: getPageIndex(dataController),
pageCount: dataController.pageCount(),
pageCount: that.dataSourceController.pageCount(),
pageSize: dataController.pageSize(),
showPageSizeSelector: pagerOptions.showPageSizeSelector,
showInfo: pagerOptions.showInfo,
Expand All @@ -95,8 +100,8 @@ export class PagerView extends modules.View {
showNavigationButtons: pagerOptions.showNavigationButtons,
label: pagerOptions.label,
allowedPageSizes: that.getPageSizes(),
itemCount: dataController.totalCount(),
hasKnownLastPage: dataController.hasKnownLastPage(),
itemCount: that.dataSourceController.totalCount(),
hasKnownLastPage: that.dataSourceController.hasKnownLastPage(),
rtlEnabled: that.option('rtlEnabled'),
isGridCompatibilityMode: true,
_getParentComponentRootNode: () => this.component.element(),
Expand Down Expand Up @@ -164,7 +169,8 @@ export class PagerView extends modules.View {
if (scrolling && (scrolling.mode === 'virtual' || scrolling.mode === 'infinite')) {
pagerVisible = false;
} else {
pagerVisible = dataController.pageCount() > 1 || (dataController.isLoaded() && !dataController.hasKnownLastPage());
pagerVisible = this.dataSourceController.pageCount() > 1
|| (dataController.isLoaded() && !this.dataSourceController.hasKnownLastPage());
}
}
return !!pagerVisible;
Expand Down
Loading
Loading