Fix name convention issue
[sdc/sdc-workflow-designer.git] / sdc-workflow-designer-ui / src / main / frontend / src / features / version / composition / custom-modeler / index.js
1 import Modeler from 'bpmn-js/lib/Modeler';
2
3 import { assign, isArray } from 'min-dash';
4
5 import inherits from 'inherits';
6
7 import CustomModule from './custom';
8
9 export default function CustomModeler(options) {
10     Modeler.call(this, options);
11
12     this._customElements = [];
13 }
14
15 inherits(CustomModeler, Modeler);
16
17 CustomModeler.prototype._modules = [].concat(CustomModeler.prototype._modules, [
18     CustomModule
19 ]);
20
21 /**
22  * Add a single custom element to the underlying diagram
23  *
24  * @param {Object} customElement
25  */
26 CustomModeler.prototype._addCustomShape = function(customElement) {
27     this._customElements.push(customElement);
28
29     var canvas = this.get('canvas'),
30         elementFactory = this.get('elementFactory');
31
32     var customAttrs = assign({ businessObject: customElement }, customElement);
33
34     var customShape = elementFactory.create('shape', customAttrs);
35
36     return canvas.addShape(customShape);
37 };
38
39 CustomModeler.prototype._addCustomConnection = function(customElement) {
40     this._customElements.push(customElement);
41
42     var canvas = this.get('canvas'),
43         elementFactory = this.get('elementFactory'),
44         elementRegistry = this.get('elementRegistry');
45
46     var customAttrs = assign({ businessObject: customElement }, customElement);
47
48     var connection = elementFactory.create(
49         'connection',
50         assign(customAttrs, {
51             source: elementRegistry.get(customElement.source),
52             target: elementRegistry.get(customElement.target)
53         }),
54         elementRegistry.get(customElement.source).parent
55     );
56
57     return canvas.addConnection(connection);
58 };
59
60 /**
61  * Add a number of custom elements and connections to the underlying diagram.
62  *
63  * @param {Array<Object>} customElements
64  */
65 CustomModeler.prototype.addCustomElements = function(customElements) {
66     if (!isArray(customElements)) {
67         throw new Error('argument must be an array');
68     }
69
70     var shapes = [],
71         connections = [];
72
73     customElements.forEach(function(customElement) {
74         if (isCustomConnection(customElement)) {
75             connections.push(customElement);
76         } else {
77             shapes.push(customElement);
78         }
79     });
80
81     // add shapes before connections so that connections
82     // can already rely on the shapes being part of the diagram
83     shapes.forEach(this._addCustomShape, this);
84
85     connections.forEach(this._addCustomConnection, this);
86 };
87
88 /**
89  * Get custom elements with their current status.
90  *
91  * @return {Array<Object>} custom elements on the diagram
92  */
93 CustomModeler.prototype.getCustomElements = function() {
94     return this._customElements;
95 };
96
97 function isCustomConnection(element) {
98     return element.type === 'custom:connection';
99 }