How to add annotation to the diagram objects while dragging it from palette onto the diagram in Javascript?
You can dynamically add an annotation to a node when dragging a node from the palette and dropping it onto the JavaScript 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.
let diagram = new Diagram({
width: '100%', height: '700px',
drop:drop
});
diagram.appendTo('#diagram');
function drop(arg: any) {
let obj = arg.element;
if (obj instanceof Node || obj instanceof Connector){
//get an selecteditem id
objectFromPalette = obj.id;
}
let dialogObj2= (document.getElementById('defaultDialog') as any).ej2_instances[0];
dialogObj2.show();
return arg;
}
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.
//initialise buttons
let buttonContent1: string = '<button id="submitButton" class="e-control e-btn e-primary" data-ripple="true">' + 'Submit</button>';
let buttonContent2: string = '<button id="cancelButton" class="e-control e-btn e-primary" data-ripple="true">' + 'Cancel</button>';
let submitButton: Button = new Button();
submitButton.appendTo('#submitButton');
let cancelbutton: Button = new Button();
cancelbutton.appendTo('#cancelButton');
let dialogObj: Dialog = new Dialog({
header: '',
target: document.getElementById('control-section'),
animationSettings: { effect: 'None' },
showCloseIcon: true,
width: '500px',
visible:false,
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" id="annotationContent" name="Required" class="e-input" /></span></td></tr></table>',
footerTemplate:buttonContent1+buttonContent2
});
// method to append a annotation to the node
document.getElementById('submitButton').onclick = (): void => {
if (objectFromPalette !== '') {
let obj = diagram.getObject(objectFromPalette);
let content = (document.getElementById('annotationContent') as HTMLInputElement).value;
if ((obj instanceof Node || obj instanceof Connector)) {
if (content == '') {
diagram.remove(obj);
} else{
obj.annotations[0].content = content;
diagram.dataBind();
}
}
}
let dialogObj= (document.getElementById('defaultDialog') as any).ej2_instances[0];
dialogObj.hide();
};
Please find the sample here for your reference.
Sample:
https://stackblitz.com/edit/clvumz-msl5bk?file=index.ts,index.html%3AL12
Conclusion
I hope you enjoyed learning about how to add annotation to the diagram objects while dragging it from palette onto the diagram in Javascript.
You can refer to our JavaScript Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for 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, Direct-Trac, or feedback portal. We are always happy to assist you!