| 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 |
925×
925×
1×
1×
3265×
3265×
2134×
1131×
1131×
331×
331×
331×
3265×
1×
3632×
3632×
3632×
1×
2613×
2613×
2613×
5×
2608×
2613×
2613×
2613×
2613×
2613×
2613×
2613×
2613×
1×
2613×
2613×
1×
628×
628×
628×
1096×
935×
1096×
628×
660×
660×
8×
652×
652×
1825×
1825×
1×
2878×
2878×
1×
93×
93×
1×
770×
770×
1×
94×
94×
1×
3633×
1×
3632×
3632×
2862×
2862×
770×
770×
3632×
12×
3620×
1×
176×
176×
1830×
| import {
forEach,
reduce
} from 'min-dash';
import {
getChildren,
getVisual
} from '../util/GraphicsUtil';
import { translate } from '../util/SvgTransformUtil';
import { clear as domClear } from 'min-dom';
import {
append as svgAppend,
attr as svgAttr,
classes as svgClasses,
create as svgCreate,
remove as svgRemove
} from 'tiny-svg';
/**
* A factory that creates graphical elements
*
* @param {EventBus} eventBus
* @param {ElementRegistry} elementRegistry
*/
export default function GraphicsFactory(eventBus, elementRegistry) {
this._eventBus = eventBus;
this._elementRegistry = elementRegistry;
}
GraphicsFactory.$inject = [ 'eventBus' , 'elementRegistry' ];
GraphicsFactory.prototype._getChildren = function(element) {
var gfx = this._elementRegistry.getGraphics(element);
var childrenGfx;
// root element
if (!element.parent) {
childrenGfx = gfx;
} else {
childrenGfx = getChildren(gfx);
if (!childrenGfx) {
childrenGfx = svgCreate('g');
svgClasses(childrenGfx).add('djs-children');
svgAppend(gfx.parentNode, childrenGfx);
}
}
return childrenGfx;
};
/**
* Clears the graphical representation of the element and returns the
* cleared visual (the <g class="djs-visual" /> element).
*/
GraphicsFactory.prototype._clear = function(gfx) {
var visual = getVisual(gfx);
domClear(visual);
return visual;
};
/**
* Creates a gfx container for shapes and connections
*
* The layout is as follows:
*
* <g class="djs-group">
*
* <!-- the gfx -->
* <g class="djs-element djs-(shape|connection)">
* <g class="djs-visual">
* <!-- the renderer draws in here -->
* </g>
*
* <!-- extensions (overlays, click box, ...) goes here
* </g>
*
* <!-- the gfx child nodes -->
* <g class="djs-children"></g>
* </g>
*
* @param {Object} parent
* @param {String} type the type of the element, i.e. shape | connection
* @param {Number} [parentIndex] position to create container in parent
*/
GraphicsFactory.prototype._createContainer = function(type, childrenGfx, parentIndex) {
var outerGfx = svgCreate('g');
svgClasses(outerGfx).add('djs-group');
// insert node at position
if (typeof parentIndex !== 'undefined') {
prependTo(outerGfx, childrenGfx, childrenGfx.childNodes[parentIndex]);
} else {
svgAppend(childrenGfx, outerGfx);
}
var gfx = svgCreate('g');
svgClasses(gfx).add('djs-element');
svgClasses(gfx).add('djs-' + type);
svgAppend(outerGfx, gfx);
// create visual
var visual = svgCreate('g');
svgClasses(visual).add('djs-visual');
svgAppend(gfx, visual);
return gfx;
};
GraphicsFactory.prototype.create = function(type, element, parentIndex) {
var childrenGfx = this._getChildren(element.parent);
return this._createContainer(type, childrenGfx, parentIndex);
};
GraphicsFactory.prototype.updateContainments = function(elements) {
var self = this,
elementRegistry = this._elementRegistry,
parents;
parents = reduce(elements, function(map, e) {
if (e.parent) {
map[e.parent.id] = e.parent;
}
return map;
}, {});
// update all parents of changed and reorganized their children
// in the correct order (as indicated in our model)
forEach(parents, function(parent) {
var children = parent.children;
if (!children) {
return;
}
var childGfx = self._getChildren(parent);
forEach(children.slice().reverse(), function(c) {
var gfx = elementRegistry.getGraphics(c);
prependTo(gfx.parentNode, childGfx);
});
});
};
GraphicsFactory.prototype.drawShape = function(visual, element) {
var eventBus = this._eventBus;
return eventBus.fire('render.shape', { gfx: visual, element: element });
};
GraphicsFactory.prototype.getShapePath = function(element) {
var eventBus = this._eventBus;
return eventBus.fire('render.getShapePath', element);
};
GraphicsFactory.prototype.drawConnection = function(visual, element) {
var eventBus = this._eventBus;
return eventBus.fire('render.connection', { gfx: visual, element: element });
};
GraphicsFactory.prototype.getConnectionPath = function(waypoints) {
var eventBus = this._eventBus;
return eventBus.fire('render.getConnectionPath', waypoints);
};
GraphicsFactory.prototype.update = function(type, element, gfx) {
// Do not update root element
if (!element.parent) {
return;
}
var visual = this._clear(gfx);
// redraw
if (type === 'shape') {
this.drawShape(visual, element);
// update positioning
translate(gfx, element.x, element.y);
} else
Eif (type === 'connection') {
this.drawConnection(visual, element);
} else {
throw new Error('unknown type: ' + type);
}
if (element.hidden) {
svgAttr(gfx, 'display', 'none');
} else {
svgAttr(gfx, 'display', 'block');
}
};
GraphicsFactory.prototype.remove = function(element) {
var gfx = this._elementRegistry.getGraphics(element);
// remove
svgRemove(gfx.parentNode);
};
// helpers //////////////////////
function prependTo(newNode, parentNode, siblingNode) {
parentNode.insertBefore(newNode, siblingNode || parentNode.firstChild);
}
|