From aa0b274f8fd80db620abd4a85c3086f511e19c88 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 26 Oct 2018 16:08:48 -0400 Subject: [PATCH] store parent node --- src/misc/serviceUtils.ts | 12 +++++++----- src/models/domain/treeNode.ts | 4 +++- src/services/collection.service.ts | 3 ++- src/services/folder.service.ts | 3 ++- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/misc/serviceUtils.ts b/src/misc/serviceUtils.ts index d12f7b61f9..a466c83fbd 100644 --- a/src/misc/serviceUtils.ts +++ b/src/misc/serviceUtils.ts @@ -5,7 +5,7 @@ import { export class ServiceUtils { static nestedTraverse(nodeTree: Array>, 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); } } diff --git a/src/models/domain/treeNode.ts b/src/models/domain/treeNode.ts index ee020c2980..beda19f158 100644 --- a/src/models/domain/treeNode.ts +++ b/src/models/domain/treeNode.ts @@ -1,8 +1,10 @@ export class TreeNode { + parent: T; node: T; children: Array> = []; - constructor(node: T, name: string) { + constructor(node: T, name: string, parent: T) { + this.parent = parent; this.node = node; this.node.name = name; } diff --git a/src/services/collection.service.ts b/src/services/collection.service.ts index 358e5408f1..c33818a3d9 100644 --- a/src/services/collection.service.ts +++ b/src/services/collection.service.ts @@ -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; } diff --git a/src/services/folder.service.ts b/src/services/folder.service.ts index dc68a55d20..70fc7f1e71 100644 --- a/src/services/folder.service.ts +++ b/src/services/folder.service.ts @@ -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; }