Skip to content

Commit

Permalink
DataHelper: fix errors after migration to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeniyKiyashko committed Jan 21, 2025
1 parent 88d815e commit bd68cb0
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 143 deletions.
1 change: 1 addition & 0 deletions packages/devextreme/js/__internal/core/m_class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const redefine = function (members) {
};

const include = function (...args) {
debugger;
const classObj = this;
let argument;
let name;
Expand Down
277 changes: 140 additions & 137 deletions packages/devextreme/js/__internal/data/m_data_helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import { DataSource } from '../../common/data/data_source/data_source';
import { extend } from '../../core/utils/extend';
import { normalizeDataSourceOptions } from '../../common/data/data_source/utils';
import DataController from '../ui/collection/m_data_controller';
import { DataSource } from '@js/common/data/data_source/data_source';
import { normalizeDataSourceOptions } from '@js/common/data/data_source/utils';
import { extend } from '@js/core/utils/extend';
import DataController from '@ts/ui/collection/m_data_controller';

const DATA_SOURCE_OPTIONS_METHOD = '_dataSourceOptions';
const DATA_SOURCE_CHANGED_METHOD = '_dataSourceChangedHandler';
Expand All @@ -12,155 +11,159 @@ const DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD = '_dataSourceFromUrlLoadMode';
const SPECIFIC_DATA_SOURCE_OPTION = '_getSpecificDataSourceOption';
const NORMALIZE_DATA_SOURCE = '_normalizeDataSource';

export const DataHelperMixin = {
postCtor(): void {
this.on('disposing', () => {
this._disposeDataSource();
});
},

_refreshDataSource(): void {
this._initDataSource();
this._loadDataSource();
},

_initDataSource(): void {
let dataSourceOptions = SPECIFIC_DATA_SOURCE_OPTION in this
? this[SPECIFIC_DATA_SOURCE_OPTION]()
: this.option('dataSource');

let widgetDataSourceOptions;
let dataSourceType;

this._disposeDataSource();

if (dataSourceOptions) {
if (dataSourceOptions instanceof DataSource) {
this._isSharedDataSource = true;
this._dataSource = dataSourceOptions;
} else {
widgetDataSourceOptions = DATA_SOURCE_OPTIONS_METHOD in this
? this[DATA_SOURCE_OPTIONS_METHOD]()
: {};

dataSourceType = this._dataSourceType ? this._dataSourceType() : DataSource;

dataSourceOptions = normalizeDataSourceOptions(dataSourceOptions, {
fromUrlLoadMode: (DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD in this) && this[DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD](),
});

// eslint-disable-next-line new-cap
this._dataSource = new dataSourceType(extend(true, {}, widgetDataSourceOptions, dataSourceOptions));
}

if (NORMALIZE_DATA_SOURCE in this) {
this._dataSource = this[NORMALIZE_DATA_SOURCE](this._dataSource);
}

this._addDataSourceHandlers();
this._initDataController();
}
},

const DataHelperMixin = {

postCtor: function() {
this.on('disposing', function() {
this._disposeDataSource();
}.bind(this));
},

_refreshDataSource: function() {
this._initDataSource();
this._loadDataSource();
},

_initDataSource: function() {
let dataSourceOptions = (SPECIFIC_DATA_SOURCE_OPTION in this) ? this[SPECIFIC_DATA_SOURCE_OPTION]() : this.option('dataSource');
let widgetDataSourceOptions;
let dataSourceType;
_initDataController(): void {
const dataController = this.option?.('_dataController');
const dataSource = this._dataSource;

this._disposeDataSource();
if (dataController) {
this._dataController = dataController;
} else {
this._dataController = new DataController(dataSource);
}
},

if(dataSourceOptions) {
if(dataSourceOptions instanceof DataSource) {
this._isSharedDataSource = true;
this._dataSource = dataSourceOptions;
} else {
widgetDataSourceOptions = (DATA_SOURCE_OPTIONS_METHOD in this) ? this[DATA_SOURCE_OPTIONS_METHOD]() : {};
dataSourceType = this._dataSourceType ? this._dataSourceType() : DataSource;
_addDataSourceHandlers(): void {
if (DATA_SOURCE_CHANGED_METHOD in this) {
this._addDataSourceChangeHandler();
}

dataSourceOptions = normalizeDataSourceOptions(dataSourceOptions, {
fromUrlLoadMode: (DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD in this) && this[DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD]()
});
if (DATA_SOURCE_LOAD_ERROR_METHOD in this) {
this._addDataSourceLoadErrorHandler();
}

this._dataSource = new dataSourceType(extend(true, {}, widgetDataSourceOptions, dataSourceOptions));
}
if (DATA_SOURCE_LOADING_CHANGED_METHOD in this) {
this._addDataSourceLoadingChangedHandler();
}

if(NORMALIZE_DATA_SOURCE in this) {
this._dataSource = this[NORMALIZE_DATA_SOURCE](this._dataSource);
}
this._addReadyWatcher();
},

_addReadyWatcher(): void {
this.readyWatcher = function (isLoading) {
this._ready && this._ready(!isLoading);
}.bind(this);
this._dataSource.on('loadingChanged', this.readyWatcher);
},

_addDataSourceChangeHandler(): void {
const dataSource = this._dataSource;
this._proxiedDataSourceChangedHandler = function (e) {
this[DATA_SOURCE_CHANGED_METHOD](dataSource.items(), e);
}.bind(this);
dataSource.on('changed', this._proxiedDataSourceChangedHandler);
},

_addDataSourceLoadErrorHandler(): void {
this._proxiedDataSourceLoadErrorHandler = this[DATA_SOURCE_LOAD_ERROR_METHOD].bind(this);
this._dataSource.on('loadError', this._proxiedDataSourceLoadErrorHandler);
},

_addDataSourceLoadingChangedHandler(): void {
this._proxiedDataSourceLoadingChangedHandler = this[DATA_SOURCE_LOADING_CHANGED_METHOD].bind(this);
this._dataSource.on('loadingChanged', this._proxiedDataSourceLoadingChangedHandler);
},

_loadDataSource(): void {
const dataSource = this._dataSource;
if (dataSource) {
if (dataSource.isLoaded()) {
this._proxiedDataSourceChangedHandler && this._proxiedDataSourceChangedHandler();
} else {
dataSource.load();
}
}
},

this._addDataSourceHandlers();
this._initDataController();
}
},
_loadSingle(key, value) {
key = key === 'this' ? this._dataSource.key() || 'this' : key;
return this._dataSource.loadSingle(key, value);
},

_initDataController: function() {
const dataController = this.option?.('_dataController');
const dataSource = this._dataSource;
_isLastPage(): boolean {
return !this._dataSource || this._dataSource.isLastPage() || !this._dataSource._pageSize;
},

if(dataController) {
this._dataController = dataController;
} else {
this._dataController = new DataController(dataSource);
}
},
_isDataSourceLoading() {
return this._dataSource && this._dataSource.isLoading();
},

_addDataSourceHandlers: function() {
if(DATA_SOURCE_CHANGED_METHOD in this) {
this._addDataSourceChangeHandler();
}
_disposeDataSource(): void {
if (this._dataSource) {
if (this._isSharedDataSource) {
delete this._isSharedDataSource;

if(DATA_SOURCE_LOAD_ERROR_METHOD in this) {
this._addDataSourceLoadErrorHandler();
}
this._proxiedDataSourceChangedHandler && this._dataSource.off('changed', this._proxiedDataSourceChangedHandler);
this._proxiedDataSourceLoadErrorHandler && this._dataSource.off('loadError', this._proxiedDataSourceLoadErrorHandler);
this._proxiedDataSourceLoadingChangedHandler && this._dataSource.off('loadingChanged', this._proxiedDataSourceLoadingChangedHandler);

if(DATA_SOURCE_LOADING_CHANGED_METHOD in this) {
this._addDataSourceLoadingChangedHandler();
if (this._dataSource._eventsStrategy) {
this._dataSource._eventsStrategy.off('loadingChanged', this.readyWatcher);
}
} else {
this._dataSource.dispose();
}

this._addReadyWatcher();
},

_addReadyWatcher: function() {
this.readyWatcher = (function(isLoading) {
this._ready && this._ready(!isLoading);
}).bind(this);
this._dataSource.on('loadingChanged', this.readyWatcher);
},

_addDataSourceChangeHandler: function() {
const dataSource = this._dataSource;
this._proxiedDataSourceChangedHandler = (function(e) {
this[DATA_SOURCE_CHANGED_METHOD](dataSource.items(), e);
}).bind(this);
dataSource.on('changed', this._proxiedDataSourceChangedHandler);
},

_addDataSourceLoadErrorHandler: function() {
this._proxiedDataSourceLoadErrorHandler = this[DATA_SOURCE_LOAD_ERROR_METHOD].bind(this);
this._dataSource.on('loadError', this._proxiedDataSourceLoadErrorHandler);
},

_addDataSourceLoadingChangedHandler: function() {
this._proxiedDataSourceLoadingChangedHandler = this[DATA_SOURCE_LOADING_CHANGED_METHOD].bind(this);
this._dataSource.on('loadingChanged', this._proxiedDataSourceLoadingChangedHandler);
},

_loadDataSource: function() {
const dataSource = this._dataSource;
if(dataSource) {
if(dataSource.isLoaded()) {
this._proxiedDataSourceChangedHandler && this._proxiedDataSourceChangedHandler();
} else {
dataSource.load();
}
}
},

_loadSingle: function(key, value) {
key = key === 'this' ? this._dataSource.key() || 'this' : key;
return this._dataSource.loadSingle(key, value);
},

_isLastPage: function() {
return !this._dataSource || this._dataSource.isLastPage() || !this._dataSource._pageSize;
},

_isDataSourceLoading: function() {
return this._dataSource && this._dataSource.isLoading();
},

_disposeDataSource: function() {
if(this._dataSource) {
if(this._isSharedDataSource) {
delete this._isSharedDataSource;

this._proxiedDataSourceChangedHandler && this._dataSource.off('changed', this._proxiedDataSourceChangedHandler);
this._proxiedDataSourceLoadErrorHandler && this._dataSource.off('loadError', this._proxiedDataSourceLoadErrorHandler);
this._proxiedDataSourceLoadingChangedHandler && this._dataSource.off('loadingChanged', this._proxiedDataSourceLoadingChangedHandler);

if(this._dataSource._eventsStrategy) {
this._dataSource._eventsStrategy.off('loadingChanged', this.readyWatcher);
}
} else {
this._dataSource.dispose();
}

delete this._dataSource;

delete this._proxiedDataSourceChangedHandler;
delete this._proxiedDataSourceLoadErrorHandler;
delete this._proxiedDataSourceLoadingChangedHandler;
}
},
delete this._dataSource;

getDataSource: function() {
return this._dataSource || null;
delete this._proxiedDataSourceChangedHandler;
delete this._proxiedDataSourceLoadErrorHandler;
delete this._proxiedDataSourceLoadingChangedHandler;
}
},

getDataSource() {
return this._dataSource || null;
},
};

export default DataHelperMixin;
2 changes: 1 addition & 1 deletion packages/devextreme/js/__internal/scheduler/m_scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { triggerResizeEvent } from '@js/common/core/events/visibility_change';
import dateLocalization from '@js/common/core/localization/date';
import messageLocalization from '@js/common/core/localization/message';
import DataHelperMixin from '@ts/data/m_data_helper';
import registerComponent from '@js/core/component_registrator';
import config from '@js/core/config';
import devices from '@js/core/devices';
Expand Down Expand Up @@ -30,6 +29,7 @@ import {
isString,
} from '@js/core/utils/type';
import { hasWindow } from '@js/core/utils/window';
import DataHelperMixin from '@js/data_helper';
import { custom as customDialog } from '@js/ui/dialog';
import type { AppointmentTooltipShowingEvent, ViewType } from '@js/ui/scheduler';
import { isMaterial, isMaterialBased } from '@js/ui/themes';
Expand Down
2 changes: 1 addition & 1 deletion packages/devextreme/js/__internal/ui/chat/chat.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Guid } from '@js/common';
import messageLocalization from '@js/common/core/localization/message';
import type { DataSourceOptions } from '@js/common/data';
import DataHelperMixin from '@ts/data/m_data_helper';
import registerComponent from '@js/core/component_registrator';
import type { dxElementWrapper } from '@js/core/renderer';
import $ from '@js/core/renderer';
import { isDefined } from '@js/core/utils/type';
import DataHelperMixin from '@js/data_helper';
import type {
Message,
MessageEnteredEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { getOuterHeight, getOuterWidth } from '@js/core/utils/size';
import { findTemplates } from '@js/core/utils/template_manager';
import { isDefined, isFunction, isPlainObject } from '@js/core/utils/type';
import type { DataSourceOptions } from '@js/data/data_source';
import DataHelperMixin from '@js/data_helper';
import type {
Cancelable, DxEvent, EventInfo, ItemInfo,
} from '@js/events';
Expand All @@ -34,7 +35,6 @@ import { focusable } from '@js/ui/widget/selectors';
import { getPublicElement } from '@ts/core/m_element';
import type { OptionChanged } from '@ts/core/widget/types';
import Widget from '@ts/core/widget/widget';
import DataHelperMixin from '@ts/data/m_data_helper';
import CollectionWidgetItem from '@ts/ui/collection/m_item';

const COLLECTION_CLASS = 'dx-collection';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ArrayStore from '@js/common/data/array_store';
import DataHelperMixin from '@ts/data/m_data_helper';
import DataSource from '@js/common/data/data_source';
import { ensureDefined, noop } from '@js/core/utils/common';
import {
Expand All @@ -12,6 +11,7 @@ import {
isDefined, isFunction, isObject as isObjectType, isString,
} from '@js/core/utils/type';
import variableWrapper from '@js/core/utils/variable_wrapper';
import DataHelperMixin from '@js/data_helper';

const DataExpressionMixin = extend({}, DataHelperMixin, {

Expand Down
2 changes: 1 addition & 1 deletion packages/devextreme/js/data_helper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { DataHelperMixin } from './common/data';
import { DataHelperMixin } from './__internal/data/m_data_helper';

export default DataHelperMixin;
2 changes: 1 addition & 1 deletion packages/devextreme/js/viz/core/data_source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { noop } from '../../core/utils/common';
import DataHelperMixin from '../../__internal/data/m_data_helper';
import DataHelperMixin from '../../data_helper';
const postCtor = DataHelperMixin.postCtor;
let name;
const members = {
Expand Down

0 comments on commit bd68cb0

Please sign in to comment.