@@ -25,8 +25,17 @@ import {
2525 Observable ,
2626 Subscription ,
2727} from 'rxjs' ;
28-
28+ import {
29+ map ,
30+ switchMap ,
31+ tap ,
32+ } from 'rxjs/operators' ;
33+ import { VocabularyService } from 'src/app/core/submission/vocabularies/vocabulary.service' ;
34+
35+ import { RemoteData } from '../../../core/data/remote-data' ;
36+ import { getFirstCompletedRemoteData } from '../../../core/shared/operators' ;
2937import { PageInfo } from '../../../core/shared/page-info.model' ;
38+ import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model' ;
3039import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model' ;
3140import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model' ;
3241import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model' ;
@@ -168,6 +177,7 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
168177 */
169178 constructor (
170179 private vocabularyTreeviewService : VocabularyTreeviewService ,
180+ protected vocabularyService : VocabularyService ,
171181 ) {
172182 this . treeFlattener = new VocabularyTreeFlattener ( this . transformer , this . getLevel ,
173183 this . isExpandable , this . getChildren ) ;
@@ -209,12 +219,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
209219 ) ;
210220 this . nodeMap . set ( entryId , newNode ) ;
211221
212- if ( ( ( ( level + 1 ) < this . preloadLevel ) && newNode . childrenLoaded )
222+ if ( ( ( ( level + 1 ) < this . preloadLevel ) )
213223 || ( newNode . isSearchNode && newNode . childrenLoaded )
214224 || newNode . isInInitValueHierarchy ) {
215- if ( ! newNode . isSearchNode ) {
225+
226+ if ( newNode . item . id === LOAD_MORE || newNode . item . id === LOAD_MORE_ROOT ) {
227+ // When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
228+ // so this is a good point to stop expanding.
229+ return newNode ;
230+ }
231+
232+ if ( ! newNode . childrenLoaded ) {
216233 this . loadChildren ( newNode ) ;
217234 }
235+
218236 this . treeControl . expand ( newNode ) ;
219237 }
220238 return newNode ;
@@ -255,15 +273,31 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
255273 */
256274 ngOnInit ( ) : void {
257275 this . subs . push (
258- this . vocabularyTreeviewService . getData ( ) . subscribe ( ( data ) => {
276+ this . vocabularyService . findVocabularyById ( this . vocabularyOptions . name ) . pipe (
277+ // Retrieve the configured preloadLevel from REST
278+ getFirstCompletedRemoteData ( ) ,
279+ map ( ( vocabularyRD : RemoteData < Vocabulary > ) => {
280+ if ( vocabularyRD . hasSucceeded &&
281+ hasValue ( vocabularyRD . payload . preloadLevel ) &&
282+ vocabularyRD . payload . preloadLevel > 1 ) {
283+ return vocabularyRD . payload . preloadLevel ;
284+ } else {
285+ // Set preload level to 1 in case request fails
286+ return 1 ;
287+ }
288+ } ) ,
289+ tap ( preloadLevel => this . preloadLevel = preloadLevel ) ,
290+ tap ( ( ) => {
291+ const entryId : string = ( this . selectedItems ?. length > 0 ) ? this . getEntryId ( this . selectedItems [ 0 ] ) : null ;
292+ this . vocabularyTreeviewService . initialize ( this . vocabularyOptions , new PageInfo ( ) , this . getSelectedEntryIds ( ) , entryId ) ;
293+ } ) ,
294+ switchMap ( ( ) => this . vocabularyTreeviewService . getData ( ) ) ,
295+ ) . subscribe ( ( data ) => {
259296 this . dataSource . data = data ;
260297 } ) ,
261298 ) ;
262299
263300 this . loading = this . vocabularyTreeviewService . isLoading ( ) ;
264-
265- const entryId : string = ( this . selectedItems ?. length > 0 ) ? this . getEntryId ( this . selectedItems [ 0 ] ) : null ;
266- this . vocabularyTreeviewService . initialize ( this . vocabularyOptions , new PageInfo ( ) , this . getSelectedEntryIds ( ) , entryId ) ;
267301 }
268302
269303 /**
0 commit comments