Articles in this section

How to restrict the node to dropped on another node in Angular Diagram?

This article explains how to restrict a node from being dropped onto another node in an Angular diagram. In the Angular 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 operation of nodes in the diagram, enable the `AllowDrop` constraints on the nodes. When each node is rendered, the node default callback method gets triggered. In the node defaults method, set the node constraints to `AllowDrop`. Thus, for every node, the constraint is set to `AllowDrop`.

Refer to the following code example to understand how to set node constraints in the node defaults method:

 

public nodeDefaults(obj: NodeModel): NodeModel {
   //style property of node is used to change appearance of the node
    obj.style = { fill: "#37909A", strokeColor: "#024249" };
   //set node constraints as AllowDrop
    obj.constraints = NodeConstraints.Default | NodeConstraints.AllowDrop;
    obj.annotations[0].margin = { left: 10, right: 10 };
  //style property of annotations used to change appearance of the annotation
    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 offsetX and offsetY values.


Refer to the following code example for how to store the node offset values:

 

public positionChange(args: IDraggingEventArgs) {
    if (args.state === "Start") {
      this.offsetX = args.newValue.offsetX;
      this.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 the mouse hovers over another node, a 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.


Refer to the following code example and sample:

 

public drop(args: IDropEventArgs) {
//Argument element is used to get the dropped node.
    if (args.element instanceof Node) {
 //Reset the node offset values with old values
      args.element.offsetX = this.offsetX;
      args.element.offsetY = this.offsetY;
      this.diagram.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 dropped on another node in Angular Diagram.

You can refer to our Angular Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our Angular 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 explore 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!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied