CreateParticipantBehavior.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import inherits from 'inherits-browser';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import { getBusinessObject, is } from '../../../util/ModelUtil';
  4. import { isLabel } from '../../../util/LabelUtil';
  5. import { getBBox } from 'diagram-js/lib/util/Elements';
  6. import {
  7. assign,
  8. find
  9. } from 'min-dash';
  10. import { asTRBL } from 'diagram-js/lib/layout/LayoutUtil';
  11. import { isConnection } from 'diagram-js/lib/util/ModelUtil';
  12. /**
  13. * @typedef {import('diagram-js/lib/core/Canvas').default} Canvas
  14. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  15. * @typedef {import('../Modeling').default} Modeling
  16. */
  17. var HORIZONTAL_PARTICIPANT_PADDING = 20,
  18. VERTICAL_PARTICIPANT_PADDING = 20;
  19. export var PARTICIPANT_BORDER_WIDTH = 30;
  20. var HIGH_PRIORITY = 2000;
  21. /**
  22. * BPMN-specific behavior for creating participants.
  23. *
  24. * @param {Canvas} canvas
  25. * @param {EventBus} eventBus
  26. * @param {Modeling} modeling
  27. */
  28. export default function CreateParticipantBehavior(canvas, eventBus, modeling) {
  29. CommandInterceptor.call(this, eventBus);
  30. // fit participant
  31. eventBus.on([
  32. 'create.start',
  33. 'shape.move.start'
  34. ], HIGH_PRIORITY, function(event) {
  35. var context = event.context,
  36. shape = context.shape,
  37. rootElement = canvas.getRootElement();
  38. if (!is(shape, 'bpmn:Participant') ||
  39. !is(rootElement, 'bpmn:Process') ||
  40. !rootElement.children.length) {
  41. return;
  42. }
  43. // ignore connections, groups and labels
  44. var children = rootElement.children.filter(function(element) {
  45. return !is(element, 'bpmn:Group') &&
  46. !isLabel(element) &&
  47. !isConnection(element);
  48. });
  49. // ensure for available children to calculate bounds
  50. if (!children.length) {
  51. return;
  52. }
  53. var childrenBBox = getBBox(children);
  54. var participantBounds = getParticipantBounds(shape, childrenBBox);
  55. // assign width and height
  56. assign(shape, participantBounds);
  57. // assign create constraints
  58. context.createConstraints = getParticipantCreateConstraints(shape, childrenBBox);
  59. });
  60. // force hovering process when creating first participant
  61. eventBus.on('create.start', HIGH_PRIORITY, function(event) {
  62. var context = event.context,
  63. shape = context.shape,
  64. rootElement = canvas.getRootElement(),
  65. rootElementGfx = canvas.getGraphics(rootElement);
  66. function ensureHoveringProcess(event) {
  67. event.element = rootElement;
  68. event.gfx = rootElementGfx;
  69. }
  70. if (is(shape, 'bpmn:Participant') && is(rootElement, 'bpmn:Process')) {
  71. eventBus.on('element.hover', HIGH_PRIORITY, ensureHoveringProcess);
  72. eventBus.once('create.cleanup', function() {
  73. eventBus.off('element.hover', ensureHoveringProcess);
  74. });
  75. }
  76. });
  77. // turn process into collaboration when creating first participant
  78. function getOrCreateCollaboration() {
  79. var rootElement = canvas.getRootElement();
  80. if (is(rootElement, 'bpmn:Collaboration')) {
  81. return rootElement;
  82. }
  83. return modeling.makeCollaboration();
  84. }
  85. // when creating mutliple elements through `elements.create` parent must be set to collaboration
  86. // and passed to `shape.create` as hint
  87. this.preExecute('elements.create', HIGH_PRIORITY, function(context) {
  88. var elements = context.elements,
  89. parent = context.parent,
  90. participant = findParticipant(elements),
  91. hints;
  92. if (participant && is(parent, 'bpmn:Process')) {
  93. context.parent = getOrCreateCollaboration();
  94. hints = context.hints = context.hints || {};
  95. hints.participant = participant;
  96. hints.process = parent;
  97. hints.processRef = getBusinessObject(participant).get('processRef');
  98. }
  99. }, true);
  100. // when creating single shape through `shape.create` parent must be set to collaboration
  101. // unless it was already set through `elements.create`
  102. this.preExecute('shape.create', function(context) {
  103. var parent = context.parent,
  104. shape = context.shape;
  105. if (is(shape, 'bpmn:Participant') && is(parent, 'bpmn:Process')) {
  106. context.parent = getOrCreateCollaboration();
  107. context.process = parent;
  108. context.processRef = getBusinessObject(shape).get('processRef');
  109. }
  110. }, true);
  111. // #execute necessary because #preExecute not called on CommandStack#redo
  112. this.execute('shape.create', function(context) {
  113. var hints = context.hints || {},
  114. process = context.process || hints.process,
  115. shape = context.shape,
  116. participant = hints.participant;
  117. // both shape.create and elements.create must be handled
  118. if (process && (!participant || shape === participant)) {
  119. // monkey-patch process ref
  120. getBusinessObject(shape).set('processRef', getBusinessObject(process));
  121. }
  122. }, true);
  123. this.revert('shape.create', function(context) {
  124. var hints = context.hints || {},
  125. process = context.process || hints.process,
  126. processRef = context.processRef || hints.processRef,
  127. shape = context.shape,
  128. participant = hints.participant;
  129. // both shape.create and elements.create must be handled
  130. if (process && (!participant || shape === participant)) {
  131. // monkey-patch process ref
  132. getBusinessObject(shape).set('processRef', processRef);
  133. }
  134. }, true);
  135. this.postExecute('shape.create', function(context) {
  136. var hints = context.hints || {},
  137. process = context.process || context.hints.process,
  138. shape = context.shape,
  139. participant = hints.participant;
  140. if (process) {
  141. var children = process.children.slice();
  142. // both shape.create and elements.create must be handled
  143. if (!participant) {
  144. modeling.moveElements(children, { x: 0, y: 0 }, shape);
  145. } else if (shape === participant) {
  146. modeling.moveElements(children, { x: 0, y: 0 }, participant);
  147. }
  148. }
  149. }, true);
  150. }
  151. CreateParticipantBehavior.$inject = [
  152. 'canvas',
  153. 'eventBus',
  154. 'modeling'
  155. ];
  156. inherits(CreateParticipantBehavior, CommandInterceptor);
  157. // helpers //////////
  158. function getParticipantBounds(shape, childrenBBox) {
  159. childrenBBox = {
  160. width: childrenBBox.width + HORIZONTAL_PARTICIPANT_PADDING * 2 + PARTICIPANT_BORDER_WIDTH,
  161. height: childrenBBox.height + VERTICAL_PARTICIPANT_PADDING * 2
  162. };
  163. var width = Math.max(shape.width, childrenBBox.width),
  164. height = Math.max(shape.height, childrenBBox.height);
  165. return {
  166. x: -width / 2,
  167. y: -height / 2,
  168. width: width,
  169. height: height
  170. };
  171. }
  172. function getParticipantCreateConstraints(shape, childrenBBox) {
  173. childrenBBox = asTRBL(childrenBBox);
  174. return {
  175. bottom: childrenBBox.top + shape.height / 2 - VERTICAL_PARTICIPANT_PADDING,
  176. left: childrenBBox.right - shape.width / 2 + HORIZONTAL_PARTICIPANT_PADDING,
  177. top: childrenBBox.bottom - shape.height / 2 + VERTICAL_PARTICIPANT_PADDING,
  178. right: childrenBBox.left + shape.width / 2 - HORIZONTAL_PARTICIPANT_PADDING - PARTICIPANT_BORDER_WIDTH
  179. };
  180. }
  181. function findParticipant(elements) {
  182. return find(elements, function(element) {
  183. return is(element, 'bpmn:Participant');
  184. });
  185. }