|
| 1 | +import { Pipe, PipeTransform } from '@angular/core'; |
| 2 | +import { cloneArray } from '../core/utils'; |
| 3 | +import { DataUtil } from '../data-operations/data-util'; |
| 4 | +import { IGroupByExpandState } from '../data-operations/groupby-expand-state.interface'; |
| 5 | +import { IgxTreeGridAPIService } from './tree-grid-api.service'; |
| 6 | +import { IGridBaseComponent } from '../grid-common/common/grid-interfaces'; |
| 7 | +import { GridBaseAPIService } from '../grid-common/api.service'; |
| 8 | +import { IgxTreeGridComponent } from './tree-grid.component'; |
| 9 | + |
| 10 | +export interface IHierarchicalRecord { |
| 11 | + data: any; |
| 12 | + children: IHierarchicalRecord[]; |
| 13 | +} |
| 14 | + |
| 15 | +export interface IHierarchizedResult { |
| 16 | + data: IHierarchicalRecord[]; |
| 17 | +} |
| 18 | + |
| 19 | +export interface IFlattenedRecord { |
| 20 | + data: any; |
| 21 | + hasChildren: boolean; |
| 22 | + indentationLevel: number; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + *@hidden |
| 27 | + */ |
| 28 | +@Pipe({ |
| 29 | + name: 'treeGridHierarchizing', |
| 30 | + pure: true |
| 31 | +}) |
| 32 | +export class IgxTreeGridHierarchizingPipe implements PipeTransform { |
| 33 | + private gridAPI: IgxTreeGridAPIService; |
| 34 | + |
| 35 | + constructor(gridAPI: GridBaseAPIService<IGridBaseComponent>) { |
| 36 | + this.gridAPI = <IgxTreeGridAPIService>gridAPI; |
| 37 | + } |
| 38 | + |
| 39 | + public transform(collection: any[], childDataKey: string, |
| 40 | + id: string, pipeTrigger: number): IHierarchizedResult { |
| 41 | + const hirerchicalRecords = this.hierarchizeRecursive(collection, childDataKey); |
| 42 | + |
| 43 | + return { |
| 44 | + data: hirerchicalRecords |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + private hierarchizeRecursive(collection: any[], childDataKey: string): IHierarchicalRecord[] { |
| 49 | + const result: IHierarchicalRecord[] = []; |
| 50 | + |
| 51 | + for (let i = 0; i < collection.length; i++) { |
| 52 | + const item = collection[i]; |
| 53 | + result.push({ |
| 54 | + data: item, |
| 55 | + children: item[childDataKey] ? this.hierarchizeRecursive(item[childDataKey], childDataKey) : null |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + return result; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + *@hidden |
| 65 | + */ |
| 66 | +@Pipe({ |
| 67 | + name: 'treeGridFlattening', |
| 68 | + pure: true |
| 69 | +}) |
| 70 | +export class IgxTreeGridFlatteningPipe implements PipeTransform { |
| 71 | + private gridAPI: IgxTreeGridAPIService; |
| 72 | + |
| 73 | + constructor(gridAPI: GridBaseAPIService<IGridBaseComponent>) { |
| 74 | + this.gridAPI = <IgxTreeGridAPIService>gridAPI; |
| 75 | + } |
| 76 | + |
| 77 | + public transform(collection: IHierarchizedResult, |
| 78 | + id: string, pipeTrigger: number): any[] { |
| 79 | + |
| 80 | + const grid: IgxTreeGridComponent = this.gridAPI.get(id); |
| 81 | + |
| 82 | + const data: IFlattenedRecord[] = []; |
| 83 | + |
| 84 | + this.getFlatDataRecusrive(collection.data, data, 0); |
| 85 | + |
| 86 | + return data; |
| 87 | + } |
| 88 | + |
| 89 | + private getFlatDataRecusrive(collection: IHierarchicalRecord[], data: IFlattenedRecord[] = [], indentationLevel: number) { |
| 90 | + |
| 91 | + if (!collection || !collection.length) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + for (let i = 0; i < collection.length; i++) { |
| 96 | + const hirarchicalRecord = collection[i]; |
| 97 | + const flatRecord: IFlattenedRecord = { |
| 98 | + data: hirarchicalRecord.data, |
| 99 | + indentationLevel: indentationLevel, |
| 100 | + hasChildren: hirarchicalRecord.children && hirarchicalRecord.children.length > 0 |
| 101 | + }; |
| 102 | + data.push(flatRecord); |
| 103 | + this.getFlatDataRecusrive(hirarchicalRecord.children, data, indentationLevel + 1); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments