11import { Pipe , PipeTransform } from '@angular/core' ;
2+ import { IPivotKeys } from 'igniteui-angular' ;
23import { cloneArray , cloneValue } from '../../core/utils' ;
34import { DataUtil } from '../../data-operations/data-util' ;
45import { FilteringExpressionsTree , IFilteringExpressionsTree } from '../../data-operations/filtering-expressions-tree' ;
@@ -9,7 +10,7 @@ import { IPivotDimension, IPivotValue } from './pivot-grid.interface';
910 * @hidden
1011 */
1112@Pipe ( {
12- name : 'gridPivotRow ' ,
13+ name : 'pivotGridRow ' ,
1314 pure : true
1415} )
1516export class IgxPivotRowPipe implements PipeTransform {
@@ -19,14 +20,15 @@ export class IgxPivotRowPipe implements PipeTransform {
1920 public transform (
2021 collection : any ,
2122 rows : IPivotDimension [ ] ,
22- values ?: IPivotValue [ ]
23+ values ?: IPivotValue [ ] ,
24+ pivotKeys : IPivotKeys = { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
2325 ) : any [ ] {
2426 // build hierarchies - groups and subgroups
25- const hierarchies = PivotUtil . getFieldsHierarchy ( collection , rows ) ;
27+ const hierarchies = PivotUtil . getFieldsHierarchy ( collection , rows , pivotKeys ) ;
2628 // apply aggregations based on the created groups
27- PivotUtil . applyAggregations ( hierarchies , values ) ;
29+ PivotUtil . applyAggregations ( hierarchies , values , pivotKeys ) ;
2830 // generate flat data from the hierarchies
29- const data = PivotUtil . flattenHierarchy ( hierarchies , collection [ 0 ] ?? [ ] ) ;
31+ const data = PivotUtil . flattenHierarchy ( hierarchies , collection [ 0 ] ?? [ ] , pivotKeys ) ;
3032 return data ;
3133 }
3234}
@@ -35,41 +37,44 @@ export class IgxPivotRowPipe implements PipeTransform {
3537 * @hidden
3638 */
3739@Pipe ( {
38- name : 'gridPivotColumn ' ,
40+ name : 'pivotGridColumn ' ,
3941 pure : true
4042} )
4143export class IgxPivotColumnPipe implements PipeTransform {
4244
4345 public transform (
4446 collection : any ,
4547 columns : IPivotDimension [ ] ,
46- values ?: IPivotValue [ ]
48+ values ?: IPivotValue [ ] ,
49+ pivotKeys : IPivotKeys = { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
4750 ) : any [ ] {
4851 // build hierarchies - groups and subgroups by columns
4952 const result = [ ] ;
5053 collection . forEach ( hierarchy => {
5154 // apply aggregations based on the created groups and generate column fields based on the hierarchies
52- this . groupColumns ( hierarchy , columns , values ) ;
53- if ( hierarchy [ ' children' ] ) {
55+ this . groupColumns ( hierarchy , columns , values , pivotKeys ) ;
56+ if ( hierarchy [ pivotKeys . children ] ) {
5457 let flatCols = { } ;
55- PivotUtil . flattenColumnHierarchy ( hierarchy [ ' children' ] , values ) . forEach ( o => {
56- delete o [ ' records' ] ;
58+ PivotUtil . flattenColumnHierarchy ( hierarchy [ pivotKeys . children ] , values , pivotKeys ) . forEach ( o => {
59+ delete o [ pivotKeys . records ] ;
5760 flatCols = { ...flatCols , ...o } ;
5861 } ) ;
62+ delete hierarchy [ pivotKeys . children ] ; /* or we can keep it
63+ and use when creating the columns in pivot grid instead of recreating it */
5964 result . push ( { ...hierarchy , ...flatCols } ) ;
6065 }
6166 } ) ;
6267
6368 return result ;
6469 }
6570
66- private groupColumns ( hierarchy , columns , values ) {
67- const children = hierarchy [ ' children' ] ;
71+ private groupColumns ( hierarchy , columns , values , pivotKeys ) {
72+ const children = hierarchy [ pivotKeys . children ] ;
6873 if ( children ) {
69- this . groupColumns ( children , columns , values ) ;
70- } else if ( hierarchy [ ' records' ] ) {
71- hierarchy [ ' children' ] = PivotUtil . getFieldsHierarchy ( hierarchy [ ' records' ] , columns ) ;
72- PivotUtil . applyAggregations ( hierarchy [ ' children' ] , values ) ;
74+ this . groupColumns ( children , columns , values , pivotKeys ) ;
75+ } else if ( hierarchy [ pivotKeys . records ] ) {
76+ hierarchy [ pivotKeys . children ] = PivotUtil . getFieldsHierarchy ( hierarchy [ pivotKeys . records ] , columns , pivotKeys ) ;
77+ PivotUtil . applyAggregations ( hierarchy [ pivotKeys . children ] , values , pivotKeys ) ;
7378 }
7479 }
7580
@@ -80,7 +85,7 @@ export class IgxPivotColumnPipe implements PipeTransform {
8085 * @hidden
8186 */
8287@Pipe ( {
83- name : 'gridPivotFilter ' ,
88+ name : 'pivotGridFilter ' ,
8489 pure : true
8590} )
8691export class IgxPivotGridFilterPipe implements PipeTransform {
@@ -107,17 +112,17 @@ export class IgxPivotGridFilterPipe implements PipeTransform {
107112}
108113
109114export class PivotUtil {
110- public static getFieldsHierarchy ( data : any [ ] , columns : IPivotDimension [ ] ) : Map < string , any > {
115+ public static getFieldsHierarchy ( data : any [ ] , columns : IPivotDimension [ ] , pivotKeys : IPivotKeys ) : Map < string , any > {
111116 const hierarchy = new Map < string , any > ( ) ;
112117 for ( const rec of data ) {
113118 const vals = this . extractValuesFromDimension ( columns , rec ) ;
114119 for ( const val of vals ) { // this should go in depth also vals.children
115120 if ( hierarchy . get ( val . value ) != null && val . children ) {
116- this . applyHierarchyChildren ( hierarchy , val , rec ) ;
121+ this . applyHierarchyChildren ( hierarchy , val , rec , pivotKeys . records ) ;
117122 } else {
118123 hierarchy . set ( val . value , cloneValue ( val ) ) ;
119124 hierarchy . get ( val . value ) . children = new Map < string , any > ( ) ;
120- this . applyHierarchyChildren ( hierarchy , val , rec ) ;
125+ this . applyHierarchyChildren ( hierarchy , val , rec , pivotKeys . records ) ;
121126 }
122127 }
123128 }
@@ -143,15 +148,15 @@ export class PivotUtil {
143148 return vals ;
144149 }
145150
146- public static applyAggregations ( hierarchies , values ) {
151+ public static applyAggregations ( hierarchies , values , pivotKeys ) {
147152 hierarchies . forEach ( ( hierarchy ) => {
148- const children = hierarchy [ ' children' ] ;
153+ const children = hierarchy [ pivotKeys . children ] ;
149154 if ( children ) {
150- this . applyAggregations ( children , values ) ;
151- const childrenAggregations = this . collectAggregations ( children ) ;
152- hierarchy [ ' aggregations' ] = this . aggregate ( childrenAggregations , values ) ;
153- } else if ( hierarchy [ ' records' ] ) {
154- hierarchy [ ' aggregations' ] = this . aggregate ( hierarchy [ ' records' ] , values ) ;
155+ this . applyAggregations ( children , values , pivotKeys ) ;
156+ const childrenAggregations = this . collectAggregations ( children , pivotKeys ) ;
157+ hierarchy [ pivotKeys . aggregations ] = this . aggregate ( childrenAggregations , values ) ;
158+ } else if ( hierarchy [ pivotKeys . records ] ) {
159+ hierarchy [ pivotKeys . aggregations ] = this . aggregate ( hierarchy [ pivotKeys . records ] , values ) ;
155160 }
156161 } ) ;
157162 }
@@ -165,33 +170,36 @@ export class PivotUtil {
165170 return result ;
166171 }
167172
168- public static flattenHierarchy ( hierarchies , rec ) {
173+ public static flattenHierarchy ( hierarchies , rec , pivotKeys ) {
169174 let flatData = [ ] ;
170175 const field = this . generateFieldValue ( rec ) ;
171176 hierarchies . forEach ( ( h , key ) => {
172177 let obj = { } ;
173178 obj [ field ] = key ;
174- obj [ ' records' ] = h [ ' records' ] ;
175- obj = { ...obj , ...h [ ' aggregations' ] } ;
179+ obj [ pivotKeys . records ] = h [ pivotKeys . records ] ;
180+ obj = { ...obj , ...h [ pivotKeys . aggregations ] } ;
176181 flatData . push ( obj ) ;
177- if ( h [ 'children' ] ) {
178- flatData = [ ...flatData , ...this . flattenHierarchy ( h [ 'children' ] , rec ) ] ;
182+ if ( h [ pivotKeys . children ] ) {
183+ let childRecords = [ ] ;
184+ h [ pivotKeys . children ] . forEach ( c => childRecords = [ ...childRecords , ...c [ pivotKeys . records ] ] ) ;
185+ obj [ pivotKeys . records ] = obj [ pivotKeys . records ] ? [ ...obj [ pivotKeys . records ] , ...childRecords ] : childRecords ;
186+ flatData = [ ...flatData , ...this . flattenHierarchy ( h [ pivotKeys . children ] , rec , pivotKeys ) ] ;
179187 }
180188 } ) ;
181189
182190 return flatData ;
183191 }
184192
185- public static flattenColumnHierarchy ( hierarchies , values ) {
193+ public static flattenColumnHierarchy ( hierarchies , values , pivotKeys ) {
186194 let flatData = [ ] ;
187195 hierarchies . forEach ( ( h , key ) => {
188196 const obj = { } ;
189197 for ( const value of values ) {
190- obj [ key ] = h [ ' aggregations' ] [ value . member ] ;
191- obj [ ' records' ] = h [ ' records' ] ;
198+ obj [ key ] = h [ pivotKeys . aggregations ] [ value . member ] ;
199+ obj [ pivotKeys . records ] = h [ pivotKeys . records ] ;
192200 flatData . push ( obj ) ;
193- if ( h [ ' children' ] ) {
194- flatData = [ ...flatData , ...this . flattenColumnHierarchy ( h [ ' children' ] , values ) ] ;
201+ if ( h [ pivotKeys . children ] ) {
202+ flatData = [ ...flatData , ...this . flattenColumnHierarchy ( h [ pivotKeys . children ] , values , pivotKeys ) ] ;
195203 }
196204 }
197205 } ) ;
@@ -205,14 +213,14 @@ export class PivotUtil {
205213 return 'field' + i ;
206214 }
207215
208- private static collectAggregations ( children ) {
216+ private static collectAggregations ( children , pivotKeys ) {
209217 const result = [ ] ;
210- children . forEach ( value => result . push ( value [ ' aggregations' ] ) ) ;
218+ children . forEach ( value => result . push ( value [ pivotKeys . aggregations ] ) ) ;
211219
212220 return result ;
213221 }
214222
215- private static applyHierarchyChildren ( hierarchy , val , rec , recordsKey = 'records' ) {
223+ private static applyHierarchyChildren ( hierarchy , val , rec , recordsKey ) {
216224 for ( const child of val . children ) {
217225 if ( ! hierarchy . get ( val . value ) . children . get ( child . value ) ) {
218226 hierarchy . get ( val . value ) . children . set ( child . value , child ) ;
0 commit comments