RootElementReferenceBehavior.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import inherits from 'inherits';
  2. import {
  3. find,
  4. isArray,
  5. matchPattern,
  6. some
  7. } from 'min-dash';
  8. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  9. import {
  10. add as collectionAdd,
  11. remove as collectionRemove
  12. } from 'diagram-js/lib/util/Collections';
  13. import {
  14. getBusinessObject,
  15. is
  16. } from '../../../util/ModelUtil';
  17. import { isAny } from '../util/ModelingUtil';
  18. import { hasEventDefinition } from '../../../util/DiUtil';
  19. var LOW_PRIORITY = 500;
  20. /**
  21. * Add referenced root elements (error, escalation, message, signal) if they don't exist.
  22. * Copy referenced root elements on copy & paste.
  23. */
  24. export default function RootElementReferenceBehavior(
  25. bpmnjs, eventBus, injector, moddleCopy, bpmnFactory
  26. ) {
  27. injector.invoke(CommandInterceptor, this);
  28. function canHaveRootElementReference(element) {
  29. return isAny(element, [ 'bpmn:ReceiveTask', 'bpmn:SendTask' ]) ||
  30. hasAnyEventDefinition(element, [
  31. 'bpmn:ErrorEventDefinition',
  32. 'bpmn:EscalationEventDefinition',
  33. 'bpmn:MessageEventDefinition',
  34. 'bpmn:SignalEventDefinition'
  35. ]);
  36. }
  37. function hasRootElement(rootElement) {
  38. var definitions = bpmnjs.getDefinitions(),
  39. rootElements = definitions.get('rootElements');
  40. return !!find(rootElements, matchPattern({ id: rootElement.id }));
  41. }
  42. function getRootElementReferencePropertyName(eventDefinition) {
  43. if (is(eventDefinition, 'bpmn:ErrorEventDefinition')) {
  44. return 'errorRef';
  45. } else if (is(eventDefinition, 'bpmn:EscalationEventDefinition')) {
  46. return 'escalationRef';
  47. } else if (is(eventDefinition, 'bpmn:MessageEventDefinition')) {
  48. return 'messageRef';
  49. } else if (is(eventDefinition, 'bpmn:SignalEventDefinition')) {
  50. return 'signalRef';
  51. }
  52. }
  53. function getRootElement(businessObject) {
  54. if (isAny(businessObject, [ 'bpmn:ReceiveTask', 'bpmn:SendTask' ])) {
  55. return businessObject.get('messageRef');
  56. }
  57. var eventDefinitions = businessObject.get('eventDefinitions'),
  58. eventDefinition = eventDefinitions[ 0 ];
  59. return eventDefinition.get(getRootElementReferencePropertyName(eventDefinition));
  60. }
  61. function setRootElement(businessObject, rootElement) {
  62. if (isAny(businessObject, [ 'bpmn:ReceiveTask', 'bpmn:SendTask' ])) {
  63. return businessObject.set('messageRef', rootElement);
  64. }
  65. var eventDefinitions = businessObject.get('eventDefinitions'),
  66. eventDefinition = eventDefinitions[ 0 ];
  67. return eventDefinition.set(getRootElementReferencePropertyName(eventDefinition), rootElement);
  68. }
  69. // create shape
  70. this.executed('shape.create', function(context) {
  71. var shape = context.shape;
  72. if (!canHaveRootElementReference(shape)) {
  73. return;
  74. }
  75. var businessObject = getBusinessObject(shape),
  76. rootElement = getRootElement(businessObject),
  77. rootElements;
  78. if (rootElement && !hasRootElement(rootElement)) {
  79. rootElements = bpmnjs.getDefinitions().get('rootElements');
  80. // add root element
  81. collectionAdd(rootElements, rootElement);
  82. context.addedRootElement = rootElement;
  83. }
  84. }, true);
  85. this.reverted('shape.create', function(context) {
  86. var addedRootElement = context.addedRootElement;
  87. if (!addedRootElement) {
  88. return;
  89. }
  90. var rootElements = bpmnjs.getDefinitions().get('rootElements');
  91. // remove root element
  92. collectionRemove(rootElements, addedRootElement);
  93. }, true);
  94. eventBus.on('copyPaste.copyElement', function(context) {
  95. var descriptor = context.descriptor,
  96. element = context.element;
  97. if (!canHaveRootElementReference(element)) {
  98. return;
  99. }
  100. var businessObject = getBusinessObject(element),
  101. rootElement = getRootElement(businessObject);
  102. if (rootElement) {
  103. descriptor.referencedRootElement = rootElement;
  104. }
  105. });
  106. eventBus.on('copyPaste.pasteElement', LOW_PRIORITY, function(context) {
  107. var descriptor = context.descriptor,
  108. businessObject = descriptor.businessObject;
  109. if (!canHaveRootElementReference(businessObject)) {
  110. return;
  111. }
  112. var referencedRootElement = descriptor.referencedRootElement;
  113. if (!referencedRootElement) {
  114. return;
  115. }
  116. if (!hasRootElement(referencedRootElement)) {
  117. referencedRootElement = moddleCopy.copyElement(
  118. referencedRootElement,
  119. bpmnFactory.create(referencedRootElement.$type)
  120. );
  121. }
  122. setRootElement(businessObject, referencedRootElement);
  123. });
  124. }
  125. RootElementReferenceBehavior.$inject = [
  126. 'bpmnjs',
  127. 'eventBus',
  128. 'injector',
  129. 'moddleCopy',
  130. 'bpmnFactory'
  131. ];
  132. inherits(RootElementReferenceBehavior, CommandInterceptor);
  133. // helpers //////////
  134. function hasAnyEventDefinition(element, types) {
  135. if (!isArray(types)) {
  136. types = [ types ];
  137. }
  138. return some(types, function(type) {
  139. return hasEventDefinition(element, type);
  140. });
  141. }