@@ -22,11 +22,20 @@ import {
2222 Observable ,
2323 Subscription ,
2424} from 'rxjs' ;
25+ import {
26+ map ,
27+ switchMap ,
28+ tap ,
29+ } from 'rxjs/operators' ;
30+ import { getFirstCompletedRemoteData } from 'src/app/core/shared/operators' ;
2531
32+ import { RemoteData } from '../../../core/data/remote-data' ;
2633import { PageInfo } from '../../../core/shared/page-info.model' ;
34+ import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model' ;
2735import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model' ;
2836import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model' ;
2937import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model' ;
38+ import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service' ;
3039import { AlertComponent } from '../../alert/alert.component' ;
3140import { AlertType } from '../../alert/alert-type' ;
3241import { BtnDisabledDirective } from '../../btn-disabled.directive' ;
@@ -163,9 +172,11 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
163172 * Initialize instance variables
164173 *
165174 * @param {VocabularyTreeviewService } vocabularyTreeviewService
175+ * @param {VocabularyService } vocabularyService
166176 */
167177 constructor (
168178 private vocabularyTreeviewService : VocabularyTreeviewService ,
179+ protected vocabularyService : VocabularyService ,
169180 ) {
170181 this . treeFlattener = new VocabularyTreeFlattener ( this . transformer , this . getLevel ,
171182 this . isExpandable , this . getChildren ) ;
@@ -207,12 +218,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
207218 ) ;
208219 this . nodeMap . set ( entryId , newNode ) ;
209220
210- if ( ( ( ( level + 1 ) < this . preloadLevel ) && newNode . childrenLoaded )
221+ if ( ( ( ( level + 1 ) < this . preloadLevel ) )
211222 || ( newNode . isSearchNode && newNode . childrenLoaded )
212223 || newNode . isInInitValueHierarchy ) {
213- if ( ! newNode . isSearchNode ) {
224+
225+ if ( newNode . item . id === LOAD_MORE || newNode . item . id === LOAD_MORE_ROOT ) {
226+ // When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
227+ // so this is a good point to stop expanding.
228+ return newNode ;
229+ }
230+
231+ if ( ! newNode . childrenLoaded ) {
214232 this . loadChildren ( newNode ) ;
215233 }
234+
216235 this . treeControl . expand ( newNode ) ;
217236 }
218237 return newNode ;
@@ -253,15 +272,31 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
253272 */
254273 ngOnInit ( ) : void {
255274 this . subs . push (
256- this . vocabularyTreeviewService . getData ( ) . subscribe ( ( data ) => {
275+ this . vocabularyService . findVocabularyById ( this . vocabularyOptions . name ) . pipe (
276+ // Retrieve the configured preloadLevel from REST
277+ getFirstCompletedRemoteData ( ) ,
278+ map ( ( vocabularyRD : RemoteData < Vocabulary > ) => {
279+ if ( vocabularyRD . hasSucceeded &&
280+ hasValue ( vocabularyRD . payload . preloadLevel ) &&
281+ vocabularyRD . payload . preloadLevel > 1 ) {
282+ return vocabularyRD . payload . preloadLevel ;
283+ } else {
284+ // Set preload level to 1 in case request fails
285+ return 1 ;
286+ }
287+ } ) ,
288+ tap ( preloadLevel => this . preloadLevel = preloadLevel ) ,
289+ tap ( ( ) => {
290+ const entryId : string = ( this . selectedItems ?. length > 0 ) ? this . getEntryId ( this . selectedItems [ 0 ] ) : null ;
291+ this . vocabularyTreeviewService . initialize ( this . vocabularyOptions , new PageInfo ( ) , this . getSelectedEntryIds ( ) , entryId ) ;
292+ } ) ,
293+ switchMap ( ( ) => this . vocabularyTreeviewService . getData ( ) ) ,
294+ ) . subscribe ( ( data ) => {
257295 this . dataSource . data = data ;
258296 } ) ,
259297 ) ;
260298
261299 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 ) ;
265300 }
266301
267302 /**
0 commit comments