Skip to content

Commit e1e2886

Browse files
FrancescoMolinarovins01-4science
authored andcommitted
Merged in task/ux-plus-2023_02_x/UXP-222 (pull request #32)
[UXP-222] fix issues with vocabulary component Approved-by: Andrea Barbasso
2 parents 8af3c4b + d1f2d59 commit e1e2886

5 files changed

Lines changed: 77 additions & 40 deletions

File tree

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
@@ -146,7 +146,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
146146
comp.selectedItems = [currentValue];
147147
fixture.detectChanges();
148148
expect(comp.dataSource.data).toEqual([]);
149-
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false);
149+
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false, false);
150150
});
151151

152152
it('should should init component properly with init value as VocabularyEntry', () => {
@@ -158,7 +158,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
158158
comp.selectedItems = [currentValue];
159159
fixture.detectChanges();
160160
expect(comp.dataSource.data).toEqual([]);
161-
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false);
161+
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false, false);
162162
});
163163

164164
it('should should init component properly with init value as VocabularyEntryDetail', () => {
@@ -168,7 +168,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
168168
comp.selectedItems = [currentValue];
169169
fixture.detectChanges();
170170
expect(comp.dataSource.data).toEqual([]);
171-
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false);
171+
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', false, false);
172172
});
173173

174174
it('should should init component properly with init value as VocabularyEntry and publicModeOnly enabled', () => {
@@ -181,7 +181,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
181181
comp.selectedItems = [currentValue];
182182
fixture.detectChanges();
183183
expect(comp.dataSource.data).toEqual([]);
184-
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', true);
184+
expect(vocabularyTreeviewServiceStub.initialize).toHaveBeenCalledWith(comp.vocabularyOptions, new PageInfo(), ['entryID'], 'entryID', true, false);
185185
});
186186

187187
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
@@ -68,6 +68,11 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
6868
*/
6969
@Input() selectedItems: VocabularyTreeItemType[] = [];
7070

71+
/**
72+
* Whether the component is used as a relation component
73+
*/
74+
@Input() isRelationComponent = false;
75+
7176
/**
7277
* A map containing the current node showed by the tree
7378
*/
@@ -228,7 +233,7 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
228233
this.loading = this.vocabularyTreeviewService.isLoading();
229234

230235
const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
231-
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId, this.publicModeOnly);
236+
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId, this.publicModeOnly, this.isRelationComponent);
232237
}
233238

234239
/**

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

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,54 +205,75 @@ describe('VocabularyTreeviewService test suite', () => {
205205
});
206206

207207
it('should set initValueHierarchy', () => {
208-
serviceAsAny.vocabularyService.getEntryDetailChildren.and.returnValue(hot('--a', {
209-
a: createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, [child, child2]))
208+
serviceAsAny.vocabularyService.searchTopEntries.and.returnValue(hot('--a', {
209+
a: createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, [item, item2, item3]))
210210
}));
211-
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValues(
211+
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValue(
212212
hot('-a', {
213-
a: createSuccessfulRemoteDataObject(child),
214-
}),
215-
hot('-b', {
216-
b: createSuccessfulRemoteDataObject(item)
213+
a: createSuccessfulRemoteDataObject(child2)
217214
})
218215
);
219216
serviceAsAny.vocabularyService.getEntryDetailParent.and.returnValue(
220217
hot('-b', {
221218
b: createSuccessfulRemoteDataObject(item)
222219
})
223220
);
224-
225-
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, [], 'root1'));
221+
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, [], 'root2'));
226222
scheduler.flush();
227223

228224
expect(serviceAsAny.vocabularyName).toEqual(vocabularyOptions.name);
229-
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child1']);
225+
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child2']);
226+
expect(serviceAsAny.dataChange.value).toEqual([itemInitNode, itemNode2, itemNode3]);
230227
});
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]))
228+
229+
it('should show only nodes restricted to init Value Hierarchy', () => {
230+
serviceAsAny.vocabularyService.searchTopEntries.and.returnValue(hot('--a', {
231+
a: createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, [item, item2, item3]))
235232
}));
236-
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValues(
233+
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValue(
237234
hot('-a', {
238-
a: createSuccessfulRemoteDataObject(child),
239-
}),
240-
hot('-b', {
241-
b: createSuccessfulRemoteDataObject(item)
235+
a: createSuccessfulRemoteDataObject(child2)
242236
})
243237
);
244238
serviceAsAny.vocabularyService.getEntryDetailParent.and.returnValue(
245239
hot('-b', {
246240
b: createSuccessfulRemoteDataObject(item)
247241
})
248242
);
249-
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, ['root1-child1'], 'root1-child1', true));
243+
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, ['root2'], 'root2', true));
250244
scheduler.flush();
251245

252246
expect(serviceAsAny.vocabularyName).toEqual(vocabularyOptions.name);
253-
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child1']);
247+
expect(serviceAsAny.initValueHierarchy).toEqual(['root1', 'root1-child2']);
254248
expect(serviceAsAny.dataChange.value).toEqual([itemInitNode]);
255249
});
250+
251+
it('should call retrieveNodesTreeByTopParentEntry properly when is a relation component', () => {
252+
serviceAsAny.vocabularyService.findEntryDetailById.and.returnValues(
253+
hot('-a', {
254+
a: createSuccessfulRemoteDataObject(child)
255+
}),
256+
hot('-b', {
257+
b: createSuccessfulRemoteDataObject(item)
258+
}),
259+
);
260+
261+
serviceAsAny.vocabularyService.getEntryDetailChildren.and.returnValue(hot('-a', {
262+
a: createSuccessfulRemoteDataObject([child])
263+
}));
264+
265+
serviceAsAny.vocabularyService.getEntryDetailParent.and.returnValue(
266+
hot('-a', {
267+
a: createSuccessfulRemoteDataObject(item)
268+
}),
269+
);
270+
spyOn(serviceAsAny, 'retrieveNodesTreeByTopParentEntry');
271+
272+
scheduler.schedule(() => service.initialize(vocabularyOptions, pageInfo, [child.id], child.id, false, true));
273+
scheduler.flush();
274+
275+
expect(serviceAsAny.retrieveNodesTreeByTopParentEntry).toHaveBeenCalledWith(child.otherInformation.parent, pageInfo, [child.id]);
276+
});
256277
});
257278

258279
describe('getData', () => {
@@ -389,4 +410,5 @@ describe('VocabularyTreeviewService test suite', () => {
389410
expect(serviceAsAny.dataChange.value).toEqual(treeNodeList);
390411
});
391412
});
413+
392414
});

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ export class VocabularyTreeviewService {
103103
* @param selectedItems The currently selected items
104104
* @param initValueId The entry id of the node to mark as selected, if any
105105
* @param publicModeOnly Whether the tree is used in a public view
106+
* @param isRelationComponent Whether the tree is used as relation component
106107
*/
107-
initialize(options: VocabularyOptions, pageInfo: PageInfo, selectedItems: string[], initValueId?: string, publicModeOnly = false): void {
108+
initialize(options: VocabularyOptions, pageInfo: PageInfo, selectedItems: string[], initValueId?: string, publicModeOnly = false, isRelationComponent = false): void {
108109
this.loading.next(true);
109110
this.vocabularyOptions = options;
110111
this.vocabularyName = options.name;
@@ -114,8 +115,9 @@ export class VocabularyTreeviewService {
114115
.subscribe((hierarchy: string[]) => {
115116
if (hasValue(hierarchy) && hierarchy.length > 0) {
116117
this.initValueHierarchy = hierarchy;
117-
//this.retrieveTopNodes(pageInfo, [], selectedItems, publicModeOnly ? hierarchy : null);
118-
this.retrieveNodesTreeByTopParentEntry(hierarchy[0], pageInfo, selectedItems);
118+
isRelationComponent ?
119+
this.retrieveNodesTreeByTopParentEntry(hierarchy[0], pageInfo, selectedItems) :
120+
this.retrieveTopNodes(pageInfo, [], selectedItems, publicModeOnly ? hierarchy : null);
119121
} else {
120122
this.loading.next(false);
121123
}
@@ -375,22 +377,29 @@ export class VocabularyTreeviewService {
375377
]).pipe(
376378
mergeMap(([rootNode, list]) => {
377379
tempList = list;
380+
let newPageInfo: PageInfo;
378381

379382
const childNodes: TreeviewNode[] = list.page.map((entryDetail: VocabularyEntryDetail) => this._generateNode(entryDetail, selectedItems));
380-
return this.getNodeHierarchy(rootNode, selectedItems, childNodes);
383+
384+
if ((tempList.pageInfo.currentPage + 1) <= tempList.pageInfo.totalPages) {
385+
// Need a new load more node
386+
newPageInfo = Object.assign(new PageInfo(), tempList.pageInfo, {
387+
currentPage: tempList.pageInfo.currentPage + 1
388+
});
389+
const loadMoreNode = new TreeviewNode(LOAD_MORE_NODE, false, newPageInfo);
390+
loadMoreNode.updatePageInfo(newPageInfo);
391+
childNodes.push(loadMoreNode);
392+
}
393+
return this.getNodeHierarchy(rootNode, selectedItems, childNodes).pipe(
394+
map(node => {
395+
node.updatePageInfo(newPageInfo);
396+
return node;
397+
}),
398+
);
381399
})
382400
).subscribe(hierarchy => {
383401
nodes.push(hierarchy);
384402

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-
}
394403
this.loading.next(false);
395404
// Notify the change.
396405
this.dataChange.next(nodes);
@@ -417,7 +426,7 @@ export class VocabularyTreeviewService {
417426
if (isNotEmpty(children)) {
418427
const newChildren = children
419428
.filter((entry: TreeviewNode) => {
420-
return findIndex(node.children, (nodeEntry) => nodeEntry.item.otherInformation.id === entry.item.otherInformation.id) === -1;
429+
return findIndex(node.children, (nodeEntry) => nodeEntry.item.otherInformation?.id === entry.item.otherInformation?.id) === -1;
421430
});
422431
newChildren.forEach((entry: TreeviewNode) => {
423432
entry.loadMoreParentItem = node.item;

0 commit comments

Comments
 (0)