Add annotation to the diagram objects while dragging it from palette onto the diagram
This article explains how to add annotations to the diagram objects while dragging them from the palette onto the diagram. You can dynamically add an annotation to a node when dragging a node from the palette and dropping it onto the diagram using the drop event. In the following article, we will explain how to achieve this by incorporating a dialog control.
Adding annotation content to the node with a dialog box
In the provided sample, when a node is dragged from the palette and dropped onto the diagram, the drop event is triggered. In this event, a dialog box is opened.
Below is the corresponding code example for reference:
<!-- define drop event -->
<ejs-diagram #diagram id="diagram" width="100%" height="100%"
(drop)="drop($event)" >
</ejs-diagram>
//drop event
public drop(args: IDropEventArgs): void {
let obj = args.element;
if (obj instanceof Node || obj instanceof Connector){
//get an selecteditem id
this.objectFromPalette = obj.id;
}
this.promptDialog.show();
//open a dialog box
this.dialogOpen();
}In the dialog box, you can enter text, which will be appended to the node or connector upon pressing the submit button. Closing the dialog or pressing the cancel button will remove the selected diagram element.
Below is the corresponding code example:
App.component.html
<!-- create a dialog control with textbox and buttons -->
<diV id="ejsDiaglog">
<ejs-dialog #promptDialog [buttons]='promptDlgButtons' [visible]='hidden' [header]='promptHeader' [animationSettings]='animationSettings' [showCloseIcon]='showCloseIcon' [target]='target' (open)="dialogOpen()" (close)="dialogClose()" [width]='promptWidth' >
<!-- Prompt Dialog content -->
<ng-template #content>
<table style="border-collapse: separate;border-spacing: 10px;width:85%;margin: 0px -5px 0px;">
<tr>
<td>Annotation Content:</td>
<td><span class="e-input-group">
<input type="text" #password id="annotationContent" (focus)='onFocus()' (blur)='onBlur()' name="Required" class="e-input" />
</span></td>
</tr>
</table>
</ng-template>
</ejs-dialog>
</diV>
App.component.ts
// bind the values entered in the textbox to the node
public promptDlgButtons: ButtonPropsModel[] = [
{ click: this.objectPropertiesChange.bind(this), buttonModel: { content: 'Submit', isPrimary: true } },
{ click: this.closeDialogButton.bind(this), buttonModel: { content: 'Cancel' } }];
// method to append a annotation to the node
public objectPropertiesChange() {
if (this.objectFromPalette !== '') {
let obj = this.diagram.getObject(this.objectFromPalette);
let content = (document.getElementById('annotationContent') as HTMLInputElement).value;
if ((obj instanceof Node || obj instanceof Connector) && obj.annotations.length > 0) {
if (content == '') {
this.diagram.remove(obj);
} else{
obj.annotations[0].content = content;
this.diagram.dataBind();
}
}
}
this.promptDialog.hide();
}
//method to remove the object while closing the dialog
public closeDialogButton() {
if (this.objectFromPalette !== '') {
let obj = this.diagram.getObject(this.objectFromPalette);
//remove the diagram element
this.diagram.remove(obj);
}
this.promptDialog.hide();
}
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 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!