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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289 | 1×
1×
1×
1×
1×
26×
26×
26×
26×
23×
26×
24×
24×
2×
59×
236×
26×
210×
22×
22×
22×
22×
22×
22×
22×
22×
22×
22×
22×
22×
22×
22×
66×
66×
66×
66×
66×
66×
66×
26×
26×
3×
23×
22×
66×
66×
66×
66×
22×
66×
22×
35×
35×
35×
33×
22×
13×
13×
13×
13×
13×
13×
13×
13×
13×
13×
3×
10×
13×
22×
22×
22×
22×
22×
22×
1×
| var LOW_PRIORITY = 750;
var MARKER_OK = 'drop-ok',
MARKER_NOT_OK = 'drop-not-ok',
MARKER_ATTACH = 'attach-ok',
MARKER_NEW_PARENT = 'new-parent';
import {
append as svgAppend,
attr as svgAttr,
classes as svgClasses,
create as svgCreate,
remove as svgRemove
} from 'tiny-svg';
import {
translate
} from '../../util/SvgTransformUtil';
/**
* Adds the ability to create new shapes via drag and drop.
*
* Create must be activated via {@link Create#start}. From that
* point on, create will invoke `shape.create` and `shape.attach`
* rules to query whether or not creation or attachment on a certain
* position is allowed.
*
* If create or attach is allowed and a source is given, Create it
* will invoke `connection.create` rules to query whether a connection
* can be drawn between source and new shape. During rule evaluation
* the target is not attached yet, however
*
* hints = { targetParent, targetAttach }
*
* are passed to the evaluating rules.
*
*
* ## Rule Return Values
*
* Return values interpreted from `shape.create`:
*
* * `true`: create is allowed
* * `false`: create is disallowed
* * `null`: create is not allowed but should be ignored visually
*
* Return values interpreted from `shape.attach`:
*
* * `true`: attach is allowed
* * `Any`: attach is allowed with the constraints
* * `false`: attach is disallowed
*
* Return values interpreted from `connection.create`:
*
* * `true`: connection can be created
* * `Any`: connection with the given attributes can be created
* * `false`: connection can't be created
*
*
* @param {EventBus} eventBus
* @param {Dragging} dragging
* @param {Rules} rules
* @param {Modeling} modeling
* @param {Canvas} canvas
* @param {Styles} styles
* @param {GraphicsFactory} graphicsFactory
*/
export default function Create(
eventBus, dragging, rules, modeling,
canvas, styles, graphicsFactory) {
// rules
function canCreate(shape, target, source, position) {
Iif (!target) {
return false;
}
var ctx = {
source: source,
shape: shape,
target: target,
position: position
};
var create,
attach,
connect;
attach = rules.allowed('shape.attach', ctx);
if (!attach) {
create = rules.allowed('shape.create', ctx);
}
if (create || attach) {
connect = source && rules.allowed('connection.create', {
source: source,
target: shape,
hints: {
targetParent: target,
targetAttach: attach
}
});
return {
attach: attach,
connect: connect
};
}
return false;
}
/** set drop marker on an element */
function setMarker(element, marker) {
[ MARKER_ATTACH, MARKER_OK, MARKER_NOT_OK, MARKER_NEW_PARENT ].forEach(function(m) {
if (m === marker) {
canvas.addMarker(element, m);
} else {
canvas.removeMarker(element, m);
}
});
}
// visual helpers
function createVisual(shape) {
var group, preview, visual;
group = svgCreate('g');
svgAttr(group, styles.cls('djs-drag-group', [ 'no-events' ]));
svgAppend(canvas.getDefaultLayer(), group);
preview = svgCreate('g');
svgClasses(preview).add('djs-dragger');
svgAppend(group, preview);
translate(preview, shape.width / -2, shape.height / -2);
var visualGroup = svgCreate('g');
svgClasses(visualGroup).add('djs-visual');
svgAppend(preview, visualGroup);
visual = visualGroup;
// hijack renderer to draw preview
graphicsFactory.drawShape(visual, shape);
return group;
}
// event handlers
eventBus.on('create.move', function(event) {
var context = event.context,
hover = event.hover,
shape = context.shape,
source = context.source,
canExecute;
var position = {
x: event.x,
y: event.y
};
canExecute = context.canExecute = hover && canCreate(shape, hover, source, position);
// ignore hover visually if canExecute is null
if (hover && canExecute !== null) {
context.target = hover;
if (canExecute && canExecute.attach) {
setMarker(hover, MARKER_ATTACH);
} else {
setMarker(hover, canExecute ? MARKER_NEW_PARENT : MARKER_NOT_OK);
}
}
});
eventBus.on('create.move', LOW_PRIORITY, function(event) {
var context = event.context,
shape = context.shape,
visual = context.visual;
// lazy init drag visual once we received the first real
// drag move event (this allows us to get the proper canvas local coordinates)
if (!visual) {
visual = context.visual = createVisual(shape);
}
translate(visual, event.x, event.y);
});
eventBus.on([ 'create.end', 'create.out', 'create.cleanup' ], function(event) {
var context = event.context,
target = context.target;
if (target) {
setMarker(target, null);
}
});
eventBus.on('create.end', function(event) {
var context = event.context,
source = context.source,
shape = context.shape,
target = context.target,
canExecute = context.canExecute,
attach = canExecute && canExecute.attach,
connect = canExecute && canExecute.connect,
position = {
x: event.x,
y: event.y
};
Iif (!canExecute) {
return false;
}
if (connect) {
// invoke append if connect is set via rules
shape = modeling.appendShape(source, shape, position, target, {
attach: attach,
connection: connect === true ? {} : connect
});
} else {
// invoke create, if connect is not set
shape = modeling.createShape(shape, position, target, {
attach: attach
});
}
// make sure we provide the actual attached
// shape with the context so that selection and
// other components can use it right after the create
// operation ends
context.shape = shape;
});
eventBus.on('create.cleanup', function(event) {
var context = event.context;
Eif (context.visual) {
svgRemove(context.visual);
}
});
// API
this.start = function(event, shape, source) {
dragging.init(event, 'create', {
cursor: 'grabbing',
autoActivate: true,
data: {
shape: shape,
context: {
shape: shape,
source: source
}
}
});
};
}
Create.$inject = [
'eventBus',
'dragging',
'rules',
'modeling',
'canvas',
'styles',
'graphicsFactory'
]; |