1
-
2
- import { DataSource } from '../../common/data/data_source/data_source' ;
3
- import { extend } from '../../core/utils/extend' ;
4
- import { normalizeDataSourceOptions } from '../../common/data/data_source/utils' ;
5
- import DataController from '../ui/collection/m_data_controller' ;
1
+ import { DataSource } from '@js/common/data/data_source/data_source' ;
2
+ import { normalizeDataSourceOptions } from '@js/common/data/data_source/utils' ;
3
+ import { extend } from '@js/core/utils/extend' ;
4
+ import type { Controller } from '@ts/grids/grid_core/m_modules' ;
5
+ import type { ModuleType } from '@ts/grids/grid_core/m_types' ;
6
+ import DataController from '@ts/ui/collection/m_data_controller' ;
6
7
7
8
const DATA_SOURCE_OPTIONS_METHOD = '_dataSourceOptions' ;
8
9
const DATA_SOURCE_CHANGED_METHOD = '_dataSourceChangedHandler' ;
@@ -12,155 +13,175 @@ const DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD = '_dataSourceFromUrlLoadMode';
12
13
const SPECIFIC_DATA_SOURCE_OPTION = '_getSpecificDataSourceOption' ;
13
14
const NORMALIZE_DATA_SOURCE = '_normalizeDataSource' ;
14
15
16
+ export const DataHelperMixin = < T extends ModuleType < Controller > > ( Base : T ) => class DataHelperMixin extends Base {
17
+ public _dataSource : any ;
15
18
16
- const DataHelperMixin = {
19
+ protected _dataController : any ;
17
20
18
- postCtor : function ( ) {
19
- this . on ( 'disposing' , function ( ) {
20
- this . _disposeDataSource ( ) ;
21
- } . bind ( this ) ) ;
22
- } ,
21
+ protected readyWatcher : any ;
23
22
24
- _refreshDataSource : function ( ) {
25
- this . _initDataSource ( ) ;
26
- this . _loadDataSource ( ) ;
27
- } ,
23
+ private _proxiedDataSourceChangedHandler : any ;
28
24
29
- _initDataSource : function ( ) {
30
- let dataSourceOptions = ( SPECIFIC_DATA_SOURCE_OPTION in this ) ? this [ SPECIFIC_DATA_SOURCE_OPTION ] ( ) : this . option ( 'dataSource' ) ;
31
- let widgetDataSourceOptions ;
32
- let dataSourceType ;
25
+ private _proxiedDataSourceLoadErrorHandler : any ;
33
26
34
- this . _disposeDataSource ( ) ;
27
+ private _proxiedDataSourceLoadingChangedHandler : any ;
35
28
36
- if ( dataSourceOptions ) {
37
- if ( dataSourceOptions instanceof DataSource ) {
38
- this . _isSharedDataSource = true ;
39
- this . _dataSource = dataSourceOptions ;
40
- } else {
41
- widgetDataSourceOptions = ( DATA_SOURCE_OPTIONS_METHOD in this ) ? this [ DATA_SOURCE_OPTIONS_METHOD ] ( ) : { } ;
42
- dataSourceType = this . _dataSourceType ? this . _dataSourceType ( ) : DataSource ;
29
+ protected _isSharedDataSource : any ;
43
30
44
- dataSourceOptions = normalizeDataSourceOptions ( dataSourceOptions , {
45
- fromUrlLoadMode : ( DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD in this ) && this [ DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD ] ( )
46
- } ) ;
31
+ private readonly _dataSourceType : any ;
47
32
48
- this . _dataSource = new dataSourceType ( extend ( true , { } , widgetDataSourceOptions , dataSourceOptions ) ) ;
49
- }
33
+ public postCtor ( ) {
34
+ this . on ( 'disposing' , ( ) => {
35
+ this . _disposeDataSource ( ) ;
36
+ } ) ;
37
+ }
50
38
51
- if ( NORMALIZE_DATA_SOURCE in this ) {
52
- this . _dataSource = this [ NORMALIZE_DATA_SOURCE ] ( this . _dataSource ) ;
53
- }
39
+ protected _refreshDataSource ( ) {
40
+ this . _initDataSource ( ) ;
41
+ this . _loadDataSource ( ) ;
42
+ }
54
43
55
- this . _addDataSourceHandlers ( ) ;
56
- this . _initDataController ( ) ;
57
- }
58
- } ,
44
+ protected _initDataSource ( ) {
45
+ let dataSourceOptions = SPECIFIC_DATA_SOURCE_OPTION in this
46
+ ? ( this [ SPECIFIC_DATA_SOURCE_OPTION ] as any ) ( )
47
+ : this . option ( 'dataSource' ) ;
59
48
60
- _initDataController : function ( ) {
61
- const dataController = this . option ?.( '_dataController' ) ;
62
- const dataSource = this . _dataSource ;
49
+ let widgetDataSourceOptions ;
50
+ let dataSourceType ;
63
51
64
- if ( dataController ) {
65
- this . _dataController = dataController ;
66
- } else {
67
- this . _dataController = new DataController ( dataSource ) ;
68
- }
69
- } ,
52
+ this . _disposeDataSource ( ) ;
70
53
71
- _addDataSourceHandlers : function ( ) {
72
- if ( DATA_SOURCE_CHANGED_METHOD in this ) {
73
- this . _addDataSourceChangeHandler ( ) ;
74
- }
54
+ if ( dataSourceOptions ) {
55
+ if ( dataSourceOptions instanceof DataSource ) {
56
+ this . _isSharedDataSource = true ;
57
+ this . _dataSource = dataSourceOptions ;
58
+ } else {
59
+ widgetDataSourceOptions = DATA_SOURCE_OPTIONS_METHOD in this
60
+ ? ( this [ DATA_SOURCE_OPTIONS_METHOD ] as any ) ( )
61
+ : { } ;
75
62
76
- if ( DATA_SOURCE_LOAD_ERROR_METHOD in this ) {
77
- this . _addDataSourceLoadErrorHandler ( ) ;
78
- }
63
+ dataSourceType = this . _dataSourceType ? this . _dataSourceType ( ) : DataSource ;
79
64
80
- if ( DATA_SOURCE_LOADING_CHANGED_METHOD in this ) {
81
- this . _addDataSourceLoadingChangedHandler ( ) ;
82
- }
65
+ dataSourceOptions = normalizeDataSourceOptions ( dataSourceOptions , {
66
+ fromUrlLoadMode : ( DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD in this ) && ( this [ DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD ] as any ) ( ) ,
67
+ } ) ;
83
68
84
- this . _addReadyWatcher ( ) ;
85
- } ,
86
-
87
- _addReadyWatcher : function ( ) {
88
- this . readyWatcher = ( function ( isLoading ) {
89
- this . _ready && this . _ready ( ! isLoading ) ;
90
- } ) . bind ( this ) ;
91
- this . _dataSource . on ( 'loadingChanged' , this . readyWatcher ) ;
92
- } ,
93
-
94
- _addDataSourceChangeHandler : function ( ) {
95
- const dataSource = this . _dataSource ;
96
- this . _proxiedDataSourceChangedHandler = ( function ( e ) {
97
- this [ DATA_SOURCE_CHANGED_METHOD ] ( dataSource . items ( ) , e ) ;
98
- } ) . bind ( this ) ;
99
- dataSource . on ( 'changed' , this . _proxiedDataSourceChangedHandler ) ;
100
- } ,
101
-
102
- _addDataSourceLoadErrorHandler : function ( ) {
103
- this . _proxiedDataSourceLoadErrorHandler = this [ DATA_SOURCE_LOAD_ERROR_METHOD ] . bind ( this ) ;
104
- this . _dataSource . on ( 'loadError' , this . _proxiedDataSourceLoadErrorHandler ) ;
105
- } ,
106
-
107
- _addDataSourceLoadingChangedHandler : function ( ) {
108
- this . _proxiedDataSourceLoadingChangedHandler = this [ DATA_SOURCE_LOADING_CHANGED_METHOD ] . bind ( this ) ;
109
- this . _dataSource . on ( 'loadingChanged' , this . _proxiedDataSourceLoadingChangedHandler ) ;
110
- } ,
111
-
112
- _loadDataSource : function ( ) {
113
- const dataSource = this . _dataSource ;
114
- if ( dataSource ) {
115
- if ( dataSource . isLoaded ( ) ) {
116
- this . _proxiedDataSourceChangedHandler && this . _proxiedDataSourceChangedHandler ( ) ;
117
- } else {
118
- dataSource . load ( ) ;
119
- }
120
- }
121
- } ,
122
-
123
- _loadSingle : function ( key , value ) {
124
- key = key === 'this' ? this . _dataSource . key ( ) || 'this' : key ;
125
- return this . _dataSource . loadSingle ( key , value ) ;
126
- } ,
127
-
128
- _isLastPage : function ( ) {
129
- return ! this . _dataSource || this . _dataSource . isLastPage ( ) || ! this . _dataSource . _pageSize ;
130
- } ,
131
-
132
- _isDataSourceLoading : function ( ) {
133
- return this . _dataSource && this . _dataSource . isLoading ( ) ;
134
- } ,
135
-
136
- _disposeDataSource : function ( ) {
137
- if ( this . _dataSource ) {
138
- if ( this . _isSharedDataSource ) {
139
- delete this . _isSharedDataSource ;
140
-
141
- this . _proxiedDataSourceChangedHandler && this . _dataSource . off ( 'changed' , this . _proxiedDataSourceChangedHandler ) ;
142
- this . _proxiedDataSourceLoadErrorHandler && this . _dataSource . off ( 'loadError' , this . _proxiedDataSourceLoadErrorHandler ) ;
143
- this . _proxiedDataSourceLoadingChangedHandler && this . _dataSource . off ( 'loadingChanged' , this . _proxiedDataSourceLoadingChangedHandler ) ;
144
-
145
- if ( this . _dataSource . _eventsStrategy ) {
146
- this . _dataSource . _eventsStrategy . off ( 'loadingChanged' , this . readyWatcher ) ;
147
- }
148
- } else {
149
- this . _dataSource . dispose ( ) ;
150
- }
151
-
152
- delete this . _dataSource ;
153
-
154
- delete this . _proxiedDataSourceChangedHandler ;
155
- delete this . _proxiedDataSourceLoadErrorHandler ;
156
- delete this . _proxiedDataSourceLoadingChangedHandler ;
69
+ // eslint-disable-next-line new-cap
70
+ this . _dataSource = new dataSourceType ( extend ( true , { } , widgetDataSourceOptions , dataSourceOptions ) ) ;
71
+ }
72
+
73
+ if ( NORMALIZE_DATA_SOURCE in this ) {
74
+ this . _dataSource = ( this [ NORMALIZE_DATA_SOURCE ] as any ) ( this . _dataSource ) ;
75
+ }
76
+
77
+ this . _addDataSourceHandlers ( ) ;
78
+ this . _initDataController ( ) ;
79
+ }
80
+ }
81
+
82
+ private _initDataController ( ) {
83
+ const dataController = this . option ?.( '_dataController' ) ;
84
+ const dataSource = this . _dataSource ;
85
+
86
+ if ( dataController ) {
87
+ this . _dataController = dataController ;
88
+ } else {
89
+ this . _dataController = new DataController ( dataSource ) ;
90
+ }
91
+ }
92
+
93
+ private _addDataSourceHandlers ( ) {
94
+ if ( DATA_SOURCE_CHANGED_METHOD in this ) {
95
+ this . _addDataSourceChangeHandler ( ) ;
96
+ }
97
+
98
+ if ( DATA_SOURCE_LOAD_ERROR_METHOD in this ) {
99
+ this . _addDataSourceLoadErrorHandler ( ) ;
100
+ }
101
+
102
+ if ( DATA_SOURCE_LOADING_CHANGED_METHOD in this ) {
103
+ this . _addDataSourceLoadingChangedHandler ( ) ;
104
+ }
105
+
106
+ this . _addReadyWatcher ( ) ;
107
+ }
108
+
109
+ private _addReadyWatcher ( ) {
110
+ this . readyWatcher = function ( isLoading ) {
111
+ this . _ready && this . _ready ( ! isLoading ) ;
112
+ } . bind ( this ) ;
113
+ this . _dataSource . on ( 'loadingChanged' , this . readyWatcher ) ;
114
+ }
115
+
116
+ private _addDataSourceChangeHandler ( ) {
117
+ const dataSource = this . _dataSource ;
118
+ this . _proxiedDataSourceChangedHandler = function ( e ) {
119
+ this [ DATA_SOURCE_CHANGED_METHOD ] ( dataSource . items ( ) , e ) ;
120
+ } . bind ( this ) ;
121
+ dataSource . on ( 'changed' , this . _proxiedDataSourceChangedHandler ) ;
122
+ }
123
+
124
+ private _addDataSourceLoadErrorHandler ( ) {
125
+ this . _proxiedDataSourceLoadErrorHandler = this [ DATA_SOURCE_LOAD_ERROR_METHOD ] . bind ( this ) ;
126
+ this . _dataSource . on ( 'loadError' , this . _proxiedDataSourceLoadErrorHandler ) ;
127
+ }
128
+
129
+ private _addDataSourceLoadingChangedHandler ( ) {
130
+ this . _proxiedDataSourceLoadingChangedHandler = this [ DATA_SOURCE_LOADING_CHANGED_METHOD ] . bind ( this ) ;
131
+ this . _dataSource . on ( 'loadingChanged' , this . _proxiedDataSourceLoadingChangedHandler ) ;
132
+ }
133
+
134
+ protected _loadDataSource ( ) {
135
+ const dataSource = this . _dataSource ;
136
+ if ( dataSource ) {
137
+ if ( dataSource . isLoaded ( ) ) {
138
+ this . _proxiedDataSourceChangedHandler && this . _proxiedDataSourceChangedHandler ( ) ;
139
+ } else {
140
+ dataSource . load ( ) ;
141
+ }
142
+ }
143
+ }
144
+
145
+ private _loadSingle ( key , value ) {
146
+ key = key === 'this' ? this . _dataSource . key ( ) || 'this' : key ;
147
+ return this . _dataSource . loadSingle ( key , value ) ;
148
+ }
149
+
150
+ private _isLastPage ( ) {
151
+ return ! this . _dataSource || this . _dataSource . isLastPage ( ) || ! this . _dataSource . _pageSize ;
152
+ }
153
+
154
+ private _isDataSourceLoading ( ) {
155
+ return this . _dataSource && this . _dataSource . isLoading ( ) ;
156
+ }
157
+
158
+ protected _disposeDataSource ( ) {
159
+ if ( this . _dataSource ) {
160
+ if ( this . _isSharedDataSource ) {
161
+ delete this . _isSharedDataSource ;
162
+
163
+ this . _proxiedDataSourceChangedHandler && this . _dataSource . off ( 'changed' , this . _proxiedDataSourceChangedHandler ) ;
164
+ this . _proxiedDataSourceLoadErrorHandler && this . _dataSource . off ( 'loadError' , this . _proxiedDataSourceLoadErrorHandler ) ;
165
+ this . _proxiedDataSourceLoadingChangedHandler && this . _dataSource . off ( 'loadingChanged' , this . _proxiedDataSourceLoadingChangedHandler ) ;
166
+
167
+ if ( this . _dataSource . _eventsStrategy ) {
168
+ this . _dataSource . _eventsStrategy . off ( 'loadingChanged' , this . readyWatcher ) ;
157
169
}
158
- } ,
170
+ } else {
171
+ this . _dataSource . dispose ( ) ;
172
+ }
173
+
174
+ delete this . _dataSource ;
159
175
160
- getDataSource : function ( ) {
161
- return this . _dataSource || null ;
176
+ delete this . _proxiedDataSourceChangedHandler ;
177
+ delete this . _proxiedDataSourceLoadErrorHandler ;
178
+ delete this . _proxiedDataSourceLoadingChangedHandler ;
162
179
}
180
+ }
163
181
182
+ protected getDataSource ( ) {
183
+ return this . _dataSource || null ;
184
+ }
164
185
} ;
165
186
166
187
export default DataHelperMixin ;
0 commit comments