From b6e80b5d1ff1e42e7a9c89ed66c3985e683a1a9e Mon Sep 17 00:00:00 2001 From: Tarek Eldeeb Date: Wed, 1 Nov 2023 11:44:31 +0000 Subject: [PATCH] Moved unflatten to services --- .../basic-popover/basic-popover.component.ts | 51 +++++-------------- .../app/demo-charts/demo-charts.service.ts | 27 ++++++++++ 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/ngx-treant-js/src/app/demo-charts/basic-popover/basic-popover.component.ts b/ngx-treant-js/src/app/demo-charts/basic-popover/basic-popover.component.ts index 659c219..ec5b6bf 100644 --- a/ngx-treant-js/src/app/demo-charts/basic-popover/basic-popover.component.ts +++ b/ngx-treant-js/src/app/demo-charts/basic-popover/basic-popover.component.ts @@ -81,28 +81,30 @@ export class BasicPopoverComponent implements AfterViewInit, OnInit { } onSubmit() { + + //(window as any).tree.addNode({'id':this.node.id}, {'text':{'name':"TESTT"}}); const node = this.nodes.find((n) => n.id == this.node.id); const hasChildren = !!node.children && !!node.children.length; - - const newEmployee = { + let nodeChildren = (hasChildren)? node.children:[]; + const newStudent = { text: { name: this.registerForm.value.name || '', - title: this.registerForm.value.title || '', - contact: this.registerForm.value.contact || '', + //title: this.registerForm.value.title || '', + //contact: this.registerForm.value.contact || '', }, id: 0, parentId: 0, - image: this.registerForm.value.image || '', - children: hasChildren ? node.children : [], + //image: this.registerForm.value.image || '', + //children: hasChildren ? node.children : [], }; - node.children = [newEmployee]; + node.children = nodeChildren.push(newStudent); this.modalRef.hide(); this.displayChart = false; this.treant.destroy(); - + this.basicPopoverData = [this.basicPopoverData[0]].concat(this.nodes); //HACK setTimeout(() => { this.displayChart = true; }); @@ -138,7 +140,7 @@ export class BasicPopoverComponent implements AfterViewInit, OnInit { }); const unflattenNodes = this.flatNodes[0]; - unflattenNodes.children = this.unflatten(this.flatNodes); + unflattenNodes.children = this.svc.unflatten(this.flatNodes); this.basicPopoverData.nodeStructure = unflattenNodes; }); @@ -147,7 +149,7 @@ export class BasicPopoverComponent implements AfterViewInit, OnInit { } onClick(obj): void { - console.log('onClick: ', obj); + console.log('onClick: ', obj.node); } onUpdate(obj): void { @@ -257,7 +259,7 @@ export class BasicPopoverComponent implements AfterViewInit, OnInit { }); const unflattenNodes = this.flatNodes[0]; - unflattenNodes.children = this.unflatten(this.flatNodes); + unflattenNodes.children = this.svc.unflatten(this.flatNodes); this.basicPopoverData.nodeStructure = unflattenNodes; }); @@ -265,31 +267,4 @@ export class BasicPopoverComponent implements AfterViewInit, OnInit { console.log('nodes: ', this.nodes); console.log('flatNodes: ', this.flatNodes); } - - private unflatten(arr) { - var tree = [], - mappedArr = {}, - arrElem, - mappedElem; - - for (var i = 0, len = arr.length; i < len; i++) { - arrElem = arr[i]; - mappedArr[arrElem.id] = arrElem; - mappedArr[arrElem.id]['children'] = []; - } - - for (var id in mappedArr) { - if (mappedArr.hasOwnProperty(id)) { - mappedElem = mappedArr[id]; - - if (mappedElem.parentId) { - mappedArr[mappedElem['parentId']] && - mappedArr[mappedElem['parentId']]['children'].push(mappedElem); - } else { - tree.push(mappedElem); - } - } - } - return tree; - } } diff --git a/ngx-treant-js/src/app/demo-charts/demo-charts.service.ts b/ngx-treant-js/src/app/demo-charts/demo-charts.service.ts index e657201..1c1b363 100644 --- a/ngx-treant-js/src/app/demo-charts/demo-charts.service.ts +++ b/ngx-treant-js/src/app/demo-charts/demo-charts.service.ts @@ -22,4 +22,31 @@ export class DemoAppService { return flattenedItems; }, []); } + + unflatten(arr): any { + var tree = [], + mappedArr = {}, + arrElem, + mappedElem; + + for (var i = 0, len = arr.length; i < len; i++) { + arrElem = arr[i]; + mappedArr[arrElem.id] = arrElem; + mappedArr[arrElem.id]['children'] = []; + } + + for (var id in mappedArr) { + if (mappedArr.hasOwnProperty(id)) { + mappedElem = mappedArr[id]; + + if (mappedElem.parentId) { + mappedArr[mappedElem['parentId']] && + mappedArr[mappedElem['parentId']]['children'].push(mappedElem); + } else { + tree.push(mappedElem); + } + } + } + return tree; + } }