Search code examples
vis.jsvis.js-network

Vis.js: Stay in addEdgeMode / Event after edge adding


I would like to stay in addEdgeMode in vis.js after adding an edge. Is there a way to achieve this?

My idea was to enable addEdgeMode again after adding an edge. Is there an event that is triggered after adding an edge? I know that there is the addEdge option in manipulation. However, this is triggered before the insertion.


Solution

  • you can enable the addEdge again right after the previous adding, like this:

    manipulation: {
              enabled: false,
              addNode: function (data, callback) {
                  // filling in the popup DOM elements
                  console.log('add', data);
              },
              editNode: function (data, callback) {
                  // filling in the popup DOM elements
                  console.log('edit', data);
              },
              addEdge: function (data, callback) {
                  console.log('add edge', data);
                  if (data.from == data.to) {
                      var r = confirm("Do you want to connect the node to itself?");
                      if (r === true) {
                          callback(data);
                      }
                  }
                  else {
                      callback(data);
                  }
                  // after each adding you will be back to addEdge mode
                  network.addEdgeMode();
              }
    

    see the last row in this code example.

    network.addEdgeMode();
    

    this will enable the addEdge mode right after the callback fired.

    see this example in plunker