Skip to content

Commit 5c419bd

Browse files
authored
Merge pull request #4522 from atmire/vocabulary-preloadlevel-fix-9_x
[Port dspace-9_x] Vocabulary preloadlevel fix
2 parents 9be1562 + c253cff commit 5c419bd

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vo
2121
import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
2222
import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model';
2323
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
24+
import { createSuccessfulRemoteDataObject$ } from '../../remote-data.utils';
2425
import { createTestComponent } from '../../testing/utils.test';
2526
import { FormFieldMetadataValueObject } from '../builder/models/form-field-metadata-value.model';
2627
import { VocabularyTreeviewComponent } from './vocabulary-treeview.component';
@@ -63,6 +64,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
6364
searchTopEntries: jasmine.createSpy('searchTopEntries'),
6465
getEntryDetailChildren: jasmine.createSpy('getEntryDetailChildren'),
6566
clearSearchTopRequests: jasmine.createSpy('clearSearchTopRequests'),
67+
findVocabularyById: createSuccessfulRemoteDataObject$({ preloadLevel: 2 }),
6668
});
6769

6870
beforeEach(waitForAsync(() => {

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

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
2633
import { PageInfo } from '../../../core/shared/page-info.model';
34+
import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model';
2735
import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model';
2836
import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
2937
import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model';
38+
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
3039
import { AlertComponent } from '../../alert/alert.component';
3140
import { AlertType } from '../../alert/alert-type';
3241
import { 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

Comments
 (0)