11import { Injectable } from '@angular/core' ;
2- import { Router , NavigationEnd , NavigationStart } from '@angular/router' ;
32import { FilterService } from './filter.service' ;
43import { GridExtraUtils } from './gridExtraUtils' ;
54import { GridExtraService } from './gridExtra.service' ;
@@ -33,12 +32,8 @@ export class ControlAndPluginService {
3332 gridMenuControl : any ;
3433 rowSelectionPlugin : any ;
3534
36- constructor ( private filterService : FilterService , private gridExtraService : GridExtraService , private router : Router ) { }
35+ constructor ( private filterService : FilterService , private gridExtraService : GridExtraService ) { }
3736
38- init ( grid : any , dataView : any , columnDefinitions : Column [ ] , options : GridOption ) {
39- this . _grid = grid ;
40- this . _dataView = dataView ;
41- }
4237 /**
4338 * Attach/Create different Controls or Plugins after the Grid is created
4439 * @param {any } grid
@@ -47,6 +42,10 @@ export class ControlAndPluginService {
4742 * @param {any } dataView
4843 */
4944 attachDifferentControlOrPlugins ( grid : any , columnDefinitions : Column [ ] , options : GridOption , dataView : any ) {
45+ this . _grid = grid ;
46+ this . _dataView = dataView ;
47+ this . _visibleColumns = columnDefinitions ;
48+
5049 if ( options . enableColumnPicker ) {
5150 this . columnPickerControl = new Slick . Controls . ColumnPicker ( columnDefinitions , grid , options ) ;
5251 }
@@ -56,17 +55,17 @@ export class ControlAndPluginService {
5655 this . gridMenuControl = new Slick . Controls . GridMenu ( columnDefinitions , grid , options ) ;
5756 if ( options . gridMenu ) {
5857 this . gridMenuControl . onBeforeMenuShow . subscribe ( ( e : Event , args : CellArgs ) => {
59- if ( typeof options . gridMenu . onBeforeMenuShow === 'function' ) {
58+ if ( options . gridMenu && typeof options . gridMenu . onBeforeMenuShow === 'function' ) {
6059 options . gridMenu . onBeforeMenuShow ( e , args ) ;
6160 }
6261 } ) ;
6362 this . gridMenuControl . onCommand . subscribe ( ( e : Event , args : CellArgs ) => {
64- if ( typeof options . gridMenu . onCommand === 'function' ) {
63+ if ( options . gridMenu && typeof options . gridMenu . onCommand === 'function' ) {
6564 options . gridMenu . onCommand ( e , args ) ;
6665 }
6766 } ) ;
6867 this . gridMenuControl . onMenuClose . subscribe ( ( e : Event , args : CellArgs ) => {
69- if ( typeof options . gridMenu . onMenuClose === 'function' ) {
68+ if ( options . gridMenu && typeof options . gridMenu . onMenuClose === 'function' ) {
7069 options . gridMenu . onMenuClose ( e , args ) ;
7170 }
7271 } ) ;
@@ -124,21 +123,18 @@ export class ControlAndPluginService {
124123 grid . registerPlugin ( options . registerPlugins ) ;
125124 }
126125 }
127-
128- // destroy all the Controls & Plugins when changing Route
129- this . router . events . subscribe ( ( event : NavigationEnd ) => {
130- this . destroy ( ) ;
131- } ) ;
132126 }
133127
134128 hideColumn ( column : Column ) {
135- const columnIndex = this . _grid . getColumnIndex ( column . id ) ;
136- this . _visibleColumns = this . removeColumnByIndex ( this . _visibleColumns , columnIndex ) ;
137- this . _grid . setColumns ( this . _visibleColumns ) ;
129+ if ( this . _grid && this . _visibleColumns ) {
130+ const columnIndex = this . _grid . getColumnIndex ( column . id ) ;
131+ this . _visibleColumns = this . removeColumnByIndex ( this . _visibleColumns , columnIndex ) ;
132+ this . _grid . setColumns ( this . _visibleColumns ) ;
133+ }
138134 }
139135
140- removeColumnByIndex ( array , index ) {
141- return array . filter ( ( el , i ) => {
136+ removeColumnByIndex ( array : any [ ] , index : number ) {
137+ return array . filter ( ( el : any , i : number ) => {
142138 return index !== i ;
143139 } ) ;
144140 }
@@ -184,7 +180,7 @@ export class ControlAndPluginService {
184180
185181 private addGridMenuCustomCommands ( grid : any , options : GridOption ) {
186182 if ( options . enableFiltering ) {
187- if ( options . gridMenu . customItems . filter ( ( item ) => item . command === 'clear-filter' ) . length === 0 ) {
183+ if ( options && options . gridMenu && options . gridMenu . customItems && options . gridMenu . customItems . filter ( ( item : CustomGridMenu ) => item . command === 'clear-filter' ) . length === 0 ) {
188184 options . gridMenu . customItems . push (
189185 {
190186 iconCssClass : 'fa fa-filter text-danger' ,
@@ -194,7 +190,7 @@ export class ControlAndPluginService {
194190 }
195191 ) ;
196192 }
197- if ( options . gridMenu . customItems . filter ( ( item ) => item . command === 'toggle-filter' ) . length === 0 ) {
193+ if ( options && options . gridMenu && options . gridMenu . customItems && options . gridMenu . customItems . filter ( ( item : CustomGridMenu ) => item . command === 'toggle-filter' ) . length === 0 ) {
198194 options . gridMenu . customItems . push (
199195 {
200196 iconCssClass : 'fa fa-random' ,
@@ -204,32 +200,34 @@ export class ControlAndPluginService {
204200 }
205201 ) ;
206202 }
207- options . gridMenu . onCommand = ( e , args ) => {
208- if ( args . command === 'toggle-filter' ) {
209- grid . setHeaderRowVisibility ( ! grid . getOptions ( ) . showHeaderRow ) ;
210- } else if ( args . command === 'toggle-toppanel' ) {
211- grid . setTopPanelVisibility ( ! grid . getOptions ( ) . showTopPanel ) ;
212- } else if ( args . command === 'clear-filter' ) {
213- this . filterService . clearFilters ( ) ;
214- this . _dataView . refresh ( ) ;
215- } else {
216- alert ( 'Command: ' + args . command ) ;
217- }
218- } ;
203+ if ( options . gridMenu ) {
204+ options . gridMenu . onCommand = ( e , args ) => {
205+ if ( args . command === 'toggle-filter' ) {
206+ grid . setHeaderRowVisibility ( ! grid . getOptions ( ) . showHeaderRow ) ;
207+ } else if ( args . command === 'toggle-toppanel' ) {
208+ grid . setTopPanelVisibility ( ! grid . getOptions ( ) . showTopPanel ) ;
209+ } else if ( args . command === 'clear-filter' ) {
210+ this . filterService . clearFilters ( ) ;
211+ this . _dataView . refresh ( ) ;
212+ } else {
213+ alert ( 'Command: ' + args . command ) ;
214+ }
215+ } ;
216+ }
219217 }
220218
221219 // remove the custom command title if there's no command
222- if ( options . gridMenu . customItems && options . gridMenu . customItems . length > 0 ) {
220+ if ( options && options . gridMenu && options . gridMenu . customItems && options . gridMenu . customItems . length > 0 ) {
223221 options . gridMenu . customTitle = options . gridMenu . customTitle || 'Commands' ;
224222 }
225223 }
226224
227- private prepareGridMenu ( grid , options ) {
225+ private prepareGridMenu ( grid : any , options : GridOption ) {
228226 options . gridMenu = options . gridMenu || { } ;
229227 options . gridMenu . columnTitle = options . gridMenu . columnTitle || 'Columns' ;
230228 options . gridMenu . iconCssClass = options . gridMenu . iconCssClass || 'fa fa-bars' ;
231229 options . gridMenu . menuWidth = options . gridMenu . menuWidth || 18 ;
232- options . gridMenu . customTitle = options . gridMenu . customTitle || null ;
230+ options . gridMenu . customTitle = options . gridMenu . customTitle || undefined ;
233231 options . gridMenu . customItems = options . gridMenu . customItems || [ ] ;
234232 this . addGridMenuCustomCommands ( grid , options ) ;
235233 // options.gridMenu.resizeOnShowHeaderRow = options.showHeaderRow;
@@ -248,4 +246,3 @@ export class ControlAndPluginService {
248246 }
249247 }
250248}
251-
0 commit comments