@@ -22,8 +22,17 @@ import {
2222 Observable ,
2323 Subscription ,
2424} from 'rxjs' ;
25-
25+ import {
26+ map ,
27+ switchMap ,
28+ tap ,
29+ } from 'rxjs/operators' ;
30+ import { VocabularyService } from 'src/app/core/submission/vocabularies/vocabulary.service' ;
31+
32+ import { RemoteData } from '../../../core/data/remote-data' ;
33+ import { getFirstCompletedRemoteData } from '../../../core/shared/operators' ;
2634import { PageInfo } from '../../../core/shared/page-info.model' ;
35+ import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model' ;
2736import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model' ;
2837import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model' ;
2938import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model' ;
@@ -166,6 +175,7 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
166175 */
167176 constructor (
168177 private vocabularyTreeviewService : VocabularyTreeviewService ,
178+ protected vocabularyService : VocabularyService ,
169179 ) {
170180 this . treeFlattener = new VocabularyTreeFlattener ( this . transformer , this . getLevel ,
171181 this . isExpandable , this . getChildren ) ;
@@ -207,12 +217,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
207217 ) ;
208218 this . nodeMap . set ( entryId , newNode ) ;
209219
210- if ( ( ( ( level + 1 ) < this . preloadLevel ) && newNode . childrenLoaded )
220+ if ( ( ( ( level + 1 ) < this . preloadLevel ) )
211221 || ( newNode . isSearchNode && newNode . childrenLoaded )
212222 || newNode . isInInitValueHierarchy ) {
213- if ( ! newNode . isSearchNode ) {
223+
224+ if ( newNode . item . id === LOAD_MORE || newNode . item . id === LOAD_MORE_ROOT ) {
225+ // When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
226+ // so this is a good point to stop expanding.
227+ return newNode ;
228+ }
229+
230+ if ( ! newNode . childrenLoaded ) {
214231 this . loadChildren ( newNode ) ;
215232 }
233+
216234 this . treeControl . expand ( newNode ) ;
217235 }
218236 return newNode ;
@@ -253,15 +271,31 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
253271 */
254272 ngOnInit ( ) : void {
255273 this . subs . push (
256- this . vocabularyTreeviewService . getData ( ) . subscribe ( ( data ) => {
274+ this . vocabularyService . findVocabularyById ( this . vocabularyOptions . name ) . pipe (
275+ // Retrieve the configured preloadLevel from REST
276+ getFirstCompletedRemoteData ( ) ,
277+ map ( ( vocabularyRD : RemoteData < Vocabulary > ) => {
278+ if ( vocabularyRD . hasSucceeded &&
279+ hasValue ( vocabularyRD . payload . preloadLevel ) &&
280+ vocabularyRD . payload . preloadLevel > 1 ) {
281+ return vocabularyRD . payload . preloadLevel ;
282+ } else {
283+ // Set preload level to 1 in case request fails
284+ return 1 ;
285+ }
286+ } ) ,
287+ tap ( preloadLevel => this . preloadLevel = preloadLevel ) ,
288+ tap ( ( ) => {
289+ const entryId : string = ( this . selectedItems ?. length > 0 ) ? this . getEntryId ( this . selectedItems [ 0 ] ) : null ;
290+ this . vocabularyTreeviewService . initialize ( this . vocabularyOptions , new PageInfo ( ) , this . getSelectedEntryIds ( ) , entryId ) ;
291+ } ) ,
292+ switchMap ( ( ) => this . vocabularyTreeviewService . getData ( ) ) ,
293+ ) . subscribe ( ( data ) => {
257294 this . dataSource . data = data ;
258295 } ) ,
259296 ) ;
260297
261298 this . loading = this . vocabularyTreeviewService . isLoading ( ) ;
262-
263- const entryId : string = ( this . selectedItems ?. length > 0 ) ? this . getEntryId ( this . selectedItems [ 0 ] ) : null ;
264- this . vocabularyTreeviewService . initialize ( this . vocabularyOptions , new PageInfo ( ) , this . getSelectedEntryIds ( ) , entryId ) ;
265299 }
266300
267301 /**
0 commit comments