BpmnCopyPaste.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {
  2. getBusinessObject,
  3. is
  4. } from '../../util/ModelUtil';
  5. import ModelCloneHelper from '../../util/model/ModelCloneHelper';
  6. import {
  7. getProperties,
  8. IGNORED_PROPERTIES
  9. } from '../../util/model/ModelCloneUtils';
  10. import {
  11. filter,
  12. forEach
  13. } from 'min-dash';
  14. function setProperties(descriptor, data, properties) {
  15. forEach(properties, function(property) {
  16. if (data[property] !== undefined) {
  17. descriptor[property] = data[property];
  18. }
  19. });
  20. }
  21. function removeProperties(element, properties) {
  22. forEach(properties, function(prop) {
  23. if (element[prop]) {
  24. delete element[prop];
  25. }
  26. });
  27. }
  28. export default function BpmnCopyPaste(
  29. bpmnFactory, eventBus, copyPaste,
  30. clipboard, canvas, bpmnRules) {
  31. var helper = new ModelCloneHelper(eventBus, bpmnFactory);
  32. copyPaste.registerDescriptor(function(element, descriptor) {
  33. var businessObject = descriptor.oldBusinessObject = getBusinessObject(element);
  34. var colors = {};
  35. descriptor.type = element.type;
  36. setProperties(descriptor, businessObject.di, [ 'isExpanded' ]);
  37. setProperties(colors, businessObject.di, [ 'fill', 'stroke' ]);
  38. descriptor.colors = colors;
  39. if (element.type === 'label') {
  40. return descriptor;
  41. }
  42. setProperties(descriptor, businessObject, [
  43. 'processRef',
  44. 'triggeredByEvent'
  45. ]);
  46. if (businessObject.default) {
  47. descriptor.default = businessObject.default.id;
  48. }
  49. return descriptor;
  50. });
  51. eventBus.on('element.paste', function(context) {
  52. var descriptor = context.descriptor,
  53. createdElements = context.createdElements,
  54. parent = descriptor.parent,
  55. rootElement = canvas.getRootElement(),
  56. oldBusinessObject = descriptor.oldBusinessObject,
  57. newBusinessObject,
  58. source,
  59. target,
  60. canConnect;
  61. newBusinessObject = bpmnFactory.create(oldBusinessObject.$type);
  62. var properties = getProperties(oldBusinessObject.$descriptor);
  63. properties = filter(properties, function(property) {
  64. return IGNORED_PROPERTIES.indexOf(property.replace(/bpmn:/, '')) === -1;
  65. });
  66. descriptor.businessObject = helper.clone(oldBusinessObject, newBusinessObject, properties);
  67. if (descriptor.type === 'label') {
  68. return;
  69. }
  70. if (is(parent, 'bpmn:Process')) {
  71. descriptor.parent = is(rootElement, 'bpmn:Collaboration') ? rootElement : parent;
  72. }
  73. if (descriptor.type === 'bpmn:DataOutputAssociation' ||
  74. descriptor.type === 'bpmn:DataInputAssociation' ||
  75. descriptor.type === 'bpmn:MessageFlow') {
  76. descriptor.parent = rootElement;
  77. }
  78. if (is(parent, 'bpmn:Lane')) {
  79. descriptor.parent = parent.parent;
  80. }
  81. // make sure that the correct type of connection is created
  82. if (descriptor.waypoints) {
  83. source = createdElements[descriptor.source];
  84. target = createdElements[descriptor.target];
  85. if (source && target) {
  86. source = source.element;
  87. target = target.element;
  88. }
  89. canConnect = bpmnRules.canConnect(source, target);
  90. if (canConnect) {
  91. descriptor.type = canConnect.type;
  92. }
  93. }
  94. // remove the id or else we cannot paste multiple times
  95. delete newBusinessObject.id;
  96. // assign an ID
  97. bpmnFactory._ensureId(newBusinessObject);
  98. if (descriptor.type === 'bpmn:Participant' && descriptor.processRef) {
  99. descriptor.processRef = newBusinessObject.processRef = bpmnFactory.create('bpmn:Process');
  100. }
  101. setProperties(newBusinessObject, descriptor, [
  102. 'isExpanded',
  103. 'triggeredByEvent'
  104. ]);
  105. removeProperties(descriptor, [
  106. 'triggeredByEvent'
  107. ]);
  108. });
  109. }
  110. BpmnCopyPaste.$inject = [
  111. 'bpmnFactory',
  112. 'eventBus',
  113. 'copyPaste',
  114. 'clipboard',
  115. 'canvas',
  116. 'bpmnRules'
  117. ];