Skip to content

Commit 89091ba

Browse files
Merged in task/ux-plus-2023_02_x/UXP-153 (pull request #7)
[UXP-153] improve tree loading of root nodes
2 parents 258e57a + 5824b4d commit 89091ba

2 files changed

Lines changed: 67 additions & 18 deletions

File tree

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ describe('VocabularyTreeviewService test suite', () => {
6464
searchTopEntries: jasmine.createSpy('searchTopEntries'),
6565
getEntryDetailChildren: jasmine.createSpy('getEntryDetailChildren'),
6666
clearSearchTopRequests: jasmine.createSpy('clearSearchTopRequests'),
67-
getPublicVocabularyEntryByValue: jasmine.createSpy('getPublicVocabularyEntryByValue')
67+
getPublicVocabularyEntryByValue: jasmine.createSpy('getPublicVocabularyEntryByValue'),
68+
getPublicVocabularyEntryByID: jasmine.createSpy('getPublicVocabularyEntryByID'),
6869
});
6970

7071
function init() {
@@ -204,46 +205,52 @@ describe('VocabularyTreeviewService test suite', () => {
204205
});
205206

206207
it('should set initValueHierarchy', () => {
207-
serviceAsAny.vocabularyService.searchTopEntries.and.returnValue(hot('--a', {
208-
a: createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, [item, item2, item3]))
208+
serviceAsAny.vocabularyService.getEntryDetailChildren.and.returnValue(hot('--a', {
209+
a: createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, [child, child2]))
209210
}));
210-
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValue(
211+
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValues(
211212
hot('-a', {
212-
a: createSuccessfulRemoteDataObject(child2)
213+
a: createSuccessfulRemoteDataObject(child),
214+
}),
215+
hot('-b', {
216+
b: createSuccessfulRemoteDataObject(item)
213217
})
214218
);
215219
serviceAsAny.vocabularyService.getEntryDetailParent.and.returnValue(
216220
hot('-b', {
217221
b: createSuccessfulRemoteDataObject(item)
218222
})
219223
);
220-
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, [], 'root2'));
224+
225+
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, [], 'root1'));
221226
scheduler.flush();
222227

223228
expect(serviceAsAny.vocabularyName).toEqual(vocabularyOptions.name);
224-
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child2']);
225-
expect(serviceAsAny.dataChange.value).toEqual([itemInitNode, itemNode2, itemNode3]);
229+
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child1']);
226230
});
227-
228-
it('should show only nodes restricted to init Value Hierarchy', () => {
229-
serviceAsAny.vocabularyService.searchTopEntries.and.returnValue(hot('--a', {
230-
a: createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, [item, item2, item3]))
231+
// Disabled as we don't limt the tree anymore to the first value of the hierarchy but we start building the tree directly from that one (root node) for any case
232+
xit('should show only nodes restricted to init Value Hierarchy', () => {
233+
serviceAsAny.vocabularyService.getEntryDetailChildren.and.returnValue(hot('--a', {
234+
a: createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, [child, child2]))
231235
}));
232-
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValue(
236+
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValues(
233237
hot('-a', {
234-
a: createSuccessfulRemoteDataObject(child2)
238+
a: createSuccessfulRemoteDataObject(child),
239+
}),
240+
hot('-b', {
241+
b: createSuccessfulRemoteDataObject(item)
235242
})
236243
);
237244
serviceAsAny.vocabularyService.getEntryDetailParent.and.returnValue(
238245
hot('-b', {
239246
b: createSuccessfulRemoteDataObject(item)
240247
})
241248
);
242-
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, ['root2'], 'root2', true));
249+
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, ['root1-child1'], 'root1-child1', true));
243250
scheduler.flush();
244251

245252
expect(serviceAsAny.vocabularyName).toEqual(vocabularyOptions.name);
246-
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child2']);
253+
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child1']);
247254
expect(serviceAsAny.dataChange.value).toEqual([itemInitNode]);
248255
});
249256
});

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RemoteData } from 'src/app/core/data/remote-data';
22
import { Injectable } from '@angular/core';
33

4-
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
4+
import { BehaviorSubject, combineLatest, Observable, of as observableOf } from 'rxjs';
55
import { map, merge, mergeMap, scan } from 'rxjs/operators';
66
import findIndex from 'lodash/findIndex';
77

@@ -114,7 +114,8 @@ export class VocabularyTreeviewService {
114114
.subscribe((hierarchy: string[]) => {
115115
if (hasValue(hierarchy) && hierarchy.length > 0) {
116116
this.initValueHierarchy = hierarchy;
117-
this.retrieveTopNodes(pageInfo, [], selectedItems, publicModeOnly ? hierarchy : null);
117+
//this.retrieveTopNodes(pageInfo, [], selectedItems, publicModeOnly ? hierarchy : null);
118+
this.retrieveNodesTreeByTopParentEntry(hierarchy[0], pageInfo, selectedItems);
118119
} else {
119120
this.loading.next(false);
120121
}
@@ -357,6 +358,47 @@ export class VocabularyTreeviewService {
357358
});
358359
}
359360

361+
/**
362+
* Retrieve the top level vocabulary entries based on current entry
363+
* @param entry The root node to use to start building the tree
364+
* @param pageInfo The {@link PageInfo} object
365+
* @param selectedItems The currently selected items
366+
*/
367+
private retrieveNodesTreeByTopParentEntry(entry: string, pageInfo: PageInfo, selectedItems: string[]): void {
368+
const nodes = [];
369+
const rootNode$ = this.getById(entry);
370+
let tempList;
371+
372+
combineLatest([
373+
rootNode$,
374+
this.getChildrenNodesByParent(entry, pageInfo)
375+
]).pipe(
376+
mergeMap(([rootNode, list]) => {
377+
tempList = list;
378+
379+
const childNodes: TreeviewNode[] = list.page.map((entryDetail: VocabularyEntryDetail) => this._generateNode(entryDetail, selectedItems));
380+
return this.getNodeHierarchy(rootNode, selectedItems, childNodes);
381+
})
382+
).subscribe(hierarchy => {
383+
nodes.push(hierarchy);
384+
385+
if ((tempList.pageInfo.currentPage + 1) <= tempList.pageInfo.totalPages) {
386+
// Need a new load more node
387+
const newPageInfo: PageInfo = Object.assign(new PageInfo(), tempList.pageInfo, {
388+
currentPage: tempList.pageInfo.currentPage + 1
389+
});
390+
const loadMoreNode = new TreeviewNode(LOAD_MORE_ROOT_NODE, false, newPageInfo);
391+
loadMoreNode.updatePageInfo(newPageInfo);
392+
nodes.push(loadMoreNode);
393+
}
394+
this.loading.next(false);
395+
// Notify the change.
396+
this.dataChange.next(nodes);
397+
398+
});
399+
400+
}
401+
360402
/**
361403
* Build and return the tree node hierarchy by a given vocabulary entry
362404
*

0 commit comments

Comments
 (0)