| 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 |
1×
1×
7×
5×
5×
5×
5×
5×
5×
7×
5×
5×
5×
5×
5×
5×
5×
5×
7×
5×
5×
5×
5×
5×
5×
5×
7×
7×
7×
5×
7×
7×
7×
6×
6×
5×
7×
5×
5×
5×
5×
5×
7×
5×
5×
5×
5×
5×
5×
5×
2×
3×
3×
3×
3×
3×
7×
6×
4×
4×
6×
1×
| import {
getElementLineIntersection
} from '../../layout/LayoutUtil';
import {
getMid
} from '../../layout/LayoutUtil';
var MARKER_OK = 'connect-ok',
MARKER_NOT_OK = 'connect-not-ok';
import {
append as svgAppend,
attr as svgAttr,
create as svgCreate,
remove as svgRemove
} from 'tiny-svg';
export default function Connect(
eventBus, dragging, modeling,
rules, canvas, graphicsFactory) {
// TODO(nre): separate UI and events
// rules
function canConnect(source, target) {
return rules.allowed('connection.create', {
source: source,
target: target
});
}
// layouting
function crop(start, end, source, target) {
var sourcePath = graphicsFactory.getShapePath(source),
targetPath = target && graphicsFactory.getShapePath(target),
connectionPath = graphicsFactory.getConnectionPath({ waypoints: [ start, end ] });
start = getElementLineIntersection(sourcePath, connectionPath, true) || start;
end = (target && getElementLineIntersection(targetPath, connectionPath, false)) || end;
return [ start, end ];
}
// event handlers
eventBus.on('connect.move', function(event) {
var context = event.context,
source = context.source,
target = context.target,
visual = context.visual,
sourcePosition = context.sourcePosition,
endPosition,
waypoints;
// update connection visuals during drag
endPosition = {
x: event.x,
y: event.y
};
waypoints = crop(sourcePosition, endPosition, source, target);
svgAttr(visual, { 'points': [ waypoints[0].x, waypoints[0].y, waypoints[1].x, waypoints[1].y ] });
});
eventBus.on('connect.hover', function(event) {
var context = event.context,
source = context.source,
hover = event.hover,
canExecute;
canExecute = context.canExecute = canConnect(source, hover);
// simply ignore hover
Iif (canExecute === null) {
return;
}
context.target = hover;
canvas.addMarker(hover, canExecute ? MARKER_OK : MARKER_NOT_OK);
});
eventBus.on([ 'connect.out', 'connect.cleanup' ], function(event) {
var context = event.context;
if (context.target) {
canvas.removeMarker(context.target, context.canExecute ? MARKER_OK : MARKER_NOT_OK);
}
context.target = null;
context.canExecute = false;
});
eventBus.on('connect.cleanup', function(event) {
var context = event.context;
if (context.visual) {
svgRemove(context.visual);
}
});
eventBus.on('connect.start', function(event) {
var context = event.context,
visual;
visual = svgCreate('polyline');
svgAttr(visual, {
'stroke': '#333',
'strokeDasharray': [ 1 ],
'strokeWidth': 2,
'pointer-events': 'none'
});
svgAppend(canvas.getDefaultLayer(), visual);
context.visual = visual;
});
eventBus.on('connect.end', function(event) {
var context = event.context,
source = context.source,
sourcePosition = context.sourcePosition,
target = context.target,
targetPosition = {
x: event.x,
y: event.y
},
canExecute = context.canExecute || canConnect(source, target);
if (!canExecute) {
return false;
}
var attrs = null,
hints = {
connectionStart: sourcePosition,
connectionEnd: targetPosition
};
Eif (typeof canExecute === 'object') {
attrs = canExecute;
}
modeling.connect(source, target, attrs, hints);
});
// API
/**
* Start connect operation.
*
* @param {DOMEvent} event
* @param {djs.model.Base} source
* @param {Point} [sourcePosition]
* @param {Boolean} [autoActivate=false]
*/
this.start = function(event, source, sourcePosition, autoActivate) {
if (typeof sourcePosition !== 'object') {
autoActivate = sourcePosition;
sourcePosition = getMid(source);
}
dragging.init(event, 'connect', {
autoActivate: autoActivate,
data: {
shape: source,
context: {
source: source,
sourcePosition: sourcePosition
}
}
});
};
}
Connect.$inject = [
'eventBus',
'dragging',
'modeling',
'rules',
'canvas',
'graphicsFactory'
]; |