Skip to content

Commit 9c6bb82

Browse files
authored
Merge pull request #3819 from atmire/vocabulary-preloadlevel-fix-main
Vocabulary preloadlevel fix
2 parents 9f09c33 + 5df7471 commit 9c6bb82

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,17 @@ import {
2222
Observable,
2323
Subscription,
2424
} from 'rxjs';
25-
25+
import {
26+
map,
27+
switchMap,
28+
tap,
29+
} from 'rxjs/operators';
30+
import { VocabularyService } from 'src/app/core/submission/vocabularies/vocabulary.service';
31+
32+
import { RemoteData } from '../../../core/data/remote-data';
33+
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
2634
import { PageInfo } from '../../../core/shared/page-info.model';
35+
import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model';
2736
import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model';
2837
import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
2938
import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model';
@@ -166,6 +175,7 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
166175
*/
167176
constructor(
168177
private vocabularyTreeviewService: VocabularyTreeviewService,
178+
protected vocabularyService: VocabularyService,
169179
) {
170180
this.treeFlattener = new VocabularyTreeFlattener(this.transformer, this.getLevel,
171181
this.isExpandable, this.getChildren);
@@ -207,12 +217,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
207217
);
208218
this.nodeMap.set(entryId, newNode);
209219

210-
if ((((level + 1) < this.preloadLevel) && newNode.childrenLoaded)
220+
if ((((level + 1) < this.preloadLevel))
211221
|| (newNode.isSearchNode && newNode.childrenLoaded)
212222
|| newNode.isInInitValueHierarchy) {
213-
if (!newNode.isSearchNode) {
223+
224+
if (newNode.item.id === LOAD_MORE || newNode.item.id === LOAD_MORE_ROOT) {
225+
// When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
226+
// so this is a good point to stop expanding.
227+
return newNode;
228+
}
229+
230+
if (!newNode.childrenLoaded) {
214231
this.loadChildren(newNode);
215232
}
233+
216234
this.treeControl.expand(newNode);
217235
}
218236
return newNode;
@@ -253,15 +271,31 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
253271
*/
254272
ngOnInit(): void {
255273
this.subs.push(
256-
this.vocabularyTreeviewService.getData().subscribe((data) => {
274+
this.vocabularyService.findVocabularyById(this.vocabularyOptions.name).pipe(
275+
// Retrieve the configured preloadLevel from REST
276+
getFirstCompletedRemoteData(),
277+
map((vocabularyRD: RemoteData<Vocabulary>) => {
278+
if (vocabularyRD.hasSucceeded &&
279+
hasValue(vocabularyRD.payload.preloadLevel) &&
280+
vocabularyRD.payload.preloadLevel > 1) {
281+
return vocabularyRD.payload.preloadLevel;
282+
} else {
283+
// Set preload level to 1 in case request fails
284+
return 1;
285+
}
286+
}),
287+
tap(preloadLevel => this.preloadLevel = preloadLevel),
288+
tap(() => {
289+
const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
290+
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId);
291+
}),
292+
switchMap(() => this.vocabularyTreeviewService.getData()),
293+
).subscribe((data) => {
257294
this.dataSource.data = data;
258295
}),
259296
);
260297

261298
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);
265299
}
266300

267301
/**

0 commit comments

Comments
 (0)