Skip to content

Commit

Permalink
GridCore: native classes - dataSourceAdapter (DevExpress#26701)
Browse files Browse the repository at this point in the history
Co-authored-by: Alyar <>
  • Loading branch information
Alyar666 authored Feb 19, 2024
1 parent f15048f commit 35482e7
Show file tree
Hide file tree
Showing 11 changed files with 2,272 additions and 1,984 deletions.
271 changes: 145 additions & 126 deletions packages/devextreme/js/__internal/grids/data_grid/grouping/m_grouping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { getHeight } from '@js/core/utils/size';
import { isDefined, isString } from '@js/core/utils/type';
import messageLocalization from '@js/localization/message';
import { restoreFocus, setTabIndex } from '@js/ui/shared/accessibility';
import type DataSourceAdapter from '@ts/grids/grid_core/data_source_adapter/m_data_source_adapter';
import { registerKeyboardAction } from '@ts/grids/grid_core/m_accessibility';
import type { ModuleType } from '@ts/grids/grid_core/m_types';

import gridCore from '../m_core';
import dataSourceAdapter from '../m_data_source_adapter';
import dataSourceAdapterProvider from '../m_data_source_adapter';
import { GroupingHelper as CollapsedGroupingHelper } from './m_grouping_collapsed';
import { GroupingHelper as ExpandedGroupingHelper } from './m_grouping_expanded';

Expand All @@ -28,148 +30,165 @@ export interface GroupingDataControllerExtension {
changeRowExpand(key, isRowClick?): any;
}

const GroupingDataSourceAdapterExtender = (function () {
return {
init() {
this.callBase.apply(this, arguments);
this._initGroupingHelper();
},
_initGroupingHelper(options) {
const grouping = this._grouping;
const isAutoExpandAll = this.option('grouping.autoExpandAll');
const isFocusedRowEnabled = this.option('focusedRowEnabled');
const remoteOperations = options ? options.remoteOperations : this.remoteOperations();
const isODataRemoteOperations = remoteOperations.filtering && remoteOperations.sorting && remoteOperations.paging;

if (isODataRemoteOperations && !remoteOperations.grouping && (isAutoExpandAll || !isFocusedRowEnabled)) {
if (!grouping || grouping instanceof CollapsedGroupingHelper) {
this._grouping = new ExpandedGroupingHelper(this);
}
} else if (!grouping || grouping instanceof ExpandedGroupingHelper) {
this._grouping = new CollapsedGroupingHelper(this);
}
},
totalItemsCount() {
const that = this;
const totalCount = that.callBase();
const dataSourceAdapterExtender = (Base: ModuleType<DataSourceAdapter>) => class GroupingDataSourceAdapterExtender extends Base {
_grouping: any;

return totalCount > 0 && that._dataSource.group() && that._dataSource.requireTotalCount() ? totalCount + that._grouping.totalCountCorrection() : totalCount;
},
itemsCount() {
return this._dataSource.group() ? this._grouping.itemsCount() || 0 : this.callBase.apply(this, arguments);
},
allowCollapseAll() {
return this._grouping.allowCollapseAll();
},
isGroupItemCountable(item) {
return this._grouping.isGroupItemCountable(item);
},
isRowExpanded(key) {
const groupInfo = this._grouping.findGroupInfo(key);
return groupInfo ? groupInfo.isExpanded : !this._grouping.allowCollapseAll();
},
collapseAll(groupIndex) {
return this._collapseExpandAll(groupIndex, false);
},
expandAll(groupIndex) {
return this._collapseExpandAll(groupIndex, true);
},
_collapseExpandAll(groupIndex, isExpand) {
const that = this;
const dataSource = that._dataSource;
const group = dataSource.group();
const groups = gridCore.normalizeSortingInfo(group || []);

if (groups.length) {
for (let i = 0; i < groups.length; i++) {
if (groupIndex === undefined || groupIndex === i) {
groups[i].isExpanded = isExpand;
} else if (group && group[i]) {
groups[i].isExpanded = group[i].isExpanded;
}
}
dataSource.group(groups);
that._grouping.foreachGroups((groupInfo, parents) => {
if (groupIndex === undefined || groupIndex === parents.length - 1) {
groupInfo.isExpanded = isExpand;
}
}, false, true);
init() {
super.init.apply(this, arguments as any);
this._initGroupingHelper();
}

that.resetPagesCache();
_initGroupingHelper(options?) {
const grouping = this._grouping;
const isAutoExpandAll = this.option('grouping.autoExpandAll');
const isFocusedRowEnabled = this.option('focusedRowEnabled');
const remoteOperations = options ? options.remoteOperations : this.remoteOperations();
const isODataRemoteOperations = remoteOperations.filtering && remoteOperations.sorting && remoteOperations.paging;

if (isODataRemoteOperations && !remoteOperations.grouping && (isAutoExpandAll || !isFocusedRowEnabled)) {
if (!grouping || grouping instanceof CollapsedGroupingHelper) {
this._grouping = new ExpandedGroupingHelper(this);
}
return true;
},
refresh() {
this.callBase.apply(this, arguments);
} else if (!grouping || grouping instanceof ExpandedGroupingHelper) {
this._grouping = new CollapsedGroupingHelper(this);
}
}

return this._grouping.refresh.apply(this._grouping, arguments);
},
changeRowExpand(path) {
const that = this;
const dataSource = that._dataSource;
totalItemsCount() {
const totalCount = super.totalItemsCount();

if (dataSource.group()) {
dataSource.beginLoading();
if (that._lastLoadOptions) {
that._lastLoadOptions.groupExpand = true;
return totalCount > 0 && this._dataSource.group() && this._dataSource.requireTotalCount() ? totalCount + this._grouping.totalCountCorrection() : totalCount;
}

itemsCount() {
return this._dataSource.group() ? this._grouping.itemsCount() || 0 : super.itemsCount.apply(this, arguments as any);
}

allowCollapseAll() {
return this._grouping.allowCollapseAll();
}

isGroupItemCountable(item) {
return this._grouping.isGroupItemCountable(item);
}

isRowExpanded(key) {
const groupInfo = this._grouping.findGroupInfo(key);
return groupInfo ? groupInfo.isExpanded : !this._grouping.allowCollapseAll();
}

collapseAll(groupIndex) {
return this._collapseExpandAll(groupIndex, false);
}

expandAll(groupIndex) {
return this._collapseExpandAll(groupIndex, true);
}

_collapseExpandAll(groupIndex, isExpand) {
const that = this;
const dataSource = that._dataSource;
const group = dataSource.group();
const groups = gridCore.normalizeSortingInfo(group || []);

if (groups.length) {
for (let i = 0; i < groups.length; i++) {
if (groupIndex === undefined || groupIndex === i) {
groups[i].isExpanded = isExpand;
} else if (group && group[i]) {
groups[i].isExpanded = group[i].isExpanded;
}
return that._changeRowExpandCore(path).always(() => {
dataSource.endLoading();
});
}
},
_changeRowExpandCore(path) {
return this._grouping.changeRowExpand(path);
},
/// #DEBUG
getGroupsInfo() {
return this._grouping._groupsInfo;
},
/// #ENDDEBUG
// @ts-expect-error
_hasGroupLevelsExpandState(group, isExpanded) {
if (group && Array.isArray(group)) {
for (let i = 0; i < group.length; i++) {
if (group[i].isExpanded === isExpanded) {
return true;
}
dataSource.group(groups);
that._grouping.foreachGroups((groupInfo, parents) => {
if (groupIndex === undefined || groupIndex === parents.length - 1) {
groupInfo.isExpanded = isExpand;
}
}, false, true);

that.resetPagesCache();
}
return true;
}

refresh() {
super.refresh.apply(this, arguments as any);

return this._grouping.refresh.apply(this._grouping, arguments);
}

changeRowExpand(path) {
const that = this;
const dataSource = that._dataSource;

if (dataSource.group()) {
dataSource.beginLoading();
if (that._lastLoadOptions) {
that._lastLoadOptions.groupExpand = true;
}
},
_customizeRemoteOperations(options, operationTypes) {
const { remoteOperations } = options;
return that._changeRowExpandCore(path).always(() => {
dataSource.endLoading();
});
}
}

if (options.storeLoadOptions.group) {
if (remoteOperations.grouping && !options.isCustomLoading) {
if (!remoteOperations.groupPaging || this._hasGroupLevelsExpandState(options.storeLoadOptions.group, true)) {
remoteOperations.paging = false;
}
_changeRowExpandCore(path) {
return this._grouping.changeRowExpand(path);
}

/// #DEBUG
getGroupsInfo() {
return this._grouping._groupsInfo;
}

/// #ENDDEBUG
// @ts-expect-error
_hasGroupLevelsExpandState(group, isExpanded) {
if (group && Array.isArray(group)) {
for (let i = 0; i < group.length; i++) {
if (group[i].isExpanded === isExpanded) {
return true;
}
}
}
}

if (!remoteOperations.grouping && (!remoteOperations.sorting || !remoteOperations.filtering || options.isCustomLoading || this._hasGroupLevelsExpandState(options.storeLoadOptions.group, false))) {
_customizeRemoteOperations(options, operationTypes) {
const { remoteOperations } = options;

if (options.storeLoadOptions.group) {
if (remoteOperations.grouping && !options.isCustomLoading) {
if (!remoteOperations.groupPaging || this._hasGroupLevelsExpandState(options.storeLoadOptions.group, true)) {
remoteOperations.paging = false;
}
} else if (!options.isCustomLoading && remoteOperations.paging && operationTypes.grouping) {
this.resetCache();
}

this.callBase.apply(this, arguments);
},
_handleDataLoading(options) {
this.callBase(options);
this._initGroupingHelper(options);
return this._grouping.handleDataLoading(options);
},
_handleDataLoaded(options) {
return this._grouping.handleDataLoaded(options, this.callBase.bind(this));
},
_handleDataLoadedCore(options) {
return this._grouping.handleDataLoadedCore(options, this.callBase.bind(this));
},
};
}());
if (!remoteOperations.grouping && (!remoteOperations.sorting || !remoteOperations.filtering || options.isCustomLoading || this._hasGroupLevelsExpandState(options.storeLoadOptions.group, false))) {
remoteOperations.paging = false;
}
} else if (!options.isCustomLoading && remoteOperations.paging && operationTypes.grouping) {
this.resetCache();
}

super._customizeRemoteOperations.apply(this, arguments as any);
}

_handleDataLoading(options) {
super._handleDataLoading(options);
this._initGroupingHelper(options);
return this._grouping.handleDataLoading(options);
}

_handleDataLoaded(options) {
return this._grouping.handleDataLoaded(options, super._handleDataLoaded.bind(this));
}

_handleDataLoadedCore(options) {
return this._grouping.handleDataLoadedCore(options, super._handleDataLoadedCore.bind(this));
}
};

dataSourceAdapter.extend(GroupingDataSourceAdapterExtender);
dataSourceAdapterProvider.extend(dataSourceAdapterExtender);

const GroupingDataControllerExtender = (function () {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import DataSourceAdapter from '@ts/grids/grid_core/data_source_adapter/m_data_source_adapter';

let dataSourceAdapterType: any = DataSourceAdapter;
let DataSourceAdapterType: any = DataSourceAdapter;

export default {
extend(extender) {
dataSourceAdapterType = dataSourceAdapterType.inherit(extender);
DataSourceAdapterType = extender(DataSourceAdapterType);
},
create(component) {
// eslint-disable-next-line new-cap
return new dataSourceAdapterType(component);
return new DataSourceAdapterType(component);
},
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { virtualScrollingModule } from '@ts/grids/grid_core/virtual_scrolling/m_virtual_scrolling';
import { dataSourceAdapterExtender, virtualScrollingModule } from '@ts/grids/grid_core/virtual_scrolling/m_virtual_scrolling';

import gridCore from '../m_core';
import dataSourceAdapter from '../m_data_source_adapter';
import dataSourceAdapterProvider from '../m_data_source_adapter';

gridCore.registerModule('virtualScrolling', virtualScrollingModule);

dataSourceAdapter.extend(virtualScrollingModule.extenders.dataSourceAdapter);
dataSourceAdapterProvider.extend(dataSourceAdapterExtender);
Loading

0 comments on commit 35482e7

Please sign in to comment.