Skip to content

Commit 5df7471

Browse files
committed
Merge branch 'w2p-122839_vocabulary-preloadlevel-fix' into vocabulary-preloadlevel-fix-main
# Conflicts: # src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.spec.ts # src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts
2 parents 952611a + 4ffde92 commit 5df7471

2 files changed

Lines changed: 43 additions & 7 deletions

File tree

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
@@ -25,8 +25,17 @@ import {
2525
Observable,
2626
Subscription,
2727
} from 'rxjs';
28-
28+
import {
29+
map,
30+
switchMap,
31+
tap,
32+
} from 'rxjs/operators';
33+
import { VocabularyService } from 'src/app/core/submission/vocabularies/vocabulary.service';
34+
35+
import { RemoteData } from '../../../core/data/remote-data';
36+
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
2937
import { PageInfo } from '../../../core/shared/page-info.model';
38+
import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model';
3039
import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model';
3140
import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
3241
import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model';
@@ -168,6 +177,7 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
168177
*/
169178
constructor(
170179
private vocabularyTreeviewService: VocabularyTreeviewService,
180+
protected vocabularyService: VocabularyService,
171181
) {
172182
this.treeFlattener = new VocabularyTreeFlattener(this.transformer, this.getLevel,
173183
this.isExpandable, this.getChildren);
@@ -209,12 +219,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
209219
);
210220
this.nodeMap.set(entryId, newNode);
211221

212-
if ((((level + 1) < this.preloadLevel) && newNode.childrenLoaded)
222+
if ((((level + 1) < this.preloadLevel))
213223
|| (newNode.isSearchNode && newNode.childrenLoaded)
214224
|| newNode.isInInitValueHierarchy) {
215-
if (!newNode.isSearchNode) {
225+
226+
if (newNode.item.id === LOAD_MORE || newNode.item.id === LOAD_MORE_ROOT) {
227+
// When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
228+
// so this is a good point to stop expanding.
229+
return newNode;
230+
}
231+
232+
if (!newNode.childrenLoaded) {
216233
this.loadChildren(newNode);
217234
}
235+
218236
this.treeControl.expand(newNode);
219237
}
220238
return newNode;
@@ -255,15 +273,31 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
255273
*/
256274
ngOnInit(): void {
257275
this.subs.push(
258-
this.vocabularyTreeviewService.getData().subscribe((data) => {
276+
this.vocabularyService.findVocabularyById(this.vocabularyOptions.name).pipe(
277+
// Retrieve the configured preloadLevel from REST
278+
getFirstCompletedRemoteData(),
279+
map((vocabularyRD: RemoteData<Vocabulary>) => {
280+
if (vocabularyRD.hasSucceeded &&
281+
hasValue(vocabularyRD.payload.preloadLevel) &&
282+
vocabularyRD.payload.preloadLevel > 1) {
283+
return vocabularyRD.payload.preloadLevel;
284+
} else {
285+
// Set preload level to 1 in case request fails
286+
return 1;
287+
}
288+
}),
289+
tap(preloadLevel => this.preloadLevel = preloadLevel),
290+
tap(() => {
291+
const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
292+
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId);
293+
}),
294+
switchMap(() => this.vocabularyTreeviewService.getData()),
295+
).subscribe((data) => {
259296
this.dataSource.data = data;
260297
}),
261298
);
262299

263300
this.loading = this.vocabularyTreeviewService.isLoading();
264-
265-
const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
266-
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId);
267301
}
268302

269303
/**

0 commit comments

Comments
 (0)