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:
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:
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