Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Cancel the containerChildAdding event (Read 3445 times)
Shafi
Full Member
***
Offline


I Love MindFusion!

Posts: 109
Joined: Nov 8th, 2016
Cancel the containerChildAdding event
Jul 5th, 2019 at 3:14pm
Print Post  
Can you please let me know how to stop the containerChildAdding event? I have tried args.cancelDrag() and args.setCancel(true) but it does not really cancel this event.

Also I have noticed that the containerChildAdding is called even if the node is moved inside the same parent container. How to stop this behavior?
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Cancel the containerChildAdding event
Reply #1 - Jul 8th, 2019 at 5:14am
Print Post  
Hi,

To cancel the operation from a containerChildAdding event handler, you need to use:
Code (Javascript)
Select All
args.setCancel(true); 



To distinguish from a containerChildAdding handler if a node is already a child of the particular container use:
Code (Javascript)
Select All
var node = args.getNode();
var container = args.getContainer();

var isAlreadyChild = container === node.getContainer(); 



To forbid all child adding operations for a container use:
Code (Javascript)
Select All
container.setAllowAddChildren(false); 



Regards,
Lyubo
  
Back to top
 
IP Logged
 
Shafi
Full Member
***
Offline


I Love MindFusion!

Posts: 109
Joined: Nov 8th, 2016
Re: Cancel the containerChildAdding event
Reply #2 - Jul 8th, 2019 at 3:03pm
Print Post  
Thanks for the suggestions. Could you also please explain whats the difference between cancelDrag and setCancel(true)?
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3253
Joined: Oct 19th, 2005
Re: Cancel the containerChildAdding event
Reply #3 - Jul 10th, 2019 at 7:03am
Print Post  
cancelDrag can be called from nodeCreating / nodeModifying event handlers to immediately stop the drag operation, instead of only changing the mouse cursor. It probably won't have any effect if called from containerChildAdding.
  
Back to top
 
IP Logged
 
Shafi
Full Member
***
Offline


I Love MindFusion!

Posts: 109
Joined: Nov 8th, 2016
Re: Cancel the containerChildAdding event
Reply #4 - Jul 11th, 2019 at 1:57pm
Print Post  
I have tried your suggestion for checking isAlreadyChild, but it does not exactly fit my scenario.

Please refer to the screenshot attached.
I want to cancel the adding of child2 to child1, but both the nodes are child of "Parent" node.

The containerChildAdding is called for "child1" as ContainerNode as well as "Parent" as ContainerNode . So the isAlreadyChild fails when the args.getContainer() is "Parent" and also the last event it checks for "Parent" node, but it gets added in the "Child1". I have attached the logs for this scenario.
  

ContainerNode.png ( 3 KB | 142 Downloads )
ContainerNode.png
ContainerNodeAsChild.png ( 6 KB | 157 Downloads )
ContainerNodeAsChild.png
ContainerChildAddingLog.png ( 26 KB | 150 Downloads )
ContainerChildAddingLog.png
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3253
Joined: Oct 19th, 2005
Re: Cancel the containerChildAdding event
Reply #5 - Jul 16th, 2019 at 8:02am
Print Post  
Get this new build -
https://mindfusion.eu/_beta/jsdiag334.zip

and you should be able to handle that like this -
Code
Select All
function onContainerChildAdding(sender, args)
{
	var ctr = args.getContainer();

	// prevent third level nested containers

	// do not allow dropping container into an already nested container
	if (args.getNode().children && ctr.getContainer())
	{
		args.setCancel(true);
		args.setHandled(true);
	}

	// do not allow dropping parent of nested containers into a container
	if (args.getNode().children)
	{
		for (var child of args.getNode().children)
		{
			if (child.children)
			{
				args.setCancel(true);
				args.setHandled(true);
				break;
			}
		}
	}
}
 



If you set args.Handled, it stops propagation of the event to parent containers and the dropped node resets to original position, otherwise it could still be accepted by another container in the hierarchy.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
Shafi
Full Member
***
Offline


I Love MindFusion!

Posts: 109
Joined: Nov 8th, 2016
Re: Cancel the containerChildAdding event
Reply #6 - Aug 5th, 2019 at 8:54am
Print Post  
Thanks for the solution. Could you please add it to the  d.ts file ?
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3253
Joined: Oct 19th, 2005
Re: Cancel the containerChildAdding event
Reply #7 - Aug 5th, 2019 at 12:56pm
Print Post  
Please find it attached.
  

jsdiag_d_003.zip ( 35 KB | 170 Downloads )
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint