From 79046811b1e7c9f54d6be6d01c70756b7bcc976b Mon Sep 17 00:00:00 2001 From: Stephen Lujan Date: Mon, 27 Jun 2016 10:40:38 -0400 Subject: [PATCH] added findNode method to TreeModel / ITreeModel --- lib/defs/api.ts | 6 ++++++ lib/models/tree.model.ts | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/defs/api.ts b/lib/defs/api.ts index 2caab71c..f5903dc5 100644 --- a/lib/defs/api.ts +++ b/lib/defs/api.ts @@ -165,4 +165,10 @@ export interface ITreeModel { * Focuses on the parent of the current focused node (same as left arrow on a collapsed node) */ focusDrillUp(); + + /** + * @returns The node with a matching id + * @param id must match the id property of the target ITreeNode + */ + findNode(id:any): ITreeNode; } diff --git a/lib/models/tree.model.ts b/lib/models/tree.model.ts index 4dee1db5..2df38166 100644 --- a/lib/models/tree.model.ts +++ b/lib/models/tree.model.ts @@ -8,6 +8,7 @@ import * as _ from 'lodash'; @Injectable() export class TreeModel implements ITreeModel { + roots: TreeNode[]; options: TreeOptions = new TreeOptions(); activeNode: TreeNode = null; @@ -123,4 +124,28 @@ export class TreeModel implements ITreeModel { nextNode && nextNode.focus(); } } + + /** recursively search a TreeNode[] then each TreeNode's children array */ + private static _find(nodes:TreeNode[], id:any) { + let found = nodes.find((node) => { + return node.id === id; + }); + if (found){ + return found; + } else { + for (let node of nodes) { + if (node.hasChildren) { + found = TreeModel._find(node.children, id); + if (found){ + return found; + } + } + } + } + return null; + } + + findNode(id:any) { + return TreeModel._find(this.roots, id); + } }