From b0a2209185e729d9b10251dff53f0e1a2e002b10 Mon Sep 17 00:00:00 2001 From: Omar Desogus Date: Tue, 14 Aug 2018 14:42:58 +0200 Subject: [PATCH] [feat] add function to return the children of a node --- src/map/handlers/nodes.ts | 23 +++++++++++++++++++++++ src/map/map.ts | 2 ++ 2 files changed, 25 insertions(+) diff --git a/src/map/handlers/nodes.ts b/src/map/handlers/nodes.ts index 31200ef..45bd5f5 100644 --- a/src/map/handlers/nodes.ts +++ b/src/map/handlers/nodes.ts @@ -267,6 +267,29 @@ export default class Nodes { } }; + /** + * Return the children of the node. + * @param {string} id + * @returns {ExportNodeProperties[]} + */ + public nodeChildren = (id?: string): ExportNodeProperties[] => { + if (id && typeof id !== "string") { + Log.error("The node id must be a string", "type"); + } + + let node: Node = id ? this.getNode(id) : this.selectedNode; + + if (node === undefined) { + Log.error("There are no nodes with id \"" + id + "\""); + } + + return this.nodes.values().filter((n: Node) => { + return n.parent && n.parent.id === node.id; + }).map((n: Node) => { + return this.getNodeProperties(n); + }); + }; + /** * Return the export properties of the node. * @param {Node} node diff --git a/src/map/map.ts b/src/map/map.ts index 032b492..b219b48 100644 --- a/src/map/map.ts +++ b/src/map/map.ts @@ -101,6 +101,7 @@ export default class Map { addNode: this.nodes.addNode, selectNode: this.nodes.selectNode, deselectNode: this.nodes.deselectNode, + nodeChildren: this.nodes.nodeChildren, updateNode: this.nodes.updateNode, removeNode: this.nodes.removeNode, copyNode: this.copyPaste.copy, @@ -127,6 +128,7 @@ export interface MmpInstance { addNode: Function; selectNode: Function; deselectNode: Function; + nodeChildren: Function; updateNode: Function; removeNode: Function; copyNode: Function;