This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
forked from maxGraph/maxGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerimeter.stories.js
82 lines (67 loc) · 2.56 KB
/
Perimeter.stories.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Graph, RubberBandHandler, GraphView, mathUtils } from '@maxgraph/core';
import { globalTypes } from '../.storybook/preview';
export default {
title: 'Labels/Perimeter',
argTypes: {
...globalTypes,
rubberBand: {
type: 'boolean',
defaultValue: true,
},
},
};
const Template = ({ label, ...args }) => {
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';
// Redirects the perimeter to the label bounds if intersection
// between edge and label is found
const mxGraphViewGetPerimeterPoint = GraphView.prototype.getPerimeterPoint;
GraphView.prototype.getPerimeterPoint = function (terminal, next, orthogonal, border) {
let point = mxGraphViewGetPerimeterPoint.apply(this, arguments);
if (point != null) {
const perimeter = this.getPerimeterFunction(terminal);
if (terminal.text != null && terminal.text.boundingBox != null) {
// Adds a small border to the label bounds
const b = terminal.text.boundingBox.clone();
b.grow(3);
if (mathUtils.rectangleIntersectsSegment(b, point, next)) {
point = perimeter(b, terminal, next, orthogonal);
}
}
}
return point;
};
// Creates the graph inside the given container
const graph = new Graph(container);
graph.setVertexLabelsMovable(true);
graph.setConnectable(true);
// Uncomment the following if you want the container
// to fit the size of the graph
// graph.setResizeContainer(true);
// Enables rubberband selection
if (args.rubberBand) new RubberBandHandler(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.batchUpdate(() => {
const v1 = graph.insertVertex(parent, null, 'Label', 20, 20, 80, 30, {
verticalLabelPosition: 'bottom',
});
const v2 = graph.insertVertex(parent, null, 'Label', 200, 20, 80, 30, {
verticalLabelPosition: 'bottom',
});
const v3 = graph.insertVertex(parent, null, 'Label', 20, 150, 80, 30, {
verticalLabelPosition: 'bottom',
});
var e1 = graph.insertEdge(parent, null, '', v1, v2);
var e1 = graph.insertEdge(parent, null, '', v1, v3);
});
return container;
};
export const Default = Template.bind({});