Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
calculatePositions(points: PointModel[], event: MouseEvent, index: number, coordinate: string) {
// If path is first or last add another point to keep node port on its position
if (index === 0) {
let point = new PointModel({
link: this.props.link,
position: new Point(points[index].getX(), points[index].getY())
});
this.props.link.addPoint(point, index);
this.dragging_index++;
return;
} else if (index === points.length - 2) {
let point = new PointModel({
link: this.props.link,
position: new Point(points[index + 1].getX(), points[index + 1].getY())
});
this.props.link.addPoint(point, index + 1);
return;
}
// Merge two points if it is not close to node port and close to each other
calculatePositions(points: PointModel[], event: MouseEvent, index: number, coordinate: string) {
// If path is first or last add another point to keep node port on its position
if (index === 0) {
let point = new PointModel({
link: this.props.link,
position: new Point(points[index].getX(), points[index].getY())
});
this.props.link.addPoint(point, index);
this.dragging_index++;
return;
} else if (index === points.length - 2) {
let point = new PointModel({
link: this.props.link,
position: new Point(points[index + 1].getX(), points[index + 1].getY())
});
this.props.link.addPoint(point, index + 1);
return;
}
// Merge two points if it is not close to node port and close to each other
if (index - 2 > 0) {
let _points = {
[index - 2]: points[index - 2].getPosition(),
[index + 1]: points[index + 1].getPosition(),
[index - 1]: points[index - 1].getPosition()
};
if (Math.abs(_points[index - 1][coordinate] - _points[index + 1][coordinate]) < 5) {
_points[index - 2][coordinate] = this.props.diagramEngine.getRelativeMousePoint(event)[coordinate];
}
} else {
if (this.props.link.getLastPathXdirection()) {
points[i - 1].setPosition(points[i - 1].getX(), points[i].getY());
} else {
points[i - 1].setPosition(points[i].getX(), points[i - 1].getY());
}
}
}
}
// If there is existing link which has two points add one
// NOTE: It doesn't matter if check is for dy or dx
if (points.length === 2 && dy !== 0 && !this.state.canDrag) {
this.props.link.addPoint(
new PointModel({
link: this.props.link,
position: new Point(pointLeft.getX(), pointRight.getY())
})
);
}
for (let j = 0; j < points.length - 1; j++) {
paths.push(
this.generateLink(
LinkWidget.generateLinePath(points[j], points[j + 1]),
{
'data-linkid': this.props.link.getID(),
'data-point': j,
onMouseDown: (event: MouseEvent) => {
if (event.button === 0) {
this.setState({ canDrag: true });
g.edges().forEach(e => {
const edge = g.edge(e);
const link = model.getLink(e.name);
const points = [link.getFirstPoint()];
for (let i = 1; i < edge.points.length - 2; i++) {
points.push(new PointModel({ link: link, position: new Point(edge.points[i].x, edge.points[i].y) }));
}
link.setPoints(points.concat(link.getLastPoint()));
});
}
addPointToLink(event: MouseEvent, index: number) {
if (
!event.shiftKey &&
!this.props.link.isLocked() &&
this.props.link.getPoints().length - 1 <= this.props.diagramEngine.getMaxNumberPointsPerLink()
) {
const point = new PointModel({
link: this.props.link,
position: this.props.diagramEngine.getRelativeMousePoint(event)
});
this.props.link.addPoint(point, index);
event.persist();
event.stopPropagation();
this.forceUpdate(() => {
this.props.diagramEngine.getActionEventBus().fireAction({
event,
model: point
});
});
}
}
[...Array(2)].forEach(item => {
this.props.link.addPoint(
new PointModel({
link: this.props.link,
position: new Point(pointLeft.getX(), pointRight.getY())
}),
1
);
});
this.props.link.setManuallyFirstAndLastPathsDirection(true, true);