@@ -16,7 +16,6 @@ import { SortService } from './sort.service';
16
16
import { Subject , Subscription } from 'rxjs' ;
17
17
18
18
// using external non-typed js libraries
19
- declare var $ : any ;
20
19
declare var Slick : any ;
21
20
22
21
export class GridStateService {
@@ -63,6 +62,8 @@ export class GridStateService {
63
62
}
64
63
} ) ;
65
64
this . subscriptions = [ ] ;
65
+ this . _currentColumns = [ ] ;
66
+ this . _columns = [ ] ;
66
67
}
67
68
68
69
/**
@@ -88,7 +89,7 @@ export class GridStateService {
88
89
* @return current columns
89
90
*/
90
91
getColumns ( ) : Column [ ] {
91
- return this . _columns || this . _grid . getColumns ( ) ;
92
+ return this . _columns ;
92
93
}
93
94
94
95
/**
@@ -203,38 +204,6 @@ export class GridStateService {
203
204
return null ;
204
205
}
205
206
206
- /**
207
- * Hook a SlickGrid Extension Event to a Grid State change event
208
- * @param extension name
209
- * @param grid
210
- */
211
- hookExtensionEventToGridStateChange ( extensionName : ExtensionName , eventName : string ) {
212
- const extension = this . extensionService && this . extensionService . getExtensionByName ( extensionName ) ;
213
-
214
- if ( extension && extension . class && extension . class [ eventName ] && extension . class [ eventName ] . subscribe ) {
215
- this . _eventHandler . subscribe ( extension . class [ eventName ] , ( e : Event , args : any ) => {
216
- const columns : Column [ ] = args && args . columns ;
217
- const currentColumns : CurrentColumn [ ] = this . getAssociatedCurrentColumns ( columns ) ;
218
- this . onGridStateChanged . next ( { change : { newValues : currentColumns , type : GridStateType . columns } , gridState : this . getCurrentGridState ( ) } ) ;
219
- } ) ;
220
- }
221
- }
222
-
223
- /**
224
- * Hook a Grid Event to a Grid State change event
225
- * @param event name
226
- * @param grid
227
- */
228
- hookSlickGridEventToGridStateChange ( eventName : string , grid : any ) {
229
- if ( grid && grid [ eventName ] && grid [ eventName ] . subscribe ) {
230
- this . _eventHandler . subscribe ( grid [ eventName ] , ( e : Event , args : any ) => {
231
- const columns : Column [ ] = grid . getColumns ( ) ;
232
- const currentColumns : CurrentColumn [ ] = this . getAssociatedCurrentColumns ( columns ) ;
233
- this . onGridStateChanged . next ( { change : { newValues : currentColumns , type : GridStateType . columns } , gridState : this . getCurrentGridState ( ) } ) ;
234
- } ) ;
235
- }
236
- }
237
-
238
207
resetColumns ( columnDefinitions ?: Column [ ] ) {
239
208
const columns : Column [ ] = columnDefinitions || this . _columns ;
240
209
const currentColumns : CurrentColumn [ ] = this . getAssociatedCurrentColumns ( columns ) ;
@@ -289,11 +258,50 @@ export class GridStateService {
289
258
) ;
290
259
291
260
// Subscribe to ColumnPicker and/or GridMenu for show/hide Columns visibility changes
292
- this . hookExtensionEventToGridStateChange ( ExtensionName . columnPicker , 'onColumnsChanged' ) ;
293
- this . hookExtensionEventToGridStateChange ( ExtensionName . gridMenu , 'onColumnsChanged' ) ;
261
+ this . bindExtensionAddonEventToGridStateChange ( ExtensionName . columnPicker , 'onColumnsChanged' ) ;
262
+ this . bindExtensionAddonEventToGridStateChange ( ExtensionName . gridMenu , 'onColumnsChanged' ) ;
294
263
295
264
// subscribe to Column Resize & Reordering
296
- this . hookSlickGridEventToGridStateChange ( 'onColumnsReordered' , grid ) ;
297
- this . hookSlickGridEventToGridStateChange ( 'onColumnsResized' , grid ) ;
265
+ this . bindSlickGridEventToGridStateChange ( 'onColumnsReordered' , grid ) ;
266
+ this . bindSlickGridEventToGridStateChange ( 'onColumnsResized' , grid ) ;
267
+ }
268
+
269
+ // --
270
+ // private methods
271
+ // ------------------
272
+
273
+ /**
274
+ * Bind a SlickGrid Extension Event to a Grid State change event
275
+ * @param extension name
276
+ * @param grid
277
+ */
278
+ private bindExtensionAddonEventToGridStateChange ( extensionName : ExtensionName , eventName : string ) {
279
+ const extension = this . extensionService && this . extensionService . getExtensionByName && this . extensionService . getExtensionByName ( extensionName ) ;
280
+ const slickEvent = extension && extension . addon && extension . addon [ eventName ] ;
281
+
282
+ if ( slickEvent && slickEvent . subscribe ) {
283
+ this . _eventHandler . subscribe ( slickEvent , ( e : Event , args : any ) => {
284
+ const columns : Column [ ] = args && args . columns ;
285
+ const currentColumns : CurrentColumn [ ] = this . getAssociatedCurrentColumns ( columns ) ;
286
+ this . onGridStateChanged . next ( { change : { newValues : currentColumns , type : GridStateType . columns } , gridState : this . getCurrentGridState ( ) } ) ;
287
+ } ) ;
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Bind a Grid Event to a Grid State change event
293
+ * @param event name
294
+ * @param grid
295
+ */
296
+ private bindSlickGridEventToGridStateChange ( eventName : string , grid : any ) {
297
+ const slickGridEvent = grid && grid [ eventName ] ;
298
+
299
+ if ( slickGridEvent && slickGridEvent . subscribe ) {
300
+ this . _eventHandler . subscribe ( slickGridEvent , ( e : Event , args : any ) => {
301
+ const columns : Column [ ] = grid . getColumns ( ) ;
302
+ const currentColumns : CurrentColumn [ ] = this . getAssociatedCurrentColumns ( columns ) ;
303
+ this . onGridStateChanged . next ( { change : { newValues : currentColumns , type : GridStateType . columns } , gridState : this . getCurrentGridState ( ) } ) ;
304
+ } ) ;
305
+ }
298
306
}
299
307
}
0 commit comments