all files / Github/bpmn-js/lib/features/copy-paste/ BpmnCopyPaste.js

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