2021-04-20 12:22:55 +00:00
|
|
|
import mxgraph from '@mxgraph/core';
|
|
|
|
|
2021-04-21 04:54:09 +00:00
|
|
|
import { globalTypes } from '../.storybook/preview';
|
2021-04-20 12:22:55 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Connections/HelloPort',
|
|
|
|
argTypes: {
|
2021-04-21 04:54:09 +00:00
|
|
|
...globalTypes,
|
2021-04-20 12:22:55 +00:00
|
|
|
rubberBand: {
|
|
|
|
type: 'boolean',
|
|
|
|
defaultValue: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const Template = ({ label, ...args }) => {
|
|
|
|
const {
|
|
|
|
mxGraph,
|
|
|
|
mxRubberband,
|
|
|
|
mxEdgeStyle,
|
|
|
|
mxPoint,
|
|
|
|
mxConstants,
|
2021-04-25 03:39:40 +00:00
|
|
|
mxDomHelpers,
|
|
|
|
mxClient
|
2021-04-20 12:22:55 +00:00
|
|
|
} = mxgraph;
|
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
mxClient.setImageBasePath('/images');
|
|
|
|
|
2021-04-20 12:22:55 +00:00
|
|
|
const div = document.createElement('div');
|
|
|
|
|
|
|
|
const container = document.createElement('div');
|
|
|
|
container.style.position = 'relative';
|
|
|
|
container.style.overflow = 'hidden';
|
|
|
|
container.style.width = `${args.width}px`;
|
|
|
|
container.style.height = `${args.height}px`;
|
|
|
|
container.style.background = 'url(/images/grid.gif)';
|
|
|
|
container.style.cursor = 'default';
|
|
|
|
div.appendChild(container);
|
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
|
|
|
const graph = new mxGraph(container);
|
|
|
|
graph.setConnectable(true);
|
|
|
|
graph.setTooltips(true);
|
|
|
|
|
|
|
|
// Sets the default edge style
|
|
|
|
const style = graph.getStylesheet().getDefaultEdgeStyle();
|
2021-05-02 06:04:34 +00:00
|
|
|
style.edge = mxEdgeStyle.ElbowConnector;
|
2021-04-20 12:22:55 +00:00
|
|
|
|
|
|
|
// Ports are not used as terminals for edges, they are
|
|
|
|
// only used to compute the graphical connection point
|
|
|
|
graph.isPort = function(cell) {
|
|
|
|
const geo = this.getCellGeometry(cell);
|
|
|
|
|
|
|
|
return geo != null ? geo.relative : false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Implements a tooltip that shows the actual
|
|
|
|
// source and target of an edge
|
|
|
|
graph.getTooltipForCell = function(cell) {
|
2021-04-25 03:39:40 +00:00
|
|
|
if (cell.isEdge()) {
|
2021-04-20 12:22:55 +00:00
|
|
|
return `${this.convertValueToString(
|
2021-04-25 03:39:40 +00:00
|
|
|
cell.getTerminal(true)
|
2021-04-20 12:22:55 +00:00
|
|
|
)} => ${this.convertValueToString(
|
2021-04-25 03:39:40 +00:00
|
|
|
cell.getTerminal(false)
|
2021-04-20 12:22:55 +00:00
|
|
|
)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mxGraph.prototype.getTooltipForCell.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Removes the folding icon and disables any folding
|
|
|
|
graph.isCellFoldable = function(cell) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Enables rubberband selection
|
|
|
|
if (args.rubberBand)
|
|
|
|
new mxRubberband(graph);
|
|
|
|
|
|
|
|
// Gets the default parent for inserting new cells. This
|
|
|
|
// is normally the first child of the root (ie. layer 0).
|
|
|
|
const parent = graph.getDefaultParent();
|
|
|
|
|
|
|
|
// Adds cells to the model in a single step
|
|
|
|
graph.getModel().beginUpdate();
|
|
|
|
try {
|
|
|
|
const v1 = graph.insertVertex(parent, null, 'Hello', 20, 80, 80, 30);
|
|
|
|
v1.setConnectable(false);
|
|
|
|
const v11 = graph.insertVertex(v1, null, '', 1, 1, 10, 10);
|
|
|
|
v11.geometry.offset = new mxPoint(-5, -5);
|
|
|
|
v11.geometry.relative = true;
|
|
|
|
const v12 = graph.insertVertex(v1, null, '', 1, 0, 10, 10);
|
|
|
|
v12.geometry.offset = new mxPoint(-5, -5);
|
|
|
|
v12.geometry.relative = true;
|
|
|
|
const v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
|
|
|
|
const v3 = graph.insertVertex(parent, null, 'World2', 200, 20, 80, 30);
|
|
|
|
var e1 = graph.insertEdge(parent, null, '', v11, v2);
|
|
|
|
var e1 = graph.insertEdge(parent, null, '', v12, v3);
|
|
|
|
} finally {
|
|
|
|
// Updates the display
|
|
|
|
graph.getModel().endUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
const controller = document.createElement('div');
|
|
|
|
div.appendChild(controller);
|
|
|
|
|
|
|
|
const button = mxDomHelpers.button('View XML', function() {
|
|
|
|
const encoder = new mxCodec();
|
|
|
|
const node = encoder.encode(graph.getModel());
|
2021-06-06 13:04:44 +00:00
|
|
|
utils.popup(utils.getPrettyXml(node), true);
|
2021-04-20 12:22:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
controller.appendChild(button);
|
|
|
|
|
|
|
|
return div;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Default = Template.bind({});
|