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

222 lines
6.0 KiB
TypeScript
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';
2022-01-13 01:15:07 +01:00
import { TopicShape } from './model/INodeModel';
2022-02-15 04:01:48 +01:00
import RelationshipModel from './model/RelationshipModel';
import Topic from './Topic';
import TopicConfig from './TopicConfig';
2022-02-15 04:01:48 +01:00
import Workspace from './Workspace';
2021-07-16 16:41:58 +02:00
2021-12-05 00:39:20 +01:00
class ConnectionLine {
2022-02-15 04:01:48 +01:00
protected _targetTopic: Topic;
protected _sourceTopic: Topic;
protected _lineType: number;
protected _line2d: Line;
protected _model: RelationshipModel;
constructor(sourceNode: Topic, targetNode: Topic, lineType?: number) {
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;
2022-02-15 04:01:48 +01:00
let line: Line;
2021-10-05 02:05:34 +02:00
const ctrlPoints = this._getCtrlPoints(sourceNode, targetNode);
2022-01-02 23:16:18 +01:00
if (targetNode.getType() === 'CentralTopic') {
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
2022-02-15 04:01:48 +01:00
private _getCtrlPoints(sourceNode: Topic, targetNode: Topic) {
2021-10-05 02:05:34 +02:00
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
2022-02-15 04:01:48 +01:00
protected _createLine(lineTypeParam: number, defaultStyle: number): Line {
const lineType = $defined(lineTypeParam) ? lineTypeParam : 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
2022-03-01 01:12:45 +01:00
setVisibility(value: boolean, fade = 0): void {
this._line2d.setVisibility(value, fade);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-03-08 04:43:04 +01:00
isVisible(): boolean {
2021-10-05 02:05:34 +02:00
return this._line2d.isVisible();
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
setOpacity(opacity: number): void {
2021-10-05 02:05:34 +02:00
this._line2d.setOpacity(opacity);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
redraw(): void {
2021-10-05 02:05:34 +02:00
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
2022-02-15 04:01:48 +01:00
protected _positionateConnector(targetTopic: Topic): void {
2021-10-05 02:05:34 +02:00
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
2022-02-19 00:52:57 +01:00
setStroke(color: string, style: string, opacity: number) {
2021-10-05 02:05:34 +02:00
this._line2d.setStroke(null, null, color, opacity);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
addToWorkspace(workspace: Workspace) {
2021-10-05 02:05:34 +02:00
workspace.append(this._line2d);
this._line2d.moveToBack();
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
removeFromWorkspace(workspace: Workspace) {
2021-10-05 02:05:34 +02:00
workspace.removeChild(this._line2d);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
getTargetTopic(): Topic {
2021-10-05 02:05:34 +02:00
return this._targetTopic;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
getSourceTopic(): Topic {
2021-10-05 02:05:34 +02:00
return this._sourceTopic;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
getLineType(): number {
2021-10-05 02:05:34 +02:00
return this._lineType;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
getLine(): Line {
2021-10-05 02:05:34 +02:00
return this._line2d;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
getModel(): RelationshipModel {
2021-10-05 02:05:34 +02:00
return this._model;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
setModel(model: RelationshipModel): void {
2021-10-05 02:05:34 +02:00
this._model = model;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
getType(): string {
2021-10-05 02:05:34 +02:00
return 'ConnectionLine';
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
getId(): number {
2021-10-05 02:05:34 +02:00
return this._model.getId();
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-15 04:01:48 +01:00
moveToBack(): void {
2021-10-05 02:05:34 +02:00
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
2022-02-15 04:01:48 +01:00
static SIMPLE = 0;
static POLYLINE = 1;
static CURVED = 2;
2021-07-16 16:41:58 +02:00
2022-02-15 04:01:48 +01:00
static SIMPLE_CURVED = 3;
static getStrokeColor = () => '#495879';
}
2021-07-16 16:41:58 +02:00
export default ConnectionLine;