SVG Port Tooltips
by Roman Bruckner
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.6.2/joint.css" />
</head>
<body>
<!-- content -->
<div id="paper"></div>
<button id="toggle">
tooltips on/off
</button>
<!-- dependencies -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.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.6.2/joint.js"></script>
</body>
</html>
CSS
button { position: absolute; top: 10px; left: 10px; }
JavaScript
const graph = new joint.dia.Graph({}, {
cellNamespace: joint.shapes
});
const paper = new joint.dia.Paper({
el: document.getElementById('paper'),
width: 800,
height: 900,
model: graph,
cellViewNamespace: joint.shapes,
gridSize: 10,
async: true,
sorting: joint.dia.Paper.sorting.APPROX,
});
const rect1 = new joint.shapes.standard.Rectangle({
ports: {
groups: {
'in': {
position: {
name: 'left'
}
},
'out': {
position: {
name: 'right'
}
}
}
}
});
rect1.position(100, 50);
rect1.resize(100, 100);
rect1.addPorts([{
id: 'port1',
group: 'in'
}, {
id: 'port2',
group: 'out',
name: 'Port Number 1'
}, {
id: 'port3',
group: 'out',
name: 'Port Number 2'
}]);
const rect2 = rect1.clone();
rect2.position(300, 200);
rect2.removePorts().addPorts([{
id: 'port1',
group: 'in'
}, {
id: 'port2',
group: 'out',
name: 'Another Port 1'
}, {
id: 'port3',
group: 'out',
name: 'Another Port 2'
}]);
graph.addCells([rect1, rect2]);
paper.on('element:mouseover', function(elementView, evt) {
if (allTooltipsShown) return;
if (elementView.findAttribute('port-group', evt.target) === 'out') {
showOutputPortsInfo(elementView.model);
}
});
paper.on('element:mouseout', function(elementView, evt) {
if (allTooltipsShown) return;
if (elementView.findAttribute('port-group', evt.target) === 'out') {
hideAllOutputPortsInfo(graph);
}
});
function showOutputPortsInfo(element) {
const positions = element.getPortsPositions('out');
Object.keys(positions).forEach((portId) => {
const elementPosition = element.position();
const portPosition = positions[portId];
const {
name
} = element.getPort(portId);
const tooltip = new joint.shapes.standard.Path({
size: {
width: 100,
height: 30
},
position: {
x: elementPosition.x + portPosition.x + 30,
y: elementPosition.y + portPosition.y -...