DetachEventBehavior.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import inherits from 'inherits';
  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. var LOW_PRIORITY = 500;
  9. /**
  10. * Replace boundary event with intermediate event when creating or moving results in detached event.
  11. */
  12. export default function DetachEventBehavior(bpmnReplace, injector) {
  13. injector.invoke(CommandInterceptor, this);
  14. this._bpmnReplace = bpmnReplace;
  15. var self = this;
  16. this.postExecuted('elements.create', LOW_PRIORITY, function(context) {
  17. var elements = context.elements;
  18. elements.filter(function(shape) {
  19. var host = shape.host;
  20. return shouldReplace(shape, host);
  21. }).map(function(shape) {
  22. return elements.indexOf(shape);
  23. }).forEach(function(index) {
  24. context.elements[ index ] = self.replaceShape(elements[ index ]);
  25. });
  26. }, true);
  27. this.preExecute('elements.move', LOW_PRIORITY, function(context) {
  28. var shapes = context.shapes,
  29. newHost = context.newHost;
  30. shapes.forEach(function(shape, index) {
  31. var host = shape.host;
  32. if (shouldReplace(shape, includes(shapes, host) ? host : newHost)) {
  33. shapes[ index ] = self.replaceShape(shape);
  34. }
  35. });
  36. }, true);
  37. }
  38. DetachEventBehavior.$inject = [
  39. 'bpmnReplace',
  40. 'injector'
  41. ];
  42. inherits(DetachEventBehavior, CommandInterceptor);
  43. DetachEventBehavior.prototype.replaceShape = function(shape) {
  44. var eventDefinition = getEventDefinition(shape),
  45. intermediateEvent;
  46. if (eventDefinition) {
  47. intermediateEvent = {
  48. type: 'bpmn:IntermediateCatchEvent',
  49. eventDefinitionType: eventDefinition.$type
  50. };
  51. } else {
  52. intermediateEvent = {
  53. type: 'bpmn:IntermediateThrowEvent'
  54. };
  55. }
  56. return this._bpmnReplace.replaceElement(shape, intermediateEvent, { 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) && is(shape, 'bpmn:BoundaryEvent') && !host;
  66. }
  67. function includes(array, item) {
  68. return array.indexOf(item) !== -1;
  69. }