1 import Modeler from 'bpmn-js/lib/Modeler';
3 import { assign, isArray } from 'min-dash';
5 import inherits from 'inherits';
7 import CustomModule from './custom';
9 export default function CustomModeler(options) {
10 Modeler.call(this, options);
12 this._customElements = [];
15 inherits(CustomModeler, Modeler);
17 CustomModeler.prototype._modules = [].concat(CustomModeler.prototype._modules, [
22 * Add a single custom element to the underlying diagram
24 * @param {Object} customElement
26 CustomModeler.prototype._addCustomShape = function(customElement) {
27 this._customElements.push(customElement);
29 var canvas = this.get('canvas'),
30 elementFactory = this.get('elementFactory');
32 var customAttrs = assign({ businessObject: customElement }, customElement);
34 var customShape = elementFactory.create('shape', customAttrs);
36 return canvas.addShape(customShape);
39 CustomModeler.prototype._addCustomConnection = function(customElement) {
40 this._customElements.push(customElement);
42 var canvas = this.get('canvas'),
43 elementFactory = this.get('elementFactory'),
44 elementRegistry = this.get('elementRegistry');
46 var customAttrs = assign({ businessObject: customElement }, customElement);
48 var connection = elementFactory.create(
51 source: elementRegistry.get(customElement.source),
52 target: elementRegistry.get(customElement.target)
54 elementRegistry.get(customElement.source).parent
57 return canvas.addConnection(connection);
61 * Add a number of custom elements and connections to the underlying diagram.
63 * @param {Array<Object>} customElements
65 CustomModeler.prototype.addCustomElements = function(customElements) {
66 if (!isArray(customElements)) {
67 throw new Error('argument must be an array');
73 customElements.forEach(function(customElement) {
74 if (isCustomConnection(customElement)) {
75 connections.push(customElement);
77 shapes.push(customElement);
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);
85 connections.forEach(this._addCustomConnection, this);
89 * Get custom elements with their current status.
91 * @return {Array<Object>} custom elements on the diagram
93 CustomModeler.prototype.getCustomElements = function() {
94 return this._customElements;
97 function isCustomConnection(element) {
98 return element.type === 'custom:connection';