-
Notifications
You must be signed in to change notification settings - Fork 8
/
CustomElementFactory.js
136 lines (111 loc) · 3.23 KB
/
CustomElementFactory.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
import {
assign
} from 'min-dash';
import inherits from 'inherits-browser';
import BpmnElementFactory from 'bpmn-js/lib/features/modeling/ElementFactory';
import {
DEFAULT_LABEL_SIZE
} from 'bpmn-js/lib/util/LabelUtil';
/**
* A custom factory that knows how to create BPMN _and_ custom elements.
*/
export default function CustomElementFactory(bpmnFactory, moddle) {
BpmnElementFactory.call(this, bpmnFactory, moddle);
var self = this;
/**
* Create a diagram-js element with the given type (any of shape, connection, label).
*
* @param {String} elementType
* @param {Object} attrs
*
* @return {djs.model.Base}
*/
this.create = function(elementType, attrs) {
var type = attrs.type;
if (elementType === 'label') {
return self._baseCreate(elementType, assign({ type: 'label' }, DEFAULT_LABEL_SIZE, attrs));
}
// add type to businessObject if custom
if (/^custom:/.test(type)) {
if (!attrs.businessObject) {
attrs.businessObject = {
type: type
};
if (attrs.id) {
assign(attrs.businessObject, {
id: attrs.id
});
}
}
// add width and height if shape
if (!/:connection$/.test(type)) {
assign(attrs, self._getCustomElementSize(type));
}
// we mimic the ModdleElement API to allow interoperability with
// other components, i.e. the Modeler and Properties Panel
if (!('$model' in attrs.businessObject)) {
Object.defineProperty(attrs.businessObject, '$model', {
value: moddle
});
}
if (!('$instanceOf' in attrs.businessObject)) {
// ensures we can use ModelUtil#is for type checks
Object.defineProperty(attrs.businessObject, '$instanceOf', {
value: function(type) {
return this.type === type;
}
});
}
if (!('get' in attrs.businessObject)) {
Object.defineProperty(attrs.businessObject, 'get', {
value: function(key) {
return this[key];
}
});
}
if (!('set' in attrs.businessObject)) {
Object.defineProperty(attrs.businessObject, 'set', {
value: function(key, value) {
return this[key] = value;
}
});
}
// END minic ModdleElement API
return self._baseCreate(elementType, attrs);
}
return this.createElement(elementType, attrs);
};
}
inherits(CustomElementFactory, BpmnElementFactory);
CustomElementFactory.$inject = [
'bpmnFactory',
'moddle'
];
/**
* Returns the default size of custom shapes.
*
* The following example shows an interface on how
* to setup the custom shapes's dimensions.
*
* @example
*
* var shapes = {
* triangle: { width: 40, height: 40 },
* rectangle: { width: 100, height: 20 }
* };
*
* return shapes[type];
*
*
* @param {String} type
*
* @return {Dimensions} a {width, height} object representing the size of the element
*/
CustomElementFactory.prototype._getCustomElementSize = function(type) {
var shapes = {
__default: { width: 100, height: 80 },
'custom:triangle': { width: 40, height: 40 },
'custom:circle': { width: 140, height: 140 }
};
return shapes[type] || shapes.__default;
};