11import { FlatTreeControl } from '@angular/cdk/tree' ;
22import { Component , ElementRef , EventEmitter , Input , OnDestroy , OnInit , Output , OnChanges , SimpleChanges , ViewChild } from '@angular/core' ;
33
4+ import { map , tap , switchMap } from 'rxjs/operators' ;
45import { Observable , Subscription } from 'rxjs' ;
56import { Store } from '@ngrx/store' ;
67import { TranslateService } from '@ngx-translate/core' ;
@@ -16,8 +17,10 @@ import { VocabularyTreeFlattener } from './vocabulary-tree-flattener';
1617import { VocabularyTreeFlatDataSource } from './vocabulary-tree-flat-data-source' ;
1718import { CoreState } from '../../../core/core-state.model' ;
1819import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service' ;
19- import { getFirstSucceededRemoteDataPayload } from '../../../core/shared/operators' ;
20+ import { getFirstSucceededRemoteDataPayload , getFirstCompletedRemoteData } from '../../../core/shared/operators' ;
2021import { AlertType } from '../../alert/alert-type' ;
22+ import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model' ;
23+ import { RemoteData } from '../../../core/data/remote-data' ;
2124
2225/**
2326 * Component that shows a hierarchical vocabulary in a tree view
@@ -166,12 +169,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
166169 ) ;
167170 this . nodeMap . set ( node . item . id , newNode ) ;
168171
169- if ( ( ( ( level + 1 ) < this . preloadLevel ) && newNode . childrenLoaded )
172+ if ( ( ( ( level + 1 ) < this . preloadLevel ) )
170173 || ( newNode . isSearchNode && newNode . childrenLoaded )
171174 || newNode . isInInitValueHierarchy ) {
172- if ( ! newNode . isSearchNode ) {
175+
176+ if ( newNode . item . id === LOAD_MORE || newNode . item . id === LOAD_MORE_ROOT ) {
177+ // When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
178+ // so this is a good point to stop expanding.
179+ return newNode ;
180+ }
181+
182+ if ( ! newNode . childrenLoaded ) {
173183 this . loadChildren ( newNode ) ;
174184 }
185+
175186 this . treeControl . expand ( newNode ) ;
176187 }
177188 return newNode ;
@@ -212,14 +223,29 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
212223 */
213224 ngOnInit ( ) : void {
214225 this . subs . push (
215- this . vocabularyTreeviewService . getData ( ) . subscribe ( ( data ) => {
226+ this . vocabularyService . findVocabularyById ( this . vocabularyOptions . name ) . pipe (
227+ // Retrieve the configured preloadLevel from REST
228+ getFirstCompletedRemoteData ( ) ,
229+ map ( ( vocabularyRD : RemoteData < Vocabulary > ) => {
230+ if ( vocabularyRD . hasSucceeded &&
231+ hasValue ( vocabularyRD . payload . preloadLevel ) &&
232+ vocabularyRD . payload . preloadLevel > 1 ) {
233+ return vocabularyRD . payload . preloadLevel ;
234+ } else {
235+ // Set preload level to 1 in case request fails
236+ return 1 ;
237+ }
238+ } ) ,
239+ tap ( preloadLevel => this . preloadLevel = preloadLevel ) ,
240+ tap ( ( ) => this . vocabularyTreeviewService . initialize ( this . vocabularyOptions , new PageInfo ( ) , this . selectedItems , null ) ) ,
241+ switchMap ( ( ) => this . vocabularyTreeviewService . getData ( ) ) ,
242+ ) . subscribe ( ( data ) => {
216243 this . dataSource . data = data ;
217244 } )
218245 ) ;
219246
220- this . loading = this . vocabularyTreeviewService . isLoading ( ) ;
221247
222- this . vocabularyTreeviewService . initialize ( this . vocabularyOptions , new PageInfo ( ) , this . selectedItems , null ) ;
248+ this . loading = this . vocabularyTreeviewService . isLoading ( ) ;
223249 }
224250
225251 /**
0 commit comments