wisemapping-frontend/packages/mindplot/src/components/ConnectionLine.js

206 lines
5.5 KiB
JavaScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
2021-12-25 23:39:34 +01:00
* Copyright [2021] [wisemapping]
2021-07-16 16:41:58 +02:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* 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-12-14 16:08:54 +01:00
2021-12-04 01:43:50 +01:00
import { $assert, $defined } from '@wisemapping/core-js';
2021-12-19 18:11:45 +01:00
import {
Point, CurvedLine, PolyLine, Line,
} from '@wisemapping/web2d';
import INodeModel, { TopicShape } from './model/INodeModel';
import TopicConfig from './TopicConfig';
2021-07-16 16:41:58 +02:00
2021-12-05 00:39:20 +01:00
class ConnectionLine {
constructor(sourceNode, targetNode, lineType) {
2021-10-05 02:05:34 +02:00
$assert(targetNode, 'parentNode node can not be null');
$assert(sourceNode, 'childNode node can not be null');
2021-12-04 01:43:50 +01:00
$assert(sourceNode !== targetNode, 'Circular connection');
2021-10-05 02:05:34 +02:00
this._targetTopic = targetNode;
this._sourceTopic = sourceNode;
let line;
const ctrlPoints = this._getCtrlPoints(sourceNode, targetNode);
2021-12-04 01:43:50 +01:00
if (targetNode.getType() === INodeModel.CENTRAL_TOPIC_TYPE) {
2021-10-05 02:05:34 +02:00
line = this._createLine(lineType, ConnectionLine.CURVED);
line.setSrcControlPoint(ctrlPoints[0]);
line.setDestControlPoint(ctrlPoints[1]);
} else {
line = this._createLine(lineType, ConnectionLine.SIMPLE_CURVED);
line.setSrcControlPoint(ctrlPoints[0]);
line.setDestControlPoint(ctrlPoints[1]);
}
// Set line styles ...
const strokeColor = ConnectionLine.getStrokeColor();
line.setStroke(1, 'solid', strokeColor, 1);
line.setFill(strokeColor, 1);
this._line2d = line;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
_getCtrlPoints(sourceNode, targetNode) {
const srcPos = sourceNode.workoutOutgoingConnectionPoint(targetNode.getPosition());
const destPos = targetNode.workoutIncomingConnectionPoint(sourceNode.getPosition());
const deltaX = (srcPos.x - destPos.x) / 3;
2021-12-19 17:31:29 +01:00
return [new Point(deltaX, 0), new Point(-deltaX, 0)];
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
_createLine(lineTypeParam, defaultStyle) {
const lineType = $defined(lineTypeParam) ? parseInt(lineTypeParam, 10) : defaultStyle;
2021-10-05 02:05:34 +02:00
this._lineType = lineType;
let line = null;
switch (lineType) {
case ConnectionLine.POLYLINE:
2021-12-19 17:31:29 +01:00
line = new PolyLine();
2021-10-05 02:05:34 +02:00
break;
case ConnectionLine.CURVED:
2021-12-19 17:31:29 +01:00
line = new CurvedLine();
2021-10-05 02:05:34 +02:00
break;
case ConnectionLine.SIMPLE_CURVED:
2021-12-19 17:31:29 +01:00
line = new CurvedLine();
line.setStyle(CurvedLine.SIMPLE_LINE);
2021-10-05 02:05:34 +02:00
break;
default:
2021-12-19 17:31:29 +01:00
line = new Line();
2021-10-05 02:05:34 +02:00
break;
}
return line;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
setVisibility(value) {
this._line2d.setVisibility(value);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
isVisible() {
return this._line2d.isVisible();
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
setOpacity(opacity) {
this._line2d.setOpacity(opacity);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
redraw() {
const line2d = this._line2d;
const sourceTopic = this._sourceTopic;
const sourcePosition = sourceTopic.getPosition();
const targetTopic = this._targetTopic;
const targetPosition = targetTopic.getPosition();
2021-12-19 18:11:45 +01:00
const sPos = sourceTopic.workoutOutgoingConnectionPoint(targetPosition);
const tPos = targetTopic.workoutIncomingConnectionPoint(sourcePosition);
2021-10-05 02:05:34 +02:00
line2d.setFrom(tPos.x, tPos.y);
line2d.setTo(sPos.x, sPos.y);
2021-12-19 17:31:29 +01:00
if (line2d.getType() === 'CurvedLine') {
2021-10-05 02:05:34 +02:00
const ctrlPoints = this._getCtrlPoints(this._sourceTopic, this._targetTopic);
line2d.setSrcControlPoint(ctrlPoints[0]);
line2d.setDestControlPoint(ctrlPoints[1]);
}
// Add connector ...
this._positionateConnector(targetTopic);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
_positionateConnector(targetTopic) {
const targetPosition = targetTopic.getPosition();
const offset = TopicConfig.CONNECTOR_WIDTH / 2;
2021-10-05 02:05:34 +02:00
const targetTopicSize = targetTopic.getSize();
let y;
let x;
2021-12-04 01:43:50 +01:00
if (targetTopic.getShapeType() === TopicShape.LINE) {
2021-10-05 02:05:34 +02:00
y = targetTopicSize.height;
} else {
y = targetTopicSize.height / 2;
}
y -= offset;
const connector = targetTopic.getShrinkConnector();
if ($defined(connector)) {
if (Math.sign(targetPosition.x) > 0) {
x = targetTopicSize.width;
connector.setPosition(x, y);
} else {
x = -TopicConfig.CONNECTOR_WIDTH;
2021-10-05 02:05:34 +02:00
}
connector.setPosition(x, y);
}
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
setStroke(color, style, opacity) {
this._line2d.setStroke(null, null, color, opacity);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
addToWorkspace(workspace) {
workspace.append(this._line2d);
this._line2d.moveToBack();
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
removeFromWorkspace(workspace) {
workspace.removeChild(this._line2d);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getTargetTopic() {
return this._targetTopic;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getSourceTopic() {
return this._sourceTopic;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getLineType() {
return this._lineType;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getLine() {
return this._line2d;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getModel() {
return this._model;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
setModel(model) {
this._model = model;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getType() {
return 'ConnectionLine';
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getId() {
return this._model.getId();
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
moveToBack() {
this._line2d.moveToBack();
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
moveToFront() {
this._line2d.moveToFront();
2021-12-05 00:39:20 +01:00
}
}
2021-07-16 16:41:58 +02:00
2021-12-04 01:43:50 +01:00
ConnectionLine.getStrokeColor = () => '#495879';
2021-07-16 16:41:58 +02:00
ConnectionLine.SIMPLE = 0;
ConnectionLine.POLYLINE = 1;
ConnectionLine.CURVED = 2;
ConnectionLine.SIMPLE_CURVED = 3;
export default ConnectionLine;