How to restrict the node to dropped on another node in Vue Diagram?
This article explains how to restrict a node from being dropped onto another node in Vue Diagram. In the Vue Diagram, you can restrict a node from being dropped onto another node by using the positionChange and drop events. To enable the drag-and-drop functionality for nodes in the diagram, set the AllowDrop constraint for the nodes. When each node is rendered, the nodeDefaults callback method is triggered. In the nodeDefaults method, set the node constraints to AllowDrop. This ensures that the AllowDrop constraint is applied to each node.
Please refer to the following code example for how to set node constraints in the node defaults method.
// Sets the default values of a node
getNodeDefaults: (obj) => {
obj.width = 100;
obj.height = 100;
obj.constraints = NodeConstraints.Default | 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 a node, the positionChange event is triggered. At the start of the drag action, the positionChange event is triggered for the Start state. In this state, store the node’s offsetX and offsetY values. Please refer to the following code example for storing the node offset values.
positionChange: (args) => {
if (args.state === 'Start') {
offsetX = args.newValue.offsetX;
offsetY = args.newValue.offsetY;
}
},
Using the drop event, you can drag and drop one node onto another in the diagram. While dragging a node, when the mouse hovers over another node, a highlighter appears. After the node is dropped, the drop event is triggered. In this event, retrieve the dropped node from the element arguments and reset the node’s offset values to the old offsets that were stored in the positionChange event. Please refer to the following code example and sample.
drop: (args) =>
if (args.element instanceof Node) {
args.element.offsetX = offsetX;
args.element.offsetY = offsetY;
diagramInstance.dataBind();
}
},
Refer to the working sample for additional details and implementation: Sample
Conclusion
We hope you enjoyed learning about how to restrict the node to be dropped on another node in Vue Diagram.
You can refer to our Vue Diagram feature tour page to learn more about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our Vue 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!