forked from bptlab/fCM-design-support
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOmModeler.js
386 lines (331 loc) · 12.9 KB
/
OmModeler.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
import inherits from 'inherits';
import {findIndex, without} from 'min-dash'
import BaseModeler from './BaseModeler';
import Viewer from './Viewer';
import NavigatedViewer from './NavigatedViewer';
import KeyboardMoveModule from 'diagram-js/lib/navigation/keyboard-move';
import MoveCanvasModule from 'diagram-js/lib/navigation/movecanvas';
import TouchModule from 'diagram-js/lib/navigation/touch';
import ZoomScrollModule from 'diagram-js/lib/navigation/zoomscroll';
import AlignElementsModule from 'diagram-js/lib/features/align-elements';
import AutoplaceModule from './features/auto-place';
import AutoScrollModule from 'diagram-js/lib/features/auto-scroll';
import BendpointsModule from 'diagram-js/lib/features/bendpoints';
import ConnectionPreviewModule from 'diagram-js/lib/features/connection-preview';
import ConnectModule from 'diagram-js/lib/features/connect';
import ContextPadModule from './features/context-pad';
import CopyPasteModule from './features/copy-paste';
import CreateModule from 'diagram-js/lib/features/create';
import EditorActionsModule from '../common/editor-actions';
import GridSnappingModule from './features/grid-snapping';
import KeyboardModule from '../common/keyboard';
import KeyboardMoveSelectionModule from 'diagram-js/lib/features/keyboard-move-selection';
import LabelEditingModule from './features/label-editing';
import ModelingModule from './features/modeling';
import MoveModule from 'diagram-js/lib/features/move';
import ObjectiveEvents from "./ObjectiveEvents";
import OmButtonBarModule from './buttonbar';
import OmObjectDropdown from './omObjectLabelHandling';
import PaletteModule from './features/palette';
import ResizeModule from 'diagram-js/lib/features/resize';
import SnappingModule from './features/snapping';
import SpaceToolBehaviorModule from './behavior';
import {nextPosition} from '../util/Util';
import {is} from "bpmn-js/lib/util/ModelUtil";
var initialDiagram =
`<?xml version="1.0" encoding="UTF-8"?>
<om:definitions xmlns:om="http://tk/schema/od" xmlns:odDi="http://tk/schema/odDi">
<om:odBoard id="Board" />
<odDi:odRootBoard id="StartBoard" name="Start State" objectiveRef="start_state">
<odDi:odPlane id="Plane" boardElement="Board" />
</odDi:odRootBoard>
</om:definitions>`;
export default function OmModeler(options) {
BaseModeler.call(this, options);
}
inherits(OmModeler, BaseModeler);
OmModeler.Viewer = Viewer;
OmModeler.NavigatedViewer = NavigatedViewer;
/**
* The createDiagram result.
*
* @typedef {Object} CreateDiagramResult
*
* @property {Array<string>} warnings
*/
/**
* The createDiagram error.
*
* @typedef {Error} CreateDiagramError
*
* @property {Array<string>} warnings
*/
/**
* Create a new diagram to start modeling.
*
* @returns {Promise<CreateDiagramResult, CreateDiagramError>}
*
*/
OmModeler.prototype.createDiagram = function () {
return this.importXML(initialDiagram);
};
OmModeler.prototype._interactionModules = [
// non-modeling components
KeyboardMoveModule,
MoveCanvasModule,
OmButtonBarModule,
TouchModule,
ZoomScrollModule
];
OmModeler.prototype._modelingModules = [
// modeling components
AutoplaceModule,
AlignElementsModule,
AutoScrollModule,
BendpointsModule,
ConnectModule,
ConnectionPreviewModule,
ContextPadModule,
CopyPasteModule,
CreateModule,
EditorActionsModule,
GridSnappingModule,
KeyboardModule,
KeyboardMoveSelectionModule,
LabelEditingModule,
ModelingModule,
MoveModule,
OmObjectDropdown,
PaletteModule,
ResizeModule,
SnappingModule,
SpaceToolBehaviorModule
];
// modules the modeler is composed of
//
// - viewer modules
// - interaction modules
// - modeling modules
OmModeler.prototype._modules = [].concat(
Viewer.prototype._modules,
OmModeler.prototype._interactionModules,
OmModeler.prototype._modelingModules
);
OmModeler.prototype.id = "OM";
OmModeler.prototype.rank = 3;
OmModeler.prototype.name = function (constructionMode) {
if (constructionMode) {
return "Milestones";
} else {
return "Objective Model";
}
}
OmModeler.prototype.createObject = function (name) {
const modeling = this.get('modeling');
const canvas = this.get('canvas');
const diagramRoot = canvas.getRootElement();
const {x, y} = nextPosition(this, 'om:Object');
const shape = modeling.createShape({
type: 'om:Object',
name: name
}, {x, y}, diagramRoot);
return shape.businessObject;
}
OmModeler.prototype.renameObject = function (object, name) {
this.get('modeling').updateLabel(this.get('elementRegistry').get(object.id), name);
}
OmModeler.prototype.deleteObject = function (object) {
this.get('modeling').removeShape(object);
}
OmModeler.prototype.updateProperty = function (object, property) {
this.get('modeling').updateProperties(object, property);
}
OmModeler.prototype.getObjectives = function () {
return this._definitions.get('rootBoards');
}
OmModeler.prototype.showObjective = function (objective) {
const container = this.get('canvas').getContainer();
this._objective = objective;
this.clear();
if (objective) {
container.style.visibility = '';
this.open(objective);
} else {
container.style.visibility = 'hidden';
}
}
OmModeler.prototype.getCurrentObjective = function () {
return this._objective;
}
OmModeler.prototype.addObjective = function (objectiveReference) {
var rootBoard = this.get('elementFactory').createRootBoard(objectiveReference.name || 'undefined', objectiveReference);
this._definitions.get('rootBoards').push(rootBoard[0]);
this._definitions.get('rootElements').push(rootBoard[1]);
this._emit(ObjectiveEvents.DEFINITIONS_CHANGED, {definitions: this._definitions});
this.showObjective(rootBoard[0]);
}
OmModeler.prototype.deleteObjective = function (objectiveReference) {
var objective = this.getObjectiveByReference(objectiveReference);
if (objective.id !== 'StartBoard') {
var currentIndex = findIndex(this._definitions.get('rootElements'), objective.plane.boardElement);
this._definitions.get('rootElements').splice(currentIndex, 1);
currentIndex = findIndex(this._definitions.get('rootBoards'), objective);
var indexAfterRemoval = Math.min(currentIndex, this._definitions.get('rootBoards').length - 2);
this._definitions.get('rootBoards').splice(currentIndex, 1);
this._emit(ObjectiveEvents.DEFINITIONS_CHANGED, {definitions: this._definitions});
if (this.getCurrentObjective() === objective) {
this.showObjective(this._definitions.get('rootBoards')[indexAfterRemoval]);
}
}
}
OmModeler.prototype.renameObjective = function (objectiveReference, name) {
var objective = this.getObjectiveByReference(objectiveReference);
objective.name = name;
this._emit(ObjectiveEvents.DEFINITIONS_CHANGED, {definitions: this._definitions});
}
OmModeler.prototype.createInstance = function (name, clazz) {
const objectInstance = this.get('elementFactory').createObjectInstance(name, clazz);
this._definitions.get('objectInstances').push(objectInstance);
return objectInstance;
}
OmModeler.prototype.renameInstance = function (instance, name) {
instance.name = name;
this.getVisualsWithInstance(instance).forEach(element => {
this.get('eventBus').fire('element.changed', {
element
})
});
}
OmModeler.prototype.deleteInstance = function (instance) {
let changedVisuals = this.getVisualsWithInstance(instance);
this.getObjectsWithInstance(instance).forEach(element => {
element.instance = undefined;
});
changedVisuals.forEach(element =>
this.get('eventBus').fire('element.changed', {
element
})
);
let instances = this._definitions.get('objectInstances');
let index = instances.indexOf(instance);
if (index > -1) {
instances.splice(index, 1);
}
}
OmModeler.prototype.handleOlcListChanged = function (olcs, dryRun = false) {
this._olcs = olcs;
}
OmModeler.prototype.handleStateRenamed = function (olcState) {
this.getVisualsInState(olcState).forEach(element =>
this.get('eventBus').fire('element.changed', {
element
})
);
}
OmModeler.prototype.handleStateDeleted = function (olcState) {
let changedVisual = this.getVisualsInState(olcState);
this.getObjectsInState(olcState).forEach(element => {
element.states = without(element.states, olcState);
});
changedVisual.forEach(element =>
this.get('eventBus').fire('element.changed', {
element
})
);
}
OmModeler.prototype.handleClassRenamed = function (clazz) {
this.getVisualsOfClass(clazz).forEach(element => {
this.get('eventBus').fire('element.changed', {
element
})
});
}
OmModeler.prototype.handleClassDeleted = function (clazz) {
let objectives = this._definitions.get('rootElements');
objectives.forEach(objective => {
let elements = objective.get('boardElements');
for (let i = 0; i < elements.length; i++) {
if (this.isObjectOfDeletedClass(clazz, elements[i]) || this.isLinkConnectedToObjectOfDeletedClass(clazz, elements[i])) {
elements.splice(i, 1);
i--;
}
}
})
let instances = this._definitions.get('objectInstances');
for (let i = 0; i < instances.length; i++) {
if (clazz.id && instances[i].classRef?.id === clazz.id) {
instances.splice(i, 1);
i--;
}
}
this.showObjective(this.getCurrentObjective());
// This is needed to update the visual representation of the objective that is currently loaded.
// This may need to be adapted once the error of links between deleted objects is resolved
}
OmModeler.prototype.getVisualsInState = function (olcState) {
return this.get('elementRegistry').filter(element =>
is(element, 'om:Object') &&
olcState.id &&
element.businessObject.states?.some(state => state.id === olcState.id)
);
}
OmModeler.prototype.getObjectsInState = function (olcState) {
let objectives = this._definitions.get('rootElements');
let objects = objectives.map(objective => objective.get('boardElements')).flat(1).filter((element) =>
is(element, 'om:Object') &&
olcState.id &&
element.states?.some(state => state.id === olcState.id));
return objects;
}
OmModeler.prototype.getVisualsOfClass = function (clazz) {
return this.get('elementRegistry').filter(element =>
is(element, 'om:Object') &&
clazz.id &&
element.businessObject.classRef?.id === clazz.id
);
}
OmModeler.prototype.getVisualsWithInstance = function (instance) {
return this.get('elementRegistry').filter(element =>
is(element, 'om:Object') &&
instance.id &&
element.businessObject.instance?.id === instance.id
);
}
OmModeler.prototype.getObjectsWithInstance = function (instance) {
let objectives = this._definitions.get('rootElements');
let objects = objectives.map(objective => objective.get('boardElements')).flat(1).filter((element) =>
is(element, 'om:Object') &&
instance.id &&
element.instance?.id === instance.id);
return objects;
}
OmModeler.prototype.getObjectsOfClass = function (clazz) {
let objectives = this._definitions.get('rootElements');
let objects = objectives.map(objective => objective.get('boardElements')).flat(1).filter((element) =>
is(element, 'om:Object') &&
clazz.id &&
element.classRef?.id === clazz.id);
return objects;
}
OmModeler.prototype.getObjectInstancesOfClass = function (clazz) {
let instances = this._definitions.get('objectInstances');
return instances.filter(instance =>
is(instance, 'om:ObjectInstance') &&
clazz.id &&
instance.classRef?.id === clazz.id
);
}
OmModeler.prototype.getObjectiveByReference = function (objectiveReference) {
const objective = this.getObjectives().filter(objective => objective.objectiveRef === objectiveReference)[0];
if (!objective) {
throw 'Unknown rootBoard of objective \"' + objectiveReference.name + '\"';
} else {
return objective;
}
}
OmModeler.prototype.isObjectOfDeletedClass = function (clazz, element) {
return is(element, 'om:Object') && clazz.id && element.classRef?.id === clazz.id;
}
OmModeler.prototype.isLinkConnectedToObjectOfDeletedClass = function (clazz, element) {
return is(element, 'om:Link') && clazz.id && ((element.sourceRef?.classRef?.id === clazz.id) || (element.targetRef?.classRef?.id === clazz.id));
}