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
@@ -1,4 +1,4 @@
import { stateStoringModule } from '@ts/grids/grid_core/state_storing/m_state_storing';
import { stateStoringModule } from '@ts/grids/grid_core/state_storing/state_storing_module';

import gridCore from '../m_core';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import inflector from '@ts/core/utils/m_inflector';
import type { Column, ColumnsChanges, FilterField } from '@ts/grids/grid_core/columns_controller/types';
import type { DataController } from '@ts/grids/grid_core/data_controller/data_controller';
import type { FocusController } from '@ts/grids/grid_core/focus/m_focus';
import type { StateStoringController } from '@ts/grids/grid_core/state_storing/m_state_storing_core';
import type { StateStoringController } from '@ts/grids/grid_core/state_storing/m_state_storing_controller';

import { AI_COLUMN_NAME } from '../ai_column/const';
import modules from '../m_modules';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import type {
} from '@ts/grids/grid_core/m_types';
import gridCoreUtils from '@ts/grids/grid_core/m_utils';
import type { SelectionController } from '@ts/grids/grid_core/selection/m_selection';
import type { StateStoringController } from '@ts/grids/grid_core/state_storing/m_state_storing_core';
import type { VirtualScrollController } from '@ts/grids/grid_core/virtual_scrolling/m_virtual_scrolling_core';

import { DataHelperMixin } from './data_helper_mixin';
Expand Down Expand Up @@ -151,8 +150,6 @@ export class DataController extends DataHelperMixin(modules.Controller) {

protected _selectionController!: SelectionController;

protected _stateStoringController!: StateStoringController;

private loadErrorHandlerProxy!: (e: Error | string) => void;

private dataPushedHandlerProxy!: (changes: StoreChange[]) => void;
Expand All @@ -173,7 +170,6 @@ export class DataController extends DataHelperMixin(modules.Controller) {
this._focusController = this.getController('focus');
this._headerFilterController = this.getController('headerFilter');
this._selectionController = this.getController('selection');
this._stateStoringController = this.getController('stateStoring');

this._isPaging = false;
this._currentOperationTypes = null;
Expand Down Expand Up @@ -1305,7 +1301,7 @@ export class DataController extends DataHelperMixin(modules.Controller) {
/**
* @extended: state_storing
*/
public isLoading() {
public isLoading(): boolean {
return this._isLoading || this._isCustomLoading;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DataSource } from '@js/common/data/data_source/data_source';
import { normalizeDataSourceOptions } from '@js/common/data/data_source/utils';
import type { DeferredObj } from '@js/core/utils/deferred';
import { extend } from '@js/core/utils/extend';
import DataController from '@ts/ui/collection/m_data_controller';

Expand Down Expand Up @@ -49,9 +50,11 @@ export const DataHelperMixin = <T extends ModuleType<Controller>>(Base: T) => cl
/**
* @extended: state_storing, virtual_scrolling
*/
protected _refreshDataSource(): void {
protected _refreshDataSource(): DeferredObj<unknown> | undefined {
this._initDataSource();
this._loadDataSource();

return undefined;
}

protected _initDataSource(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export interface Controllers {
selection: import('./selection/m_selection').SelectionController;
validating: import('./validating/m_validating').ValidatingController;
searchPanel: import('./search/m_search').SearchPanelViewController;
stateStoring: import('./state_storing/m_state_storing_core').StateStoringController;
stateStoring: import('./state_storing/m_state_storing_controller').StateStoringController;
synchronizeScrolling: import('./views/m_grid_view').SynchronizeScrollingController;
tablePosition: import('./columns_resizing_reordering/m_columns_resizing_reordering').TablePositionViewController;
toastViewController: import('./toast/m_toast_controller').ToastViewController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { ColumnsController } from '@ts/grids/grid_core/columns_controller/m
import type { Column } from '@ts/grids/grid_core/columns_controller/types';
import type { ContextMenuController } from '@ts/grids/grid_core/context_menu/m_context_menu';
import type { ModuleType } from '@ts/grids/grid_core/m_types';
import type { StateStoringController } from '@ts/grids/grid_core/state_storing/m_state_storing_core';
import type { StateStoringController } from '@ts/grids/grid_core/state_storing/m_state_storing_controller';
import type { RowsView } from '@ts/grids/grid_core/views/m_rows_view';
import Selection from '@ts/ui/selection/selection';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import type { Callback } from '@js/core/utils/callbacks';
import type { DeferredObj } from '@js/core/utils/deferred';
import { Deferred } from '@js/core/utils/deferred';
import { isDefined } from '@js/core/utils/type';
import type { DataController } from '@ts/grids/grid_core/data_controller/data_controller';
import type { ModuleType } from '@ts/grids/grid_core/m_types';

import type { StateStoringController } from '../m_state_storing_controller';

export interface StateStoringDataControllerExtension {
stateLoaded: Callback<[]>;
}

export const stateStoringDataControllerExtender = (
Base: ModuleType<DataController>,
): ModuleType<
Comment thread
anna-shakhova marked this conversation as resolved.
DataController & StateStoringDataControllerExtension
> => class StateStoringDataExtender extends Base {
public stateLoaded!: Callback<[]>;

protected _stateStoringController!: StateStoringController;

private _restoreStateTimeoutID?: ReturnType<typeof setTimeout> | null;

public init(): void {
this._stateStoringController = this.getController('stateStoring');
super.init();
}

public dispose(): void {
clearTimeout(this._restoreStateTimeoutID ?? undefined);
super.dispose();
}

protected callbackNames(): string[] {
return super.callbackNames().concat(['stateLoaded']);
}

protected _refreshDataSource(): DeferredObj<unknown> | undefined {
if (this._stateStoringController.isEnabled() && !this._stateStoringController.isLoaded()) {
clearTimeout(this._restoreStateTimeoutID ?? undefined);

const deferred = Deferred();
// eslint-disable-next-line no-restricted-globals
this._restoreStateTimeoutID = setTimeout(() => {
this._stateStoringController.load()
.always(() => {
this._restoreStateTimeoutID = null;
})
.done(() => {
super._refreshDataSource();

this.stateLoaded.fire();
deferred.resolve();
})
.fail((error: Error) => {
this.stateLoaded.fire();
this.loadErrorHandler(error ?? 'Unknown error');
deferred.reject();
});
});

// @ts-expect-error promise() is typed as Promise but returns a Deferred-like value at runtime
return deferred.promise();
}

if (!this.isStateLoading()) {
super._refreshDataSource();
}

return undefined;
}

public isLoading(): boolean {
return super.isLoading() || this._stateStoringController.isLoading();
}

private isStateLoading(): boolean {
return isDefined(this._restoreStateTimeoutID);
}

public isLoaded(): boolean {
return super.isLoaded() && !this.isStateLoading();
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable max-classes-per-file */
import { equalByValue, getKeyHash } from '@js/core/utils/common';
import { Deferred } from '@js/core/utils/deferred';
import { extend } from '@js/core/utils/extend';
import { isDefined } from '@js/core/utils/type';

Expand All @@ -9,7 +8,8 @@ import type { DataController } from '../data_controller/data_controller';
import type { ModuleType } from '../m_types';
import type { SelectionController } from '../selection/m_selection';
import type { RowsView } from '../views/m_rows_view';
import { StateStoringController } from './m_state_storing_core';
import type { StateStoringDataControllerExtension } from './extenders/state_storing_data_controller';
import type { StateStoringController } from './m_state_storing_controller';

const getDataState = (that) => {
// TODO getView
Expand Down Expand Up @@ -91,11 +91,12 @@ const getFilterValue = (that, state) => {
return that._initialFilterValue ?? filterSyncController.getFilterValueFromColumns(columns);
};

const rowsView = (Base: ModuleType<RowsView>) => class StateStoringRowsViewExtender extends Base {
export const rowsView = (Base: ModuleType<RowsView>) => class StateStoringRowsViewExtender extends Base {
protected _dataController!: DataController & StateStoringDataControllerExtension;

public init() {
super.init();

// @ts-expect-error
this._dataController.stateLoaded.add(() => {
if (this._dataController.isLoaded() && !this._dataController.getDataSource()) {
this.setLoading(false);
Expand All @@ -109,7 +110,7 @@ const rowsView = (Base: ModuleType<RowsView>) => class StateStoringRowsViewExten
}
};

const stateStoring = (Base: ModuleType<StateStoringController>) => class StateStoringExtender extends Base {
export const stateStoring = (Base: ModuleType<StateStoringController>) => class StateStoringExtender extends Base {
private readonly _initialPageSize: any;

public init() {
Expand Down Expand Up @@ -209,7 +210,7 @@ const stateStoring = (Base: ModuleType<StateStoringController>) => class StateSt
}
};

const columns = (Base: ModuleType<ColumnsController>) => class StateStoringColumnsExtender extends Base {
export const columns = (Base: ModuleType<ColumnsController>) => class StateStoringColumnsExtender extends Base {
protected _shouldReturnVisibleColumns() {
// @ts-expect-error
const result = super._shouldReturnVisibleColumns.apply(this, arguments);
Expand All @@ -218,59 +219,7 @@ const columns = (Base: ModuleType<ColumnsController>) => class StateStoringColum
}
};

const data = (Base: ModuleType<DataController>) => class StateStoringDataExtender extends Base {
private _restoreStateTimeoutID: any;

public dispose() {
clearTimeout(this._restoreStateTimeoutID);
super.dispose();
}

protected callbackNames() {
return super.callbackNames().concat(['stateLoaded']);
}

protected _refreshDataSource() {
if (this._stateStoringController.isEnabled() && !this._stateStoringController.isLoaded()) {
clearTimeout(this._restoreStateTimeoutID);

// @ts-expect-error
const deferred = new Deferred();
this._restoreStateTimeoutID = setTimeout(() => {
this._stateStoringController.load().always(() => {
this._restoreStateTimeoutID = null;
}).done(() => {
super._refreshDataSource();
// @ts-expect-error
this.stateLoaded.fire();
deferred.resolve();
}).fail((error) => {
// @ts-expect-error
this.stateLoaded.fire();
this.loadErrorHandler(error || 'Unknown error');
deferred.reject();
});
});
return deferred.promise();
} if (!this.isStateLoading()) {
super._refreshDataSource();
}
}

public isLoading() {
return super.isLoading() || this._stateStoringController.isLoading();
}

private isStateLoading() {
return isDefined(this._restoreStateTimeoutID);
}

public isLoaded() {
return super.isLoaded() && !this.isStateLoading();
}
};

const selection = (Base: ModuleType<SelectionController>) => class StateStoringSelectionExtender extends Base {
export const selection = (Base: ModuleType<SelectionController>) => class StateStoringSelectionExtender extends Base {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected _fireSelectionChanged(options) {
const isDeferredSelection = this.option('selection.deferred');
Expand All @@ -281,32 +230,3 @@ const selection = (Base: ModuleType<SelectionController>) => class StateStoringS
super._fireSelectionChanged.apply(this, arguments);
}
};

export const stateStoringModule = {
defaultOptions() {
return {
stateStoring: {
enabled: false,
storageKey: undefined,
type: 'localStorage',
customLoad: undefined,
customSave: undefined,
savingTimeout: 2000,
},
};
},
controllers: {
stateStoring: StateStoringController,
},
extenders: {
views: {
rowsView,
},
controllers: {
stateStoring,
columns,
data,
selection,
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class StateStoringController extends modules.ViewController {

private _isLoaded: any;

private _isLoading: any;
private _isLoading!: boolean;

private _windowUnloadHandler: any;

Expand Down Expand Up @@ -145,7 +145,7 @@ export class StateStoringController extends modules.ViewController {
return this._isLoaded;
}

public isLoading() {
public isLoading(): boolean {
return this._isLoading;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { StateStoring } from '@js/common/grids';

import { stateStoringDataControllerExtender } from './extenders/state_storing_data_controller';
import {
columns,
rowsView,
selection,
stateStoring,
} from './m_state_storing';
import { StateStoringController } from './m_state_storing_controller';

export const stateStoringModule = {
defaultOptions(): { stateStoring: StateStoring } {
return {
stateStoring: {
enabled: false,
storageKey: undefined,
type: 'localStorage',
customLoad: undefined,
customSave: undefined,
savingTimeout: 2000,
},
};
},
controllers: {
stateStoring: StateStoringController,
},
extenders: {
views: {
rowsView,
},
controllers: {
stateStoring,
columns,
data: stateStoringDataControllerExtender,
selection,
},
},
};
Loading
Loading