Skip to content

Commit b7b9e8a

Browse files
FrancescoMolinarovins01-4science
authored andcommitted
Merged in task/ux-plus-cris/UXP-222 (pull request #33)
Task/ux plus cris/UXP-222 Approved-by: Andrea Barbasso
2 parents 81712aa + 534e227 commit b7b9e8a

File tree

5 files changed

+90
-32
lines changed

5 files changed

+90
-32
lines changed

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/hierarchy/hierarchy.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ <h4 class="modal-title border-bottom mb-3" *ngIf="vocabularyHeader">{{'vocabular
66
[selectedItems]="[selectedItem]"
77
[enabledSearch]="false"
88
[publicModeOnly]="true"
9+
[isRelationComponent]="true"
910
></ds-vocabulary-treeview>
1011
</div>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
160160
comp.selectedItems = [currentValue];
161161
fixture.detectChanges();
162162
expect(comp.dataSource.data).toEqual([]);
163-
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false);
163+
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false, false);
164164
});
165165

166166
it('should should init component properly with init value as VocabularyEntry', () => {
@@ -172,7 +172,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
172172
comp.selectedItems = [currentValue];
173173
fixture.detectChanges();
174174
expect(comp.dataSource.data).toEqual([]);
175-
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false);
175+
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false, false);
176176
});
177177

178178
it('should should init component properly with init value as VocabularyEntryDetail', () => {
@@ -182,7 +182,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
182182
comp.selectedItems = [currentValue];
183183
fixture.detectChanges();
184184
expect(comp.dataSource.data).toEqual([]);
185-
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false);
185+
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false, false);
186186
});
187187

188188
it('should should init component properly with init value as VocabularyEntry and publicModeOnly enabled', () => {
@@ -195,7 +195,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
195195
comp.selectedItems = [currentValue];
196196
fixture.detectChanges();
197197
expect(comp.dataSource.data).toEqual([]);
198-
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', true);
198+
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', true, false);
199199
});
200200

201201
it('should call loadMore function', () => {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
115115
*/
116116
@Input() selectedItems: VocabularyTreeItemType[] = [];
117117

118+
/**
119+
* Whether the component is used as a relation component
120+
*/
121+
@Input() isRelationComponent = false;
122+
118123
/**
119124
* A map containing the current node showed by the tree
120125
*/
@@ -275,7 +280,7 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
275280
this.loading = this.vocabularyTreeviewService.isLoading();
276281

277282
const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
278-
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId, this.publicModeOnly);
283+
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId, this.publicModeOnly, this.isRelationComponent);
279284
}
280285

281286
/**

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

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,28 +224,47 @@ describe('VocabularyTreeviewService test suite', () => {
224224
});
225225

226226
it('should set initValueHierarchy', () => {
227-
serviceAsAny.vocabularyService.getEntryDetailChildren.and.returnValue(hot('--a', {
228-
a: createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, [child, child2])),
227+
serviceAsAny.vocabularyService.searchTopEntries.and.returnValue(hot('--a', {
228+
a: createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, [item, item2, item3])),
229229
}));
230-
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValues(
230+
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValue(
231231
hot('-a', {
232-
a: createSuccessfulRemoteDataObject(child),
232+
a: createSuccessfulRemoteDataObject(child2),
233233
}),
234+
);
235+
serviceAsAny.vocabularyService.getEntryDetailParent.and.returnValue(
234236
hot('-b', {
235237
b: createSuccessfulRemoteDataObject(item),
236238
}),
237239
);
240+
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, [], 'root2'));
241+
scheduler.flush();
242+
243+
expect(serviceAsAny.vocabularyName).toEqual(vocabularyOptions.name);
244+
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child2']);
245+
expect(serviceAsAny.dataChange.value).toEqual([itemInitNode, itemNode2, itemNode3]);
246+
});
247+
248+
it('should show only nodes restricted to init Value Hierarchy', () => {
249+
serviceAsAny.vocabularyService.searchTopEntries.and.returnValue(hot('--a', {
250+
a: createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, [item, item2, item3])),
251+
}));
252+
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValue(
253+
hot('-a', {
254+
a: createSuccessfulRemoteDataObject(child2),
255+
}),
256+
);
238257
serviceAsAny.vocabularyService.getEntryDetailParent.and.returnValue(
239258
hot('-b', {
240259
b: createSuccessfulRemoteDataObject(item),
241260
}),
242261
);
243-
244-
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, [], 'root1'));
262+
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, ['root2'], 'root2', true));
245263
scheduler.flush();
246264

247265
expect(serviceAsAny.vocabularyName).toEqual(vocabularyOptions.name);
248-
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child1']);
266+
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child2']);
267+
expect(serviceAsAny.dataChange.value).toEqual([itemInitNode]);
249268
});
250269
// 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
251270
xit('should show only nodes restricted to init Value Hierarchy', () => {
@@ -254,24 +273,48 @@ describe('VocabularyTreeviewService test suite', () => {
254273
}));
255274
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValues(
256275
hot('-a', {
257-
a: createSuccessfulRemoteDataObject(child),
258-
}),
259-
hot('-b', {
260-
b: createSuccessfulRemoteDataObject(item),
276+
a: createSuccessfulRemoteDataObject(child2),
261277
}),
262278
);
263279
serviceAsAny.vocabularyService.getEntryDetailParent.and.returnValue(
264280
hot('-b', {
265281
b: createSuccessfulRemoteDataObject(item),
266282
}),
267283
);
268-
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, ['root1-child1'], 'root1-child1', true));
284+
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, ['root2'], 'root2', true));
269285
scheduler.flush();
270286

271287
expect(serviceAsAny.vocabularyName).toEqual(vocabularyOptions.name);
272-
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child1']);
288+
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child2']);
273289
expect(serviceAsAny.dataChange.value).toEqual([itemInitNode]);
274290
});
291+
292+
it('should call retrieveNodesTreeByTopParentEntry properly when is a relation component', () => {
293+
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValues(
294+
hot('-a', {
295+
a: createSuccessfulRemoteDataObject(child),
296+
}),
297+
hot('-b', {
298+
b: createSuccessfulRemoteDataObject(item),
299+
}),
300+
);
301+
302+
serviceAsAny.vocabularyService.getEntryDetailChildren.and.returnValue(hot('-a', {
303+
a: createSuccessfulRemoteDataObject([child]),
304+
}));
305+
306+
serviceAsAny.vocabularyService.getEntryDetailParent.and.returnValue(
307+
hot('-a', {
308+
a: createSuccessfulRemoteDataObject(item),
309+
}),
310+
);
311+
spyOn(serviceAsAny, 'retrieveNodesTreeByTopParentEntry');
312+
313+
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, [child.id], child.id, false, true));
314+
scheduler.flush();
315+
316+
expect(serviceAsAny.retrieveNodesTreeByTopParentEntry).toHaveBeenCalledWith(child.otherInformation.parent, pageInfo, [child.id]);
317+
});
275318
});
276319

277320
describe('getData', () => {

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ export class VocabularyTreeviewService {
121121
* @param selectedItems The currently selected items
122122
* @param initValueId The entry id of the node to mark as selected, if any
123123
* @param publicModeOnly Whether the tree is used in a public view
124+
* @param isRelationComponent Whether the tree is used as relation component
124125
*/
125-
initialize(options: VocabularyOptions, pageInfo: PageInfo, selectedItems: string[], initValueId?: string, publicModeOnly = false): void {
126+
initialize(options: VocabularyOptions, pageInfo: PageInfo, selectedItems: string[], initValueId?: string, publicModeOnly = false, isRelationComponent = false): void {
126127
this.loading.next(true);
127128
this.vocabularyOptions = options;
128129
this.vocabularyName = options.name;
@@ -132,8 +133,9 @@ export class VocabularyTreeviewService {
132133
.subscribe((hierarchy: string[]) => {
133134
if (hasValue(hierarchy) && hierarchy.length > 0) {
134135
this.initValueHierarchy = hierarchy;
135-
//this.retrieveTopNodes(pageInfo, [], selectedItems, publicModeOnly ? hierarchy : null);
136-
this.retrieveNodesTreeByTopParentEntry(hierarchy[0], pageInfo, selectedItems);
136+
isRelationComponent ?
137+
this.retrieveNodesTreeByTopParentEntry(hierarchy[0], pageInfo, selectedItems) :
138+
this.retrieveTopNodes(pageInfo, [], selectedItems, publicModeOnly ? hierarchy : null);
137139
} else {
138140
this.loading.next(false);
139141
}
@@ -393,22 +395,29 @@ export class VocabularyTreeviewService {
393395
]).pipe(
394396
mergeMap(([rootNode, list]) => {
395397
tempList = list;
398+
let newPageInfo: PageInfo;
396399

397400
const childNodes: TreeviewNode[] = list.page.map((entryDetail: VocabularyEntryDetail) => this._generateNode(entryDetail, selectedItems));
398-
return this.getNodeHierarchy(rootNode, selectedItems, childNodes);
401+
402+
if ((tempList.pageInfo.currentPage + 1) <= tempList.pageInfo.totalPages) {
403+
// Need a new load more node
404+
newPageInfo = Object.assign(new PageInfo(), tempList.pageInfo, {
405+
currentPage: tempList.pageInfo.currentPage + 1,
406+
});
407+
const loadMoreNode = new TreeviewNode(LOAD_MORE_NODE, false, newPageInfo);
408+
loadMoreNode.updatePageInfo(newPageInfo);
409+
childNodes.push(loadMoreNode);
410+
}
411+
return this.getNodeHierarchy(rootNode, selectedItems, childNodes).pipe(
412+
map(node => {
413+
node.updatePageInfo(newPageInfo);
414+
return node;
415+
}),
416+
);
399417
}),
400418
).subscribe(hierarchy => {
401419
nodes.push(hierarchy);
402420

403-
if ((tempList.pageInfo.currentPage + 1) <= tempList.pageInfo.totalPages) {
404-
// Need a new load more node
405-
const newPageInfo: PageInfo = Object.assign(new PageInfo(), tempList.pageInfo, {
406-
currentPage: tempList.pageInfo.currentPage + 1,
407-
});
408-
const loadMoreNode = new TreeviewNode(LOAD_MORE_ROOT_NODE, false, newPageInfo);
409-
loadMoreNode.updatePageInfo(newPageInfo);
410-
nodes.push(loadMoreNode);
411-
}
412421
this.loading.next(false);
413422
// Notify the change.
414423
this.dataChange.next(nodes);
@@ -435,7 +444,7 @@ export class VocabularyTreeviewService {
435444
if (isNotEmpty(children)) {
436445
const newChildren = children
437446
.filter((entry: TreeviewNode) => {
438-
return findIndex(node.children, (nodeEntry) => nodeEntry.item.otherInformation.id === entry.item.otherInformation.id) === -1;
447+
return findIndex(node.children, (nodeEntry) => nodeEntry.item.otherInformation?.id === entry.item.otherInformation?.id) === -1;
439448
});
440449
newChildren.forEach((entry: TreeviewNode) => {
441450
entry.loadMoreParentItem = node.item;

0 commit comments

Comments
 (0)