SubprocessCompatibility.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import { asBounds, asTRBL } from 'diagram-js/lib/layout/LayoutUtil';
  2. import { is, isAny } from '../../util/ModelUtil';
  3. /**
  4. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  5. * @typedef {import('../../model/Types').Moddle} Moddle
  6. *
  7. * @typedef {import('../../model/Types').Element} Element
  8. * @typedef {import('../../model/Types').Shape} Shape
  9. *
  10. * @typedef {import('diagram-js/lib/core/Canvas').CanvasPlane} CanvasPlane
  11. *
  12. * @typedef {import('diagram-js/lib/util/Types').Rect} Rect
  13. */
  14. var DEFAULT_POSITION = {
  15. x: 180,
  16. y: 160
  17. };
  18. /**
  19. * Hook into `import.render.start` and create new planes for diagrams with
  20. * collapsed subprocesses and all DI elements on the same plane.
  21. *
  22. * @param {EventBus} eventBus
  23. * @param {Moddle} moddle
  24. */
  25. export default function SubprocessCompatibility(eventBus, moddle) {
  26. this._eventBus = eventBus;
  27. this._moddle = moddle;
  28. var self = this;
  29. eventBus.on('import.render.start', 1500, function(e, context) {
  30. self._handleImport(context.definitions);
  31. });
  32. }
  33. /**
  34. * @param {ModdleElement} definitions
  35. */
  36. SubprocessCompatibility.prototype._handleImport = function(definitions) {
  37. if (!definitions.diagrams) {
  38. return;
  39. }
  40. var self = this;
  41. this._definitions = definitions;
  42. this._processToDiagramMap = {};
  43. definitions.diagrams.forEach(function(diagram) {
  44. if (!diagram.plane || !diagram.plane.bpmnElement) {
  45. return;
  46. }
  47. self._processToDiagramMap[diagram.plane.bpmnElement.id] = diagram;
  48. });
  49. var newDiagrams = [];
  50. definitions.diagrams.forEach(function(diagram) {
  51. var createdDiagrams = self._createNewDiagrams(diagram.plane);
  52. Array.prototype.push.apply(newDiagrams, createdDiagrams);
  53. });
  54. newDiagrams.forEach(function(diagram) {
  55. self._movePlaneElementsToOrigin(diagram.plane);
  56. });
  57. };
  58. /**
  59. * Moves all DI elements from collapsed subprocesses to a new plane.
  60. *
  61. * @param {CanvasPlane} plane
  62. *
  63. * @return {ModdleElement[]} new diagrams created for the collapsed subprocesses
  64. */
  65. SubprocessCompatibility.prototype._createNewDiagrams = function(plane) {
  66. var self = this;
  67. var collapsedElements = [];
  68. var elementsToMove = [];
  69. plane.get('planeElement').forEach(function(diElement) {
  70. var businessObject = diElement.bpmnElement;
  71. if (!businessObject) {
  72. return;
  73. }
  74. var parent = businessObject.$parent;
  75. if (is(businessObject, 'bpmn:SubProcess') && !diElement.isExpanded) {
  76. collapsedElements.push(businessObject);
  77. }
  78. if (shouldMoveToPlane(businessObject, plane)) {
  79. // don't change the array while we iterate over it
  80. elementsToMove.push({ diElement: diElement, parent: parent });
  81. }
  82. });
  83. var newDiagrams = [];
  84. // create new planes for all collapsed subprocesses, even when they are empty
  85. collapsedElements.forEach(function(element) {
  86. if (!self._processToDiagramMap[ element.id ]) {
  87. var diagram = self._createDiagram(element);
  88. self._processToDiagramMap[element.id] = diagram;
  89. newDiagrams.push(diagram);
  90. }
  91. });
  92. elementsToMove.forEach(function(element) {
  93. var diElement = element.diElement;
  94. var parent = element.parent;
  95. // parent is expanded, get nearest collapsed parent
  96. while (parent && collapsedElements.indexOf(parent) === -1) {
  97. parent = parent.$parent;
  98. }
  99. // false positive, all parents are expanded
  100. if (!parent) {
  101. return;
  102. }
  103. var diagram = self._processToDiagramMap[ parent.id ];
  104. self._moveToDiPlane(diElement, diagram.plane);
  105. });
  106. return newDiagrams;
  107. };
  108. /**
  109. * @param {CanvasPlane} plane
  110. */
  111. SubprocessCompatibility.prototype._movePlaneElementsToOrigin = function(plane) {
  112. var elements = plane.get('planeElement');
  113. // get bounding box of all elements
  114. var planeBounds = getPlaneBounds(plane);
  115. var offset = {
  116. x: planeBounds.x - DEFAULT_POSITION.x,
  117. y: planeBounds.y - DEFAULT_POSITION.y
  118. };
  119. elements.forEach(function(diElement) {
  120. if (diElement.waypoint) {
  121. diElement.waypoint.forEach(function(waypoint) {
  122. waypoint.x = waypoint.x - offset.x;
  123. waypoint.y = waypoint.y - offset.y;
  124. });
  125. } else if (diElement.bounds) {
  126. diElement.bounds.x = diElement.bounds.x - offset.x;
  127. diElement.bounds.y = diElement.bounds.y - offset.y;
  128. }
  129. });
  130. };
  131. /**
  132. * @param {ModdleElement} diElement
  133. * @param {CanvasPlane} newPlane
  134. */
  135. SubprocessCompatibility.prototype._moveToDiPlane = function(diElement, newPlane) {
  136. var containingDiagram = findRootDiagram(diElement);
  137. // remove DI from old Plane and add it to the new one
  138. var parentPlaneElement = containingDiagram.plane.get('planeElement');
  139. parentPlaneElement.splice(parentPlaneElement.indexOf(diElement), 1);
  140. newPlane.get('planeElement').push(diElement);
  141. };
  142. /**
  143. * @param {ModdleElement} businessObject
  144. *
  145. * @return {ModdleElement}
  146. */
  147. SubprocessCompatibility.prototype._createDiagram = function(businessObject) {
  148. var plane = this._moddle.create('bpmndi:BPMNPlane', {
  149. bpmnElement: businessObject
  150. });
  151. var diagram = this._moddle.create('bpmndi:BPMNDiagram', {
  152. plane: plane
  153. });
  154. plane.$parent = diagram;
  155. plane.bpmnElement = businessObject;
  156. diagram.$parent = this._definitions;
  157. this._definitions.diagrams.push(diagram);
  158. return diagram;
  159. };
  160. SubprocessCompatibility.$inject = [ 'eventBus', 'moddle' ];
  161. // helpers //////////
  162. function findRootDiagram(element) {
  163. if (is(element, 'bpmndi:BPMNDiagram')) {
  164. return element;
  165. } else {
  166. return findRootDiagram(element.$parent);
  167. }
  168. }
  169. /**
  170. * @param {CanvasPlane} plane
  171. *
  172. * @return {Rect}
  173. */
  174. function getPlaneBounds(plane) {
  175. var planeTrbl = {
  176. top: Infinity,
  177. right: -Infinity,
  178. bottom: -Infinity,
  179. left: Infinity
  180. };
  181. plane.planeElement.forEach(function(element) {
  182. if (!element.bounds) {
  183. return;
  184. }
  185. var trbl = asTRBL(element.bounds);
  186. planeTrbl.top = Math.min(trbl.top, planeTrbl.top);
  187. planeTrbl.left = Math.min(trbl.left, planeTrbl.left);
  188. });
  189. return asBounds(planeTrbl);
  190. }
  191. /**
  192. * @param {ModdleElement} businessObject
  193. * @param {CanvasPlane} plane
  194. *
  195. * @return {boolean}
  196. */
  197. function shouldMoveToPlane(businessObject, plane) {
  198. var parent = businessObject.$parent;
  199. // don't move elements that are already on the plane
  200. if (!is(parent, 'bpmn:SubProcess') || parent === plane.bpmnElement) {
  201. return false;
  202. }
  203. // dataAssociations are children of the subprocess but rendered on process level
  204. // cf. https://github.com/bpmn-io/bpmn-js/issues/1619
  205. if (isAny(businessObject, [ 'bpmn:DataInputAssociation', 'bpmn:DataOutputAssociation' ])) {
  206. return false;
  207. }
  208. return true;
  209. }