forked from bptlab/fCM-design-support
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OlcModeler.js
414 lines (343 loc) · 13.5 KB
/
OlcModeler.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import inherits from 'inherits';
import {findIndex, groupBy, without} from 'min-dash'
import Diagram from 'diagram-js';
import ConnectModule from 'diagram-js/lib/features/connect';
import ConnectionPreviewModule from 'diagram-js/lib/features/connection-preview';
import ContextPadModule from 'diagram-js/lib/features/context-pad';
import CreateModule from 'diagram-js/lib/features/create';
import LassoToolModule from 'diagram-js/lib/features/lasso-tool';
import ModelingModule from 'diagram-js/lib/features/modeling';
import MoveCanvasModule from 'diagram-js/lib/navigation/movecanvas';
import MoveModule from 'diagram-js/lib/features/move';
import OutlineModule from 'diagram-js/lib/features/outline';
import PaletteModule from 'diagram-js/lib/features/palette';
// import ResizeModule from 'diagram-js/lib/features/resize';
import RulesModule from 'diagram-js/lib/features/rules';
import SelectionModule from 'diagram-js/lib/features/selection';
import ZoomScrollModule from 'diagram-js/lib/navigation/zoomscroll';
import EditorActionsModule from '../common/editor-actions';
import CopyPasteModule from 'diagram-js/lib/features/copy-paste';
import KeyboardModule from '../common/keyboard';
import OlcPaletteModule from './palette';
import OlcDrawModule from './draw';
import OlcRulesModule from './rules';
import OlcModelingModule from './modeling';
import OlcButtonBarModule from './buttonbar';
import OlcAutoPlaceModule from './auto-place';
import OlcModdle from './moddle';
import OlcEvents from './OlcEvents';
import {is, nextPosition, root} from '../util/Util';
var emptyDiagram =
`<?xml version="1.0" encoding="UTF-8"?>
<olc:definitions xmlns:olc="http://bptlab/schema/olc" xmlns:olcDi="http://bptlab/schema/olcDi">
</olc:definitions>`;
var exampleDiagram =
`<?xml version="1.0" encoding="UTF-8"?>
<olc:definitions xmlns:olc="http://bptlab/schema/olc" xmlns:olcDi="http://bptlab/schema/olcDi">
<olc:olc id="MainOlc" name="Olc Uno">
<olc:state name="Foo" id="State_0" type="olc:State" x="119" y="177" />
<olc:state name="Bar" id="State_1" type="olc:State" x="289" y="176" />
<olc:transition id="Transition_1" sourceState="State_0" targetState="State_1" type="olc:Transition" />
</olc:olc>
<olc:olc id="SecondOlc" name="Olc Dos">
<olc:state name="Boo" id="State_3" type="olc:State" x="119" y="177" />
<olc:state name="Klar" id="State_4" type="olc:State" x="289" y="176" />
<olc:transition id="Transition_2" sourceState="State_3" targetState="State_4" type="olc:Transition" />
</olc:olc>
</olc:definitions>`;
/**
* Our editor constructor
*
* @param { { container: Element, additionalModules?: Array<any> } } options
*
* @return {Diagram}
*/
export default function OlcModeler(options) {
const {
container,
additionalModules = [],
keyboard
} = options;
// default modules provided by the toolbox
const builtinModules = [
ConnectModule,
ConnectionPreviewModule,
ContextPadModule,
CreateModule,
LassoToolModule,
ModelingModule,
MoveCanvasModule,
MoveModule,
OutlineModule,
PaletteModule,
RulesModule,
SelectionModule,
ZoomScrollModule,
EditorActionsModule,
KeyboardModule,
CopyPasteModule
];
// our own modules, contributing controls, customizations, and more
const customModules = [
OlcPaletteModule,
OlcDrawModule,
OlcRulesModule,
OlcModelingModule,
OlcButtonBarModule,
OlcAutoPlaceModule,
{
moddle: ['value', new OlcModdle({})],
olcModeler: ['value', this]
}
];
const diagramOptions = {
canvas: {
container
},
keyboard,
modules: [
...builtinModules,
...customModules,
...additionalModules
]
};
Diagram.call(this, diagramOptions);
this.get('eventBus').fire('attach'); // Needed for key listeners to work
// Hide canvas when no olc is available
this.get('eventBus').on(OlcEvents.DEFINITIONS_CHANGED, event => {
const container = this.get('canvas').getContainer();
const shouldBeVisible = event.definitions.get('olcs').length !== 0;
const currentVisibility = container.style.visibility;
if (!currentVisibility || (shouldBeVisible !== (currentVisibility !== 'hidden'))) {
if (shouldBeVisible) {
container.style.visibility = '';
} else {
container.style.visibility = 'hidden';
}
}
});
}
inherits(OlcModeler, Diagram);
OlcModeler.prototype.id = "OLC";
OlcModeler.prototype.rank = 7;
OlcModeler.prototype.name = function (constructionMode) {
if (constructionMode) {
return "OLCs";
} else {
return "OLCs";
}
};
OlcModeler.prototype.createNew = function () {
return this.importXML(emptyDiagram);
}
OlcModeler.prototype.importXML = function (xml) {
var self = this;
return new Promise(function (resolve, reject) {
// hook in pre-parse listeners +
// allow xml manipulation
xml = self._emit('import.parse.start', {xml: xml}) || xml;
self.get('moddle').fromXML(xml).then(function (result) {
var definitions = result.rootElement;
var references = result.references;
var parseWarnings = result.warnings;
var elementsById = result.elementsById;
var context = {
references: references,
elementsById: elementsById,
warnings: parseWarnings
};
for (let id in elementsById) {
self.get('elementFactory')._ids.claim(id, elementsById[id]);
}
// hook in post parse listeners +
// allow definitions manipulation
definitions = self._emit('import.parse.complete', {
definitions: definitions,
context: context
}) || definitions;
self.importDefinitions(definitions);
self._emit('import.done', {error: null, warnings: null});
resolve();
}).catch(function (err) {
self._emit('import.parse.failed', {
error: err
});
self._emit('import.done', {error: err, warnings: err.warnings});
return reject(err);
});
});
};
//TODO handle errors during import
OlcModeler.prototype.importDefinitions = function (definitions) {
this.get('elementFactory')._ids.clear();
this._definitions = definitions;
this._emit(OlcEvents.DEFINITIONS_CHANGED, {definitions: definitions});
this._emit('import.render.start', {definitions: definitions});
this.showOlc(definitions.olcs[0]);
this._emit('import.render.complete', {});
}
OlcModeler.prototype.showOlc = function (olc) {
this.clear();
this._olc = olc;
if (olc) {
const elementFactory = this.get('elementFactory');
var diagramRoot = elementFactory.createRoot({type: 'olc:Olc', businessObject: olc});
const canvas = this.get('canvas');
canvas.setRootElement(diagramRoot);
var elements = groupBy(olc.get('Elements'), element => element.$type);
var states = {};
(elements['olc:State'] || []).forEach(state => {
var stateVisual = elementFactory.createShape({
type: 'olc:State',
businessObject: state,
x: parseInt(state.get('x')),
y: parseInt(state.get('y'))
});
states[state.get('id')] = stateVisual;
canvas.addShape(stateVisual, diagramRoot);
});
(elements['olc:Transition'] || []).forEach(transition => {
var source = states[transition.get('sourceState').get('id')];
var target = states[transition.get('targetState').get('id')];
var transitionVisual = elementFactory.createConnection({
type: 'olc:Transition',
businessObject: transition,
source: source,
target: target,
waypoints: this.get('olcUpdater').connectionWaypoints(source, target)
});
canvas.addConnection(transitionVisual, diagramRoot);
});
}
this._emit(OlcEvents.SELECTED_OLC_CHANGED, {olc: olc});
}
OlcModeler.prototype.showOlcById = function (id) {
if (id && this._definitions && id !== (this._olc && this._olc.get('id'))) {
var olc = this._definitions.get('olcs').filter(olc => olc.get('id') === id)[0];
if (olc) {
this.showOlc(olc);
} else {
throw 'Unknown olc with class id \"' + id + '\"';
}
}
}
OlcModeler.prototype.addOlc = function (clazz) {
var olc = this.get('elementFactory').createBusinessObject('olc:Olc', {
name: clazz.name || '<TBD>',
classRef: clazz
});
this._definitions.get('olcs').push(olc);
this._emit(OlcEvents.DEFINITIONS_CHANGED, {definitions: this._definitions});
this.showOlc(olc);
}
OlcModeler.prototype.getCurrentOlc = function () {
return this._olc;
}
OlcModeler.prototype.deleteCurrentOlc = function () {
this.deleteOlc(this._olc.classRef);
}
OlcModeler.prototype.deleteOlc = function (clazz) {
var olc = this.getOlcByClass(clazz);
var currentIndex = findIndex(this._definitions.olcs, olc);
var indexAfterRemoval = Math.min(currentIndex, this._definitions.olcs.length - 2);
this._definitions.olcs = without(this._definitions.olcs, olc);
this._emit(OlcEvents.DEFINITIONS_CHANGED, {definitions: this._definitions});
if (olc === this._olc) {
this.showOlc(this._definitions.olcs[indexAfterRemoval]);
}
}
OlcModeler.prototype.renameOlc = function (name, clazz) {
const olc = this.getOlcByClass(clazz);
olc.name = name;
this._emit(OlcEvents.DEFINITIONS_CHANGED, {definitions: this._definitions});
}
OlcModeler.prototype.getOlcById = function (id) {
return this.getOlcs().filter(olc => olc.id === id)[0];
}
OlcModeler.prototype.getOlcByClass = function (clazz) {
const olc = this.getOlcs().filter(olc => olc.classRef === clazz)[0];
if (!olc) {
throw 'Unknown olc of class \"' + clazz.name + '\"';
} else {
return olc;
}
}
OlcModeler.prototype.getStateById = function (id) {
return this.getOlcs().flatMap(olc => olc.get('Elements')).filter(element => is(element, 'olc:State')).filter(state => state.id === id)[0];
}
OlcModeler.prototype.getOlcs = function () {
return this._definitions.get('olcs');
}
OlcModeler.prototype.createState = function (name, olc) {
this.showOlcById(olc.id);
const modeling = this.get('modeling');
const canvas = this.get('canvas');
const diagramRoot = canvas.getRootElement();
const {x, y} = nextPosition(this, 'olc:State');
const shape = modeling.createShape({
type: 'olc:State',
name: name,
x: parseInt(x),
y: parseInt(y)
}, {x, y}, diagramRoot);
return shape.businessObject;
}
OlcModeler.prototype.createTransition = function (sourceState, targetState) {
this.showOlcById(root(sourceState).id);
const modeling = this.get('modeling');
const sourceVisual = this.get('elementRegistry').get(sourceState.id);
const targetVisual = this.get('elementRegistry').get(targetState.id);
const transitionVisual = modeling.connect(sourceVisual, targetVisual, {
type: 'olc:Transition',
source: sourceState,
target: targetState,
waypoints: this.get('olcUpdater').connectionWaypoints(sourceState, targetState)
});
return transitionVisual.businessObject;
}
OlcModeler.prototype.saveXML = function (options) {
options = options || {};
var self = this;
var definitions = this._definitions;
return new Promise(function (resolve, reject) {
if (!definitions) {
var err = new Error('no xml loaded');
return reject(err);
}
// allow to fiddle around with definitions
definitions = self._emit('saveXML.start', {
definitions: definitions
}) || definitions;
self.get('moddle').toXML(definitions, options).then(function (result) {
var xml = result.xml;
try {
xml = self._emit('saveXML.serialized', {
error: null,
xml: xml
}) || xml;
self._emit('saveXML.done', {
error: null,
xml: xml
});
} catch (e) {
console.error('error in saveXML life-cycle listener', e);
}
return resolve({xml: xml});
}).catch(function (err) {
return reject(err);
});
});
};
OlcModeler.prototype._emit = function (type, event) {
return this.get('eventBus').fire(type, event);
};
OlcModeler.prototype.ensureElementIsOnCanvas = function (element) {
if (!this.get('elementRegistry').get(element.id)) {
const rootElement = root(element);
if (this.getOlcs().includes(rootElement)) {
this.showOlc(rootElement);
} else {
throw 'Cannot display element. Is not part of a known olc';
}
}
}