221 lines
5.9 KiB
TypeScript
Raw Normal View History

2021-07-16 11:41:58 -03:00
/*
2021-12-25 14:39:34 -08:00
* Copyright [2021] [wisemapping]
2021-07-16 11:41:58 -03: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 15:08:54 +00:00
2022-12-09 20:56:01 -08:00
import { $assert } from '@wisemapping/core-js';
2022-07-13 01:45:36 +00:00
import { Point, CurvedLine, PolyLine, Line } from '@wisemapping/web2d';
2022-01-12 16:15:07 -08:00
import { TopicShape } from './model/INodeModel';
2022-02-14 19:01:48 -08:00
import RelationshipModel from './model/RelationshipModel';
import Topic from './Topic';
import TopicConfig from './TopicConfig';
2022-02-14 19:01:48 -08:00
import Workspace from './Workspace';
2021-07-16 11:41:58 -03:00
2022-12-09 20:56:01 -08:00
// eslint-disable-next-line no-shadow
export enum LineType {
SIMPLE,
POLYLINE,
CURVED,
SIMPLE_CURVED,
}
2021-12-04 15:39:20 -08:00
class ConnectionLine {
2022-02-14 19:01:48 -08:00
protected _targetTopic: Topic;
protected _sourceTopic: Topic;
2022-12-09 20:56:01 -08:00
protected _lineType: LineType;
2022-02-14 19:01:48 -08:00
protected _line2d: Line;
protected _model: RelationshipModel;
2022-12-09 20:56:01 -08:00
constructor(sourceNode: Topic, targetNode: Topic) {
2021-10-04 17:05:34 -07:00
$assert(targetNode, 'parentNode node can not be null');
$assert(sourceNode, 'childNode node can not be null');
2021-12-03 16:43:50 -08:00
$assert(sourceNode !== targetNode, 'Circular connection');
2021-10-04 17:05:34 -07:00
this._targetTopic = targetNode;
this._sourceTopic = sourceNode;
2022-02-14 19:01:48 -08:00
let line: Line;
2021-10-04 17:05:34 -07:00
const ctrlPoints = this._getCtrlPoints(sourceNode, targetNode);
2022-01-02 14:16:18 -08:00
if (targetNode.getType() === 'CentralTopic') {
2022-12-09 20:56:01 -08:00
line = this._createLine(LineType.CURVED);
2021-10-04 17:05:34 -07:00
line.setSrcControlPoint(ctrlPoints[0]);
line.setDestControlPoint(ctrlPoints[1]);
} else {
2022-12-09 20:56:01 -08:00
line = this._createLine(LineType.SIMPLE_CURVED);
2021-10-04 17:05:34 -07:00
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-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
private _getCtrlPoints(sourceNode: Topic, targetNode: Topic) {
2021-10-04 17:05:34 -07:00
const srcPos = sourceNode.workoutOutgoingConnectionPoint(targetNode.getPosition());
const destPos = targetNode.workoutIncomingConnectionPoint(sourceNode.getPosition());
const deltaX = (srcPos.x - destPos.x) / 3;
2021-12-19 08:31:29 -08:00
return [new Point(deltaX, 0), new Point(-deltaX, 0)];
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-12-09 20:56:01 -08:00
protected _createLine(lineType: LineType): Line {
2021-10-04 17:05:34 -07:00
this._lineType = lineType;
2022-11-29 21:48:53 -08:00
let line: ConnectionLine;
2021-10-04 17:05:34 -07:00
switch (lineType) {
2022-12-09 20:56:01 -08:00
case LineType.POLYLINE:
2021-12-19 08:31:29 -08:00
line = new PolyLine();
2021-10-04 17:05:34 -07:00
break;
2022-12-09 20:56:01 -08:00
case LineType.CURVED:
2021-12-19 08:31:29 -08:00
line = new CurvedLine();
2021-10-04 17:05:34 -07:00
break;
2022-12-09 20:56:01 -08:00
case LineType.SIMPLE_CURVED:
2021-12-19 08:31:29 -08:00
line = new CurvedLine();
2022-11-29 21:48:53 -08:00
(line as CurvedLine).setStyle(CurvedLine.SIMPLE_LINE);
2021-10-04 17:05:34 -07:00
break;
2022-12-09 20:56:01 -08:00
case LineType.SIMPLE:
2021-12-19 08:31:29 -08:00
line = new Line();
2021-10-04 17:05:34 -07:00
break;
2022-12-09 20:56:01 -08:00
default:
throw new Error(`Unexpected line type. ${lineType}`);
2021-10-04 17:05:34 -07:00
}
return line;
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-28 16:12:45 -08:00
setVisibility(value: boolean, fade = 0): void {
this._line2d.setVisibility(value, fade);
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-03-07 19:43:04 -08:00
isVisible(): boolean {
2021-10-04 17:05:34 -07:00
return this._line2d.isVisible();
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
setOpacity(opacity: number): void {
2021-10-04 17:05:34 -07:00
this._line2d.setOpacity(opacity);
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
redraw(): void {
2021-10-04 17:05:34 -07:00
const line2d = this._line2d;
const sourceTopic = this._sourceTopic;
const sourcePosition = sourceTopic.getPosition();
const targetTopic = this._targetTopic;
const targetPosition = targetTopic.getPosition();
2021-12-19 09:11:45 -08:00
const sPos = sourceTopic.workoutOutgoingConnectionPoint(targetPosition);
const tPos = targetTopic.workoutIncomingConnectionPoint(sourcePosition);
2021-10-04 17:05:34 -07:00
line2d.setFrom(tPos.x, tPos.y);
line2d.setTo(sPos.x, sPos.y);
2021-12-19 08:31:29 -08:00
if (line2d.getType() === 'CurvedLine') {
2021-10-04 17:05:34 -07:00
const ctrlPoints = this._getCtrlPoints(this._sourceTopic, this._targetTopic);
line2d.setSrcControlPoint(ctrlPoints[0]);
line2d.setDestControlPoint(ctrlPoints[1]);
}
// Add connector ...
this._positionateConnector(targetTopic);
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
protected _positionateConnector(targetTopic: Topic): void {
2021-10-04 17:05:34 -07:00
const targetPosition = targetTopic.getPosition();
const offset = TopicConfig.CONNECTOR_WIDTH / 2;
2021-10-04 17:05:34 -07:00
const targetTopicSize = targetTopic.getSize();
let y;
let x;
2021-12-03 16:43:50 -08:00
if (targetTopic.getShapeType() === TopicShape.LINE) {
2021-10-04 17:05:34 -07:00
y = targetTopicSize.height;
} else {
y = targetTopicSize.height / 2;
}
y -= offset;
const connector = targetTopic.getShrinkConnector();
2022-11-29 21:48:53 -08:00
if (connector) {
2021-10-04 17:05:34 -07:00
if (Math.sign(targetPosition.x) > 0) {
x = targetTopicSize.width;
connector.setPosition(x, y);
} else {
x = -TopicConfig.CONNECTOR_WIDTH;
2021-10-04 17:05:34 -07:00
}
connector.setPosition(x, y);
}
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-18 15:52:57 -08:00
setStroke(color: string, style: string, opacity: number) {
2021-10-04 17:05:34 -07:00
this._line2d.setStroke(null, null, color, opacity);
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
addToWorkspace(workspace: Workspace) {
2021-10-04 17:05:34 -07:00
workspace.append(this._line2d);
this._line2d.moveToBack();
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
removeFromWorkspace(workspace: Workspace) {
2021-10-04 17:05:34 -07:00
workspace.removeChild(this._line2d);
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
getTargetTopic(): Topic {
2021-10-04 17:05:34 -07:00
return this._targetTopic;
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
getSourceTopic(): Topic {
2021-10-04 17:05:34 -07:00
return this._sourceTopic;
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
getLineType(): number {
2021-10-04 17:05:34 -07:00
return this._lineType;
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
getLine(): Line {
2021-10-04 17:05:34 -07:00
return this._line2d;
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
getModel(): RelationshipModel {
2021-10-04 17:05:34 -07:00
return this._model;
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
setModel(model: RelationshipModel): void {
2021-10-04 17:05:34 -07:00
this._model = model;
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
getType(): string {
2021-10-04 17:05:34 -07:00
return 'ConnectionLine';
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
getId(): number {
2021-10-04 17:05:34 -07:00
return this._model.getId();
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-14 19:01:48 -08:00
moveToBack(): void {
2021-10-04 17:05:34 -07:00
this._line2d.moveToBack();
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
moveToFront() {
this._line2d.moveToFront();
2021-12-04 15:39:20 -08:00
}
2021-07-16 11:41:58 -03:00
2022-02-14 19:01:48 -08:00
static getStrokeColor = () => '#495879';
}
2021-07-16 11:41:58 -03:00
export default ConnectionLine;