forked from bptlab/fCM-design-support
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediator.js
513 lines (442 loc) · 17.7 KB
/
Mediator.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
import inherits from 'inherits';
import { isFunction, without } from 'min-dash';
import { is } from '../util/Util';
import OlcEvents from '../olcmodeler/OlcEvents';
import { namespace, root } from '../util/Util';
import AbstractHook from './AbstractHook';
import CommonEvents from '../common/CommonEvents';
const DEFAULT_EVENT_PRIORITY = 1000; //From diagram-js/lib/core/EventBus.DEFAULT_PRIORITY
// Test: var a = new Mediator(); var b = new Mediator; assert new a.XYHook().mediator === a;
// a = new Mediator(); b = new Mediator(); new a.foobar().mediator === a
export default function Mediator() {
var self = this;
this._hooks = [];
for (let propName in this) {
let prototypeProp = this[propName];
if (typeof prototypeProp === 'function' && prototypeProp.isHook) {
this[propName] = function (...args) {
if (new.target) {
this.mediator = self;
this.name = propName;
}
const callresult = prototypeProp.call(this, ...args);
if (new.target) {
this.mediator.handleHookCreated(this);
}
return callresult;
}
this[propName].$inject = prototypeProp.$inject;
this[propName].isHook = true;
inherits(this[propName], prototypeProp);
}
}
this._executed = [];
this._on = [];
//Propagate mouse events in order to defocus elements and close menus
this.on(['element.mousedown', 'element.mouseup', 'element.click'], DEFAULT_EVENT_PRIORITY - 1, (event, data, hook) => {
if (!event.handledByMediator) {
const { originalEvent, element } = event;
without(this.getHooks(), hook).forEach(propagateHook => {
propagateHook.eventBus?.fire(event.type, { originalEvent, element, handledByMediator: true });
});
} else {
// Do not propagate handle these events by low priority listeners such as canvas-move
event.cancelBubble = true;
}
});
this.on(CommonEvents.DATACLASS_CREATION_REQUESTED, event => {
return this.createDataclass(event.name);
});
this.on(CommonEvents.STATE_CREATION_REQUESTED, event => {
return this.createState(event.name, event.olc);
});
}
Mediator.prototype.getHooks = function () {
return this._hooks;
}
Mediator.prototype.getModelers = function () {
return this.getHooks().map(hook => hook.modeler);
}
Mediator.prototype.handleHookCreated = function (hook) {
this._hooks.push(hook);
this._executed.forEach(({events, callback}) => {
if (hook.executed) {
hook.executed(events, callback);
}
});
this._on.forEach(({events, priority, callback}) => {
hook.eventBus?.on(events, priority, wrapCallback(callback, hook));
});
}
Mediator.prototype.executed = function(events, callback) {
this._executed.push({events, callback});
this.getHooks().forEach(hook => {
if (hook.executed) {
hook.executed(events, callback);
}
});
}
Mediator.prototype.on = function(events, priority, callback) {
if (isFunction(priority)) {
callback = priority;
priority = DEFAULT_EVENT_PRIORITY;
}
this._on.push({events, priority, callback});
this.getHooks().forEach(hook => {
hook.eventBus?.on(events, priority, wrapCallback(callback, hook));
});
}
function wrapCallback(callback, hook) {
return (...args) => callback(...args, hook);
}
Mediator.prototype.addedClass = function (clazz) {
this.olcModelerHook.modeler.addOlc(clazz);
}
Mediator.prototype.confirmClassDeletion = function (clazz) {
const affectedInitialObjects = this.objectiveModelerHook.modeler.getObjectsOfClass(clazz);
const affectedStates = this.olcModelerHook.modeler.getOlcByClass(clazz).get('Elements').filter(element => is(element, 'olc:State'));
const affectedDataObjectReferences = this.fragmentModelerHook.modeler.getDataObjectReferencesOfClass(clazz);
return confirm('Do you really want to delete class \"' + clazz.name + '\" ?' + '\n'
+ affectedInitialObjects.length + ' initial object(s), '
+ affectedStates.length + ' olc state(s), and '
+ affectedDataObjectReferences.length + ' data object reference(s) would be deleted as well.');
}
Mediator.prototype.deletedClass = function (clazz) {
this.fragmentModelerHook.modeler.handleClassDeleted(clazz);
this.olcModelerHook.modeler.deleteOlc(clazz);
this.objectiveModelerHook.modeler.handleClassDeleted(clazz);
}
Mediator.prototype.renamedClass = function (clazz) {
this.olcModelerHook.modeler.renameOlc(clazz.name, clazz);
this.fragmentModelerHook.modeler.handleClassRenamed(clazz);
this.objectiveModelerHook.modeler.handleClassRenamed(clazz);
}
Mediator.prototype.addedState = function (olcState) {
}
Mediator.prototype.confirmStateDeletion = function (olcState) {
const affectedInitialObjects = this.objectiveModelerHook.modeler.getObjectsInState(olcState);
const affectedDataObjectReferences = this.fragmentModelerHook.modeler.getDataObjectReferencesInState(olcState);
return confirm('Do you really want to delete state \"' + olcState.name + '\" ?' + '\n'
+ 'It would be removed from '
+ affectedInitialObjects.length + ' initial object(s), and '
+ affectedDataObjectReferences.length + ' data object reference(s).');
}
Mediator.prototype.deletedState = function (olcState) {
this.objectiveModelerHook.modeler.handleStateDeleted(olcState);
this.fragmentModelerHook.modeler.handleStateDeleted(olcState);
}
Mediator.prototype.renamedState = function (olcState) {
this.objectiveModelerHook.modeler.handleStateRenamed(olcState);
this.fragmentModelerHook.modeler.handleStateRenamed(olcState);
}
Mediator.prototype.olcListChanged = function (olcs) {
this.objectiveModelerHook.modeler.handleOlcListChanged(olcs);
this.fragmentModelerHook.modeler.handleOlcListChanged(olcs);
}
Mediator.prototype.olcRenamed = function (olc, name) {
this.dataModelerHook.modeler.renameClass(olc.classRef, name);
}
Mediator.prototype.olcDeletionRequested = function (olc) {
const clazz = olc.classRef;
if (this.confirmClassDeletion(clazz)) {
this.dataModelerHook.modeler.deleteClass(clazz);
}
}
Mediator.prototype.createState = function (name, olc) {
const state = this.olcModelerHook.modeler.createState(name, olc);
this.olcModelerHook.focusElement(state);
return state;
}
Mediator.prototype.createDataclass = function (name) {
const clazz = this.dataModelerHook.modeler.createDataclass(name);
this.dataModelerHook.focusElement(clazz);
return clazz;
}
Mediator.prototype.focusElement = function(element) {
const hook = this.getHookForElement(element);
const modeler = hook.modeler;
this.focus(modeler);
if (element !== hook.getRootObject()) {
hook.focusElement(element);
}
}
Mediator.prototype.getHookForElement = function(element) {
const elementNamespace = namespace(element);
const modelers = this.getHooks().filter(hook => hook.getNamespace() === elementNamespace);
if (modelers.length !== 1) {
throw new Error('Modeler for element '+element+' was not unique or present: '+modelers);
}
return modelers[0];
}
// === Objective model helpers
Mediator.prototype.createInstance = function (name, clazz) {
const instance = this.objectiveModelerHook.modeler.createInstance(name, clazz);
return instance;
}
// === Olc Modeler Hook
Mediator.prototype.OlcModelerHook = function (eventBus, olcModeler) {
CommandInterceptor.call(this, eventBus);
AbstractHook.call(this, olcModeler, 'OLCs', 'https://github.com/bptlab/fCM-design-support/wiki/Object-Lifecycle-(OLC)');
this.mediator.olcModelerHook = this;
this.eventBus = eventBus;
this.executed([
'shape.create'
], event => {
if (is(event.context.shape, 'olc:State')) {
this.mediator.addedState(event.context.shape.businessObject);
}
});
this.executed([
'shape.delete'
], event => {
if (is(event.context.shape, 'olc:State')) {
this.mediator.deletedState(event.context.shape.businessObject);
}
});
this.preExecute([
'elements.delete'
], event => {
event.context.elements = event.context.elements.filter(element => {
if (is(element, 'olc:State')) {
return this.mediator.confirmStateDeletion(element.businessObject);
} else {
return true;
}
});
});
this.executed([
'element.updateLabel'
], event => {
if (is(event.context.element, 'olc:State')) {
this.mediator.renamedState(event.context.element.businessObject);
}
});
this.reverted([
'element.updateLabel'
], event => {
if (is(event.context.element, 'olc:State')) {
this.mediator.renamedState(event.context.element.businessObject);
}
});
eventBus.on(OlcEvents.DEFINITIONS_CHANGED, event => {
this.mediator.olcListChanged(event.definitions.olcs);
});
eventBus.on(OlcEvents.OLC_RENAME, event => {
this.mediator.olcRenamed(event.olc, event.name);
});
eventBus.on(OlcEvents.OLC_DELETION_REQUESTED, event => {
this.mediator.olcDeletionRequested(event.olc);
return false; // Deletion should never be directly done in olc modeler, will instead propagate from data modeler
});
eventBus.on('import.parse.complete', ({context}) => {
context.warnings.filter(({message}) => message.startsWith('unresolved reference')).forEach(({property, value, element}) => {
if (property === 'olc:classRef') {
const dataClass = this.mediator.dataModelerHook.modeler.get('elementRegistry').get(value).businessObject;
if (!dataClass) { throw new Error('Could not resolve data class with id '+value); }
element.classRef = dataClass;
}
});
});
this.locationOfElement = function(element) {
return 'Olc ' + root(element).name;
}
}
inherits(Mediator.prototype.OlcModelerHook, CommandInterceptor);
Mediator.prototype.OlcModelerHook.$inject = [
'eventBus',
'olcModeler'
];
Mediator.prototype.OlcModelerHook.isHook = true;
// === Data Modeler Hook
Mediator.prototype.DataModelerHook = function (eventBus, dataModeler) {
CommandInterceptor.call(this, eventBus);
AbstractHook.call(this, dataModeler, 'Data Model' ,'https://github.com/bptlab/fCM-design-support/wiki/Data-Model');
this.mediator.dataModelerHook = this;
this.eventBus = eventBus;
this.executed([
'shape.create'
], event => {
if (is(event.context.shape, 'od:Class')) {
this.mediator.addedClass(event.context.shape.businessObject);
}
});
this.reverted([
'shape.create'
], event => {
if (is(event.context.shape, 'od:Class')) {
console.log(event);
//this.mediator.addedState(event.context.shape.businessObject);
}
});
this.executed([
'shape.delete'
], event => {
if (is(event.context.shape, 'od:Class')) {
this.mediator.deletedClass(event.context.shape.businessObject);
}
});
this.reverted([
'shape.delete'
], event => {
if (is(event.context.shape, 'od:Class')) {
console.log(event);
//this.mediator.deletedState(event.context.shape.businessObject);
}
});
this.preExecute([
'elements.delete'
], event => {
event.context.elements = event.context.elements.filter(element => {
if (is(element, 'od:Class')) {
return this.mediator.confirmClassDeletion(element.businessObject);
} else {
return true;
}
});
});
this.executed([
'element.updateLabel'
], event => {
var changedLabel = event.context.element.businessObject.labelAttribute;
if (is(event.context.element, 'od:Class') && (changedLabel === 'name' || !changedLabel)) {
this.mediator.renamedClass(event.context.element.businessObject);
}
});
this.reverted([
'element.updateLabel'
], event => {
var changedLabel = event.context.element.businessObject.labelAttribute;
if (is(event.context.element, 'od:Class') && (changedLabel === 'name' || !changedLabel)) {
this.mediator.renamedClass(event.context.element.businessObject);
}
});
}
inherits(Mediator.prototype.DataModelerHook, CommandInterceptor);
Mediator.prototype.DataModelerHook.$inject = [
'eventBus',
'dataModeler'
];
Mediator.prototype.DataModelerHook.isHook = true;
// === Fragment Modeler Hook
Mediator.prototype.FragmentModelerHook = function (eventBus, fragmentModeler) {
CommandInterceptor.call(this, eventBus);
AbstractHook.call(this, fragmentModeler, 'Fragments', 'https://github.com/bptlab/fCM-design-support/wiki/Fragments');
this.mediator.fragmentModelerHook = this;
this.eventBus = eventBus;
eventBus.on('import.parse.complete', ({warnings}) => {
warnings.filter(({message}) => message.startsWith('unresolved reference')).forEach(({property, value, element}) => {
if (property === 'fcm:dataclass') {
const dataClass = this.mediator.dataModelerHook.modeler.get('elementRegistry').get(value).businessObject;
if (!dataClass) { throw new Error('Could not resolve data class with id '+value); }
element.dataclass = dataClass;
} else if (property === 'fcm:states') {
const state = this.mediator.olcModelerHook.modeler.getStateById(value)
if (!state) { throw new Error('Could not resolve olc state with id '+value); }
element.get('states').push(state);
}
});
});
}
inherits(Mediator.prototype.FragmentModelerHook, CommandInterceptor);
Mediator.prototype.FragmentModelerHook.$inject = [
'eventBus',
'fragmentModeler'
];
Mediator.prototype.FragmentModelerHook.isHook = true;
// === Objective Modeler Hook
Mediator.prototype.ObjectiveModelerHook = function (eventBus, objectiveModeler) {
CommandInterceptor.call(this, eventBus);
AbstractHook.call(this, objectiveModeler, 'Objective Model', 'https://github.com/Noel-Bastubbe/for-Construction-Modeling/wiki/Objective-Modeler');
this.mediator.objectiveModelerHook = this;
this.eventBus = eventBus;
this.executed([
'shape.create'
], event => {
if (is(event.context.shape, 'om:Object')) {
//this.mediator.addedClass(event.context.shape.businessObject);
}
});
this.reverted([
'shape.create'
], event => {
if (is(event.context.shape, 'om:Object')) {
console.log(event);
//this.mediator.addedState(event.context.shape.businessObject);
}
});
this.executed([
'shape.delete'
], event => {
if (is(event.context.shape, 'om:Object')) {
//this.mediator.deletedClass(event.context.shape.businessObject);
}
});
this.reverted([
'shape.delete'
], event => {
if (is(event.context.shape, 'om:Object')) {
console.log(event);
//this.mediator.deletedState(event.context.shape.businessObject);
}
});
this.preExecute([
'elements.delete'
], event => {
event.context.elements = event.context.elements.filter(element => {
if (is(element, 'om:Object')) {
return this.modeler.deleteObject(element);
} else {
return true;
}
});
});
this.executed([
'element.updateLabel'
], event => {
var changedLabel = event.context.element.businessObject.labelAttribute;
if (is(event.context.element, 'om:Object') && (changedLabel === 'name' || !changedLabel)) {
//this.mediator.renamedClass(event.context.element.businessObject);
}
});
this.reverted([
'element.updateLabel'
], event => {
var changedLabel = event.context.element.businessObject.labelAttribute;
if (is(event.context.element, 'om:Object') && (changedLabel === 'name' || !changedLabel)) {
//this.mediator.renamedClass(event.context.element.businessObject);
}
});
eventBus.on("import.parse.complete", ({context}) => {
context.warnings
.filter(({message}) => message.startsWith("unresolved reference"))
.forEach(({property, value, element}) => {
if (property === "om:classRef") {
const dataClass = this.mediator.dataModelerHook.modeler
.get("elementRegistry")
.get(value).businessObject;
if (!dataClass) {
throw new Error("Could not resolve data class with id " + value);
}
element.classRef = dataClass;
}
if (property === "om:states") {
const state = this.mediator.olcModelerHook.modeler.getStateById(value);
if (!state) {
throw new Error("Could not resolve state with id " + value);
}
if (element.states) {
element.states.push(state);
} else {
element.states = [state];
}
}
});
});
}
inherits(Mediator.prototype.ObjectiveModelerHook, CommandInterceptor);
Mediator.prototype.ObjectiveModelerHook.$inject = [
'eventBus',
'objectiveModeler'
];
Mediator.prototype.ObjectiveModelerHook.isHook = true;