Skip to content

Commit 70df81a

Browse files
[UXP-242] refactor
1 parent ce5bbf5 commit 70df81a

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,17 @@ export class VocabularyTreeviewService {
159159
}
160160
const parent: TreeviewNode = this.nodeMap.get(item.otherInformation.id)!;
161161
const children = this.nodeMap.get(item.otherInformation.id)!.children || [];
162-
this.getChildrenNodesByParent(item.otherInformation.id, parent.pageInfo).subscribe((list: PaginatedList<VocabularyEntryDetail>) => {
163-
164-
if (onlyFirstTime && parent.children!.length > 0) {
162+
this.getChildrenNodesByParent(item.otherInformation.id, parent.pageInfo, loadAll).subscribe((list: PaginatedList<VocabularyEntryDetail>) => {
163+
if (onlyFirstTime && parent.children!.length > 0 && !loadAll) {
165164
return;
166165
}
167166

168167
const newNodes: TreeviewNode[] = list.page.map((entry) => this._generateNode(entry, selectedItems));
169168
if (newNodes.length > 0) {
170-
children.pop();
169+
if (!loadAll) {
170+
//remove load more button
171+
children.pop();
172+
}
171173
children.push(...newNodes);
172174
}
173175

@@ -179,13 +181,10 @@ export class VocabularyTreeviewService {
179181
parent.updatePageInfo(newPageInfo);
180182
parent.childrenChange.next(children);
181183

182-
if (loadAll) {
183-
// Need a new load more node
184-
this.loadMore(item, selectedItems);
185-
return;
184+
if (!loadAll) {
185+
// if not all loaded add a load more button
186+
children.push(new TreeviewNode(LOAD_MORE_NODE, false, newPageInfo, item));
186187
}
187-
188-
children.push(new TreeviewNode(LOAD_MORE_NODE, false, newPageInfo, item));
189188
} else {
190189
parent.childrenChange.next(children);
191190
}
@@ -306,18 +305,27 @@ export class VocabularyTreeviewService {
306305
return this.vocabularyService.getEntryDetailChildren(parentId, this.vocabularyName, pageInfo).pipe(
307306
getFirstSucceededRemoteDataPayload(),
308307
).pipe(
309-
expand(res => {
310-
if (res.pageInfo.currentPage + 1 <= res.pageInfo.totalPages && loadAll) {
311-
const newPageInfo = Object.assign({}, res.pageInfo, {currentPage: res.pageInfo.currentPage + 1});
312-
return this.vocabularyService.getEntryDetailChildren(parentId, this.vocabularyName, newPageInfo).pipe(
313-
getFirstSucceededRemoteDataPayload(),
314-
);
315-
}
316-
return EMPTY;
317-
})
308+
expand(res => this.getPaginatedChildren(res, parentId, loadAll))
318309
);
319310
}
320311

312+
/**
313+
* Get children recursively in expand to load all children
314+
* @param res
315+
* @param parentId
316+
* @param loadAll
317+
* @private
318+
*/
319+
private getPaginatedChildren(res: PaginatedList<VocabularyEntryDetail>, parentId: string, loadAll: boolean): Observable<PaginatedList<VocabularyEntryDetail>> {
320+
if (res.pageInfo.currentPage + 1 <= res.pageInfo.totalPages && loadAll) {
321+
const newPageInfo = Object.assign({}, res.pageInfo, {currentPage: res.pageInfo.currentPage + 1});
322+
return this.vocabularyService.getEntryDetailChildren(parentId, this.vocabularyName, newPageInfo).pipe(
323+
getFirstSucceededRemoteDataPayload()
324+
);
325+
}
326+
return EMPTY;
327+
}
328+
321329
/**
322330
* Return the vocabulary entry's parent
323331
* @param entryId The entry id

0 commit comments

Comments
 (0)