| 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 |
559×
559×
1×
1×
26×
26×
26×
26×
26×
26×
3×
26×
26×
26×
1×
26×
25×
25×
26×
26×
26×
2×
2×
2×
2×
2×
2×
26×
1×
1×
1×
1×
1×
1×
1×
26×
26×
26×
26×
2×
26×
1×
26×
1×
1×
1×
26×
26×
1×
1×
1×
1×
2×
2×
| import { forEach } from 'min-dash';
/**
* A handler that implements reversible replacing of shapes.
* Internally the old shape will be removed and the new shape will be added.
*
*
* @class
* @constructor
*
* @param {canvas} Canvas
*/
export default function ReplaceShapeHandler(modeling, rules) {
this._modeling = modeling;
this._rules = rules;
}
ReplaceShapeHandler.$inject = [ 'modeling', 'rules' ];
// api //////////////////////
/**
* Replaces a shape with an replacement Element.
*
* The newData object should contain type, x, y.
*
* If possible also the incoming/outgoing connection
* will be restored.
*
* @param {Object} context
*/
ReplaceShapeHandler.prototype.preExecute = function(context) {
var self = this,
modeling = this._modeling,
rules = this._rules;
var oldShape = context.oldShape,
newData = context.newData,
hints = context.hints,
newShape;
function canReconnect(type, source, target, connection) {
return rules.allowed(type, {
source: source,
target: target,
connection: connection
});
}
// (1) place a new shape at the given position
var position = {
x: newData.x,
y: newData.y
};
newShape = context.newShape = context.newShape || self.createShape(newData, position, oldShape.parent);
// (2) update the host
if (oldShape.host) {
modeling.updateAttachment(newShape, oldShape.host);
}
// (3) adopt all children from the old shape
var children;
if (hints.moveChildren !== false) {
children = oldShape.children.slice();
modeling.moveElements(children, { x: 0, y: 0 }, newShape);
}
// (4) reconnect connections to the new shape (where allowed)
var incoming = oldShape.incoming.slice(),
outgoing = oldShape.outgoing.slice();
forEach(incoming, function(connection) {
var waypoints = connection.waypoints,
docking = waypoints[waypoints.length - 1],
source = connection.source,
allowed = canReconnect('connection.reconnectEnd', source, newShape, connection);
Eif (allowed) {
self.reconnectEnd(connection, newShape, docking);
}
});
forEach(outgoing, function(connection) {
var waypoints = connection.waypoints,
docking = waypoints[0],
target = connection.target,
allowed = canReconnect('connection.reconnectStart', newShape, target, connection);
Eif (allowed) {
self.reconnectStart(connection, newShape, docking);
}
});
};
ReplaceShapeHandler.prototype.postExecute = function(context) {
var modeling = this._modeling;
var oldShape = context.oldShape,
newShape = context.newShape;
// if an element gets resized on replace, layout the connection again
forEach(newShape.incoming, function(c) {
modeling.layoutConnection(c, { endChanged: true });
});
forEach(newShape.outgoing, function(c) {
modeling.layoutConnection(c, { startChanged: true });
});
modeling.removeShape(oldShape);
};
ReplaceShapeHandler.prototype.execute = function(context) {};
ReplaceShapeHandler.prototype.revert = function(context) {};
ReplaceShapeHandler.prototype.createShape = function(shape, position, target) {
var modeling = this._modeling;
return modeling.createShape(shape, position, target);
};
ReplaceShapeHandler.prototype.reconnectStart = function(connection, newSource, dockingPoint) {
var modeling = this._modeling;
modeling.reconnectStart(connection, newSource, dockingPoint);
};
ReplaceShapeHandler.prototype.reconnectEnd = function(connection, newTarget, dockingPoint) {
var modeling = this._modeling;
modeling.reconnectEnd(connection, newTarget, dockingPoint);
};
|