ReplaceElementBehaviour.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import inherits from 'inherits';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import {
  4. forEach
  5. } from 'min-dash';
  6. import {
  7. isEventSubProcess
  8. } from '../../../util/DiUtil';
  9. import { is } from '../../../util/ModelUtil';
  10. /**
  11. * Defines the behaviour of what happens to the elements inside a container
  12. * that morphs into another BPMN element
  13. */
  14. export default function ReplaceElementBehaviour(
  15. eventBus, bpmnReplace, bpmnRules,
  16. elementRegistry, selection, modeling) {
  17. CommandInterceptor.call(this, eventBus);
  18. this._bpmnReplace = bpmnReplace;
  19. this._elementRegistry = elementRegistry;
  20. this._selection = selection;
  21. this._modeling = modeling;
  22. this.postExecuted([ 'elements.move' ], 500, function(event) {
  23. var context = event.context,
  24. target = context.newParent,
  25. newHost = context.newHost,
  26. elements = [];
  27. forEach(context.closure.topLevel, function(topLevelElements) {
  28. if (isEventSubProcess(topLevelElements)) {
  29. elements = elements.concat(topLevelElements.children);
  30. } else {
  31. elements = elements.concat(topLevelElements);
  32. }
  33. });
  34. // Change target to host when the moving element is a `bpmn:BoundaryEvent`
  35. if (elements.length === 1 && newHost) {
  36. target = newHost;
  37. }
  38. var canReplace = bpmnRules.canReplace(elements, target);
  39. if (canReplace) {
  40. this.replaceElements(elements, canReplace.replacements, newHost);
  41. }
  42. }, this);
  43. // update attachments if the host is replaced
  44. this.postExecute([ 'shape.replace' ], 1500, function(e) {
  45. var context = e.context,
  46. oldShape = context.oldShape,
  47. newShape = context.newShape,
  48. attachers = oldShape.attachers,
  49. canReplace;
  50. if (attachers && attachers.length) {
  51. canReplace = bpmnRules.canReplace(attachers, newShape);
  52. this.replaceElements(attachers, canReplace.replacements);
  53. }
  54. }, this);
  55. this.postExecuted([ 'shape.replace' ], 1500, function(e) {
  56. var context = e.context,
  57. oldShape = context.oldShape,
  58. newShape = context.newShape;
  59. modeling.unclaimId(oldShape.businessObject.id, oldShape.businessObject);
  60. modeling.updateProperties(newShape, { id: oldShape.id });
  61. });
  62. }
  63. inherits(ReplaceElementBehaviour, CommandInterceptor);
  64. ReplaceElementBehaviour.prototype.replaceElements = function(elements, newElements, newHost) {
  65. var elementRegistry = this._elementRegistry,
  66. bpmnReplace = this._bpmnReplace,
  67. selection = this._selection,
  68. modeling = this._modeling;
  69. forEach(newElements, function(replacement) {
  70. var newElement = {
  71. type: replacement.newElementType
  72. };
  73. var oldElement = elementRegistry.get(replacement.oldElementId);
  74. if (newHost && is(oldElement, 'bpmn:BoundaryEvent')) {
  75. modeling.updateAttachment(oldElement, null);
  76. }
  77. var idx = elements.indexOf(oldElement);
  78. elements[idx] = bpmnReplace.replaceElement(oldElement, newElement, { select: false });
  79. if (newHost && is(elements[idx], 'bpmn:BoundaryEvent')) {
  80. modeling.updateAttachment(elements[idx], newHost);
  81. }
  82. });
  83. if (newElements) {
  84. selection.select(elements);
  85. }
  86. };
  87. ReplaceElementBehaviour.$inject = [
  88. 'eventBus',
  89. 'bpmnReplace',
  90. 'bpmnRules',
  91. 'elementRegistry',
  92. 'selection',
  93. 'modeling'
  94. ];