jerosoler / Drawflow

Simple flow library 🖥️🖱️
https://jerosoler.github.io/Drawflow/
MIT License
4.42k stars 714 forks source link

Confirm to remove connection #779

Open wagnerdracha opened 8 months ago

wagnerdracha commented 8 months ago

Hello

First of all, congratilations! This project is amazing!

So, I copied a suggestion to confirm before delete a node.

class Drawflowoverride extends Drawflow {
removeNodeId(id) {
    let $this = this;
  confirmPanel({
    'title'   :'Hello!',
    'msg'     :'Remove?',
    'callback': function(a, b, c) {
      if(b == "yes") {
          let removeNode = $this.dispatch('nodeBeforeRemoved', id.slice(5));

          console.log(removeNode);

          if(removeNode === true) {

             let moduleName = $this.getModuleFromNodeId(id.slice(5))
             if($this.module === moduleName) {
               document.getElementById(id).remove();
             }
             delete $this.drawflow.drawflow[moduleName].data[id.slice(5)];
             $this.dispatch('nodeRemoved', id.slice(5));
             $this.removeConnectionNodeId(id);
           }
      }
    }
  });
 }

 dispatch (event, details) {
   // Check if this event not exists
   let result = false;
   if (this.events[event] === undefined) {
       // console.error(`This event: ${event} does not exist`);
       return false;
   }

   this.events[event].listeners.forEach((listener) => {
        result = listener(details);
        if(result === undefined) {
          result = true;
        }

   });
   return result;
 }
}

However, I need to confirm when I click to remove a connection too!

I am trying with next code, it fire correctly when I click on remove connection, but the connection parameter passed to removeConnection is undefined.

How can I do that?

class Drawflowoverride extends Drawflow {
 removeConnection(connection) {
  let $this = this;
  console.log(connection);
  console.log($this);
  confirmPanel({
    'title'   :'Hello!',
    'msg'     :'Remove Connection?',
    'callback': function(a, b, c) {
      if(b == "yes") {
        $this.removeConnectionNodeId();
      }
    }
  });
 }
....

Thanks in advanced!

wagnerdracha commented 8 months ago

I need same function when nodeCreated.

jerosoler commented 8 months ago

Hi!

removeConnection function, not required parameter.

The parameter is editor.connection_selected. View: https://github.com/jerosoler/Drawflow/blob/300c9efda3bf8820131b3e93ea50cd9b76c892d3/src/drawflow.js#L1730

For addNode you can create a normal function since it doesn't interfere with the UI.