UNPKG

2.05 kBJavaScriptView Raw
1import inherits from 'inherits';
2
3import Ids from 'ids';
4
5import BaseViewer from './BaseViewer';
6
7
8/**
9 * A base modeler for BPMN 2.0 diagrams.
10 *
11 * Have a look at {@link Modeler} for a bundle that includes actual features.
12 *
13 * @param {Object} [options] configuration options to pass to the viewer
14 * @param {DOMElement} [options.container] the container to render the viewer in, defaults to body.
15 * @param {String|Number} [options.width] the width of the viewer
16 * @param {String|Number} [options.height] the height of the viewer
17 * @param {Object} [options.moddleExtensions] extension packages to provide
18 * @param {Array<didi.Module>} [options.modules] a list of modules to override the default modules
19 * @param {Array<didi.Module>} [options.additionalModules] a list of modules to use with the default modules
20 */
21export default function BaseModeler(options) {
22 BaseViewer.call(this, options);
23
24 // hook ID collection into the modeler
25 this.on('import.parse.complete', function(event) {
26 if (!event.error) {
27 this._collectIds(event.definitions, event.context);
28 }
29 }, this);
30
31 this.on('diagram.destroy', function() {
32 this.get('moddle').ids.clear();
33 }, this);
34}
35
36inherits(BaseModeler, BaseViewer);
37
38
39/**
40 * Create a moddle instance, attaching ids to it.
41 *
42 * @param {Object} options
43 */
44BaseModeler.prototype._createModdle = function(options) {
45 var moddle = BaseViewer.prototype._createModdle.call(this, options);
46
47 // attach ids to moddle to be able to track
48 // and validated ids in the BPMN 2.0 XML document
49 // tree
50 moddle.ids = new Ids([ 32, 36, 1 ]);
51
52 return moddle;
53};
54
55/**
56 * Collect ids processed during parsing of the
57 * definitions object.
58 *
59 * @param {ModdleElement} definitions
60 * @param {Context} context
61 */
62BaseModeler.prototype._collectIds = function(definitions, context) {
63
64 var moddle = definitions.$model,
65 ids = moddle.ids,
66 id;
67
68 // remove references from previous import
69 ids.clear();
70
71 for (id in context.elementsById) {
72 ids.claim(id, context.elementsById[id]);
73 }
74};
\No newline at end of file