@@ -25,11 +25,20 @@ import {
2525 Observable ,
2626 Subscription ,
2727} from 'rxjs' ;
28+ import {
29+ map ,
30+ switchMap ,
31+ tap ,
32+ } from 'rxjs/operators' ;
2833
34+ import { RemoteData } from '../../../core/data/remote-data' ;
35+ import { getFirstCompletedRemoteData } from '../../../core/shared/operators' ;
2936import { PageInfo } from '../../../core/shared/page-info.model' ;
37+ import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model' ;
3038import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model' ;
3139import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model' ;
3240import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model' ;
41+ import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service' ;
3342import { AlertComponent } from '../../alert/alert.component' ;
3443import { AlertType } from '../../alert/alert-type' ;
3544import {
@@ -165,9 +174,11 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
165174 * Initialize instance variables
166175 *
167176 * @param {VocabularyTreeviewService } vocabularyTreeviewService
177+ * @param {VocabularyService } vocabularyService
168178 */
169179 constructor (
170180 private vocabularyTreeviewService : VocabularyTreeviewService ,
181+ protected vocabularyService : VocabularyService ,
171182 ) {
172183 this . treeFlattener = new VocabularyTreeFlattener ( this . transformer , this . getLevel ,
173184 this . isExpandable , this . getChildren ) ;
@@ -209,12 +220,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
209220 ) ;
210221 this . nodeMap . set ( entryId , newNode ) ;
211222
212- if ( ( ( ( level + 1 ) < this . preloadLevel ) && newNode . childrenLoaded )
223+ if ( ( ( ( level + 1 ) < this . preloadLevel ) )
213224 || ( newNode . isSearchNode && newNode . childrenLoaded )
214225 || newNode . isInInitValueHierarchy ) {
215- if ( ! newNode . isSearchNode ) {
226+
227+ if ( newNode . item . id === LOAD_MORE || newNode . item . id === LOAD_MORE_ROOT ) {
228+ // When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
229+ // so this is a good point to stop expanding.
230+ return newNode ;
231+ }
232+
233+ if ( ! newNode . childrenLoaded ) {
216234 this . loadChildren ( newNode ) ;
217235 }
236+
218237 this . treeControl . expand ( newNode ) ;
219238 }
220239 return newNode ;
@@ -255,15 +274,31 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
255274 */
256275 ngOnInit ( ) : void {
257276 this . subs . push (
258- this . vocabularyTreeviewService . getData ( ) . subscribe ( ( data ) => {
277+ this . vocabularyService . findVocabularyById ( this . vocabularyOptions . name ) . pipe (
278+ // Retrieve the configured preloadLevel from REST
279+ getFirstCompletedRemoteData ( ) ,
280+ map ( ( vocabularyRD : RemoteData < Vocabulary > ) => {
281+ if ( vocabularyRD . hasSucceeded &&
282+ hasValue ( vocabularyRD . payload . preloadLevel ) &&
283+ vocabularyRD . payload . preloadLevel > 1 ) {
284+ return vocabularyRD . payload . preloadLevel ;
285+ } else {
286+ // Set preload level to 1 in case request fails
287+ return 1 ;
288+ }
289+ } ) ,
290+ tap ( ( preloadLevel : number ) => this . preloadLevel = preloadLevel ) ,
291+ tap ( ( ) => {
292+ const entryId : string = ( this . selectedItems ?. length > 0 ) ? this . getEntryId ( this . selectedItems [ 0 ] ) : null ;
293+ this . vocabularyTreeviewService . initialize ( this . vocabularyOptions , new PageInfo ( ) , this . getSelectedEntryIds ( ) , entryId ) ;
294+ } ) ,
295+ switchMap ( ( ) => this . vocabularyTreeviewService . getData ( ) ) ,
296+ ) . subscribe ( ( data ) => {
259297 this . dataSource . data = data ;
260298 } ) ,
261299 ) ;
262300
263301 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 ) ;
267302 }
268303
269304 /**
0 commit comments