Fixes parsing the list into sub-lists (#3815)

This commit is contained in:
Erik Jan de Wit 2022-11-22 12:48:38 -05:00 committed by GitHub
parent fb01fefe60
commit 03520dc30a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 14 deletions

View file

@ -188,4 +188,41 @@ describe("ExecutionList", () => {
}, },
]); ]);
}); });
it("When a sub-list has a sub-list all root nodes should not become part of first list", () => {
//given
const list = [
{ id: "0", level: 0, index: 0 },
{ id: "1", level: 1, index: 0 },
{ id: "2", level: 2, index: 0 },
{ id: "3", level: 0, index: 1 },
];
//when
const result = new ExecutionList(list);
//then
expect(result.expandableList).toEqual([
{
executionList: [
{
id: "1",
index: 0,
isCollapsed: false,
level: 1,
executionList: [{ id: "2", level: 2, index: 0 }],
},
],
id: "0",
index: 0,
isCollapsed: false,
level: 0,
},
{
id: "3",
level: 0,
index: 1,
},
]);
});
}); });

View file

@ -39,32 +39,31 @@ export class ExecutionList {
executionList: [], executionList: [],
isCollapsed: false, isCollapsed: false,
}; };
this.transformToExpandableList(0, exList); this.transformToExpandableList(0, -1, exList);
this.expandableList = exList.executionList; this.expandableList = exList.executionList;
} }
private transformToExpandableList( private transformToExpandableList(
currIndex: number, currentIndex: number,
currentLevel: number,
execution: ExpandableExecution execution: ExpandableExecution
) { ) {
for (let index = currIndex; index < this.list.length; index++) { for (let index = currentIndex; index < this.list.length; index++) {
const ex = this.list[index]; const ex = this.list[index];
const level = ex.level || 0; const level = ex.level || 0;
const nextRowLevel = this.list[index + 1]?.level || 0; if (level <= currentLevel) {
const isLeaf = level > nextRowLevel; return index - 1;
const hasChild = level < nextRowLevel;
if (isLeaf) {
execution.executionList?.push(ex);
return index;
} }
if (ex.level === level && !hasChild) { const nextRowLevel = this.list[index + 1]?.level || 0;
execution.executionList?.push(ex); const hasChild = level < nextRowLevel;
} else {
if (hasChild) {
const subLevel = { ...ex, executionList: [], isCollapsed: false }; const subLevel = { ...ex, executionList: [], isCollapsed: false };
index = this.transformToExpandableList(index + 1, subLevel); index = this.transformToExpandableList(index + 1, level, subLevel);
execution.executionList?.push(subLevel); execution.executionList?.push(subLevel);
} else {
execution.executionList?.push(ex);
} }
} }
return this.list.length; return this.list.length;