@@ -27,13 +27,14 @@ import {
2727 OnInit ,
2828 ChangeDetectionStrategy ,
2929 SimpleChanges ,
30- ChangeDetectorRef
30+ ChangeDetectorRef ,
31+ TemplateRef
3132} from '@angular/core' ;
3233import { List , Map , Set } from 'immutable' ;
3334
3435import { AbstractListFieldComponent } from '../abstract-list-field' ;
3536import { AppGlobalsService , JsonStoreService , DomUtilService , PathUtilService } from '../shared/services' ;
36- import { LongListNavigatorConfig , JSONSchema } from '../shared/interfaces' ;
37+ import { LongListNavigatorConfig , JSONSchema , PaginatedItem } from '../shared/interfaces' ;
3738
3839@Component ( {
3940 selector : 'complex-list-field' ,
@@ -49,7 +50,7 @@ export class ComplexListFieldComponent extends AbstractListFieldComponent implem
4950 @Input ( ) schema : JSONSchema ;
5051 @Input ( ) path : Array < any > ;
5152
52- paginatedIndices : Array < number > ;
53+ paginatedItems : Array < PaginatedItem > ;
5354
5455 foundIndices : Array < number > ;
5556 currentFound = 0 ;
@@ -68,12 +69,7 @@ export class ComplexListFieldComponent extends AbstractListFieldComponent implem
6869
6970 ngOnInit ( ) {
7071 this . navigator = this . schema . longListNavigatorConfig ;
71- if ( this . navigator ) {
72- this . paginatedIndices = this . getIndicesForPage ( this . currentPage ) ;
73- } else {
74- // set all indices as paginated indices if pagination is not enabled.
75- this . paginatedIndices = this . values . keySeq ( ) . toArray ( ) ;
76- }
72+ this . paginatedItems = this . getItemsForPage ( this . currentPage ) ;
7773 }
7874
7975 ngOnChanges ( changes : SimpleChanges ) {
@@ -88,14 +84,10 @@ export class ComplexListFieldComponent extends AbstractListFieldComponent implem
8884 // change the page if a new element is added and it's not on the last page
8985 if ( curSize > preSize && this . currentPage !== lastPage ) {
9086 this . currentPage = lastPage ;
91- } else {
92- this . paginatedIndices = this . getIndicesForPage ( this . currentPage ) ;
9387 }
94- } else {
95- this . paginatedIndices = this . values . keySeq ( ) . toArray ( ) ;
9688 }
89+ this . paginatedItems = this . getItemsForPage ( this . currentPage ) ;
9790 }
98-
9991 }
10092 }
10193
@@ -150,23 +142,46 @@ export class ComplexListFieldComponent extends AbstractListFieldComponent implem
150142
151143 onPageChange ( page : number ) {
152144 this . currentPage = page ;
153- this . paginatedIndices = this . getIndicesForPage ( page ) ;
145+ this . paginatedItems = this . getItemsForPage ( page ) ;
154146 }
155147
156- getIndicesForPage ( page : number ) : Array < number > {
157- let start = ( page - 1 ) * this . navigator . itemsPerPage ;
158- let indices : Array < number > = Array . apply ( 0 , Array ( this . navigator . itemsPerPage ) )
159- . map ( ( el , index ) => start + index ) ;
160- // check if the indices includes some numbers that are out of values index range.
161- let lastIndexDiff = indices [ indices . length - 1 ] - ( this . values . size - 1 ) ;
162- if ( lastIndexDiff > 0 ) {
163- indices . splice ( indices . length - lastIndexDiff ) ;
164- }
165- return indices ;
166- }
148+ getItemsForPage ( page : number ) : Array < PaginatedItem > {
149+ let indices = this . getIndicesToDisplay ( page ) ;
167150
151+ return indices . map ( ( index ) => {
152+ let showEditForm = this . schema . viewTemplateConfig ? this . schema . viewTemplateConfig . showEditForm : undefined ;
153+ let isEditFormVisible = ! showEditForm || showEditForm ( this . values . get ( index ) ) ;
154+ return { index, isEditFormVisible} ;
155+ } ) ;
156+ }
168157
169158 getPageForIndex ( index : number ) : number {
170159 return Math . floor ( ( index / this . navigator . itemsPerPage ) + 1 ) ;
171160 }
161+
162+ get customTemplate ( ) : TemplateRef < any > {
163+ return this . appGlobalsService . templates [ this . schema . viewTemplateConfig . itemTemplateName ] ;
164+ }
165+
166+ get shouldDisplayTemplate ( ) : boolean {
167+ return this . schema . viewTemplateConfig !== undefined ;
168+ }
169+
170+ private getIndicesToDisplay ( page : number ) : Array < number > {
171+ let indices : Array < number > ;
172+ if ( this . navigator ) {
173+ let start = ( page - 1 ) * this . navigator . itemsPerPage ;
174+ indices = Array . apply ( 0 , Array ( this . navigator . itemsPerPage ) )
175+ . map ( ( el , index ) => start + index ) ;
176+ // check if the indices includes some numbers that are out of values index range.
177+ let lastIndexDiff = indices [ indices . length - 1 ] - ( this . values . size - 1 ) ;
178+ if ( lastIndexDiff > 0 ) {
179+ indices . splice ( indices . length - lastIndexDiff ) ;
180+ }
181+ } else {
182+ indices = this . values . keySeq ( ) . toArray ( ) ;
183+ }
184+
185+ return indices ;
186+ }
172187}
0 commit comments