ReplaceElementBehaviour.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import inherits from 'inherits';
  2. import { forEach } from 'min-dash';
  3. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  4. import { isEventSubProcess } from '../../../util/DiUtil';
  5. /**
  6. * BPMN-specific replace behavior.
  7. */
  8. export default function ReplaceElementBehaviour(
  9. bpmnReplace,
  10. bpmnRules,
  11. elementRegistry,
  12. injector,
  13. modeling,
  14. selection
  15. ) {
  16. injector.invoke(CommandInterceptor, this);
  17. this._bpmnReplace = bpmnReplace;
  18. this._elementRegistry = elementRegistry;
  19. this._selection = selection;
  20. // replace elements on move
  21. this.postExecuted([ 'elements.move' ], 500, function(event) {
  22. var context = event.context,
  23. target = context.newParent,
  24. newHost = context.newHost,
  25. elements = [];
  26. forEach(context.closure.topLevel, function(topLevelElements) {
  27. if (isEventSubProcess(topLevelElements)) {
  28. elements = elements.concat(topLevelElements.children);
  29. } else {
  30. elements = elements.concat(topLevelElements);
  31. }
  32. });
  33. // set target to host if attaching
  34. if (elements.length === 1 && newHost) {
  35. target = newHost;
  36. }
  37. var canReplace = bpmnRules.canReplace(elements, target);
  38. if (canReplace) {
  39. this.replaceElements(elements, canReplace.replacements, newHost);
  40. }
  41. }, this);
  42. // update attachments on host replace
  43. this.postExecute([ 'shape.replace' ], 1500, function(e) {
  44. var context = e.context,
  45. oldShape = context.oldShape,
  46. newShape = context.newShape,
  47. attachers = oldShape.attachers,
  48. canReplace;
  49. if (attachers && attachers.length) {
  50. canReplace = bpmnRules.canReplace(attachers, newShape);
  51. this.replaceElements(attachers, canReplace.replacements);
  52. }
  53. }, this);
  54. // keep ID on shape replace
  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) {
  65. var elementRegistry = this._elementRegistry,
  66. bpmnReplace = this._bpmnReplace,
  67. selection = this._selection;
  68. forEach(newElements, function(replacement) {
  69. var newElement = {
  70. type: replacement.newElementType
  71. };
  72. var oldElement = elementRegistry.get(replacement.oldElementId);
  73. var idx = elements.indexOf(oldElement);
  74. elements[idx] = bpmnReplace.replaceElement(oldElement, newElement, { select: false });
  75. });
  76. if (newElements) {
  77. selection.select(elements);
  78. }
  79. };
  80. ReplaceElementBehaviour.$inject = [
  81. 'bpmnReplace',
  82. 'bpmnRules',
  83. 'elementRegistry',
  84. 'injector',
  85. 'modeling',
  86. 'selection'
  87. ];