2018-10-25 18:09:22 +02:00
|
|
|
import {
|
|
|
|
ITreeNodeObject,
|
|
|
|
TreeNode,
|
|
|
|
} from '../models/domain/treeNode';
|
|
|
|
|
|
|
|
export class ServiceUtils {
|
2020-08-12 21:42:42 +02:00
|
|
|
static nestedTraverse(nodeTree: TreeNode<ITreeNodeObject>[], partIndex: number, parts: string[],
|
2018-10-26 22:08:48 +02:00
|
|
|
obj: ITreeNodeObject, parent: ITreeNodeObject, delimiter: string) {
|
2018-10-25 18:09:22 +02:00
|
|
|
if (parts.length <= partIndex) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const end = partIndex === parts.length - 1;
|
|
|
|
const partName = parts[partIndex];
|
|
|
|
|
|
|
|
for (let i = 0; i < nodeTree.length; i++) {
|
2018-11-30 15:49:08 +01:00
|
|
|
if (nodeTree[i].node.name !== parts[partIndex]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (end && nodeTree[i].node.id !== obj.id) {
|
|
|
|
// Another node with the same name.
|
|
|
|
nodeTree.push(new TreeNode(obj, partName, parent));
|
2018-10-25 18:09:22 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-11-30 15:49:08 +01:00
|
|
|
ServiceUtils.nestedTraverse(nodeTree[i].children, partIndex + 1, parts,
|
|
|
|
obj, nodeTree[i].node, delimiter);
|
|
|
|
return;
|
2018-10-25 18:09:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nodeTree.filter((n) => n.node.name === partName).length === 0) {
|
|
|
|
if (end) {
|
2018-10-26 22:08:48 +02:00
|
|
|
nodeTree.push(new TreeNode(obj, partName, parent));
|
2018-10-25 18:09:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const newPartName = parts[partIndex] + delimiter + parts[partIndex + 1];
|
2018-10-26 22:08:48 +02:00
|
|
|
ServiceUtils.nestedTraverse(nodeTree, 0, [newPartName, ...parts.slice(partIndex + 2)],
|
|
|
|
obj, parent, delimiter);
|
2018-10-25 18:09:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-26 16:48:06 +02:00
|
|
|
|
2020-08-12 21:42:42 +02:00
|
|
|
static getTreeNodeObject(nodeTree: TreeNode<ITreeNodeObject>[], id: string): TreeNode<ITreeNodeObject> {
|
2018-10-26 16:48:06 +02:00
|
|
|
for (let i = 0; i < nodeTree.length; i++) {
|
|
|
|
if (nodeTree[i].node.id === id) {
|
|
|
|
return nodeTree[i];
|
|
|
|
} else if (nodeTree[i].children != null) {
|
|
|
|
const node = ServiceUtils.getTreeNodeObject(nodeTree[i].children, id);
|
|
|
|
if (node !== null) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2018-10-25 18:09:22 +02:00
|
|
|
}
|