From 198b374806bfc54f95bea30c13b5ef4f9fc8f9a0 Mon Sep 17 00:00:00 2001 From: Mariusz Jamro Date: Fri, 23 Oct 2020 13:39:03 +0200 Subject: [PATCH] Add function for getting parent based on path. --- README.md | 7 ++++--- src/sl-vue-tree.d.ts | 1 + src/sl-vue-tree.js | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c8a9dd6..19f0cde 100644 --- a/README.md +++ b/README.md @@ -140,9 +140,10 @@ interface ICursorPosition { | insert(position: ICursorPosition, nodeModel: ISlTreeNodeModel) | Insert nodes by the current cursor position. | | remove(paths: number[][]) | Remove nodes by paths. For example `.remove([[0,1], [0,2]])` | getFirstNode(): ISlTreeNode | Get the first node in the tree | -| getLastNode(): ISlTreeNode | Get the last node in the tree -| getNextNode(path: number[], filter?: (node: ISlTreeNode) => boolean): ISlTreeNode; | Get the next node. You can skip the next nodes by using `filter` -| getPrevNode(path: number[], filter?: (node: ISlTreeNode) => boolean): ISlTreeNode; | Get the previous node. You can skip the previous nodes by using `filter` +| getLastNode(): ISlTreeNode | Get the last node in the tree | +| getNextNode(path: number[], filter?: (node: ISlTreeNode) => boolean): ISlTreeNode; | Get the next node. You can skip the next nodes by using `filter` | +| getPrevNode(path: number[], filter?: (node: ISlTreeNode) => boolean): ISlTreeNode; | Get the previous node. You can skip the previous nodes by using `filter` | +| getParentNode(path: number[]): ISlTreeNode; | Get the parent node for given path | # Slots diff --git a/src/sl-vue-tree.d.ts b/src/sl-vue-tree.d.ts index c1c5ddc..3685d60 100644 --- a/src/sl-vue-tree.d.ts +++ b/src/sl-vue-tree.d.ts @@ -45,6 +45,7 @@ export default class SlVueTree extends Vue { getLastNode(): ISlTreeNode; getNextNode(path: number[], filter?: (node: ISlTreeNode) => boolean): ISlTreeNode; getPrevNode(path: number[], filter?: (node: ISlTreeNode) => boolean): ISlTreeNode; + getParentNode(path: number[]): ISlTreeNode; updateNode(path: number[], patch: Partial>): void; getSelected(): ISlTreeNode[]; traverse(cb: (node: ISlTreeNode, nodeModel: ISlTreeNodeModel, siblings: ISlTreeNodeModel[]) => boolean | void, nodeModels?: ISlTreeNodeModel[], parentPath?: number[]): ISlTreeNode[] | boolean; diff --git a/src/sl-vue-tree.js b/src/sl-vue-tree.js index 585871f..e091f05 100644 --- a/src/sl-vue-tree.js +++ b/src/sl-vue-tree.js @@ -703,6 +703,16 @@ export default { return !shouldStop ? nodes : false; }, + getParentNode(path) { + if (!path) return null; + + if(path.length <= 1) return null; + + var parentPath = path.slice(0, -1) + + return this.getNode(parentPath); + }, + traverseModels(cb, nodeModels) { let i = nodeModels.length; while (i--) {