2022-08-30 15:36:33 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021-present The maxGraph project Contributors
|
|
|
|
Copyright (c) 2006-2020, JGraph Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-09-07 09:07:27 +00:00
|
|
|
import {
|
|
|
|
Graph,
|
2022-01-08 01:49:35 +00:00
|
|
|
RubberBandHandler,
|
2021-09-07 09:07:27 +00:00
|
|
|
ConnectionHandler,
|
|
|
|
ConnectionConstraint,
|
|
|
|
ConstraintHandler,
|
|
|
|
Point,
|
|
|
|
CellState,
|
|
|
|
EdgeHandler,
|
|
|
|
} from '@maxgraph/core';
|
2021-04-20 12:22:55 +00:00
|
|
|
|
2021-04-21 04:54:09 +00:00
|
|
|
import { globalTypes } from '../.storybook/preview';
|
2022-01-08 01:49:35 +00:00
|
|
|
import { intersects } from '@maxgraph/core/util/mathUtils';
|
2021-04-20 12:22:55 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Connections/FixedPoints',
|
|
|
|
argTypes: {
|
2021-04-21 04:54:09 +00:00
|
|
|
...globalTypes,
|
2021-04-20 12:22:55 +00:00
|
|
|
rubberBand: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: true,
|
|
|
|
},
|
|
|
|
},
|
2021-04-20 12:22:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class MyCustomConstraintHandler extends ConstraintHandler {
|
2021-04-20 12:22:55 +00:00
|
|
|
// Snaps to fixed points
|
|
|
|
intersects(icon, point, source, existingEdge) {
|
2021-08-30 14:20:26 +00:00
|
|
|
return !source || existingEdge || intersects(icon.bounds, point);
|
2021-04-20 12:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class MyCustomConnectionHandler extends ConnectionHandler {
|
|
|
|
// connectImage = new ImageBox('images/connector.gif', 16, 16);
|
2021-04-20 12:22:55 +00:00
|
|
|
|
|
|
|
isConnectableCell(cell) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Special case: Snaps source of new connections to fixed points
|
|
|
|
* Without a connect preview in connectionHandler.createEdgeState mouseMove
|
|
|
|
* and getSourcePerimeterPoint should be overriden by setting sourceConstraint
|
|
|
|
* sourceConstraint to null in mouseMove and updating it and returning the
|
|
|
|
* nearest point (cp) in getSourcePerimeterPoint (see below)
|
|
|
|
*/
|
|
|
|
updateEdgeState(pt, constraint) {
|
|
|
|
if (pt != null && this.previous != null) {
|
2021-08-30 14:20:26 +00:00
|
|
|
const constraints = this.graph.getAllConnectionConstraints(this.previous);
|
2021-04-20 12:22:55 +00:00
|
|
|
let nearestConstraint = null;
|
|
|
|
let dist = null;
|
|
|
|
|
|
|
|
for (let i = 0; i < constraints.length; i++) {
|
2021-08-30 14:20:26 +00:00
|
|
|
const cp = this.graph.getConnectionPoint(this.previous, constraints[i]);
|
2021-04-20 12:22:55 +00:00
|
|
|
|
|
|
|
if (cp != null) {
|
2021-08-30 14:20:26 +00:00
|
|
|
const tmp = (cp.x - pt.x) * (cp.x - pt.x) + (cp.y - pt.y) * (cp.y - pt.y);
|
2021-04-20 12:22:55 +00:00
|
|
|
|
|
|
|
if (dist == null || tmp < dist) {
|
|
|
|
nearestConstraint = constraints[i];
|
|
|
|
dist = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nearestConstraint != null) {
|
|
|
|
this.sourceConstraint = nearestConstraint;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case the edge style must be changed during the preview:
|
2021-05-02 13:59:43 +00:00
|
|
|
// this.edgeState.style.edgeStyle = 'orthogonalEdgeStyle';
|
2021-04-20 12:22:55 +00:00
|
|
|
// And to use the new edge style in the new edge inserted into the graph,
|
|
|
|
// update the cell style as follows:
|
2021-08-30 14:20:26 +00:00
|
|
|
// this.edgeState.cell.style = utils.setStyle(this.edgeState.cell.style, 'edgeStyle', this.edgeState.style.edgeStyle);
|
2021-04-20 12:22:55 +00:00
|
|
|
}
|
|
|
|
return super.updateEdgeState(pt, constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
createEdgeState(me) {
|
|
|
|
// Connect preview
|
2022-04-17 06:58:35 +00:00
|
|
|
const edge = this.graph.createEdge(null, null, null, null, null, {
|
|
|
|
edgeStyle: 'orthogonalEdgeStyle',
|
|
|
|
});
|
2021-04-20 12:22:55 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
return new CellState(this.graph.view, edge, this.graph.getCellStyle(edge));
|
2021-04-20 12:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class MyCustomEdgeHandler extends EdgeHandler {
|
2021-04-20 12:22:55 +00:00
|
|
|
// Disables floating connections (only use with no connect image)
|
|
|
|
isConnectableCell(cell) {
|
2022-01-08 01:49:35 +00:00
|
|
|
return graph.getPlugin('ConnectionHandler').isConnectableCell(cell);
|
2021-04-20 12:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class MyCustomGraph extends Graph {
|
2021-04-20 12:22:55 +00:00
|
|
|
createConnectionHandler() {
|
|
|
|
const r = new MyCustomConnectionHandler();
|
|
|
|
r.constraintHandler = new MyCustomConstraintHandler(this);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
createEdgeHandler(state, edgeStyle) {
|
|
|
|
const r = new MyCustomEdgeHandler(state, edgeStyle);
|
|
|
|
r.constraintHandler = new MyCustomConstraintHandler(this);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllConnectionConstraints(terminal) {
|
2021-05-02 06:04:34 +00:00
|
|
|
if (terminal != null && terminal.cell.isVertex()) {
|
2021-04-20 12:22:55 +00:00
|
|
|
return [
|
2021-08-30 14:20:26 +00:00
|
|
|
new ConnectionConstraint(new Point(0, 0), true),
|
|
|
|
new ConnectionConstraint(new Point(0.5, 0), true),
|
|
|
|
new ConnectionConstraint(new Point(1, 0), true),
|
|
|
|
new ConnectionConstraint(new Point(0, 0.5), true),
|
|
|
|
new ConnectionConstraint(new Point(1, 0.5), true),
|
|
|
|
new ConnectionConstraint(new Point(0, 1), true),
|
|
|
|
new ConnectionConstraint(new Point(0.5, 1), true),
|
|
|
|
new ConnectionConstraint(new Point(1, 1), true),
|
2021-04-20 12:22:55 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
|
|
|
const graph = new MyCustomGraph(container);
|
|
|
|
graph.setConnectable(true);
|
|
|
|
|
|
|
|
// Enables rubberband selection
|
2022-01-08 01:49:35 +00:00
|
|
|
if (args.rubberBand) new RubberBandHandler(graph);
|
2021-04-20 12:22:55 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
value: 'Hello,',
|
|
|
|
position: [20, 20],
|
|
|
|
size: [80, 60],
|
2022-04-17 06:58:35 +00:00
|
|
|
style: { shape: 'triangle', perimeter: 'trianglePerimeter' },
|
2021-04-20 12:22:55 +00:00
|
|
|
});
|
|
|
|
const v2 = graph.insertVertex({
|
|
|
|
parent,
|
|
|
|
value: 'World!',
|
|
|
|
position: [200, 150],
|
|
|
|
size: [80, 60],
|
2022-04-17 06:58:35 +00:00
|
|
|
style: { shape: 'ellipse', perimeter: 'ellipsePerimeter' },
|
2021-04-20 12:22:55 +00:00
|
|
|
});
|
|
|
|
const v3 = graph.insertVertex({
|
|
|
|
parent,
|
|
|
|
value: 'Hello,',
|
|
|
|
position: [200, 20],
|
|
|
|
size: [80, 30],
|
|
|
|
});
|
|
|
|
const e1 = graph.insertEdge({
|
|
|
|
parent,
|
|
|
|
value: '',
|
|
|
|
source: v1,
|
|
|
|
target: v2,
|
2022-04-17 06:58:35 +00:00
|
|
|
style: {
|
|
|
|
edgeStyle: 'elbowEdgeStyle',
|
|
|
|
elbow: 'horizontal',
|
|
|
|
exitX: 0.5,
|
|
|
|
exitY: 1,
|
|
|
|
exitPerimeter: 1,
|
|
|
|
entryX: 0,
|
|
|
|
entryY: 0,
|
|
|
|
entryPerimeter: 1,
|
|
|
|
},
|
2021-04-20 12:22:55 +00:00
|
|
|
});
|
|
|
|
const e2 = graph.insertEdge({
|
|
|
|
parent,
|
|
|
|
value: '',
|
|
|
|
source: v3,
|
|
|
|
target: v2,
|
2022-04-17 06:58:35 +00:00
|
|
|
style: {
|
|
|
|
edgeStyle: 'elbowEdgeStyle',
|
|
|
|
elbow: 'horizontal',
|
|
|
|
orthogonal: 0,
|
|
|
|
entryX: 0,
|
|
|
|
entryY: 0,
|
|
|
|
entryPerimeter: 1,
|
|
|
|
},
|
2021-04-20 12:22:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Use this code to snap the source point for new connections without a connect preview,
|
2022-01-08 01:49:35 +00:00
|
|
|
// ie. without an overridden graph.getPlugin('ConnectionHandler').createEdgeState
|
2021-04-20 12:22:55 +00:00
|
|
|
/*
|
2021-08-30 14:20:26 +00:00
|
|
|
let mxConnectionHandlerMouseMove = ConnectionHandler.prototype.mouseMove;
|
|
|
|
ConnectionHandler.prototype.mouseMove = function(sender, me)
|
2021-04-20 12:22:55 +00:00
|
|
|
{
|
|
|
|
this.sourceConstraint = null;
|
|
|
|
|
|
|
|
mxConnectionHandlerMouseMove.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
let mxConnectionHandlerGetSourcePerimeterPoint = ConnectionHandler.prototype.getSourcePerimeterPoint;
|
|
|
|
ConnectionHandler.prototype.getSourcePerimeterPoint = function(state, pt, me)
|
2021-04-20 12:22:55 +00:00
|
|
|
{
|
|
|
|
let result = null;
|
|
|
|
|
|
|
|
if (this.previous != null && pt != null)
|
|
|
|
{
|
|
|
|
let constraints = this.graph.getAllConnectionConstraints(this.previous);
|
|
|
|
let nearestConstraint = null;
|
|
|
|
let nearest = null;
|
|
|
|
let dist = null;
|
|
|
|
|
|
|
|
for (let i = 0; i < constraints.length; i++)
|
|
|
|
{
|
|
|
|
let cp = this.graph.getConnectionPoint(this.previous, constraints[i]);
|
|
|
|
|
|
|
|
if (cp != null)
|
|
|
|
{
|
|
|
|
let tmp = (cp.x - pt.x) * (cp.x - pt.x) + (cp.y - pt.y) * (cp.y - pt.y);
|
|
|
|
|
|
|
|
if (dist == null || tmp < dist)
|
|
|
|
{
|
|
|
|
nearestConstraint = constraints[i];
|
|
|
|
nearest = cp;
|
|
|
|
dist = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nearestConstraint != null)
|
|
|
|
{
|
|
|
|
this.sourceConstraint = nearestConstraint;
|
|
|
|
result = nearest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
{
|
|
|
|
result = mxConnectionHandlerGetSourcePerimeterPoint.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
return container;
|
2021-08-30 14:20:26 +00:00
|
|
|
};
|
2021-04-20 12:22:55 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
export const Default = Template.bind({});
|