UNPKG

@egeonu/tree

Version:

Tree package for building Tree UI compoenent. It includes a fully customizable react component, stand-alone object builder and a custome HTML element. ## Getting Started `npm i @egeonu/tree` ## Usage A few examples of useful commands and/or tasks. #

54 lines 1.87 kB
export class TreeBuilder { constructor(props) { this.data = []; const self = this; self.config = Object.assign({}, props); if (typeof this.config.data !== "undefined") { self.data = TreeBuilder.createTree(this.config.data); return ((self) => ({ config: self.config, data: self.data, createTree: TreeBuilder.createTree.bind(self) }))(self); } return ((self) => ({ config: self.config, data: null, createTree: TreeBuilder.createTree.bind(self) }))(self); } static createTree(inputs) { const output = []; for (let i = 0; i < inputs.length; i++) { if (typeof inputs[i].parents === "undefined") { const newNode = { id: inputs[i].id, children: TreeBuilder.createChildren(inputs, inputs[i].id), data: inputs[i].data }; output.push(newNode); } } return output; } static createChildren(inputs, parentId) { const output = []; for (let i = 0; i < inputs.length; i++) { if (Array.isArray(inputs[i].parents)) { for (const parent of inputs[i].parents) { if (parent === parentId) { const myId = parentId + '.' + i; const newNode = { id: inputs[i].id, children: this.createChildren(inputs, inputs[i].id), data: inputs[i].data }; output.push(newNode); } } } } return output; } } //# sourceMappingURL=TreeBuilder.js.map