-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
PortModel.ts
143 lines (122 loc) · 3.25 KB
/
PortModel.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { NodeModel } from '../node/NodeModel';
import { LinkModel } from '../link/LinkModel';
import * as _ from 'lodash';
import { DiagramEngine } from '../../DiagramEngine';
import { Point } from '@projectstorm/geometry';
import {
BaseEntityEvent,
BaseModelOptions,
BasePositionModel,
BasePositionModelGenerics,
BasePositionModelListener
} from '@projectstorm/react-canvas-core';
export enum PortModelAlignment {
TOP = 'top',
LEFT = 'left',
BOTTOM = 'bottom',
RIGHT = 'right'
}
export interface PortModelListener extends BasePositionModelListener {
/**
* fires when it first receives positional information
*/
reportInitialPosition?: (event: BaseEntityEvent<PortModel>) => void;
}
export interface PortModelOptions extends BaseModelOptions {
alignment?: PortModelAlignment;
maximumLinks?: number;
name: string;
}
export interface PortModelGenerics extends BasePositionModelGenerics {
OPTIONS: PortModelOptions;
PARENT: NodeModel;
LISTENER: PortModelListener;
}
export class PortModel<G extends PortModelGenerics = PortModelGenerics> extends BasePositionModel<G> {
links: { [id: string]: LinkModel };
// calculated post rendering so routing can be done correctly
width: number;
height: number;
reportedPosition: boolean;
constructor(options: G['OPTIONS']) {
super(options);
this.links = {};
this.reportedPosition = false;
}
deSerialize(ob, engine: DiagramEngine) {
super.deSerialize(ob, engine);
this.reportedPosition = false;
this.options.name = ob.name;
this.options.alignment = ob.alignment;
}
serialize() {
return {
...super.serialize(),
name: this.options.name,
alignment: this.options.alignment,
parentNode: this.parent.getID(),
links: _.map(this.links, link => {
return link.getID();
})
};
}
doClone(lookupTable = {}, clone) {
clone.links = {};
clone.parentNode = this.getParent().clone(lookupTable);
}
getNode(): NodeModel {
return this.getParent();
}
getName(): string {
return this.options.name;
}
getMaximumLinks(): number {
return this.options.maximumLinks;
}
setMaximumLinks(maximumLinks: number) {
this.options.maximumLinks = maximumLinks;
}
removeLink(link: LinkModel) {
delete this.links[link.getID()];
}
addLink(link: LinkModel) {
this.links[link.getID()] = link;
}
getLinks(): { [id: string]: LinkModel } {
return this.links;
}
public createLinkModel(): LinkModel | null {
if (_.isFinite(this.options.maximumLinks)) {
var numberOfLinks: number = _.size(this.links);
if (this.options.maximumLinks === 1 && numberOfLinks >= 1) {
return _.values(this.links)[0];
} else if (numberOfLinks >= this.options.maximumLinks) {
return null;
}
}
return null;
}
updateCoords(cords: { x: number; y: number; width: number; height: number }) {
const { x, y, width, height } = cords;
this.width = width;
this.height = height;
this.setPosition(x, y);
const center = new Point(x + width / 2, y + height / 2);
_.forEach(this.getLinks(), link => {
link.getPointForPort(this).setPosition(center.clone());
});
this.reportedPosition = true;
this.fireEvent(
{
entity: this
},
'reportInitialPosition'
);
}
canLinkToPort(port: PortModel): boolean {
return true;
}
isLocked() {
return super.isLocked() || this.getParent().isLocked();
}
}