Skip to content

Commit fca28d7

Browse files
committed
feat(state): use interface instead of class #7025
1 parent 9371e2b commit fca28d7

File tree

2 files changed

+90
-161
lines changed

2 files changed

+90
-161
lines changed

projects/igniteui-angular/src/lib/grids/state.directive.ts

Lines changed: 77 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -77,33 +77,12 @@ export interface IColumnState {
7777
searchable: boolean;
7878
}
7979

80-
export enum GridFeatures {
81-
COLUMNS = 'columns',
82-
FILTERING = 'filtering',
83-
ADVANCED_FILTERING = 'advancedFiltering',
84-
SORTING = 'sorting',
85-
PAGING = 'paging',
86-
PINNING_CONFIG = 'pinningConfig',
87-
ROW_PINNING = 'rowPinning',
88-
CELL_SELECTION = 'cellSelection',
89-
ROW_SELECTION = 'rowSelection',
90-
COLUMN_SELECTION = 'columnSelection',
91-
EXPANSION = 'expansion',
92-
ROW_ISLANDS = 'rowIslands'
93-
}
80+
export type GridFeatures = keyof IGridStateOptions;
9481

95-
export enum FlatGridFeatures {
96-
GROUP_BY = 'groupBy',
97-
}
98-
99-
abstract class Feature {
100-
public name: string;
101-
constructor(name: string) {
102-
this.name = lowerize(name);
103-
}
104-
abstract getFeatureState(context: IgxGridStateDirective): IGridState;
105-
abstract restoreFeatureState(context: IgxGridStateDirective, state: IColumnState[] | IPagingState | ISortingExpression[] |
106-
IGroupingState | FilteringExpressionsTree | GridSelectionRange[] | IPinningConfig | any[]): void;
82+
export interface Feature {
83+
getFeatureState: (context: IgxGridStateDirective) => IGridState;
84+
restoreFeatureState: (context: IgxGridStateDirective, state: IColumnState[] | IPagingState | ISortingExpression[] |
85+
IGroupingState | FilteringExpressionsTree | GridSelectionRange[] | IPinningConfig | any[]) => void;
10786
}
10887

10988
@Directive({
@@ -223,8 +202,8 @@ export class IgxGridStateDirective {
223202
let gridState = {} as IGridState;
224203
this.features.forEach(f => {
225204
if (this.options[f]) {
226-
f = f === 'inheritance' ? GridFeatures.ROW_ISLANDS : f;
227-
if (!(this.grid instanceof IgxGridComponent) && f === FlatGridFeatures.GROUP_BY) {
205+
f = f === 'inheritance' ? 'rowIslands' : f;
206+
if (!(this.grid instanceof IgxGridComponent) && f === 'groupBy') {
228207
return;
229208
}
230209
const feature = this.getFeature(f);
@@ -243,7 +222,7 @@ export class IgxGridStateDirective {
243222
this.applyFeatures(features);
244223
this.features.forEach(f => {
245224
if (this.options[f]) {
246-
f = f === 'inheritance' ? GridFeatures.ROW_ISLANDS : f;
225+
f = f === 'inheritance' ? 'rowIslands' : f;
247226
const featureState = state[f];
248227
if (featureState) {
249228
const feature = this.getFeature(f);
@@ -341,49 +320,40 @@ export class IgxGridStateDirective {
341320
}
342321

343322
private getFeature(key: string): Feature {
344-
key = capitalize(key);
345-
const feature = new Features[key](key);
323+
const feature: Feature = FEATURES[key];
346324
return feature;
347325
}
348326
}
349-
350-
namespace Features {
351-
export class Sorting extends Feature {
352-
353-
public getFeatureState(context: IgxGridStateDirective): IGridState {
327+
const FEATURES = {
328+
sorting: {
329+
getFeatureState(context: IgxGridStateDirective): IGridState {
354330
const sortingState = context.currGrid.sortingExpressions;
355331
sortingState.forEach(s => {
356332
delete s.strategy;
357333
delete s.owner;
358334
});
359335
return { sorting: sortingState };
360-
}
361-
362-
public restoreFeatureState(context: IgxGridStateDirective, state: ISortingExpression[]): void {
336+
},
337+
restoreFeatureState(context: IgxGridStateDirective, state: ISortingExpression[]): void {
363338
context.currGrid.sortingExpressions = state;
364339
}
365-
}
366-
367-
export class Filtering extends Feature {
368-
369-
public getFeatureState(context: IgxGridStateDirective): IGridState {
340+
},
341+
filtering: {
342+
getFeatureState(context: IgxGridStateDirective): IGridState {
370343
const filteringState = context.currGrid.filteringExpressionsTree;
371344
delete filteringState.owner;
372345
for (const item of filteringState.filteringOperands) {
373346
delete (item as IFilteringExpressionsTree).owner;
374347
}
375348
return { filtering: filteringState };
376-
}
377-
378-
public restoreFeatureState(context: IgxGridStateDirective, state: FilteringExpressionsTree): void {
349+
},
350+
restoreFeatureState(context: IgxGridStateDirective, state: FilteringExpressionsTree): void {
379351
const filterTree = context.createExpressionsTreeFromObject(state);
380352
context.currGrid.filteringExpressionsTree = filterTree as FilteringExpressionsTree;
381353
}
382-
}
383-
384-
export class AdvancedFiltering extends Feature {
385-
386-
public getFeatureState(context: IgxGridStateDirective): IGridState {
354+
},
355+
advancedFiltering: {
356+
getFeatureState(context: IgxGridStateDirective): IGridState {
387357
const filteringState = context.currGrid.advancedFilteringExpressionsTree;
388358
let advancedFiltering: any;
389359
if (filteringState) {
@@ -396,17 +366,14 @@ namespace Features {
396366
advancedFiltering = {};
397367
}
398368
return { advancedFiltering: advancedFiltering };
399-
}
400-
401-
public restoreFeatureState(context: IgxGridStateDirective, state: FilteringExpressionsTree): void {
369+
},
370+
restoreFeatureState(context: IgxGridStateDirective, state: FilteringExpressionsTree): void {
402371
const filterTree = context.createExpressionsTreeFromObject(state);
403372
context.currGrid.advancedFilteringExpressionsTree = filterTree as FilteringExpressionsTree;
404373
}
405-
}
406-
407-
export class Columns extends Feature {
408-
409-
public getFeatureState(context: IgxGridStateDirective): IGridState {
374+
},
375+
columns: {
376+
getFeatureState(context: IgxGridStateDirective): IGridState {
410377
const gridColumns: IColumnState[] = context.currGrid.columns.map((c) => {
411378
return {
412379
pinned: c.pinned,
@@ -432,9 +399,8 @@ namespace Features {
432399
};
433400
});
434401
return { columns: gridColumns };
435-
}
436-
437-
public restoreFeatureState(context: IgxGridStateDirective, state: IColumnState[]): void {
402+
},
403+
restoreFeatureState(context: IgxGridStateDirective, state: IColumnState[]): void {
438404
const newColumns = [];
439405
const factory = context.resolver.resolveComponentFactory(IgxColumnComponent);
440406
state.forEach((colState) => {
@@ -446,11 +412,9 @@ namespace Features {
446412
context.currGrid.columnList.reset(newColumns);
447413
context.currGrid.columnList.notifyOnChanges();
448414
}
449-
}
450-
451-
export class GroupBy extends Feature {
452-
453-
public getFeatureState(context: IgxGridStateDirective): IGridState {
415+
},
416+
groupBy: {
417+
getFeatureState(context: IgxGridStateDirective): IGridState {
454418
const grid = context.currGrid as IgxGridComponent;
455419
const groupingExpressions = grid.groupingExpressions;
456420
groupingExpressions.forEach(expr => {
@@ -460,9 +424,8 @@ namespace Features {
460424
const groupsExpanded = grid.groupsExpanded;
461425

462426
return { groupBy: { expressions: groupingExpressions, expansion: expansionState, defaultExpanded: groupsExpanded} };
463-
}
464-
465-
public restoreFeatureState(context: IgxGridStateDirective, state: IGroupingState): void {
427+
},
428+
restoreFeatureState(context: IgxGridStateDirective, state: IGroupingState): void {
466429
const grid = context.currGrid as IgxGridComponent;
467430
grid.groupingExpressions = state.expressions as IGroupingExpression[];
468431
if (grid.groupsExpanded !== state.defaultExpanded) {
@@ -471,107 +434,84 @@ namespace Features {
471434
grid.groupingExpansionState = state.expansion as IGroupByExpandState[];
472435
}
473436
}
474-
}
475-
476-
export class Paging extends Feature {
477-
478-
public getFeatureState(context: IgxGridStateDirective): IGridState {
437+
},
438+
paging: {
439+
getFeatureState(context: IgxGridStateDirective): IGridState {
479440
const pagingState = context.currGrid.pagingState;
480441
return { paging: pagingState };
481-
}
482-
483-
public restoreFeatureState(context: IgxGridStateDirective, state: IPagingState): void {
442+
},
443+
restoreFeatureState(context: IgxGridStateDirective, state: IPagingState): void {
484444
if (context.currGrid.perPage !== state.recordsPerPage) {
485445
context.currGrid.perPage = state.recordsPerPage;
486446
context.currGrid.cdr.detectChanges();
487447
}
488448
context.currGrid.page = state.index;
489449
}
490-
}
491-
492-
export class RowSelection extends Feature {
493-
494-
public getFeatureState(context: IgxGridStateDirective): IGridState {
450+
},
451+
rowSelection: {
452+
getFeatureState(context: IgxGridStateDirective): IGridState {
495453
const selection = context.currGrid.selectedRows();
496454
return { rowSelection: selection };
497-
}
498-
499-
public restoreFeatureState(context: IgxGridStateDirective, state: any[]): void {
455+
},
456+
restoreFeatureState(context: IgxGridStateDirective, state: any[]): void {
500457
context.currGrid.selectRows(state);
501458
}
502-
}
503-
504-
export class CellSelection extends Feature {
505-
506-
public getFeatureState(context: IgxGridStateDirective): IGridState {
459+
},
460+
cellSelection: {
461+
getFeatureState(context: IgxGridStateDirective): IGridState {
507462
const selection = context.currGrid.getSelectedRanges().map(range => {
508463
return { rowStart: range.rowStart, rowEnd: range.rowEnd, columnStart: range.columnStart, columnEnd: range.columnEnd };
509464
});
510465
return { cellSelection: selection };
511-
}
512-
513-
public restoreFeatureState(context: IgxGridStateDirective, state: GridSelectionRange[]): void {
466+
},
467+
restoreFeatureState(context: IgxGridStateDirective, state: GridSelectionRange[]): void {
514468
state.forEach(r => {
515469
const range = { rowStart: r.rowStart, rowEnd: r.rowEnd, columnStart: r.columnStart, columnEnd: r.columnEnd};
516470
context.currGrid.selectRange(range);
517471
});
518472
}
519-
}
520-
521-
export class ColumnSelection extends Feature {
522-
523-
public getFeatureState(context: IgxGridStateDirective): IGridState {
473+
},
474+
columnSelection: {
475+
getFeatureState(context: IgxGridStateDirective): IGridState {
524476
const selection = context.currGrid.selectedColumns().map(c => c.field);
525477
return { columnSelection: selection };
526-
}
527-
528-
public restoreFeatureState(context: IgxGridStateDirective, state: string[]): void {
478+
},
479+
restoreFeatureState(context: IgxGridStateDirective, state: string[]): void {
529480
context.currGrid.deselectAllColumns();
530481
context.currGrid.selectColumns(state);
531482
}
532-
}
533-
534-
export class RowPinning extends Feature {
535-
536-
public getFeatureState(context: IgxGridStateDirective): IGridState {
483+
},
484+
rowPinning: {
485+
getFeatureState(context: IgxGridStateDirective): IGridState {
537486
const pinned = context.currGrid.pinnedRows.map(x => x.rowID);
538487
return { rowPinning: pinned };
539-
}
540-
541-
public restoreFeatureState(context: IgxGridStateDirective, state: any[]): void {
488+
},
489+
restoreFeatureState(context: IgxGridStateDirective, state: any[]): void {
542490
// clear current state.
543491
context.currGrid.pinnedRows.forEach(row => row.unpin());
544492
state.forEach(rowID => context.currGrid.pinRow(rowID));
545493
}
546-
}
547-
548-
export class PinningConfig extends Feature {
549-
550-
public getFeatureState(context: IgxGridStateDirective): IGridState {
494+
},
495+
pinningConfig: {
496+
getFeatureState(context: IgxGridStateDirective): IGridState {
551497
return { pinningConfig: context.currGrid.pinning };
552-
}
553-
554-
public restoreFeatureState(context: IgxGridStateDirective, state: IPinningConfig): void {
498+
},
499+
restoreFeatureState(context: IgxGridStateDirective, state: IPinningConfig): void {
555500
context.currGrid.pinning = state;
556501
}
557-
}
558-
559-
export class Expansion extends Feature {
560-
561-
public getFeatureState(context: IgxGridStateDirective): IGridState {
502+
},
503+
expansion: {
504+
getFeatureState(context: IgxGridStateDirective): IGridState {
562505
const expansionStates = Array.from(context.currGrid.expansionStates);
563506
return { expansion: expansionStates };
564-
}
565-
566-
public restoreFeatureState(context: IgxGridStateDirective, state: any[]): void {
507+
},
508+
restoreFeatureState(context: IgxGridStateDirective, state: any[]): void {
567509
const expansionStates = new Map<any, boolean>(state);
568510
context.currGrid.expansionStates = expansionStates;
569511
}
570-
}
571-
572-
export class RowIslands extends Feature {
573-
574-
public getFeatureState(context: IgxGridStateDirective): IGridState {
512+
},
513+
rowIslands: {
514+
getFeatureState(context: IgxGridStateDirective): IGridState {
575515
const childGridStates: IGridStateCollection[] = [];
576516
const rowIslands = (context.currGrid as any).allLayoutList;
577517
if (rowIslands) {
@@ -589,9 +529,8 @@ namespace Features {
589529
}
590530
context.currGrid = context.grid;
591531
return { rowIslands: childGridStates };
592-
}
593-
594-
public restoreFeatureState(context: IgxGridStateDirective, state: any): void {
532+
},
533+
restoreFeatureState(context: IgxGridStateDirective, state: any): void {
595534
const rowIslands = (context.currGrid as any).allLayoutList;
596535
if (rowIslands) {
597536
rowIslands.forEach(rowIsland => {
@@ -607,12 +546,11 @@ namespace Features {
607546
});
608547
}
609548
context.currGrid = context.grid;
610-
}
611-
549+
},
612550
/**
613551
* Traverses the hierarchy up to the root grid to return the ID of the expanded row.
614552
*/
615-
private getParentRowID(grid: IgxHierarchicalGridComponent) {
553+
getParentRowID(grid: IgxHierarchicalGridComponent) {
616554
let childGrid, childRow;
617555
while (grid.parent) {
618556
childRow = grid.childRow;
@@ -622,16 +560,7 @@ namespace Features {
622560
return grid.hgridAPI.getParentRowId(childGrid);
623561
}
624562
}
625-
626-
}
627-
628-
function capitalize(key: string): string {
629-
return key.charAt(0).toUpperCase() + key.slice(1);
630-
}
631-
632-
function lowerize(key: string): string {
633-
return key.charAt(0).toLowerCase() + key.slice(1);
634-
}
563+
};
635564

636565
/**
637566
* @hidden

0 commit comments

Comments
 (0)