1+ import { Injectable } from '@angular/core' ;
2+ import { Router , NavigationEnd , NavigationStart } from '@angular/router' ;
3+ import { FilterService } from './filter.service' ;
14import { CellArgs , Column , GridOption } from './../models' ;
25
36// using external js modules in Angular
47declare var Slick : any ;
58
9+ @Injectable ( )
610export class ControlAndPluginService {
11+ _dataView : any ;
712 _grid : any ;
813 _visibleColumns : Column [ ] ;
914
15+ // controls & plugins
16+ autoTooltipPlugin ;
17+ columnPickerControl ;
18+ headerButtonsPlugin ;
19+ headerMenuPlugin ;
20+ gridMenuControl ;
21+ rowSelectionPlugin ;
22+
23+ constructor ( private filterService : FilterService , private router : Router ) { }
24+
1025 attachDifferentControlOrPlugins ( grid : any , columnDefinitions : Column [ ] , options : GridOption , dataView : any ) {
1126 this . _visibleColumns = columnDefinitions ;
27+ this . _dataView = dataView ;
1228 this . _grid = grid ;
1329
1430 if ( options . enableColumnPicker ) {
15- const columnPickerControl = new Slick . Controls . ColumnPicker ( columnDefinitions , grid , options ) ;
31+ this . columnPickerControl = new Slick . Controls . ColumnPicker ( columnDefinitions , grid , options ) ;
1632 }
1733 if ( options . enableGridMenu ) {
18- options . gridMenu = options . gridMenu || { } ;
19- options . gridMenu . columnTitle = options . gridMenu . columnTitle || 'Columns' ;
20- options . gridMenu . iconCssClass = options . gridMenu . iconCssClass || 'fa fa-bars' ;
21- options . gridMenu . menuWidth = options . gridMenu . menuWidth || 18 ;
22- options . gridMenu . resizeOnShowHeaderRow = options . showHeaderRow ;
23-
24- const gridMenuControl = new Slick . Controls . GridMenu ( columnDefinitions , grid , options ) ;
25- gridMenuControl . onCommand . subscribe ( ( e : Event , args : CellArgs ) => {
34+ this . prepareGridMenu ( options ) ;
35+
36+ this . gridMenuControl = new Slick . Controls . GridMenu ( columnDefinitions , grid , options ) ;
37+ this . gridMenuControl . onCommand . subscribe ( ( e : Event , args : CellArgs ) => {
2638 if ( typeof options . onGridMenuCommand === 'function' ) {
2739 options . onGridMenuCommand ( e , args ) ;
2840 }
2941 } ) ;
3042 }
3143 if ( options . enableAutoTooltip ) {
32- grid . registerPlugin ( new Slick . AutoTooltips ( options . autoTooltipOptions || { } ) ) ;
44+ this . autoTooltipPlugin = new Slick . AutoTooltips ( options . autoTooltipOptions || { } ) ;
45+ grid . registerPlugin ( this . autoTooltipPlugin ) ;
3346 }
3447 if ( options . enableRowSelection ) {
35- grid . setSelectionModel ( new Slick . RowSelectionModel ( options . rowSelectionOptions || { } ) ) ;
48+ this . rowSelectionPlugin = new Slick . RowSelectionModel ( options . rowSelectionOptions || { } ) ;
49+ grid . setSelectionModel ( this . rowSelectionPlugin ) ;
3650 }
3751 if ( options . enableHeaderButton ) {
38- const headerButtonsPlugin = new Slick . Plugins . HeaderButtons ( options . headerButtonOptions || { } ) ;
39- grid . registerPlugin ( headerButtonsPlugin ) ;
40- headerButtonsPlugin . onCommand . subscribe ( ( e : Event , args : CellArgs ) => {
52+ this . headerButtonsPlugin = new Slick . Plugins . HeaderButtons ( options . headerButtonOptions || { } ) ;
53+ grid . registerPlugin ( this . headerButtonsPlugin ) ;
54+ this . headerButtonsPlugin . onCommand . subscribe ( ( e : Event , args : CellArgs ) => {
4155 if ( typeof options . onHeaderButtonCommand === 'function' ) {
4256 options . onHeaderButtonCommand ( e , args ) ;
4357 }
4458 } ) ;
4559 }
4660 if ( options . enableHeaderMenu ) {
47- const headerMenuPlugin = new Slick . Plugins . HeaderMenu ( options . headerMenuOptions || { } ) ;
48- grid . registerPlugin ( headerMenuPlugin ) ;
49- headerMenuPlugin . onCommand . subscribe ( ( e : Event , args : CellArgs ) => {
61+ this . headerMenuPlugin = new Slick . Plugins . HeaderMenu ( options . headerMenuOptions || { } ) ;
62+ grid . registerPlugin ( this . headerMenuPlugin ) ;
63+ this . headerMenuPlugin . onCommand . subscribe ( ( e : Event , args : CellArgs ) => {
5064 if ( typeof options . onHeaderMenuCommand === 'function' ) {
5165 options . onHeaderMenuCommand ( e , args ) ;
5266 }
@@ -61,6 +75,18 @@ export class ControlAndPluginService {
6175 grid . registerPlugin ( options . registerPlugins ) ;
6276 }
6377 }
78+
79+ // destroy all the Controls & Plugins when changing Route
80+ this . router . events . subscribe ( ( event : NavigationEnd ) => {
81+ this . columnPickerControl . destroy ( ) ;
82+ this . gridMenuControl . destroy ( ) ;
83+
84+ /* The following plugins destroy are causing a page reload, not sure why, will leave commented out until I find why */
85+ // this.autoTooltipPlugin.destroy();
86+ // this.headerButtonsPlugin.destroy();
87+ // this.headerMenuPlugin.destroy();
88+ // this.rowSelectionPlugin.destroy();
89+ } ) ;
6490 }
6591
6692 hideColumn ( column : Column ) {
@@ -78,4 +104,58 @@ export class ControlAndPluginService {
78104 autoResizeColumns ( ) {
79105 this . _grid . autosizeColumns ( ) ;
80106 }
107+
108+ private addGridMenuCustomCommands ( options : GridOption ) {
109+ if ( options . enableFiltering ) {
110+ if ( options . gridMenu . customItems . filter ( ( item ) => item . command === 'clear-filter' ) . length === 0 ) {
111+ options . gridMenu . customItems . push (
112+ {
113+ iconCssClass : 'fa fa-filter text-danger' ,
114+ title : 'Clear All Filters' ,
115+ disabled : false ,
116+ command : 'clear-filter'
117+ }
118+ ) ;
119+ }
120+ if ( options . gridMenu . customItems . filter ( ( item ) => item . command === 'toggle-filter' ) . length === 0 ) {
121+ options . gridMenu . customItems . push (
122+ {
123+ iconCssClass : 'fa fa-random' ,
124+ title : 'Toggle Filter Row' ,
125+ disabled : false ,
126+ command : 'toggle-filter'
127+ }
128+ ) ;
129+ }
130+ options . onGridMenuCommand = ( e , args ) => {
131+ if ( args . command === 'toggle-filter' ) {
132+ this . _grid . setHeaderRowVisibility ( ! this . _grid . getOptions ( ) . showHeaderRow ) ;
133+ } else if ( args . command === 'toggle-toppanel' ) {
134+ this . _grid . setTopPanelVisibility ( ! this . _grid . getOptions ( ) . showTopPanel ) ;
135+ } else if ( args . command === 'clear-filter' ) {
136+ this . filterService . clearFilters ( ) ;
137+ this . _dataView . refresh ( ) ;
138+ } else {
139+ alert ( 'Command: ' + args . command ) ;
140+ }
141+ } ;
142+ }
143+
144+ // remove the custom command title if there's no command
145+ if ( options . gridMenu . customItems && options . gridMenu . customItems . length > 0 ) {
146+ options . gridMenu . customTitle = options . gridMenu . customTitle || 'Commands' ;
147+ }
148+ }
149+
150+ private prepareGridMenu ( options ) {
151+ options . gridMenu = options . gridMenu || { } ;
152+ options . gridMenu . columnTitle = options . gridMenu . columnTitle || 'Columns' ;
153+ options . gridMenu . iconCssClass = options . gridMenu . iconCssClass || 'fa fa-bars' ;
154+ options . gridMenu . menuWidth = options . gridMenu . menuWidth || 18 ;
155+ options . gridMenu . customTitle = options . gridMenu . customTitle || null ;
156+ options . gridMenu . customItems = options . gridMenu . customItems || [ ] ;
157+ this . addGridMenuCustomCommands ( options ) ;
158+
159+ options . gridMenu . resizeOnShowHeaderRow = options . showHeaderRow ;
160+ }
81161}
0 commit comments