/
AppendShapeHandler.js
66 lines (49 loc) · 1.5 KB
/
AppendShapeHandler.js
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
import { some } from 'min-dash';
/**
* A handler that implements reversible appending of shapes
* to a source shape.
*
* @param {canvas} Canvas
* @param {elementFactory} ElementFactory
* @param {modeling} Modeling
*/
export default function AppendShapeHandler(modeling) {
this._modeling = modeling;
}
AppendShapeHandler.$inject = [ 'modeling' ];
// api //////////////////////
/**
* Creates a new shape
*
* @param {Object} context
* @param {ElementDescriptor} context.shape the new shape
* @param {ElementDescriptor} context.source the source object
* @param {ElementDescriptor} context.parent the parent object
* @param {Point} context.position position of the new element
*/
AppendShapeHandler.prototype.preExecute = function(context) {
var source = context.source;
if (!source) {
throw new Error('source required');
}
var target = context.target || source.parent,
shape = context.shape;
shape = context.shape =
this._modeling.createShape(
shape,
context.position,
target, { attach: context.attach });
context.shape = shape;
};
AppendShapeHandler.prototype.postExecute = function(context) {
var parent = context.connectionParent || context.shape.parent;
if (!existsConnection(context.source, context.shape)) {
// create connection
this._modeling.connect(context.source, context.shape, context.connection, parent);
}
};
function existsConnection(source, target) {
return some(source.outgoing, function(c) {
return c.target === target;
});
}