DetachEventBehavior.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import inherits from 'inherits-browser';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import {
  4. getBusinessObject,
  5. is
  6. } from '../../../util/ModelUtil';
  7. import { isLabel } from '../../../util/LabelUtil';
  8. /**
  9. * @typedef {import('../../replace/BpmnReplace').default} BpmnReplace
  10. * @typedef {import('didi').Injector} Injector
  11. */
  12. var LOW_PRIORITY = 500;
  13. /**
  14. * Replace boundary event with intermediate event when creating or moving results in detached event.
  15. *
  16. * @param {BpmnReplace} bpmnReplace
  17. * @param {Injector} injector
  18. */
  19. export default function DetachEventBehavior(bpmnReplace, injector) {
  20. injector.invoke(CommandInterceptor, this);
  21. this._bpmnReplace = bpmnReplace;
  22. var self = this;
  23. this.postExecuted('elements.create', LOW_PRIORITY, function(context) {
  24. var elements = context.elements;
  25. elements.filter(function(shape) {
  26. var host = shape.host;
  27. return shouldReplace(shape, host);
  28. }).map(function(shape) {
  29. return elements.indexOf(shape);
  30. }).forEach(function(index) {
  31. context.elements[ index ] = self._replaceShape(elements[ index ]);
  32. });
  33. }, true);
  34. this.preExecute('elements.move', LOW_PRIORITY, function(context) {
  35. var shapes = context.shapes,
  36. newHost = context.newHost;
  37. shapes.forEach(function(shape, index) {
  38. var host = shape.host;
  39. if (shouldReplace(shape, includes(shapes, host) ? host : newHost)) {
  40. shapes[ index ] = self._replaceShape(shape);
  41. }
  42. });
  43. }, true);
  44. }
  45. DetachEventBehavior.$inject = [
  46. 'bpmnReplace',
  47. 'injector'
  48. ];
  49. inherits(DetachEventBehavior, CommandInterceptor);
  50. DetachEventBehavior.prototype._replaceShape = function(shape) {
  51. var eventDefinition = getEventDefinition(shape),
  52. intermediateEvent;
  53. if (eventDefinition) {
  54. intermediateEvent = {
  55. type: 'bpmn:IntermediateCatchEvent',
  56. eventDefinitionType: eventDefinition.$type
  57. };
  58. } else {
  59. intermediateEvent = {
  60. type: 'bpmn:IntermediateThrowEvent'
  61. };
  62. }
  63. return this._bpmnReplace.replaceElement(shape, intermediateEvent, { 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) && is(shape, 'bpmn:BoundaryEvent') && !host;
  73. }
  74. function includes(array, item) {
  75. return array.indexOf(item) !== -1;
  76. }