GitHub Discussion #2081

https://github.com/clientIO/joint/discussions/2081

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,
    linkPinning: false,
    defaultLink: (elementView, magnet) => {
        const portId = elementView.findAttribute('port', magnet);
        if (elementView.model.portProp(portId, 'targetMagnet')) {
            return new joint.shapes.standard.Link({
                temporary: true,
                attrs: {
                    line: {
                        targetMarker: null,
                        sourceMarker: {
                            'type': 'path',
                            'd': 'M 10 -5 0 0 10 5 z'
                        },
                        stroke: 'red'
                    }
                }
            });
        } else {
            return new joint.shapes.standard.Link({
                attrs: {
                    line: {
                        stroke: 'blue'
                    }
                }
            });
        }

    }
});

paper.on('link:connect', (linkView) => {
    const tmpLink = linkView.model;
    if (!tmpLink.get('temporary')) return;
    const link = new joint.shapes.standard.Link({
        source: tmpLink.target(),
        target: tmpLink.source(),
        attrs: {
            line: {
                stroke: 'blue'
            }
        }
    });
    tmpLink.remove();
    graph.addCell(link);
});


// Example

var el1 = new joint.shapes.standard.Rectangle({
    position: {
        x: 10,
        y: 170
    },
    size: {
        width: 100,
        height: 90
    },
    attrs: {
        label: {
            text: 'A'
        }
    },
    ports: {
        groups: {
            right: {
                position: 'right'
            }
   ...