Custom Router

Does not really take `vertices` into account .

by Roman Bruckner

HTML

<html>
<head>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.4.4/joint.css" />
</head>
<body>
      <!-- content -->
      <div id="paper"></div>

      <!-- dependencies -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.4.0/backbone.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.4.4/joint.js"></script>
    
    </body>
</html>

JavaScript

var graph = new joint.dia.Graph({}, {
  cellNamespace: joint.shapes
});

const paper = new joint.dia.Paper({
  el: document.getElementById('paper'),
  width: 800,
  height: 900,
  model: graph,
  defaultConnectionPoint: {
    name: 'boundary'
  },
  cellViewNamespace: joint.shapes,
  gridSize: 10,
  async: true,
  sorting: joint.dia.Paper.sorting.APPROX,

/*   defaultRouter: {
    name: 'manhattan',
    args: {
      padding: 20,
      endDirections: ['left'],
      startDirections: ['right']
    }
  },
   */
defaultRouter: function(vertices, opt, linkView) {
    const sourceBBox = linkView.sourceBBox;
    const a = vertices.slice(-1)[0] || linkView.getEndAnchor('source');
    const b = linkView.getEndAnchor('target');
    const minGap = 40;
    const x1 = Math.min(b.x - minGap, (a.x + b.x) / 2);
    const p1 = {
      x: x1,
      y: a.y
    };
    if (sourceBBox.containsPoint(p1)) {
      const y1 = (a.y > b.y) ?  Math.max(sourceBBox.y - minGap, b.y) : Math.min(sourceBBox.y + sourceBBox.height + minGap, b.y);
      const x2 = Math.max(a.x, b.x - minGap);
      const p1 = {
        x: a.x,
        y: y1
      }
      const p2 = {
        x: x2,
        y: y1
      };
      const p3 = {
        x: x2,
        y: b.y
      };
      return [...vertices, p1, p2,p3];
    }
    const p2 = {
      x: x1,
      y: b.y
    };
    return [...vertices, p1, p2];
    // TODO: make the vertices orthogonal
  }
});

// Example

var el1 = new joint.shapes.standard.Circle({
  position: {
    x: 10,
    y: 170
  },
  size: {
    width: 50,
    height: 50
  },
  attrs: {
    label: {
      text: 'A'
    }
  }
});
var el2 = new joint.shapes.standard.Rectangle({
  position: {
    x: 400,
    y: 150
  },
  size: {
    width: 100,
    height: 90
  },
  attrs: {
    label: {
      text: 'B'
    }
  },
  ports: {
    items: [{
      id: 'p1'
    }, {
      id: 'p2'
    }, {
      id: 'p3'
    }]
  }
});

const l1 = new joint.shapes.standard.Link({
  source: {
    id: el1.id
  },
 ...