Skip to content

Commit

Permalink
DataController
Browse files Browse the repository at this point in the history
  • Loading branch information
Raushen committed Dec 24, 2024
1 parent 41a9fa9 commit e3b3d54
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable no-param-reassign */
/* eslint-disable spellcheck/spell-checker */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { Subscribable } from '@ts/core/reactive/index';
import type { SubsGets } from '@ts/core/reactive/index';
import {
computed, effect, state,
} from '@ts/core/reactive/index';

import gridCoreUtils from '../../../grid_core/m_utils';
// import { EditingController } from '../editing/controller';
// import type { Change } from '../editing/types';
import { OptionsController } from '../options_controller/options_controller';
Expand All @@ -22,17 +23,33 @@ export class DataController {
[this.dataSourceConfiguration, this.keyExpr],
);

public readonly cacheEnabled = this.options.oneWay('cacheEnabled');

public readonly pagingEnabled = this.options.twoWay('paging.enabled');

public readonly pageIndex = this.options.twoWay('paging.pageIndex');

public readonly pageSize = this.options.twoWay('paging.pageSize');

public readonly remoteFiltering = this.options.oneWay('remoteOperations.filtering');

public readonly remotePaging = this.options.oneWay('remoteOperations.paging');

public readonly remoteSorting = this.options.oneWay('remoteOperations.sorting');

public readonly remoteSummary = this.options.oneWay('remoteOperations.summary');

public readonly dateSerializationFormat = this.options.oneWay('dateSerializationFormat');

public readonly onDataErrorOccurred = this.options.oneWay('onDataErrorOccurred');

private readonly _items = state<DataObject[]>([]);

public readonly items: Subscribable<DataObject[]> = this._items;
public readonly items: SubsGets<DataObject[]> = this._items;

private readonly _totalCount = state(0);

public readonly totalCount: Subscribable<number> = this._totalCount;
public readonly totalCount: SubsGets<number> = this._totalCount;

public readonly isLoading = state(false);

Expand Down Expand Up @@ -124,4 +141,8 @@ export class DataController {
public getDataKey(data: unknown): unknown {
return this.dataSource.unreactive_get().store().keyOf(data);
}

public getCardIndexByKey(key: object): number {
return gridCoreUtils.getIndexByKey(key, this.items);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import type { DataSourceLike } from '@js/data/data_source';

interface PagingOptions {
enabled?: boolean;
pageSize?: number;

pageIndex?: number;
}

export interface Options {
paging?: PagingOptions;
interface RemoteOperationsOptions {
filtering?: boolean;
paging?: boolean;
sorting?: boolean;
summary?: boolean;
}

export interface Options {
cacheEnabled?: boolean;
dataSource?: DataSourceLike<unknown>;

dateSerializationFormat?: string;
keyExpr?: string | string[];
onDataErrorOccurred?: unknown;
paging?: PagingOptions;
remoteOperations?: RemoteOperationsOptions | boolean;
}

export const defaultOptions = {
paging: {
enabled: true,
pageSize: 6,
pageIndex: 0,
},
remoteOperations: {
filtering: false,
paging: false,
sorting: false,
summary: false,
},
} satisfies Options;
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,63 @@ import type DataSource from '@js/data/data_source';
import type { Constructor } from '../types';
import type { GridCoreNewBase } from '../widget';
import { DataController } from './data_controller';
import { Deferred } from '@js/core/utils/deferred';
import { FilterDescriptor } from '@js/data';

export function PublicMethods<T extends Constructor<GridCoreNewBase>>(GridCore: T) {
return class GridCoreWithDataController extends GridCore {
public getDataSource(): DataSource {
return this.diContext.get(DataController).dataSource.unreactive_get();
}

public byKey(key: object): Promise<object> | undefined {
const store = this.getDataSource().store();
const cardIndex = this.diContext.get(DataController).getCardIndexByKey(key);
let result = undefined;

if (!store) return;

if (cardIndex >= 0) {
// @ts-expect-error
result = new Deferred().resolve(this.getDataSource().items()[cardIndex].data);
}

return result || store.byKey(key);
}

public getFilter(): FilterDescriptor | Array<FilterDescriptor> {
return this.getDataSource().filter();
}

public keyOf(obj: object) {
return this.diContext.get(DataController).getDataKey(obj);
}

public pageCount(): number {
return this.diContext.get(DataController).pageCount.unreactive_get();
}

public pageSize(): number;
public pageSize(value: number):void;
public pageSize(value?: number):number | void {
if(value === undefined) {
return this.diContext.get(DataController).pageSize.unreactive_get();
}
this.diContext.get(DataController).pageSize.update(value);
}

public pageIndex(): number;
public pageIndex(newIndex: number): void;
public pageIndex(newIndex?: number): number | void {
if(newIndex === undefined) {
return this.diContext.get(DataController).pageIndex.unreactive_get();
}
// TODO: Promise<void> (jQuery or native)
return this.diContext.get(DataController).pageIndex.update(newIndex);
}

public totalCount(): number {
return this.diContext.get(DataController).totalCount.unreactive_get();
}
};
}

0 comments on commit e3b3d54

Please sign in to comment.