AttachEventBehavior.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import inherits from 'inherits';
  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. var LOW_PRIORITY = 500;
  7. /**
  8. * Replace intermediate event with boundary event when creating or moving results in attached event.
  9. */
  10. export default function AttachEventBehavior(bpmnReplace, injector) {
  11. injector.invoke(CommandInterceptor, this);
  12. this._bpmnReplace = bpmnReplace;
  13. var self = this;
  14. this.postExecuted('elements.create', LOW_PRIORITY, function(context) {
  15. var elements = context.elements;
  16. elements = elements.filter(function(shape) {
  17. var host = shape.host;
  18. return shouldReplace(shape, host);
  19. });
  20. if (elements.length !== 1) {
  21. return;
  22. }
  23. elements.map(function(element) {
  24. return elements.indexOf(element);
  25. }).forEach(function(index) {
  26. var host = elements[ index ];
  27. context.elements[ index ] = self.replaceShape(elements[ index ], host);
  28. });
  29. }, true);
  30. this.preExecute('elements.move', LOW_PRIORITY, function(context) {
  31. var shapes = context.shapes,
  32. host = context.newHost;
  33. if (shapes.length !== 1) {
  34. return;
  35. }
  36. var shape = shapes[0];
  37. if (shouldReplace(shape, host)) {
  38. context.shapes = [ self.replaceShape(shape, host) ];
  39. }
  40. }, true);
  41. }
  42. AttachEventBehavior.$inject = [
  43. 'bpmnReplace',
  44. 'injector'
  45. ];
  46. inherits(AttachEventBehavior, CommandInterceptor);
  47. AttachEventBehavior.prototype.replaceShape = function(shape, host) {
  48. var eventDefinition = getEventDefinition(shape);
  49. var boundaryEvent = {
  50. type: 'bpmn:BoundaryEvent',
  51. host: host
  52. };
  53. if (eventDefinition) {
  54. boundaryEvent.eventDefinitionType = eventDefinition.$type;
  55. }
  56. return this._bpmnReplace.replaceElement(shape, boundaryEvent, { layoutConnection: false });
  57. };
  58. // helpers //////////
  59. function getEventDefinition(element) {
  60. var businessObject = getBusinessObject(element),
  61. eventDefinitions = businessObject.eventDefinitions;
  62. return eventDefinitions && eventDefinitions[0];
  63. }
  64. function shouldReplace(shape, host) {
  65. return !isLabel(shape) &&
  66. isAny(shape, [ 'bpmn:IntermediateThrowEvent', 'bpmn:IntermediateCatchEvent' ]) && !!host;
  67. }