How to restrict the node to dropped on to another node in Javascript Diagram?
This article explains how to restrict a node from being dropped onto another node in a JavaScript Diagram. In the JavaScript Diagram, you can restrict a node from being dropped onto another node by using the positionChange and drop events. To perform the drag and drop of nodes in the diagram, enable the AllowDrop constraint for the nodes. When each node is rendered, the node default callback method gets triggered. In the node default method, set the node constraints to AllowDrop. Thus, for every node, the constraint is set to AllowDrop.
Please refer to the following code example for how to set node constraints in the NodeDefaults method.
//Sets the default values of a nodes
getNodeDefaults: function (obj) {
obj.width = 100;
obj.height = 100;
obj.constraints =
ej.diagrams.NodeConstraints.Default |
ej.diagrams.NodeConstraints.AllowDrop;
obj.shape = { shape: 'Ellipse' };
obj.style = { fill: '#37909A', strokeColor: '#024249' };
obj.annotations[0].margin = { left: 10, right: 10 };
obj.annotations[0].style = {
color: 'white',
fill: 'none',
strokeColor: 'none',
};
return obj;
}
When you drag the node, the positionChange event gets triggered. When you start to drag the node, the positionChange event gets triggered for the Start state. In that state, store the node’s offsetX and offsetY values. Please refer to the following code example for how to store the node’s offset values.
positionChange: function (args) {
if (args.state === 'Start') {
offsetX = args.newValue.offsetX;
offsetY = args.newValue.offsetY;
}
}
By using the drop event, you can drag and drop a certain node to a different node in the diagram. When we drag the node and hover the mouse over another node, the highlighter is shown. After the node is dropped, the drop event is triggered. In that event, get the dropped node from the element arguments and reset the node offset values to the old offset that you have stored in the position change event. Please refer to the following code example and sample.
drop: function (args) {
if (args.element instanceof ej.diagrams.Node) {
args.element.offsetX = offsetX;
args.element.offsetY = offsetY;
diagram.dataBind();
}
}
Refer to the working sample for additional details and implementation:Sample
Conclusion
We hope you enjoyed learning about how to restrict a node from being dropped onto another node in JavaScript Diagram.
You can refer to our JavaScript Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, as well as how to quickly get started with configuration specifications. You can also explore our JavaScript Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!