Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Need remove all child inside container node (Read 1852 times)
Ankur shah
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Mar 16th, 2017
Need remove all child inside container node
Nov 20th, 2018 at 5:29am
Print Post  
Hi

I have checked this issue on below link but it is not working for me as required

https://mindfusion.eu/Forum/YaBB.pl?num=1366796886/2

I am not able to delete all container inside a container node
How can i achieve this?
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Need remove all child inside container node
Reply #1 - Nov 20th, 2018 at 7:22am
Print Post  
Hi,

Try with the following method:

Code (Javascript)
Select All
function removeItem(item)
{
    if (item.children)
    {
        for (var i = item.children.length - 1; i >= 0; i--)
            removeItem(item.children[i]);
    }
    item.getParent().removeItem(item);
} 



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


I Love MindFusion!

Posts: 63
Joined: Mar 16th, 2017
Re: Need remove all child inside container node
Reply #2 - Nov 20th, 2018 at 7:46am
Print Post  
It not working as it firing a delete event again and again.
Please have a look at my below code.
Code (Javascript)
Select All
this.diagram.addEventListener(MindFusion.Diagramming.Events.nodeDeleted, (eve, args) => {
            console.log('Child Removed');

            if (args.node.id.type == SystemElementTypes.Child) {
                this.rootBoundary.removeSystemElementBoundary(args.node.id.uniqueID);
            }
            if (args.node.id.type == FunctionTypes.Normal) {
                this.rootBoundary.removeFunctionBoundary(this.rootBoundary.root, args.node.id.uniqueID);
            }
            if (args.node.id.type == DefectTypes.Normal) {
                this.rootBoundary.removeDefectBoundary(this.rootBoundary.root, args.node.id.uniqueID);
            }
            this.removeItem(args.node);
        });
 

  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Need remove all child inside container node
Reply #3 - Nov 20th, 2018 at 9:04am
Print Post  
Hi,

The above method was not intended to be called from a nodeDeleted event handler, as the diagram.removeItem method raises the event too - so you enter an endless loop. To avoid that, you can for example add a boolean argument to the removeItem method to indicate whether the topmost item gets deleted:

Code (Javascript)
Select All
function removeItem(item, removeTopmost)
{
    if (item.children)
    {
        for (var i = item.children.length - 1; i >= 0; i--)
            removeItem(item.children[i], true);
    }
    if (removeTopmost)
        item.getParent().removeItem(item);
}

this.diagram.addEventListener(MindFusion.Diagramming.Events.nodeDeleted, (eve, args) => {
    console.log('Child Removed');
    // ...
    this.removeItem(args.node, false);
}); 



Or you can remove the recursion from the function and call diagram.removeItem only on the children, so that the nested containers are removed by the subsequent nodeDeleted handler invocations:

Code (Javascript)
Select All
function removeChildren(parent)
{
    if (parent.children)
    {
        for (var i = parent.children.length - 1; i >= 0; i--)
            parent.getParent().removeItem(parent.children[i]);
    }
}

this.diagram.addEventListener(MindFusion.Diagramming.Events.nodeDeleted, (eve, args) => {
    console.log('Child Removed');
    // ...
    this.removeChildren(args.node);
});
 



Regards,
Lyubo
« Last Edit: Nov 21st, 2018 at 6:24am by Lyubo »  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint