forked from bptlab/fCM-design-support
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DependencyModeler.js
308 lines (266 loc) · 10.2 KB
/
DependencyModeler.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
import DepLabelHandling from "./depLabelHandling";
import DepPaletteModule from './palette';
import DepDrawModule from './draw';
import DepRulesModule from './rules';
import DepModelingModule from './modeling';
import DepAutoPlaceModule from './auto-place';
import DepModdle from './moddle';
import {nextPosition, root} from '../util/Util';
import KeyboardModule from '../common/keyboard';
import EditorActionsModule from '../common/editor-actions';
import inherits from 'inherits';
import {groupBy} 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 RulesModule from 'diagram-js/lib/features/rules';
import SelectionModule from 'diagram-js/lib/features/selection';
import ZoomScrollModule from 'diagram-js/lib/navigation/zoomscroll';
import CopyPasteModule from 'diagram-js/lib/features/copy-paste';
var emptyDiagram =
`<?xml version="1.0" encoding="UTF-8"?>
<dep:definitions xmlns:olc="http://bptlab/schema/olc" xmlns:olcDi="http://bptlab/schema/olcDi">
<dep:goal id="MainGoal">
<dep:objective id="start_state" type="dep:Objective" name="Start State" x="465" y="293" />
</dep:goal>
</dep:definitions>`;
/**
* Our editor constructor
*
* @param { { container: Element, additionalModules?: Array<any> } } options
*
* @return {Diagram}
*/
export default function DependencyModeler(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 = [
DepPaletteModule,
DepDrawModule,
DepRulesModule,
DepModelingModule,
DepLabelHandling,
DepAutoPlaceModule,
{
moddle: ['value', new DepModdle({})],
dependencyModeler: ['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
}
inherits(DependencyModeler, Diagram);
DependencyModeler.prototype.id = "DEP";
DependencyModeler.prototype.rank = 4;
DependencyModeler.prototype.name = function (constructionMode) {
if (constructionMode) {
return "Timeline";
} else {
return "Dependency Model";
}
}
DependencyModeler.prototype.createNew = function () {
return this.importXML(emptyDiagram);
}
DependencyModeler.prototype.show = function (goal) {
this.clear();
if (goal) {
const elementFactory = this.get('elementFactory');
var diagramRoot = elementFactory.createRoot({type: 'dep:Goal', businessObject: goal});
const canvas = this.get('canvas');
canvas.setRootElement(diagramRoot);
var elements = groupBy(goal.get('Elements'), element => element.$type);
var states = {};
(elements['dep:Objective'] || []).forEach(state => {
var stateVisual = elementFactory.createShape({
type: 'dep:Objective',
businessObject: state,
x: parseInt(state.get('x')),
y: parseInt(state.get('y'))
});
states[state.get('id')] = stateVisual;
canvas.addShape(stateVisual, diagramRoot);
});
(elements['dep:Dependency'] || []).forEach(transition => {
var source = states[transition.get('sourceObjective').get('id')];
var target = states[transition.get('targetObjective').get('id')];
var transitionVisual = elementFactory.createConnection({
type: 'dep:Dependency',
businessObject: transition,
source: source,
target: target,
waypoints: this.get('depUpdater').connectionWaypoints(source, target)
});
canvas.addConnection(transitionVisual, diagramRoot);
});
}
}
DependencyModeler.prototype.createObjective = function (name) {
const modeling = this.get('modeling');
const canvas = this.get('canvas');
const diagramRoot = canvas.getRootElement();
const {x, y} = nextPosition(this, 'dep:Objective');
const shape = modeling.createShape({
type: 'dep:Objective',
name: name,
x: parseInt(x),
y: parseInt(y)
}, {x, y}, diagramRoot);
return shape.businessObject;
}
DependencyModeler.prototype.deleteObjective = function (objective) {
const modeling = this.get('modeling');
const objectiveVisual = this.get('elementRegistry').get(objective.id);
modeling.removeElements([objectiveVisual]);
}
DependencyModeler.prototype.renameObjective = function (objective, name) {
const modeling = this.get('modeling');
const objectiveVisual = this.get('elementRegistry').get(objective.id);
modeling.updateLabel(objectiveVisual, name);
}
DependencyModeler.prototype.createDependency = function (sourceState, targetState) {
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: 'dep:Dependency',
source: sourceState,
target: targetState,
waypoints: this.get('depUpdater').connectionWaypoints(sourceState, targetState)
});
return transitionVisual.businessObject;
}
DependencyModeler.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
DependencyModeler.prototype.importDefinitions = function (definitions) {
this.get('elementFactory')._ids.clear();
this._definitions = definitions;
this._emit('import.render.start', {definitions: definitions});
this._goal = definitions.get('goals')[0];
this.show(this._goal);
this._emit('import.render.complete', {});
}
DependencyModeler.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);
});
});
}
DependencyModeler.prototype._emit = function (type, event) {
return this.get('eventBus').fire(type, event);
}
DependencyModeler.prototype.ensureElementIsOnCanvas = function (element) {
if (!this.get('elementRegistry').get(element.id)) {
const rootElement = root(element);
if (this._goal === rootElement) {
this.show(rootElement);
} else {
throw 'Cannot display element. Is not part of a known goal.';
}
}
}