1
1
import {
2
2
ChangeDetectionStrategy ,
3
+ ChangeDetectorRef ,
3
4
Component ,
4
- Input
5
+ ComponentFactoryResolver ,
6
+ ElementRef ,
7
+ Inject ,
8
+ Input ,
9
+ SimpleChanges ,
10
+ TemplateRef ,
11
+ ViewChild ,
12
+ ViewContainerRef
5
13
} from '@angular/core' ;
14
+ import { IgxColumnGroupComponent } from '../columns/column-group.component' ;
15
+ import { IgxColumnLayoutComponent } from '../columns/column-layout.component' ;
16
+ import { IgxColumnComponent } from '../columns/column.component' ;
17
+ import { IGX_GRID_BASE , PivotGridType } from '../common/grid.interface' ;
6
18
import { IgxGridHeaderRowComponent } from '../headers/grid-header-row.component' ;
7
19
import { IgxRowDirective } from '../row.directive' ;
20
+ import { IPivotDimension , IPivotDimensionData } from './pivot-grid.interface' ;
21
+ import { PivotUtil } from './pivot-util' ;
8
22
9
23
/**
10
24
*
@@ -25,5 +39,186 @@ export class IgxPivotRowDimensionContentComponent extends IgxGridHeaderRowCompon
25
39
* @internal
26
40
*/
27
41
@Input ( )
28
- public intRow : IgxRowDirective ;
42
+ public rowIndex : number ;
43
+
44
+ @Input ( )
45
+ public rowData : any ;
46
+
47
+ /**
48
+ * @hidden @internal
49
+ */
50
+ @ViewChild ( 'headerTemplate' , { read : TemplateRef , static : true } )
51
+ public headerTemplate : TemplateRef < any > ;
52
+
53
+ /**
54
+ * @hidden @internal
55
+ */
56
+ @ViewChild ( 'headerDefaultTemplate' , { read : TemplateRef , static : true } )
57
+ public headerTemplateDefault : TemplateRef < any > ;
58
+
59
+ constructor (
60
+ @Inject ( IGX_GRID_BASE ) public grid : PivotGridType ,
61
+ protected ref : ElementRef < HTMLElement > ,
62
+ protected cdr : ChangeDetectorRef ,
63
+ protected resolver : ComponentFactoryResolver ,
64
+ protected viewRef : ViewContainerRef
65
+ ) {
66
+ super ( ref , cdr ) ;
67
+ }
68
+ protected rowDimensionData : IPivotDimensionData [ ] = [ ] ;
69
+
70
+ public get rowDimension ( ) {
71
+ return this . rowDimensionData ?. filter ( x => ! x . isChild ) . map ( x => x . column ) ;
72
+ }
73
+
74
+ /**
75
+ * @hidden
76
+ * @internal
77
+ */
78
+ public ngOnChanges ( changes : SimpleChanges ) {
79
+ const rowDimConfig = this . grid . rowDimensions ;
80
+ if ( changes . data || rowDimConfig . length !== this . rowDimensionData . length ) {
81
+ // generate new rowDimension on row data change
82
+ this . rowDimensionData = [ ] ;
83
+ this . viewRef . clear ( ) ;
84
+ this . extractFromDimensions ( rowDimConfig , 0 ) ;
85
+ this . viewRef . clear ( ) ;
86
+ }
87
+ if ( changes . pivotRowWidths && this . rowDimensionData ) {
88
+ for ( const dim of rowDimConfig ) {
89
+ const dimData = PivotUtil . getDimensionLevel ( dim , this . rowData , this . grid . pivotKeys ) ;
90
+ const data = this . rowDimensionData . find ( x => x . dimension . memberName === dimData . dimension . memberName ) ;
91
+ data . column . width = this . grid . resolveRowDimensionWidth ( dim ) + 'px' ;
92
+ }
93
+ }
94
+ }
95
+
96
+
97
+ /**
98
+ * @hidden
99
+ * @internal
100
+ */
101
+ public getRowDimensionKey ( col : IgxColumnComponent ) {
102
+ const dimData = this . rowDimensionData . find ( x => x . column . field === col . field ) ;
103
+ const key = PivotUtil . getRecordKey ( this . rowData , dimData . dimension , dimData . prevDimensions , this . grid . pivotKeys ) ;
104
+ return key ;
105
+ }
106
+
107
+ public getExpandState ( col : IgxColumnComponent ) {
108
+ return this . grid . gridAPI . get_row_expansion_state ( this . getRowDimensionKey ( col ) ) ;
109
+ }
110
+
111
+ public getLevel ( col : IgxColumnComponent ) {
112
+ return this . rowData [ col . field + this . grid . pivotKeys . rowDimensionSeparator + this . grid . pivotKeys . level ] ;
113
+ }
114
+
115
+
116
+ /**
117
+ * @hidden @internal
118
+ */
119
+ public selectPivotRow ( col : any , event ?: any ) {
120
+ if ( this . grid . rowSelection === 'none' ) {
121
+ return ;
122
+ }
123
+ event ?. stopPropagation ( ) ;
124
+ const key = this . getRowDimensionKey ( col ) ;
125
+ if ( this . grid . selectionService . isRowSelected ( key ) ) {
126
+ this . grid . selectionService . deselectRow ( key , event ) ;
127
+ } else {
128
+ this . grid . selectionService . selectRowById ( key , true , event ) ;
129
+ }
130
+ }
131
+
132
+ protected extractFromDimensions ( rowDimConfig : IPivotDimension [ ] , level : number ) {
133
+ let dimIndex = 0 ;
134
+ let currentLvl = 0 ;
135
+ currentLvl += level ;
136
+ const prev = [ ] ;
137
+ let prevDim ;
138
+ for ( const dim of rowDimConfig ) {
139
+ const dimData = PivotUtil . getDimensionLevel ( dim , this . rowData , this . grid . pivotKeys ) ;
140
+ dimIndex += dimData . level ;
141
+ currentLvl += dimData . level ;
142
+ const prevChildren = prevDim ? this . rowData [ prevDim . memberName + this . grid . pivotKeys . rowDimensionSeparator + this . grid . pivotKeys . children ] : [ ] ;
143
+ if ( prevChildren && prevChildren . length > 0 ) {
144
+ const childrenCols = [ ] ;
145
+ const layoutCol = this . rowDimensionData . find ( x => x . column . columnLayout ) . column ;
146
+ prevChildren . forEach ( ( childData , index ) => {
147
+ const dimData = PivotUtil . getDimensionLevel ( dim , childData , this . grid . pivotKeys ) ;
148
+ dimIndex += dimData . level ;
149
+ currentLvl += dimData . level ;
150
+ const column = this . extractFromDimension ( dimData . dimension , childData , [ ] , dimIndex , currentLvl , dim , [ ...prev ] ) ;
151
+ column . rowStart = index + 1 ;
152
+ column . rowEnd = index + 2 ;
153
+ column . colStart = 2 ;
154
+ childrenCols . push ( column ) ;
155
+ this . rowDimensionData . push ( {
156
+ column,
157
+ dimension : dimData . dimension ,
158
+ prevDimensions : [ ...prev ] ,
159
+ isChild : true
160
+ } ) ;
161
+ } ) ;
162
+ const all_children = layoutCol . children . toArray ( ) . concat ( childrenCols ) ;
163
+ layoutCol . children . reset ( all_children ) ;
164
+ continue ;
165
+ }
166
+ const children = this . rowData [ dim . memberName + this . grid . pivotKeys . rowDimensionSeparator + this . grid . pivotKeys . children ] ;
167
+ const column = this . extractFromDimension ( dimData . dimension , this . rowData , children , dimIndex , currentLvl , dim , [ ...prev ] ) ;
168
+ this . rowDimensionData . push ( {
169
+ column,
170
+ dimension : dimData . dimension ,
171
+ prevDimensions : [ ...prev ]
172
+ } ) ;
173
+ prevDim = dim ;
174
+ prev . push ( dimData . dimension ) ;
175
+ }
176
+ }
177
+
178
+ protected extractFromDimension ( dim : IPivotDimension , rowData : any [ ] , children : any [ ] , index : number = 0 , lvl = 0 , rootDim : IPivotDimension , prevDims ) {
179
+ const field = dim . memberName ;
180
+ const header = rowData [ field ] ;
181
+ let col ;
182
+ if ( children && children . length > 0 ) {
183
+ const ref = this . viewRef . createComponent ( IgxColumnLayoutComponent ) ;
184
+ col = ref . instance ;
185
+ ref . instance . field = 'group' ;
186
+ const childCol = this . _createColComponent ( field , header , children , index , dim , lvl , rootDim ) ;
187
+ ref . instance . children . reset ( [ childCol ] ) ;
188
+ this . rowDimensionData . push ( {
189
+ column : childCol ,
190
+ dimension : dim ,
191
+ prevDimensions : prevDims ,
192
+ isChild : true
193
+ } ) ;
194
+ } else {
195
+ col = this . _createColComponent ( field , header , children , index , dim , lvl , rootDim ) ;
196
+ }
197
+ return col ;
198
+ }
199
+
200
+ protected _createColComponent ( field : string , header : string , children : any [ ] ,
201
+ index : number = 0 ,
202
+ dim : IPivotDimension , lvl = 0 , rootDim : IPivotDimension ) {
203
+ const ref = this . viewRef . createComponent ( IgxColumnComponent ) ;
204
+ if ( children && children . length > 0 ) {
205
+ ref . instance . rowStart = 1 ;
206
+ ref . instance . rowEnd = children . length + 1 ;
207
+ ref . instance . colStart = 1 ;
208
+ } else {
209
+ ref . instance . colStart = 2 ;
210
+ ref . instance . rowStart = 1 ;
211
+ ref . instance . rowEnd = 2 ;
212
+ }
213
+ ref . instance . field = field ;
214
+ ref . instance . header = header ;
215
+ ref . instance . width = this . grid . resolveRowDimensionWidth ( rootDim ) + 'px' ;
216
+ ( ref as any ) . instance . _vIndex = this . grid . columns . length + index + this . rowIndex * this . grid . pivotConfiguration . rows . length ;
217
+ if ( dim . childLevel && lvl >= PivotUtil . getTotalLvl ( this . rowData , this . grid . pivotKeys ) ) {
218
+ ref . instance . headerTemplate = this . headerTemplate ;
219
+ } else {
220
+ ref . instance . headerTemplate = this . headerTemplateDefault ;
221
+ }
222
+ return ref . instance ;
223
+ }
29
224
}
0 commit comments