@@ -14,7 +14,7 @@ import type { GridEvents } from 'gridjs/dist/src/events';
14
14
import type Header from 'gridjs/dist/src/header' ;
15
15
import type { Language , Translator } from 'gridjs/dist/src/i18n/language' ;
16
16
import type Pipeline from 'gridjs/dist/src/pipeline/pipeline' ;
17
- import type { PluginManager } from 'gridjs/dist/src/plugin' ;
17
+ import type { Plugin , PluginManager } from 'gridjs/dist/src/plugin' ;
18
18
import type { ServerStorageOptions } from 'gridjs/dist/src/storage/server' ;
19
19
import type Storage from 'gridjs/dist/src/storage/storage' ;
20
20
import type Tabular from 'gridjs/dist/src/tabular' ;
@@ -38,13 +38,12 @@ import {
38
38
} )
39
39
export class GridJsAngularComponent
40
40
implements AfterViewInit , OnChanges , OnDestroy , GridJsConfig {
41
- private nativeElement : any ;
41
+ private nativeElement : HTMLElement ;
42
42
private gridInstance : Grid ;
43
43
private initialized : boolean ;
44
- private destroyed : boolean ;
45
44
private listeners : Map < string , ( ...args : any [ ] ) => void > = new Map ( ) ;
46
45
@Input ( ) gridConfig : Partial < UserConfig > ;
47
-
46
+ @ Input ( ) plugins : Plugin [ ] = [ ] ;
48
47
// TODO: auto generate Inputs/Output to easily sync with grid-js main package
49
48
// props
50
49
@Input ( ) eventEmitter : GridJsEventEmitter < GridEvents > ;
@@ -80,27 +79,35 @@ export class GridJsAngularComponent
80
79
}
81
80
82
81
ngAfterViewInit ( ) : void {
83
- this . gridInstance = new Grid ( this . getFullConfig ( ) ) ;
82
+ this . gridInstance = new Grid ( this . getConfig ( this . gridConfig ) ) ;
83
+ this . registerPlugins ( ) ;
84
84
this . registerEvents ( ) ;
85
85
this . gridInstance . render ( this . nativeElement ) ;
86
86
this . initialized = true ;
87
87
}
88
88
89
- public ngOnChanges ( changes : any ) : void {
89
+ ngOnChanges ( changes : any ) : void {
90
90
if ( this . initialized ) {
91
- this . gridInstance . updateConfig ( this . getFullConfig ( ) ) . forceRender ( ) ;
91
+ this . updateConfig ( this . gridConfig ) ;
92
92
}
93
93
}
94
94
95
- public ngOnDestroy ( ) : void {
95
+ ngOnDestroy ( ) : void {
96
96
if ( this . initialized ) {
97
- this . destroyed = true ;
98
97
if ( this . gridInstance ) {
99
98
this . unregisterEvents ( ) ;
100
- // this.gridInstance.destroy()
99
+ this . gridInstance = null ;
101
100
}
102
101
}
103
102
}
103
+ // public api to interact with grid instance
104
+ getGridInstance ( ) {
105
+ return this . gridInstance ;
106
+ }
107
+
108
+ updateConfig ( config : Partial < UserConfig > ) {
109
+ this . gridInstance . updateConfig ( this . getConfig ( config ) ) . forceRender ( ) ;
110
+ }
104
111
105
112
private registerEvents ( ) {
106
113
for ( const event of GRIDJS_EVENTS ) {
@@ -122,13 +129,21 @@ export class GridJsAngularComponent
122
129
}
123
130
}
124
131
125
- private getFullConfig ( ) : UserConfig {
126
- const newConfig = { ...this . gridConfig } ;
132
+ private registerPlugins ( ) {
133
+ for ( const plugin of this . plugins ) {
134
+ if ( ! this . gridInstance . plugin . get ( plugin . id ) ) {
135
+ this . gridInstance . plugin . add ( plugin ) ;
136
+ }
137
+ }
138
+ }
139
+ private getConfig ( config : Partial < UserConfig > ) : UserConfig {
140
+ const newConfig = { ...config } ;
127
141
for ( const [ key , value ] of Object . entries ( this ) ) {
128
142
if ( GRIDJS_PROPS . includes ( key as any ) ) {
129
143
newConfig [ key ] = value ;
130
144
}
131
145
}
146
+ this . gridConfig = newConfig ;
132
147
return newConfig ;
133
148
}
134
149
}
0 commit comments