BpmnCopyPaste.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import {
  2. getBusinessObject,
  3. is
  4. } from '../../util/ModelUtil';
  5. import {
  6. forEach,
  7. isArray,
  8. isUndefined,
  9. omit,
  10. reduce
  11. } from 'min-dash';
  12. function copyProperties(source, target, properties) {
  13. if (!isArray(properties)) {
  14. properties = [ properties ];
  15. }
  16. forEach(properties, function(property) {
  17. if (!isUndefined(source[property])) {
  18. target[property] = source[property];
  19. }
  20. });
  21. }
  22. function removeProperties(element, properties) {
  23. if (!isArray(properties)) {
  24. properties = [ properties ];
  25. }
  26. forEach(properties, function(property) {
  27. if (element[property]) {
  28. delete element[property];
  29. }
  30. });
  31. }
  32. var LOW_PRIORITY = 750;
  33. export default function BpmnCopyPaste(bpmnFactory, eventBus, moddleCopy) {
  34. eventBus.on('copyPaste.copyElement', LOW_PRIORITY, function(context) {
  35. var descriptor = context.descriptor,
  36. element = context.element;
  37. var businessObject = descriptor.oldBusinessObject = getBusinessObject(element);
  38. descriptor.type = element.type;
  39. copyProperties(businessObject, descriptor, 'name');
  40. descriptor.di = {};
  41. // fill and stroke will be set to DI
  42. copyProperties(businessObject.di, descriptor.di, [
  43. 'fill',
  44. 'stroke'
  45. ]);
  46. copyProperties(businessObject.di, descriptor, 'isExpanded');
  47. if (isLabel(descriptor)) {
  48. return descriptor;
  49. }
  50. // default sequence flow
  51. if (businessObject.default) {
  52. descriptor.default = businessObject.default.id;
  53. }
  54. });
  55. eventBus.on('moddleCopy.canCopyProperty', function(context) {
  56. var parent = context.parent,
  57. property = context.property,
  58. propertyName = context.propertyName,
  59. bpmnProcess;
  60. if (
  61. propertyName === 'processRef' &&
  62. is(parent, 'bpmn:Participant') &&
  63. is(property, 'bpmn:Process')
  64. ) {
  65. bpmnProcess = bpmnFactory.create('bpmn:Process');
  66. // return copy of process
  67. return moddleCopy.copyElement(property, bpmnProcess);
  68. }
  69. });
  70. var references;
  71. function resolveReferences(descriptor, cache) {
  72. var businessObject = getBusinessObject(descriptor);
  73. // default sequence flows
  74. if (descriptor.default) {
  75. // relationship cannot be resolved immediately
  76. references[ descriptor.default ] = {
  77. element: businessObject,
  78. property: 'default'
  79. };
  80. }
  81. // boundary events
  82. if (descriptor.host) {
  83. // relationship can be resolved immediately
  84. getBusinessObject(descriptor).attachedToRef = getBusinessObject(cache[ descriptor.host ]);
  85. }
  86. references = omit(references, reduce(references, function(array, reference, key) {
  87. var element = reference.element,
  88. property = reference.property;
  89. if (key === descriptor.id) {
  90. element[ property ] = businessObject;
  91. array.push(descriptor.id);
  92. }
  93. return array;
  94. }, []));
  95. }
  96. eventBus.on('copyPaste.pasteElements', function() {
  97. references = {};
  98. });
  99. eventBus.on('copyPaste.pasteElement', function(context) {
  100. var cache = context.cache,
  101. descriptor = context.descriptor,
  102. oldBusinessObject = descriptor.oldBusinessObject,
  103. newBusinessObject;
  104. // do NOT copy business object if external label
  105. if (isLabel(descriptor)) {
  106. descriptor.businessObject = getBusinessObject(cache[ descriptor.labelTarget ]);
  107. return;
  108. }
  109. newBusinessObject = bpmnFactory.create(oldBusinessObject.$type);
  110. descriptor.businessObject = moddleCopy.copyElement(
  111. oldBusinessObject,
  112. newBusinessObject
  113. );
  114. // resolve references e.g. default sequence flow
  115. resolveReferences(descriptor, cache);
  116. copyProperties(descriptor, newBusinessObject, [
  117. 'isExpanded',
  118. 'name'
  119. ]);
  120. removeProperties(descriptor, 'oldBusinessObject');
  121. });
  122. }
  123. BpmnCopyPaste.$inject = [
  124. 'bpmnFactory',
  125. 'eventBus',
  126. 'moddleCopy'
  127. ];
  128. // helpers //////////
  129. function isLabel(element) {
  130. return !!element.labelTarget;
  131. }