Custom Router In Grid
by Roman Bruckner
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.5.5/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.5.5/joint.js"></script>
</body>
</html>
JavaScript
const columnCount = 5;
const count = 30;
const elWidth = 80;
const elHeight = 100;
const gapX = 100;
const gapY = 50;
var graph = new joint.dia.Graph({}, {
cellNamespace: joint.shapes
});
const paper = new joint.dia.Paper({
el: document.getElementById('paper'),
width: 1000,
height: 600,
model: graph,
cellViewNamespace: joint.shapes,
defaultConnectionPoint: {
name: 'boundary',
args: {
extrapolate: true,
sticky: true,
},
},
snapLinks: true,
linkPinning: false,
interactive: { elementMove: false },
defaultLink: () => new joint.shapes.standard.Link(),
validateConnection: function(sv, sm, tv, tm) {
if (sv.model.get('column') >= tv.model.get('column')) return false;
if (tv.findAttribute('port-group', tm) === 'out') return false;
return true;
},
defaultRouter: (_, opt, linkView) => {
const sourceView = linkView.getEndView('source');
const targetView = linkView.getEndView('target');
if (!sourceView || !targetView) return [];
const source = sourceView.model;
const target = targetView.model;
const sourceColumn = source.get('column');
const targetColumn = target.get('column');
const sourceRow = source.get('row');
const targetRow = target.get('row');
const sourceAnchor = linkView.getEndAnchor('source');
const targetAnchor = linkView.getEndAnchor('target');
const neighbors = sourceColumn !== targetColumn - 1;
const fx = neighbors ? (sourceRow > targetRow ? -1 : 1) : 0;
const fx2 = (sourceRow === targetRow ? -1 : 1)
const fy = (sourceColumn > targetColumn ? -1 : 1);
// a formula to calculate the number of points to be added to the path
// to avoid overlapping of the links
const shift = (sourceAnchor.y - source.position().y) / elHeight * 20;
const p1 = {
x: sourceColumn * (gapX +...