diff --git a/src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.spec.ts b/src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.spec.ts index 9e6e8dedd35..c36e5290427 100644 --- a/src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.spec.ts +++ b/src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.spec.ts @@ -22,6 +22,7 @@ import { authReducer } from '../../../core/auth/auth.reducer'; import { storeModuleConfig } from '../../../app.reducer'; import { By } from '@angular/platform-browser'; import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service'; +import { createSuccessfulRemoteDataObject$ } from '../../remote-data.utils'; describe('VocabularyTreeviewComponent test suite', () => { @@ -56,7 +57,8 @@ describe('VocabularyTreeviewComponent test suite', () => { findEntryDetailById: jasmine.createSpy('findEntryDetailById'), searchTopEntries: jasmine.createSpy('searchTopEntries'), getEntryDetailChildren: jasmine.createSpy('getEntryDetailChildren'), - clearSearchTopRequests: jasmine.createSpy('clearSearchTopRequests') + clearSearchTopRequests: jasmine.createSpy('clearSearchTopRequests'), + findVocabularyById: createSuccessfulRemoteDataObject$({ preloadLevel: 2 }), }); initialState = { diff --git a/src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts b/src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts index 56956440406..c13526f75dd 100644 --- a/src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts +++ b/src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts @@ -1,6 +1,7 @@ import { FlatTreeControl } from '@angular/cdk/tree'; import { Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output, OnChanges, SimpleChanges, ViewChild } from '@angular/core'; +import { map, tap, switchMap } from 'rxjs/operators'; import { Observable, Subscription } from 'rxjs'; import { Store } from '@ngrx/store'; import { TranslateService } from '@ngx-translate/core'; @@ -16,8 +17,10 @@ import { VocabularyTreeFlattener } from './vocabulary-tree-flattener'; import { VocabularyTreeFlatDataSource } from './vocabulary-tree-flat-data-source'; import { CoreState } from '../../../core/core-state.model'; import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service'; -import { getFirstSucceededRemoteDataPayload } from '../../../core/shared/operators'; +import { getFirstSucceededRemoteDataPayload, getFirstCompletedRemoteData } from '../../../core/shared/operators'; import { AlertType } from '../../alert/alert-type'; +import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model'; +import { RemoteData } from '../../../core/data/remote-data'; /** * Component that shows a hierarchical vocabulary in a tree view @@ -166,12 +169,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges ); this.nodeMap.set(node.item.id, newNode); - if ((((level + 1) < this.preloadLevel) && newNode.childrenLoaded) + if ((((level + 1) < this.preloadLevel)) || (newNode.isSearchNode && newNode.childrenLoaded) || newNode.isInInitValueHierarchy) { - if (!newNode.isSearchNode) { + + if (newNode.item.id === LOAD_MORE || newNode.item.id === LOAD_MORE_ROOT) { + // When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children + // so this is a good point to stop expanding. + return newNode; + } + + if (!newNode.childrenLoaded) { this.loadChildren(newNode); } + this.treeControl.expand(newNode); } return newNode; @@ -212,14 +223,29 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges */ ngOnInit(): void { this.subs.push( - this.vocabularyTreeviewService.getData().subscribe((data) => { + this.vocabularyService.findVocabularyById(this.vocabularyOptions.name).pipe( + // Retrieve the configured preloadLevel from REST + getFirstCompletedRemoteData(), + map((vocabularyRD: RemoteData) => { + if (vocabularyRD.hasSucceeded && + hasValue(vocabularyRD.payload.preloadLevel) && + vocabularyRD.payload.preloadLevel > 1) { + return vocabularyRD.payload.preloadLevel; + } else { + // Set preload level to 1 in case request fails + return 1; + } + }), + tap(preloadLevel => this.preloadLevel = preloadLevel), + tap(() => this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.selectedItems, null)), + switchMap(() => this.vocabularyTreeviewService.getData()), + ).subscribe((data) => { this.dataSource.data = data; }) ); - this.loading = this.vocabularyTreeviewService.isLoading(); - this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.selectedItems, null); + this.loading = this.vocabularyTreeviewService.isLoading(); } /**