1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-27 12:36:14 +01:00

store parent node

This commit is contained in:
Kyle Spearrin 2018-10-26 16:08:48 -04:00
parent d4b3a16fd1
commit aa0b274f8f
4 changed files with 14 additions and 8 deletions

View File

@ -5,7 +5,7 @@ import {
export class ServiceUtils {
static nestedTraverse(nodeTree: Array<TreeNode<ITreeNodeObject>>, partIndex: number, parts: string[],
obj: ITreeNodeObject, delimiter: string) {
obj: ITreeNodeObject, parent: ITreeNodeObject, delimiter: string) {
if (parts.length <= partIndex) {
return;
}
@ -17,21 +17,23 @@ export class ServiceUtils {
if (nodeTree[i].node.name === parts[partIndex]) {
if (end && nodeTree[i].node.id !== obj.id) {
// Another node with the same name.
nodeTree.push(new TreeNode(obj, partName));
nodeTree.push(new TreeNode(obj, partName, parent));
return;
}
ServiceUtils.nestedTraverse(nodeTree[i].children, partIndex + 1, parts, obj, delimiter);
ServiceUtils.nestedTraverse(nodeTree[i].children, partIndex + 1, parts,
obj, nodeTree[i].node, delimiter);
return;
}
}
if (nodeTree.filter((n) => n.node.name === partName).length === 0) {
if (end) {
nodeTree.push(new TreeNode(obj, partName));
nodeTree.push(new TreeNode(obj, partName, parent));
return;
}
const newPartName = parts[partIndex] + delimiter + parts[partIndex + 1];
ServiceUtils.nestedTraverse(nodeTree, 0, [newPartName, ...parts.slice(partIndex + 2)], obj, delimiter);
ServiceUtils.nestedTraverse(nodeTree, 0, [newPartName, ...parts.slice(partIndex + 2)],
obj, parent, delimiter);
}
}

View File

@ -1,8 +1,10 @@
export class TreeNode<T extends ITreeNodeObject> {
parent: T;
node: T;
children: Array<TreeNode<T>> = [];
constructor(node: T, name: string) {
constructor(node: T, name: string, parent: T) {
this.parent = parent;
this.node = node;
this.node.name = name;
}

View File

@ -106,7 +106,8 @@ export class CollectionService implements CollectionServiceAbstraction {
collections.forEach((f) => {
const collectionCopy = new CollectionView();
collectionCopy.id = f.id;
ServiceUtils.nestedTraverse(nodes, 0, f.name.split(NestingDelimiter), collectionCopy, NestingDelimiter);
ServiceUtils.nestedTraverse(nodes, 0, f.name.split(NestingDelimiter), collectionCopy,
null, NestingDelimiter);
});
return nodes;
}

View File

@ -105,7 +105,8 @@ export class FolderService implements FolderServiceAbstraction {
const folderCopy = new FolderView();
folderCopy.id = f.id;
folderCopy.revisionDate = f.revisionDate;
ServiceUtils.nestedTraverse(nodes, 0, f.name.split(NestingDelimiter), folderCopy, NestingDelimiter);
ServiceUtils.nestedTraverse(nodes, 0, f.name.split(NestingDelimiter), folderCopy,
null, NestingDelimiter);
});
return nodes;
}