Articles in this section
Category / Section

Add annotation to the diagram objects while dragging it from palette onto the diagram

3 mins read

 

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();

  }

 

Please find the sample here for your reference.

Sample:

https://stackblitz.com/edit/angular-c6dmfq-bbz5sh?file=app.component.ts

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