Skip to content

Commit 64fa1e4

Browse files
committed
Merge branch 'w2p-122839_vocabulary-preloadlevel-fix' into vocabulary-preloadlevel-fix-8_x
# Conflicts: # src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.spec.ts # src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts
2 parents 587df79 + 4ffde92 commit 64fa1e4

2 files changed

Lines changed: 43 additions & 6 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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ import {
2525
Observable,
2626
Subscription,
2727
} from 'rxjs';
28+
import {
29+
map,
30+
switchMap,
31+
tap,
32+
} from 'rxjs/operators';
2833

34+
import { RemoteData } from '../../../core/data/remote-data';
35+
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
2936
import { PageInfo } from '../../../core/shared/page-info.model';
37+
import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model';
3038
import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model';
3139
import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
3240
import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model';
41+
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
3342
import { AlertComponent } from '../../alert/alert.component';
3443
import { AlertType } from '../../alert/alert-type';
3544
import {
@@ -165,9 +174,11 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
165174
* Initialize instance variables
166175
*
167176
* @param {VocabularyTreeviewService} vocabularyTreeviewService
177+
* @param {VocabularyService} vocabularyService
168178
*/
169179
constructor(
170180
private vocabularyTreeviewService: VocabularyTreeviewService,
181+
protected vocabularyService: VocabularyService,
171182
) {
172183
this.treeFlattener = new VocabularyTreeFlattener(this.transformer, this.getLevel,
173184
this.isExpandable, this.getChildren);
@@ -209,12 +220,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
209220
);
210221
this.nodeMap.set(entryId, newNode);
211222

212-
if ((((level + 1) < this.preloadLevel) && newNode.childrenLoaded)
223+
if ((((level + 1) < this.preloadLevel))
213224
|| (newNode.isSearchNode && newNode.childrenLoaded)
214225
|| newNode.isInInitValueHierarchy) {
215-
if (!newNode.isSearchNode) {
226+
227+
if (newNode.item.id === LOAD_MORE || newNode.item.id === LOAD_MORE_ROOT) {
228+
// When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
229+
// so this is a good point to stop expanding.
230+
return newNode;
231+
}
232+
233+
if (!newNode.childrenLoaded) {
216234
this.loadChildren(newNode);
217235
}
236+
218237
this.treeControl.expand(newNode);
219238
}
220239
return newNode;
@@ -255,15 +274,31 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
255274
*/
256275
ngOnInit(): void {
257276
this.subs.push(
258-
this.vocabularyTreeviewService.getData().subscribe((data) => {
277+
this.vocabularyService.findVocabularyById(this.vocabularyOptions.name).pipe(
278+
// Retrieve the configured preloadLevel from REST
279+
getFirstCompletedRemoteData(),
280+
map((vocabularyRD: RemoteData<Vocabulary>) => {
281+
if (vocabularyRD.hasSucceeded &&
282+
hasValue(vocabularyRD.payload.preloadLevel) &&
283+
vocabularyRD.payload.preloadLevel > 1) {
284+
return vocabularyRD.payload.preloadLevel;
285+
} else {
286+
// Set preload level to 1 in case request fails
287+
return 1;
288+
}
289+
}),
290+
tap((preloadLevel: number) => this.preloadLevel = preloadLevel),
291+
tap(() => {
292+
const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
293+
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId);
294+
}),
295+
switchMap(() => this.vocabularyTreeviewService.getData()),
296+
).subscribe((data) => {
259297
this.dataSource.data = data;
260298
}),
261299
);
262300

263301
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);
267302
}
268303

269304
/**

0 commit comments

Comments
 (0)