@@ -44,7 +44,9 @@ export class DsDynamicListComponent extends DynamicFormControlComponent implemen
4444 @Output ( ) focus : EventEmitter < any > = new EventEmitter < any > ( ) ;
4545
4646 public items : ListItem [ ] [ ] = [ ] ;
47- protected optionsList : VocabularyEntry [ ] ;
47+ public showLoadMore$ : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( false ) ;
48+ protected optionsList : VocabularyEntry [ ] = [ ] ;
49+ private nextPageInfo : PageInfo ;
4850
4951 constructor ( private vocabularyService : VocabularyService ,
5052 private cdr : ChangeDetectorRef ,
@@ -60,7 +62,7 @@ export class DsDynamicListComponent extends DynamicFormControlComponent implemen
6062 */
6163 ngOnInit ( ) {
6264 if ( this . model . vocabularyOptions && hasValue ( this . model . vocabularyOptions . name ) ) {
63- this . setOptionsFromVocabulary ( ) ;
65+ this . initOptionsFromVocabulary ( ) ;
6466 }
6567 }
6668
@@ -107,70 +109,18 @@ export class DsDynamicListComponent extends DynamicFormControlComponent implemen
107109 /**
108110 * Setting up the field options from vocabulary
109111 */
110- protected setOptionsFromVocabulary ( ) {
112+ protected initOptionsFromVocabulary ( ) {
111113 if ( this . model . vocabularyOptions . name && this . model . vocabularyOptions . name . length > 0 ) {
112114 const listGroup = this . group . controls [ this . model . id ] as UntypedFormGroup ;
113115 if ( this . model . repeatable && this . model . required ) {
114116 listGroup . addValidators ( this . hasAtLeastOneVocabularyEntry ( ) ) ;
115117 }
116118
117- const initialPageInfo : PageInfo = new PageInfo ( {
119+ this . nextPageInfo = new PageInfo ( {
118120 elementsPerPage : 20 , currentPage : 1 ,
119121 } as PageInfo ) ;
120122
121- this . vocabularyService . getVocabularyEntries ( this . model . vocabularyOptions , initialPageInfo ) . pipe (
122- getFirstSucceededRemoteDataPayload ( ) ,
123- expand ( ( entries : PaginatedList < VocabularyEntry > ) => {
124- if ( entries . pageInfo . currentPage < entries . pageInfo . totalPages ) {
125- const nextPageInfo : PageInfo = new PageInfo ( {
126- elementsPerPage : 20 , currentPage : entries . pageInfo . currentPage + 1 ,
127- } as PageInfo ) ;
128- return this . vocabularyService . getVocabularyEntries ( this . model . vocabularyOptions , nextPageInfo ) . pipe (
129- getFirstSucceededRemoteDataPayload ( )
130- ) ;
131- } else {
132- return EMPTY ;
133- }
134- } ) ,
135- reduce ( ( acc : VocabularyEntry [ ] , entries : PaginatedList < VocabularyEntry > ) => acc . concat ( entries . page ) , [ ] )
136- ) . subscribe ( ( allEntries : VocabularyEntry [ ] ) => {
137- let groupCounter = 0 ;
138- let itemsPerGroup = 0 ;
139- let tempList : ListItem [ ] = [ ] ;
140- this . optionsList = allEntries ;
141- // Make a list of available options (checkbox/radio) and split in groups of 'model.groupLength'
142- allEntries . forEach ( ( option : VocabularyEntry , key : number ) => {
143- const value = option . authority || option . value ;
144- const checked : boolean = isNotEmpty ( findKey (
145- this . model . value ,
146- ( v ) => v . value === option . value ) ) ;
147-
148- const item : ListItem = {
149- id : `${ this . model . id } _${ value } ` ,
150- label : option . display ,
151- value : checked ,
152- index : key
153- } ;
154- if ( this . model . repeatable ) {
155- this . formBuilderService . addFormGroupControl ( listGroup , ( this . model as DynamicListCheckboxGroupModel ) , new DynamicCheckboxModel ( item ) ) ;
156- } else {
157- ( this . model as DynamicListRadioGroupModel ) . options . push ( {
158- label : item . label ,
159- value : option
160- } ) ;
161- }
162- tempList . push ( item ) ;
163- itemsPerGroup ++ ;
164- this . items [ groupCounter ] = tempList ;
165- if ( itemsPerGroup === this . model . groupLength ) {
166- groupCounter ++ ;
167- itemsPerGroup = 0 ;
168- tempList = [ ] ;
169- }
170- } ) ;
171- this . cdr . markForCheck ( ) ;
172- } ) ;
173-
123+ this . loadEntries ( listGroup ) ;
174124 }
175125 }
176126
@@ -183,4 +133,70 @@ export class DsDynamicListComponent extends DynamicFormControlComponent implemen
183133 } ;
184134 }
185135
136+ /**
137+ * Update current page state to keep track of which one to load next
138+ * @param response
139+ */
140+ setPaginationInfo ( response : PaginatedList < VocabularyEntry > ) {
141+ if ( response . pageInfo . currentPage < response . pageInfo . totalPages ) {
142+ this . nextPageInfo = Object . assign ( new PageInfo ( ) , response . pageInfo , { currentPage : response . currentPage + 1 } ) ;
143+ this . showLoadMore$ . next ( true ) ;
144+ } else {
145+ this . showLoadMore$ . next ( false ) ;
146+ }
147+ }
148+
149+ /**
150+ * Load entries page
151+ *
152+ * @param listGroup
153+ */
154+ loadEntries ( listGroup ?: UntypedFormGroup ) {
155+ if ( ! hasValue ( listGroup ) ) {
156+ listGroup = this . group . controls [ this . model . id ] as UntypedFormGroup ;
157+ }
158+
159+ this . vocabularyService . getVocabularyEntries ( this . model . vocabularyOptions , this . nextPageInfo ) . pipe (
160+ getFirstSucceededRemoteDataPayload ( ) ,
161+ tap ( ( response ) => this . setPaginationInfo ( response ) ) ,
162+ map ( entries => entries . page ) ,
163+ ) . subscribe ( ( allEntries : VocabularyEntry [ ] ) => {
164+ this . optionsList = [ ...this . optionsList , ...allEntries ] ;
165+ let groupCounter = ( this . items . length > 0 ) ? ( this . items . length - 1 ) : 0 ;
166+ let itemsPerGroup = 0 ;
167+ let tempList : ListItem [ ] = [ ] ;
168+
169+ // Make a list of available options (checkbox/radio) and split in groups of 'model.groupLength'
170+ allEntries . forEach ( ( option : VocabularyEntry , key : number ) => {
171+ const value = option . authority || option . value ;
172+ const checked : boolean = isNotEmpty ( findKey (
173+ this . model . value ,
174+ ( v ) => v ?. value === option . value ) ) ;
175+
176+ const item : ListItem = {
177+ id : `${ this . model . id } _${ value } ` ,
178+ label : option . display ,
179+ value : checked ,
180+ index : key ,
181+ } ;
182+ if ( this . model . repeatable ) {
183+ this . formBuilderService . addFormGroupControl ( listGroup , ( this . model as DynamicListCheckboxGroupModel ) , new DynamicCheckboxModel ( item ) ) ;
184+ } else {
185+ ( this . model as DynamicListRadioGroupModel ) . options . push ( {
186+ label : item . label ,
187+ value : option ,
188+ } ) ;
189+ }
190+ tempList . push ( item ) ;
191+ itemsPerGroup ++ ;
192+ this . items [ groupCounter ] = tempList ;
193+ if ( itemsPerGroup === this . model . groupLength ) {
194+ groupCounter ++ ;
195+ itemsPerGroup = 0 ;
196+ tempList = [ ] ;
197+ }
198+ } ) ;
199+ this . cdr . markForCheck ( ) ;
200+ } ) ;
201+ }
186202}
0 commit comments