1
- import { Directive , Optional , Self , Input , NgModule , Inject , AfterViewInit } from '@angular/core' ;
1
+ import { Directive , Optional , Self , Input , NgModule , Inject } from '@angular/core' ;
2
2
import { ISortingExpression } from '../data-operations/sorting-expression.interface' ;
3
3
import { FilteringExpressionsTree , IFilteringExpressionsTree } from '../data-operations/filtering-expressions-tree' ;
4
4
import { IFilteringExpression } from '../data-operations/filtering-expression.interface' ;
@@ -7,6 +7,9 @@ import { INTERFACE_TOKEN } from './grid/grid.component';
7
7
import { IgxColumnComponent } from './columns/column.component' ;
8
8
import { IGroupingExpression } from '../data-operations/grouping-expression.interface' ;
9
9
import { IPagingState } from '../data-operations/paging-state.interface' ;
10
+ import { DataType } from '../data-operations/data-util' ;
11
+ import { IgxBooleanFilteringOperand , IgxNumberFilteringOperand , IgxDateFilteringOperand ,
12
+ IgxStringFilteringOperand } from '../data-operations/filtering-condition' ;
10
13
11
14
export interface IGridState {
12
15
columns : IColumnState [ ] ;
@@ -32,6 +35,8 @@ interface IColumnState {
32
35
pinned : boolean ;
33
36
sortable : boolean ;
34
37
filterable : boolean ;
38
+ editable : boolean ;
39
+ groupable : boolean ;
35
40
movable : boolean ;
36
41
hidden : boolean ;
37
42
dataType : string ;
@@ -56,7 +61,7 @@ const ACTION_SELECTION = 'selection';
56
61
@Directive ( {
57
62
selector : '[igxGridState]'
58
63
} )
59
- export class IgxGridStateDirective implements AfterViewInit {
64
+ export class IgxGridStateDirective {
60
65
61
66
private _options : IGridStateOptions = {
62
67
columns : true ,
@@ -74,27 +79,37 @@ export class IgxGridStateDirective implements AfterViewInit {
74
79
/**
75
80
* An object with options determining if a certain feature state should be saved.
76
81
*
82
+ * ```html
83
+ * <igx-grid [igxGridState]="options"></igx-grid>
84
+ * ```
77
85
* ```typescript
78
- * // get the row data for the first selected row
79
- * let saveSortingState = this.grid.state.options.sorting;
86
+ * public options = {selection: false, advancedFiltering: false};
80
87
* ```
81
88
*/
82
89
@Input ( 'igxGridState' )
83
90
public get options ( ) : IGridStateOptions {
84
91
return this . _options ;
85
92
}
86
93
87
- public set options ( val : IGridStateOptions ) {
88
- Object . assign ( this . _options , val ) ;
94
+ public set options ( value : IGridStateOptions ) {
95
+ Object . assign ( this . _options , value ) ;
89
96
}
90
97
91
98
constructor ( @Inject ( INTERFACE_TOKEN ) @Self ( ) @Optional ( ) private grid ) { }
92
99
93
- public ngAfterViewInit ( ) {
94
- this . restoreGridState ( this . state ) ;
95
- // this.grid.cdr.detectChanges();
96
- }
97
-
100
+ /**
101
+ * Sets the state of a feature or states of all grid features, depending on the state object passed as an argument.
102
+ * Pass an IGridState object to set the state for all features, or IPagingState object to set the paging state only.
103
+ * returns an object containing all grid features states that are enabled through the `options` property.
104
+ * ```html
105
+ * <igx-grid [igxGridState]="options"></igx-grid>
106
+ * ```
107
+ * ```typescript
108
+ * @ViewChild (IgxGridStateDirective, { static: true }) public state;
109
+ * const gridState = window.localStorage.getItem(key);
110
+ * this.state.setState(gridState);
111
+ * ```
112
+ */
98
113
public setState ( state : IGridState |
99
114
IColumnState |
100
115
IFilteringExpressionsTree |
@@ -106,9 +121,23 @@ export class IgxGridStateDirective implements AfterViewInit {
106
121
}
107
122
this . state = state as IGridState | IColumnState | IFilteringExpressionsTree |
108
123
ISortingExpression | IGroupingExpression | IPagingState ;
124
+ this . restoreGridState ( this . state ) ;
109
125
}
110
126
111
- public getState ( feature ?: string | string [ ] , serialize = true ) : IGridState |
127
+ /**
128
+ * Gets the state of a feature or states of all grid features.
129
+ * If a feature name is not passed as an argument,
130
+ * returns an object containing all grid features states that are enabled through the `options` property.
131
+ * The optional `serialize` argument determines whether the returned object will be serialized to a JSON string. Default value is false.
132
+ * ```html
133
+ * <igx-grid [igxGridState]="options"></igx-grid>
134
+ * ```
135
+ * ```typescript
136
+ * @ViewChild (IgxGridStateDirective, { static: true }) public state;
137
+ * let state = this.state.getState();
138
+ * ```
139
+ */
140
+ public getState ( serialize = true , feature ?: string | string [ ] ) : IGridState |
112
141
IColumnState |
113
142
IFilteringExpressionsTree |
114
143
ISortingExpression |
@@ -123,25 +152,33 @@ export class IgxGridStateDirective implements AfterViewInit {
123
152
if ( feature ) {
124
153
if ( Array . isArray ( feature ) ) {
125
154
feature . forEach ( f => {
126
- state [ f ] = this . getGridFeature ( f , serialize ) ;
155
+ state [ f ] = this . getGridFeature ( f ) ;
127
156
} ) ;
128
157
} else {
129
- state [ feature ] = this . getGridFeature ( feature , serialize ) ;
158
+ state [ feature ] = this . getGridFeature ( feature ) ;
130
159
}
131
160
} else {
132
- state = this . getAllGridFeatures ( serialize ) as IGridState ;
161
+ state = this . getAllGridFeatures ( ) as IGridState ;
162
+ }
163
+ if ( serialize ) {
164
+ state = JSON . stringify ( state , this . stringifyCallback ) ;
165
+ return state as string ;
166
+ } else {
167
+ return state as IGridState ;
133
168
}
134
- return state ;
135
169
}
136
170
171
+ /**
172
+ * Helper method that creates a new array with the current grid columns.
173
+ */
137
174
public restoreGridState ( state ) {
138
175
for ( const key of Object . keys ( state ) ) {
139
176
this . restoreFeature ( key , state [ key ] ) ;
140
177
}
141
178
}
142
179
143
180
/**
144
- * Applies the state for a given feature.
181
+ * Restores the state of a feature.
145
182
*/
146
183
private restoreFeature ( feature : string , state : any ) {
147
184
switch ( feature ) {
@@ -176,7 +213,10 @@ export class IgxGridStateDirective implements AfterViewInit {
176
213
}
177
214
}
178
215
179
- private getAllGridFeatures ( serialize = false ) : IGridState | string {
216
+ /**
217
+ * Returns an object containing all grid features state.
218
+ */
219
+ private getAllGridFeatures ( ) : IGridState {
180
220
let gridState = { } ;
181
221
182
222
for ( const key of Object . keys ( this . options ) ) {
@@ -187,15 +227,14 @@ export class IgxGridStateDirective implements AfterViewInit {
187
227
}
188
228
189
229
gridState = Object . assign ( { } , gridState ) ;
190
- if ( serialize ) {
191
- gridState = JSON . stringify ( gridState , this . stringifyCallback ) ;
192
- return gridState as string ;
193
- } else {
194
- return gridState as IGridState ;
195
- }
230
+ return gridState as IGridState ;
196
231
}
197
232
198
- private getGridFeature ( feature : string , serialize = false ) {
233
+ /**
234
+ * Restores an object containing the state for a grid feature.
235
+ * `serialize` param determines whether the returned object will be serialized to a JSON string. Default value is false.,
236
+ */
237
+ private getGridFeature ( feature : string ) {
199
238
let state = null ;
200
239
switch ( feature ) {
201
240
case ACTION_COLUMNS : {
@@ -227,12 +266,7 @@ export class IgxGridStateDirective implements AfterViewInit {
227
266
break ;
228
267
}
229
268
}
230
- if ( serialize ) {
231
- state = JSON . stringify ( state , this . stringifyCallback ) ;
232
- return state as string ;
233
- } else {
234
- return state ;
235
- }
269
+ return state ;
236
270
}
237
271
238
272
/**
@@ -244,6 +278,7 @@ export class IgxGridStateDirective implements AfterViewInit {
244
278
pinned : c . pinned ,
245
279
sortable : c . sortable ,
246
280
filterable : c . filterable ,
281
+ editable : c . editable ,
247
282
movable : c . movable ,
248
283
hidden : c . hidden ,
249
284
dataType : c . dataType ,
@@ -278,7 +313,7 @@ export class IgxGridStateDirective implements AfterViewInit {
278
313
}
279
314
280
315
private getGroupBy ( ) {
281
- const groupingState = this . grid . groupbyExpressions ;
316
+ const groupingState = this . grid . groupingExpressions ;
282
317
return { groupby : groupingState } ;
283
318
}
284
319
@@ -288,7 +323,7 @@ export class IgxGridStateDirective implements AfterViewInit {
288
323
}
289
324
290
325
/**
291
- * This method modifies the grid column list to restore the columns .
326
+ * Restores the grid columns by modifying the `columnList` collection of the grid .
292
327
*/
293
328
private restoreColumns ( columns : IColumnState [ ] ) : void {
294
329
const newColumns = [ ] ;
@@ -298,6 +333,8 @@ export class IgxGridStateDirective implements AfterViewInit {
298
333
ref . instance . field = col . field ;
299
334
ref . instance . dataType = col . dataType ;
300
335
ref . instance . sortable = col . sortable ;
336
+ ref . instance . groupable = col . groupable ;
337
+ ref . instance . editable = col . editable ;
301
338
ref . instance . filterable = col . filterable ;
302
339
ref . instance . resizable = col . resizable ;
303
340
ref . instance . movable = col . movable ;
@@ -314,15 +351,25 @@ export class IgxGridStateDirective implements AfterViewInit {
314
351
this . grid . columnList . notifyOnChanges ( ) ;
315
352
}
316
353
354
+ /**
355
+ * Restores the grid filtering state, i.e. sets the `filteringExpressionsTree` property value.
356
+ */
317
357
private restoreFiltering ( state : FilteringExpressionsTree ) {
318
358
const filterTree = this . createExpressionsTreeFromObject ( state ) ;
319
359
this . grid . filteringExpressionsTree = filterTree ;
320
360
}
321
361
362
+ /**
363
+ * Restores the grid advanced filtering state, i.e. sets the `advancedFilteringExpressionsTree` property value.
364
+ */
322
365
private restoreAdvancedFiltering ( state ) {
323
366
const advFilterTree = this . createExpressionsTreeFromObject ( state . advancedFiltering ) ;
324
367
this . grid . advancedFilteringExpressionsTree = advFilterTree ;
325
368
}
369
+
370
+ /**
371
+ * Restores the grid sorting state, i.e. sets the `sortingExpressions` property value.
372
+ */
326
373
private restoreSorting ( state : ISortingExpression | ISortingExpression [ ] ) {
327
374
const strategy = DefaultSortingStrategy . instance ( ) ;
328
375
@@ -335,6 +382,9 @@ export class IgxGridStateDirective implements AfterViewInit {
335
382
this . grid . sortingExpressions = state ;
336
383
}
337
384
385
+ /**
386
+ * Restores the grid grouping state, i.e. sets the `groupbyExpressions` property value.
387
+ */
338
388
private restoreGroupBy ( state : IGroupingExpression | IGroupingExpression [ ] ) {
339
389
const strategy = DefaultSortingStrategy . instance ( ) ;
340
390
@@ -344,9 +394,12 @@ export class IgxGridStateDirective implements AfterViewInit {
344
394
( state as IGroupingExpression ) . strategy = strategy ;
345
395
}
346
396
347
- this . grid . groupbyExpressions = state ;
397
+ this . grid . groupingExpressions = state ;
348
398
}
349
399
400
+ /**
401
+ * Restores the grid paging state, i.e. sets the `perPage` property value and paginate to index.
402
+ */
350
403
private restorePaging ( state : IPagingState ) {
351
404
if ( this . grid . perPage !== state . recordsPerPage ) {
352
405
this . grid . perPage = state . recordsPerPage ;
@@ -378,16 +431,39 @@ export class IgxGridStateDirective implements AfterViewInit {
378
431
expressionsTree . filteringOperands . push ( subTree ) ;
379
432
} else {
380
433
const expr = item as IFilteringExpression ;
381
- const column = this . grid . getColumnByName ( expr . fieldName ) ;
382
- expr . condition = column . filters . condition ( expr . condition . name ) ;
383
- expr . searchVal = ( column . dataType === 'date' ) ? new Date ( Date . parse ( expr . searchVal ) ) : expr . searchVal ;
434
+ const dataType = this . state [ ACTION_COLUMNS ] . find ( c => c . field === expr . fieldName ) . dataType ;
435
+ expr . condition = this . generateFilteringCondition ( dataType , expr . condition . name ) ;
436
+ expr . searchVal = ( dataType === 'date' ) ? new Date ( Date . parse ( expr . searchVal ) ) : expr . searchVal ;
384
437
expressionsTree . filteringOperands . push ( expr ) ;
385
438
}
386
439
}
387
440
388
441
return expressionsTree ;
389
442
}
390
443
444
+ /**
445
+ * Returns the filtering logic function for a given dataType and condition (contains, greaterThan, etc.)
446
+ */
447
+ private generateFilteringCondition ( dataType : string , name : string ) : any {
448
+ let filters ;
449
+ switch ( dataType ) {
450
+ case DataType . Boolean :
451
+ filters = IgxBooleanFilteringOperand . instance ( ) ;
452
+ break ;
453
+ case DataType . Number :
454
+ filters = IgxNumberFilteringOperand . instance ( ) ;
455
+ break ;
456
+ case DataType . Date :
457
+ filters = IgxDateFilteringOperand . instance ( ) ;
458
+ break ;
459
+ case DataType . String :
460
+ default :
461
+ filters = IgxStringFilteringOperand . instance ( ) ;
462
+ break ;
463
+ }
464
+ return filters . condition ( name ) ;
465
+ }
466
+
391
467
private stringifyCallback ( key : string , val : any ) {
392
468
if ( key === 'searchVal' && val instanceof Set ) {
393
469
return Array . from ( val ) ;
0 commit comments