Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Get all attached node within container node. (Read 2281 times)
Ankur shah
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Mar 16th, 2017
Get all attached node within container node.
Oct 26th, 2018 at 12:17pm
Print Post  
I am loading diagram via toJson api provided my mindfusion. now I want read a diagram get all conatiner node all node inside it.

In my container node I can have another container node and in that container node i can have shape node.

So i need to prepare tree data structure by reading this diagram. How can we achieve this functionality?
« Last Edit: Oct 29th, 2018 at 7:00am by Ankur shah »  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Get all attached node within container node.
Reply #1 - Oct 29th, 2018 at 11:29am
Print Post  
Use recursion, e.g.

Code
Select All
function traverseContainerHierarchy(nodes, treeParent)
{
	for (var n = 0; n < nodes.length; n++)
	{
		var diagNode = nodes[n];
		var treeNode = createMyTreeNode(diagNode, treeParent);
		if (diagNode.children)
			traverseContainerHierarchy(diagNode.children, treeNode);
	}
}

traverseContainerHierarchy(diagram.nodes, treeRoot); 



where createMyTreeNode function would create your tree-node instance and add it to the tree.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
Ankur shah
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Mar 16th, 2017
Re: Get all attached node within container node.
Reply #2 - Oct 29th, 2018 at 12:01pm
Print Post  
Sorry I think I am not able explain my problem properly to you. Let me show you what did ? As you see the attached pic I am trying to save and load a diagram via below code it work fine.

Code (Javascript)
Select All
 save() {
        this.json = this.diagram.toJson();
        console.log(this.json);
        this.jsonObjectInstance = JSON.parse(this.json);

    }
 



Over here this.jsonObjectInstance is instance of the diagram object created by this.diagram.toJson() method. Now you can see the load method. I am populating the diagram via fromJson method. My actual problem begins here that what ever i got in Json object instance does not tell me who is the child and who parent as i cant find these information Json Object instance. Can you please let me know how can we achieve this kind functionality.

Code (Javascript)
Select All
 load() {
        let temp = this.diagram.getCustomNodeType();
        this.jsonObjectInstance.items.forEach(item => {
            console.log("tag : " + item.tag)
            //item.get
        })
        console.log(this.diagram.fromJson(JSON.stringify(this.jsonObjectInstance)));
    }
 


I will save this Json object in to our database and then reading this data creating a tree kind data structure and show it in a different format (i.e. I want to read the Json object from database and without populating it to Diagram or canvas object)
  

MINFUSION.PNG ( 32 KB | 106 Downloads )
MINFUSION.PNG
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Get all attached node within container node.
Reply #3 - Oct 29th, 2018 at 1:26pm
Print Post  
If you mean you need to parse diagrams' JSON yourself, note that all items have an instanceId field in their own JSON objects. So each node's parent container is identified via "container" field set to that ID:

Code
Select All
// in DiagramNode.toJson: function ()
if (this.container)
	json.container = this.container.instanceId;
 



So you will have to load your tree in two stages. First load items in a linear list and map their instanceId to node instance. Next loop over the list and create tree nodes, while setting parent-child relations as per the id-node map.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
Ankur shah
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Mar 16th, 2017
Re: Get all attached node within container node.
Reply #4 - Oct 30th, 2018 at 8:31am
Print Post  
As you  can see the below code I am able to iterate the diagram nodes and got instance id correctly but it gives all the container node id and does not matter if its a child or parent. As said by you i need to iterate node id map I am not able understand what node id map and how can we iterate?

Code (Javascript)
Select All
load() {
        let temp = this.diagram.getCustomNodeType();
        this.jsonObjectInstance.items.forEach(item => {
            console.log("Instance ID: " + item.instanceId)
            //item.get
        })
  console.log(this.diagram.fromJson(JSON.stringify(this.jsonObjectInstance)));
    } 

  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Get all attached node within container node.
Reply #5 - Oct 30th, 2018 at 10:44am
Print Post  
Create idMap array (or ES6 Map) and while iterating set idMap[item.instanceId] = item; Then in a second loop you can check if item.container is defined and if so get it from the already populated map:

Code
Select All
if (typeof item.container !== "undefined")
  containerItem = idMap[item.container]; 



Regards,
Slavcho
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint