AttachEventBehavior.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import inherits from 'inherits-browser';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import { getBusinessObject } from '../../../util/ModelUtil';
  4. import { isAny } from '../util/ModelingUtil';
  5. import { isLabel } from '../../../util/LabelUtil';
  6. /**
  7. * @typedef {import('../../replace/BpmnReplace').default} BpmnReplace
  8. * @typedef {import('didi').Injector} Injector
  9. */
  10. var LOW_PRIORITY = 500;
  11. /**
  12. * Replace intermediate event with boundary event when creating or moving results in attached event.
  13. *
  14. * @param {BpmnReplace} bpmnReplace
  15. * @param {Injector} injector
  16. */
  17. export default function AttachEventBehavior(bpmnReplace, injector) {
  18. injector.invoke(CommandInterceptor, this);
  19. this._bpmnReplace = bpmnReplace;
  20. var self = this;
  21. this.postExecuted('elements.create', LOW_PRIORITY, function(context) {
  22. var elements = context.elements;
  23. elements = elements.filter(function(shape) {
  24. var host = shape.host;
  25. return shouldReplace(shape, host);
  26. });
  27. if (elements.length !== 1) {
  28. return;
  29. }
  30. elements.map(function(element) {
  31. return elements.indexOf(element);
  32. }).forEach(function(index) {
  33. var host = elements[ index ];
  34. context.elements[ index ] = self._replaceShape(elements[ index ], host);
  35. });
  36. }, true);
  37. this.preExecute('elements.move', LOW_PRIORITY, function(context) {
  38. var shapes = context.shapes,
  39. host = context.newHost;
  40. if (shapes.length !== 1) {
  41. return;
  42. }
  43. var shape = shapes[0];
  44. if (shouldReplace(shape, host)) {
  45. context.shapes = [ self._replaceShape(shape, host) ];
  46. }
  47. }, true);
  48. }
  49. AttachEventBehavior.$inject = [
  50. 'bpmnReplace',
  51. 'injector'
  52. ];
  53. inherits(AttachEventBehavior, CommandInterceptor);
  54. AttachEventBehavior.prototype._replaceShape = function(shape, host) {
  55. var eventDefinition = getEventDefinition(shape);
  56. var boundaryEvent = {
  57. type: 'bpmn:BoundaryEvent',
  58. host: host
  59. };
  60. if (eventDefinition) {
  61. boundaryEvent.eventDefinitionType = eventDefinition.$type;
  62. }
  63. return this._bpmnReplace.replaceElement(shape, boundaryEvent, { layoutConnection: false });
  64. };
  65. // helpers //////////
  66. function getEventDefinition(element) {
  67. var businessObject = getBusinessObject(element),
  68. eventDefinitions = businessObject.eventDefinitions;
  69. return eventDefinitions && eventDefinitions[0];
  70. }
  71. function shouldReplace(shape, host) {
  72. return !isLabel(shape) &&
  73. isAny(shape, [ 'bpmn:IntermediateThrowEvent', 'bpmn:IntermediateCatchEvent' ]) && !!host;
  74. }