-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.component.ts
More file actions
246 lines (218 loc) · 6.04 KB
/
app.component.ts
File metadata and controls
246 lines (218 loc) · 6.04 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { AfterViewInit, OnInit, Component, ElementRef, ViewChild } from '@angular/core';
import { linkTools, elementTools, dia, shapes, highlighters } from '@joint/plus';
import { Node, Edge } from '../shared/shapes';
import ResizeTool from '../shared/resize-tool';
import { AvoidRouterService } from './avoid-router.service';
import { AvoidRouter } from '../shared/avoid-router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, AfterViewInit {
@ViewChild('canvas') canvas: ElementRef;
private avoidRouterService: AvoidRouterService;
private graph: dia.Graph;
private paper: dia.Paper;
constructor(avoidRouterService: AvoidRouterService) {
this.avoidRouterService = avoidRouterService;
}
public async ngOnInit(): Promise<any> {
const cellNamespace = {
...shapes,
Node,
Edge,
};
const graph = this.graph = new dia.Graph({}, { cellNamespace });
const paper = this.paper = new dia.Paper({
model: graph,
cellViewNamespace: cellNamespace,
width: 1000,
height: 600,
gridSize: 10,
interactive: { linkMove: false },
linkPinning: false,
async: true,
frozen: true,
background: { color: '#F3F7F6' },
snapLinks: { radius: 30 },
overflow: true,
defaultConnector: {
name: 'straight',
args: {
cornerType: 'cubic',
cornerRadius: 4,
},
},
highlighting: {
default: {
name: 'mask',
options: {
padding: 2,
attrs: {
stroke: '#EA3C24',
strokeWidth: 2,
},
},
},
},
defaultLink: () => new Edge(),
validateConnection: (
sourceView,
sourceMagnet,
targetView,
targetMagnet,
end
) => {
const source = sourceView.model as dia.Element;
const target = targetView.model as dia.Element;
if (source.isLink() || target.isLink()) return false;
if (targetMagnet === sourceMagnet) return false;
if (end === 'target' ? targetMagnet : sourceMagnet) {
return true;
}
if (source === target) return false;
return end === 'target' ? !target.hasPorts() : !source.hasPorts();
},
});
// Add tools to the elements.
graph.getElements().forEach((el) => addElementTools(el, paper));
graph.on('add', (cell) => {
if (cell.isLink()) return;
addElementTools(cell, paper);
});
function addElementTools(el: dia.Element, paper: dia.Paper) {
const tools = [
new ResizeTool({
selector: 'body',
}),
new elementTools.Remove({
useModelGeometry: true,
x: -10,
y: -10,
}),
];
if (!el.hasPorts()) {
tools.push(
new elementTools.Connect({
useModelGeometry: true,
x: 'calc(w + 10)',
y: 'calc(h - 20)',
})
);
}
el.findView(paper).addTools(new dia.ToolsView({ tools }));
}
// Add tools to the links.
paper.on('link:mouseenter', (linkView) => {
linkView.addTools(
new dia.ToolsView({
tools: [
new linkTools.Remove(),
new linkTools.TargetArrowhead(),
],
})
);
});
paper.on('link:mouseleave', (linkView) => {
linkView.removeTools();
});
paper.on('blank:pointerdblclick', (evt, x, y) => {
const node = new Node({
position: { x: x - 50, y: y - 50 },
size: { width: 100, height: 100 },
});
graph.addCell(node);
});
// Add a class to the links when they are being interacted with.
// See `styles.css` for the styles.
paper.on('link:pointerdown', (linkView) => {
highlighters.addClass.add(linkView, 'line', 'active-link', {
className: 'active-link'
});
});
paper.on('link:pointerup', (linkView) => {
highlighters.addClass.remove(linkView);
});
}
public ngAfterViewInit(): void {
const { avoidRouterService, canvas, graph, paper } = this;
const c1 = new Node({
position: { x: 100, y: 100 },
size: { width: 100, height: 100 },
ports: {
items: [
{
group: 'top',
id: 'port1',
},
{
group: 'top',
id: 'port2',
},
{
group: 'right',
id: 'port3',
},
{
group: 'left',
id: 'port4',
// TODO: we need to redefine the port on element resize
// The port is currently defined proportionally to the element size.
// args: {
// dy: 30
// }
},
],
},
});
const c2 = c1.clone().set({
position: { x: 300, y: 300 },
size: { width: 100, height: 100 },
});
const c3 = c1.clone().set({
position: { x: 500, y: 100 },
size: { width: 100, height: 100 },
});
const c4 = new Node({
position: { x: 100, y: 400 },
size: { width: 100, height: 100 },
});
const c5 = c4.clone().set({
position: { x: 500, y: 300 },
size: { width: 100, height: 100 },
});
const l1 = new Edge({
source: { id: c1.id, port: 'port4' },
target: { id: c2.id, port: 'port4' },
});
const l2 = new Edge({
source: { id: c2.id, port: 'port2' },
target: { id: c3.id, port: 'port4' },
});
const l3 = new Edge({
source: { id: c4.id },
target: { id: c5.id },
});
const l4 = new Edge({
source: { id: c5.id },
target: { id: c4.id },
});
graph.addCells([c1, c2, c3, c4, c5, l1, l2, l3, l4]);
canvas.nativeElement.appendChild(paper.el);
paper.unfreeze();
paper.fitToContent({
useModelGeometry: true,
padding: 100,
allowNewOrigin: 'any',
});
// Start the Avoid Router.
const router = new AvoidRouter(graph, {
shapeBufferDistance: 20,
idealNudgingDistance: 10,
portOverflow: 10,
});
router.addGraphListeners();
router.routeAll();
}
}