Skip to content

Commit 44f2ad7

Browse files
authored
Merge pull request #3821 from atmire/vocabulary-preloadlevel-fix-7_x
[Port dspace-7_x] Vocabulary preloadlevel fix
2 parents c7f8ed1 + 18819b7 commit 44f2ad7

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { authReducer } from '../../../core/auth/auth.reducer';
2222
import { storeModuleConfig } from '../../../app.reducer';
2323
import { By } from '@angular/platform-browser';
2424
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
25+
import { createSuccessfulRemoteDataObject$ } from '../../remote-data.utils';
2526

2627
describe('VocabularyTreeviewComponent test suite', () => {
2728

@@ -56,7 +57,8 @@ describe('VocabularyTreeviewComponent test suite', () => {
5657
findEntryDetailById: jasmine.createSpy('findEntryDetailById'),
5758
searchTopEntries: jasmine.createSpy('searchTopEntries'),
5859
getEntryDetailChildren: jasmine.createSpy('getEntryDetailChildren'),
59-
clearSearchTopRequests: jasmine.createSpy('clearSearchTopRequests')
60+
clearSearchTopRequests: jasmine.createSpy('clearSearchTopRequests'),
61+
findVocabularyById: createSuccessfulRemoteDataObject$({ preloadLevel: 2 }),
6062
});
6163

6264
initialState = {

src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FlatTreeControl } from '@angular/cdk/tree';
22
import { Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output, OnChanges, SimpleChanges, ViewChild } from '@angular/core';
33

4+
import { map, tap, switchMap } from 'rxjs/operators';
45
import { Observable, Subscription } from 'rxjs';
56
import { Store } from '@ngrx/store';
67
import { TranslateService } from '@ngx-translate/core';
@@ -16,8 +17,10 @@ import { VocabularyTreeFlattener } from './vocabulary-tree-flattener';
1617
import { VocabularyTreeFlatDataSource } from './vocabulary-tree-flat-data-source';
1718
import { CoreState } from '../../../core/core-state.model';
1819
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
19-
import { getFirstSucceededRemoteDataPayload } from '../../../core/shared/operators';
20+
import { getFirstSucceededRemoteDataPayload, getFirstCompletedRemoteData } from '../../../core/shared/operators';
2021
import { AlertType } from '../../alert/alert-type';
22+
import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model';
23+
import { RemoteData } from '../../../core/data/remote-data';
2124

2225
/**
2326
* Component that shows a hierarchical vocabulary in a tree view
@@ -166,12 +169,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
166169
);
167170
this.nodeMap.set(node.item.id, newNode);
168171

169-
if ((((level + 1) < this.preloadLevel) && newNode.childrenLoaded)
172+
if ((((level + 1) < this.preloadLevel))
170173
|| (newNode.isSearchNode && newNode.childrenLoaded)
171174
|| newNode.isInInitValueHierarchy) {
172-
if (!newNode.isSearchNode) {
175+
176+
if (newNode.item.id === LOAD_MORE || newNode.item.id === LOAD_MORE_ROOT) {
177+
// When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
178+
// so this is a good point to stop expanding.
179+
return newNode;
180+
}
181+
182+
if (!newNode.childrenLoaded) {
173183
this.loadChildren(newNode);
174184
}
185+
175186
this.treeControl.expand(newNode);
176187
}
177188
return newNode;
@@ -212,14 +223,29 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
212223
*/
213224
ngOnInit(): void {
214225
this.subs.push(
215-
this.vocabularyTreeviewService.getData().subscribe((data) => {
226+
this.vocabularyService.findVocabularyById(this.vocabularyOptions.name).pipe(
227+
// Retrieve the configured preloadLevel from REST
228+
getFirstCompletedRemoteData(),
229+
map((vocabularyRD: RemoteData<Vocabulary>) => {
230+
if (vocabularyRD.hasSucceeded &&
231+
hasValue(vocabularyRD.payload.preloadLevel) &&
232+
vocabularyRD.payload.preloadLevel > 1) {
233+
return vocabularyRD.payload.preloadLevel;
234+
} else {
235+
// Set preload level to 1 in case request fails
236+
return 1;
237+
}
238+
}),
239+
tap(preloadLevel => this.preloadLevel = preloadLevel),
240+
tap(() => this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.selectedItems, null)),
241+
switchMap(() => this.vocabularyTreeviewService.getData()),
242+
).subscribe((data) => {
216243
this.dataSource.data = data;
217244
})
218245
);
219246

220-
this.loading = this.vocabularyTreeviewService.isLoading();
221247

222-
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.selectedItems, null);
248+
this.loading = this.vocabularyTreeviewService.isLoading();
223249
}
224250

225251
/**

0 commit comments

Comments
 (0)