-
Notifications
You must be signed in to change notification settings - Fork 418
/
ElementRegistry.js
278 lines (229 loc) · 6.17 KB
/
ElementRegistry.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
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
var ELEMENT_ID = 'data-element-id';
import { attr as svgAttr } from 'tiny-svg';
/**
* @typedef {import('./Types').ElementLike} ElementLike
*
* @typedef {import('./EventBus').default} EventBus
*
* @typedef { (element: ElementLike, gfx: SVGElement) => boolean|any } ElementRegistryFilterCallback
* @typedef { (element: ElementLike, gfx: SVGElement) => any } ElementRegistryForEachCallback
*/
/**
* A registry that keeps track of all shapes in the diagram.
*
* @class
* @constructor
*
* @param {EventBus} eventBus
*/
export default function ElementRegistry(eventBus) {
/**
* @type { {
* [id: string]: {
* element: ElementLike;
* gfx?: SVGElement;
* secondaryGfx?: SVGElement;
* }
* } }
*/
this._elements = {};
this._eventBus = eventBus;
}
ElementRegistry.$inject = [ 'eventBus' ];
/**
* Add an element and its graphical representation(s) to the registry.
*
* @param {ElementLike} element The element to be added.
* @param {SVGElement} gfx The primary graphical representation.
* @param {SVGElement} [secondaryGfx] The secondary graphical representation.
*/
ElementRegistry.prototype.add = function(element, gfx, secondaryGfx) {
var id = element.id;
this._validateId(id);
// associate dom node with element
svgAttr(gfx, ELEMENT_ID, id);
if (secondaryGfx) {
svgAttr(secondaryGfx, ELEMENT_ID, id);
}
this._elements[id] = { element: element, gfx: gfx, secondaryGfx: secondaryGfx };
};
/**
* Remove an element from the registry.
*
* @param {ElementLike|string} element
*/
ElementRegistry.prototype.remove = function(element) {
var elements = this._elements,
id = element.id || element,
container = id && elements[id];
if (container) {
// unset element id on gfx
svgAttr(container.gfx, ELEMENT_ID, '');
if (container.secondaryGfx) {
svgAttr(container.secondaryGfx, ELEMENT_ID, '');
}
delete elements[id];
}
};
/**
* Update an elements ID.
*
* @param {ElementLike|string} element The element or its ID.
* @param {string} newId The new ID.
*/
ElementRegistry.prototype.updateId = function(element, newId) {
this._validateId(newId);
if (typeof element === 'string') {
element = this.get(element);
}
this._eventBus.fire('element.updateId', {
element: element,
newId: newId
});
var gfx = this.getGraphics(element),
secondaryGfx = this.getGraphics(element, true);
this.remove(element);
element.id = newId;
this.add(element, gfx, secondaryGfx);
};
/**
* Update the graphical representation of an element.
*
* @param {ElementLike|string} filter The element or its ID.
* @param {SVGElement} gfx The new graphical representation.
* @param {boolean} [secondary=false] Whether to update the secondary graphical representation.
*/
ElementRegistry.prototype.updateGraphics = function(filter, gfx, secondary) {
var id = filter.id || filter;
var container = this._elements[id];
if (secondary) {
container.secondaryGfx = gfx;
} else {
container.gfx = gfx;
}
if (gfx) {
svgAttr(gfx, ELEMENT_ID, id);
}
return gfx;
};
/**
* Get the element with the given ID or graphical representation.
*
* @example
*
* ```javascript
* elementRegistry.get('SomeElementId_1');
*
* elementRegistry.get(gfx);
* ```
*
* @param {string|SVGElement} filter The elements ID or graphical representation.
*
* @return {ElementLike|undefined} The element.
*/
ElementRegistry.prototype.get = function(filter) {
var id;
if (typeof filter === 'string') {
id = filter;
} else {
id = filter && svgAttr(filter, ELEMENT_ID);
}
var container = this._elements[id];
return container && container.element;
};
/**
* Return all elements that match a given filter function.
*
* @param {ElementRegistryFilterCallback} fn The filter function.
*
* @return {ElementLike[]} The matching elements.
*/
ElementRegistry.prototype.filter = function(fn) {
var filtered = [];
this.forEach(function(element, gfx) {
if (fn(element, gfx)) {
filtered.push(element);
}
});
return filtered;
};
/**
* Return the first element that matches the given filter function.
*
* @param {ElementRegistryFilterCallback} fn The filter function.
*
* @return {ElementLike|undefined} The matching element.
*/
ElementRegistry.prototype.find = function(fn) {
var map = this._elements,
keys = Object.keys(map);
for (var i = 0; i < keys.length; i++) {
var id = keys[i],
container = map[id],
element = container.element,
gfx = container.gfx;
if (fn(element, gfx)) {
return element;
}
}
};
/**
* Get all elements.
*
* @return {ElementLike[]} All elements.
*/
ElementRegistry.prototype.getAll = function() {
return this.filter(function(e) { return e; });
};
/**
* Execute a given function for each element.
*
* @param {ElementRegistryForEachCallback} fn The function to execute.
*/
ElementRegistry.prototype.forEach = function(fn) {
var map = this._elements;
Object.keys(map).forEach(function(id) {
var container = map[id],
element = container.element,
gfx = container.gfx;
return fn(element, gfx);
});
};
/**
* Return the graphical representation of an element.
*
* @example
*
* ```javascript
* elementRegistry.getGraphics('SomeElementId_1');
*
* elementRegistry.getGraphics(rootElement); // <g ...>
*
* elementRegistry.getGraphics(rootElement, true); // <svg ...>
* ```
*
* @param {ElementLike|string} filter The element or its ID.
* @param {boolean} [secondary=false] Whether to return the secondary graphical representation.
*
* @return {SVGElement} The graphical representation.
*/
ElementRegistry.prototype.getGraphics = function(filter, secondary) {
var id = filter.id || filter;
var container = this._elements[id];
return container && (secondary ? container.secondaryGfx : container.gfx);
};
/**
* Validate an ID and throw an error if invalid.
*
* @param {string} id
*
* @throws {Error} Error indicating that the ID is invalid or already assigned.
*/
ElementRegistry.prototype._validateId = function(id) {
if (!id) {
throw new Error('element must have an id');
}
if (this._elements[id]) {
throw new Error('element with id ' + id + ' already added');
}
};