-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
DefaultLinkModel.ts
120 lines (106 loc) · 3.24 KB
/
DefaultLinkModel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
DiagramEngine,
LabelModel,
LinkModel,
LinkModelGenerics,
LinkModelListener,
PortModel,
PortModelAlignment
} from '@projectstorm/react-diagrams-core';
import { DefaultLabelModel } from '../label/DefaultLabelModel';
import { BezierCurve } from '@projectstorm/geometry';
import { BaseEntityEvent, BaseModelOptions, DeserializeEvent } from '@projectstorm/react-canvas-core';
export interface DefaultLinkModelListener extends LinkModelListener {
colorChanged?(event: BaseEntityEvent<DefaultLinkModel> & { color: null | string }): void;
widthChanged?(event: BaseEntityEvent<DefaultLinkModel> & { width: 0 | number }): void;
}
export interface DefaultLinkModelOptions extends BaseModelOptions {
width?: number;
color?: string;
selectedColor?: string;
curvyness?: number;
type?: string;
testName?: string;
}
export interface DefaultLinkModelGenerics extends LinkModelGenerics {
LISTENER: DefaultLinkModelListener;
OPTIONS: DefaultLinkModelOptions;
}
export class DefaultLinkModel extends LinkModel<DefaultLinkModelGenerics> {
constructor(options: DefaultLinkModelOptions = {}) {
super({
type: 'default',
width: options.width || 3,
color: options.color || 'gray',
selectedColor: options.selectedColor || 'rgb(0,192,255)',
curvyness: 50,
...options
});
}
calculateControlOffset(port: PortModel): [number, number] {
if (port.getOptions().alignment === PortModelAlignment.RIGHT) {
return [this.options.curvyness, 0];
} else if (port.getOptions().alignment === PortModelAlignment.LEFT) {
return [-this.options.curvyness, 0];
} else if (port.getOptions().alignment === PortModelAlignment.TOP) {
return [0, -this.options.curvyness];
}
return [0, this.options.curvyness];
}
getSVGPath(): string {
if (this.points.length == 2) {
const curve = new BezierCurve();
curve.setSource(this.getFirstPoint().getPosition());
curve.setTarget(this.getLastPoint().getPosition());
curve.setSourceControl(
this.getFirstPoint()
.getPosition()
.clone()
);
curve.setTargetControl(
this.getLastPoint()
.getPosition()
.clone()
);
if (this.sourcePort) {
curve.getSourceControl().translate(...this.calculateControlOffset(this.getSourcePort()));
}
if (this.targetPort) {
curve.getTargetControl().translate(...this.calculateControlOffset(this.getTargetPort()));
}
return curve.getSVGCurve();
}
}
serialize() {
return {
...super.serialize(),
width: this.options.width,
color: this.options.color,
curvyness: this.options.curvyness,
selectedColor: this.options.selectedColor
};
}
deserialize(event: DeserializeEvent<this>) {
super.deserialize(event);
this.options.color = event.data.color;
this.options.width = event.data.width;
this.options.curvyness = event.data.curvyness;
this.options.selectedColor = event.data.selectedColor;
}
addLabel(label: LabelModel | string) {
if (label instanceof LabelModel) {
return super.addLabel(label);
}
let labelOb = new DefaultLabelModel();
labelOb.setLabel(label);
return super.addLabel(labelOb);
}
setWidth(width: number) {
this.options.width = width;
this.fireEvent({ width }, 'widthChanged');
}
setColor(color: string) {
this.options.color = color;
this.fireEvent({ color }, 'colorChanged');
}
}