ElementFactory.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import {
  2. assign,
  3. forEach
  4. } from 'min-dash';
  5. import inherits from 'inherits';
  6. import { is } from '../../util/ModelUtil';
  7. import {
  8. isExpanded
  9. } from '../../util/DiUtil';
  10. import BaseElementFactory from 'diagram-js/lib/core/ElementFactory';
  11. import {
  12. DEFAULT_LABEL_SIZE
  13. } from '../../util/LabelUtil';
  14. /**
  15. * A bpmn-aware factory for diagram-js shapes
  16. */
  17. export default function ElementFactory(bpmnFactory, moddle, translate) {
  18. BaseElementFactory.call(this);
  19. this._bpmnFactory = bpmnFactory;
  20. this._moddle = moddle;
  21. this._translate = translate;
  22. }
  23. inherits(ElementFactory, BaseElementFactory);
  24. ElementFactory.$inject = [
  25. 'bpmnFactory',
  26. 'moddle',
  27. 'translate'
  28. ];
  29. ElementFactory.prototype.baseCreate = BaseElementFactory.prototype.create;
  30. ElementFactory.prototype.create = function(elementType, attrs) {
  31. // no special magic for labels,
  32. // we assume their businessObjects have already been created
  33. // and wired via attrs
  34. if (elementType === 'label') {
  35. return this.baseCreate(elementType, assign({ type: 'label' }, DEFAULT_LABEL_SIZE, attrs));
  36. }
  37. return this.createBpmnElement(elementType, attrs);
  38. };
  39. ElementFactory.prototype.createBpmnElement = function(elementType, attrs) {
  40. var size,
  41. translate = this._translate;
  42. attrs = attrs || {};
  43. var businessObject = attrs.businessObject;
  44. if (!businessObject) {
  45. if (!attrs.type) {
  46. throw new Error(translate('no shape type specified'));
  47. }
  48. businessObject = this._bpmnFactory.create(attrs.type);
  49. }
  50. if (!businessObject.di) {
  51. if (elementType === 'root') {
  52. businessObject.di = this._bpmnFactory.createDiPlane(businessObject, [], {
  53. id: businessObject.id + '_di'
  54. });
  55. } else
  56. if (elementType === 'connection') {
  57. businessObject.di = this._bpmnFactory.createDiEdge(businessObject, [], {
  58. id: businessObject.id + '_di'
  59. });
  60. } else {
  61. businessObject.di = this._bpmnFactory.createDiShape(businessObject, {}, {
  62. id: businessObject.id + '_di'
  63. });
  64. }
  65. }
  66. if (attrs.colors) {
  67. assign(businessObject.di, attrs.colors);
  68. delete attrs.colors;
  69. }
  70. applyAttributes(businessObject, attrs, [
  71. 'processRef',
  72. 'isInterrupting',
  73. 'associationDirection',
  74. 'isForCompensation'
  75. ]);
  76. if (attrs.isExpanded) {
  77. applyAttribute(businessObject.di, attrs, 'isExpanded');
  78. }
  79. if (is(businessObject, 'bpmn:ExclusiveGateway')) {
  80. businessObject.di.isMarkerVisible = true;
  81. }
  82. var eventDefinitions,
  83. newEventDefinition;
  84. if (attrs.eventDefinitionType) {
  85. eventDefinitions = businessObject.get('eventDefinitions') || [];
  86. newEventDefinition = this._moddle.create(attrs.eventDefinitionType);
  87. if (attrs.eventDefinitionType === 'bpmn:ConditionalEventDefinition') {
  88. newEventDefinition.condition = this._moddle.create('bpmn:FormalExpression');
  89. }
  90. eventDefinitions.push(newEventDefinition);
  91. newEventDefinition.$parent = businessObject;
  92. businessObject.eventDefinitions = eventDefinitions;
  93. delete attrs.eventDefinitionType;
  94. }
  95. size = this._getDefaultSize(businessObject);
  96. attrs = assign({
  97. businessObject: businessObject,
  98. id: businessObject.id
  99. }, size, attrs);
  100. return this.baseCreate(elementType, attrs);
  101. };
  102. ElementFactory.prototype._getDefaultSize = function(semantic) {
  103. if (is(semantic, 'bpmn:SubProcess')) {
  104. if (isExpanded(semantic)) {
  105. return { width: 350, height: 200 };
  106. } else {
  107. return { width: 100, height: 80 };
  108. }
  109. }
  110. if (is(semantic, 'bpmn:Task')) {
  111. return { width: 100, height: 80 };
  112. }
  113. if (is(semantic, 'bpmn:Gateway')) {
  114. return { width: 50, height: 50 };
  115. }
  116. if (is(semantic, 'bpmn:Event')) {
  117. return { width: 36, height: 36 };
  118. }
  119. if (is(semantic, 'bpmn:Participant')) {
  120. if (!isExpanded(semantic)) {
  121. return { width: 400, height: 100 };
  122. } else {
  123. return { width: 600, height: 250 };
  124. }
  125. }
  126. if (is(semantic, 'bpmn:Lane')) {
  127. return { width: 400, height: 100 };
  128. }
  129. if (is(semantic, 'bpmn:DataObjectReference')) {
  130. return { width: 36, height: 50 };
  131. }
  132. if (is(semantic, 'bpmn:DataStoreReference')) {
  133. return { width: 50, height: 50 };
  134. }
  135. if (is(semantic, 'bpmn:TextAnnotation')) {
  136. return { width: 100, height: 30 };
  137. }
  138. return { width: 100, height: 80 };
  139. };
  140. ElementFactory.prototype.createParticipantShape = function(collapsed) {
  141. var attrs = { type: 'bpmn:Participant' };
  142. if (!collapsed) {
  143. attrs.processRef = this._bpmnFactory.create('bpmn:Process');
  144. }
  145. return this.createShape(attrs);
  146. };
  147. // helpers //////////////////////
  148. /**
  149. * Apply attributes from a map to the given element,
  150. * remove attribute from the map on application.
  151. *
  152. * @param {Base} element
  153. * @param {Object} attrs (in/out map of attributes)
  154. * @param {Array<String>} attributeNames name of attributes to apply
  155. */
  156. function applyAttributes(element, attrs, attributeNames) {
  157. forEach(attributeNames, function(property) {
  158. if (attrs[property] !== undefined) {
  159. applyAttribute(element, attrs, property);
  160. }
  161. });
  162. }
  163. /**
  164. * Apply named property to element and drain it from the attrs
  165. * collection.
  166. *
  167. * @param {Base} element
  168. * @param {Object} attrs (in/out map of attributes)
  169. * @param {String} attributeName to apply
  170. */
  171. function applyAttribute(element, attrs, attributeName) {
  172. element[attributeName] = attrs[attributeName];
  173. delete attrs[attributeName];
  174. }