@@ -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 { BtnDisabledDirective } from '../../btn-disabled.directive' ;
@@ -167,9 +176,11 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
167176 * Initialize instance variables
168177 *
169178 * @param {VocabularyTreeviewService } vocabularyTreeviewService
179+ * @param {VocabularyService } vocabularyService
170180 */
171181 constructor (
172182 private vocabularyTreeviewService : VocabularyTreeviewService ,
183+ protected vocabularyService : VocabularyService ,
173184 ) {
174185 this . treeFlattener = new VocabularyTreeFlattener ( this . transformer , this . getLevel ,
175186 this . isExpandable , this . getChildren ) ;
@@ -211,12 +222,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
211222 ) ;
212223 this . nodeMap . set ( entryId , newNode ) ;
213224
214- if ( ( ( ( level + 1 ) < this . preloadLevel ) && newNode . childrenLoaded )
225+ if ( ( ( ( level + 1 ) < this . preloadLevel ) )
215226 || ( newNode . isSearchNode && newNode . childrenLoaded )
216227 || newNode . isInInitValueHierarchy ) {
217- if ( ! newNode . isSearchNode ) {
228+
229+ if ( newNode . item . id === LOAD_MORE || newNode . item . id === LOAD_MORE_ROOT ) {
230+ // When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
231+ // so this is a good point to stop expanding.
232+ return newNode ;
233+ }
234+
235+ if ( ! newNode . childrenLoaded ) {
218236 this . loadChildren ( newNode ) ;
219237 }
238+
220239 this . treeControl . expand ( newNode ) ;
221240 }
222241 return newNode ;
@@ -257,15 +276,31 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
257276 */
258277 ngOnInit ( ) : void {
259278 this . subs . push (
260- this . vocabularyTreeviewService . getData ( ) . subscribe ( ( data ) => {
279+ this . vocabularyService . findVocabularyById ( this . vocabularyOptions . name ) . pipe (
280+ // Retrieve the configured preloadLevel from REST
281+ getFirstCompletedRemoteData ( ) ,
282+ map ( ( vocabularyRD : RemoteData < Vocabulary > ) => {
283+ if ( vocabularyRD . hasSucceeded &&
284+ hasValue ( vocabularyRD . payload . preloadLevel ) &&
285+ vocabularyRD . payload . preloadLevel > 1 ) {
286+ return vocabularyRD . payload . preloadLevel ;
287+ } else {
288+ // Set preload level to 1 in case request fails
289+ return 1 ;
290+ }
291+ } ) ,
292+ tap ( ( preloadLevel : number ) => this . preloadLevel = preloadLevel ) ,
293+ tap ( ( ) => {
294+ const entryId : string = ( this . selectedItems ?. length > 0 ) ? this . getEntryId ( this . selectedItems [ 0 ] ) : null ;
295+ this . vocabularyTreeviewService . initialize ( this . vocabularyOptions , new PageInfo ( ) , this . getSelectedEntryIds ( ) , entryId ) ;
296+ } ) ,
297+ switchMap ( ( ) => this . vocabularyTreeviewService . getData ( ) ) ,
298+ ) . subscribe ( ( data ) => {
261299 this . dataSource . data = data ;
262300 } ) ,
263301 ) ;
264302
265303 this . loading = this . vocabularyTreeviewService . isLoading ( ) ;
266-
267- const entryId : string = ( this . selectedItems ?. length > 0 ) ? this . getEntryId ( this . selectedItems [ 0 ] ) : null ;
268- this . vocabularyTreeviewService . initialize ( this . vocabularyOptions , new PageInfo ( ) , this . getSelectedEntryIds ( ) , entryId ) ;
269304 }
270305
271306 /**
0 commit comments