Skip to content

Commit b815737

Browse files
authored
Merge pull request DSpace#3820 from atmire/vocabulary-preloadlevel-fix-8_x
[Port dspace-8_x] Vocabulary preloadlevel fix
2 parents 524de36 + 64fa1e4 commit b815737

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 { BtnDisabledDirective } from '../../btn-disabled.directive';
@@ -167,9 +176,11 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
167176
* Initialize instance variables
168177
*
169178
* @param {VocabularyTreeviewService} vocabularyTreeviewService
179+
* @param {VocabularyService} vocabularyService
170180
*/
171181
constructor(
172182
private vocabularyTreeviewService: VocabularyTreeviewService,
183+
protected vocabularyService: VocabularyService,
173184
) {
174185
this.treeFlattener = new VocabularyTreeFlattener(this.transformer, this.getLevel,
175186
this.isExpandable, this.getChildren);
@@ -211,12 +222,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
211222
);
212223
this.nodeMap.set(entryId, newNode);
213224

214-
if ((((level + 1) < this.preloadLevel) && newNode.childrenLoaded)
225+
if ((((level + 1) < this.preloadLevel))
215226
|| (newNode.isSearchNode && newNode.childrenLoaded)
216227
|| newNode.isInInitValueHierarchy) {
217-
if (!newNode.isSearchNode) {
228+
229+
if (newNode.item.id === LOAD_MORE || newNode.item.id === LOAD_MORE_ROOT) {
230+
// When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
231+
// so this is a good point to stop expanding.
232+
return newNode;
233+
}
234+
235+
if (!newNode.childrenLoaded) {
218236
this.loadChildren(newNode);
219237
}
238+
220239
this.treeControl.expand(newNode);
221240
}
222241
return newNode;
@@ -257,15 +276,31 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
257276
*/
258277
ngOnInit(): void {
259278
this.subs.push(
260-
this.vocabularyTreeviewService.getData().subscribe((data) => {
279+
this.vocabularyService.findVocabularyById(this.vocabularyOptions.name).pipe(
280+
// Retrieve the configured preloadLevel from REST
281+
getFirstCompletedRemoteData(),
282+
map((vocabularyRD: RemoteData<Vocabulary>) => {
283+
if (vocabularyRD.hasSucceeded &&
284+
hasValue(vocabularyRD.payload.preloadLevel) &&
285+
vocabularyRD.payload.preloadLevel > 1) {
286+
return vocabularyRD.payload.preloadLevel;
287+
} else {
288+
// Set preload level to 1 in case request fails
289+
return 1;
290+
}
291+
}),
292+
tap((preloadLevel: number) => this.preloadLevel = preloadLevel),
293+
tap(() => {
294+
const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
295+
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId);
296+
}),
297+
switchMap(() => this.vocabularyTreeviewService.getData()),
298+
).subscribe((data) => {
261299
this.dataSource.data = data;
262300
}),
263301
);
264302

265303
this.loading = this.vocabularyTreeviewService.isLoading();
266-
267-
const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
268-
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId);
269304
}
270305

271306
/**

0 commit comments

Comments
 (0)