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

100% Statements 56/56
91.3% Branches 21/23
100% Functions 12/12
100% Lines 56/56
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 160                            624× 332×     624× 916× 96×           126× 126×     126× 126× 126×                 1189× 166× 166×   166×   166×   166×   166×     166×         166×   166× 21×       145×       1189× 5097× 5097× 5097×   5097×                 126×     126×           126× 31× 31×   31×       31×       1189× 25×     1189× 143× 143× 143×       143× 17×   17×     126×   126×           126×   126×         126×                           309×  
import {
  getBusinessObject,
  is
} from '../../util/ModelUtil';
 
import {
  forEach,
  isArray,
  isUndefined,
  omit,
  reduce
} from 'min-dash';
 
function copyProperties(source, target, properties) {
  if (!isArray(properties)) {
    properties = [ properties ];
  }
 
  forEach(properties, function(property) {
    if (!isUndefined(source[property])) {
      target[property] = source[property];
    }
  });
}
 
function removeProperties(element, properties) {
  Eif (!isArray(properties)) {
    properties = [ properties ];
  }
 
  forEach(properties, function(property) {
    Eif (element[property]) {
      delete element[property];
    }
  });
}
 
var LOW_PRIORITY = 750;
 
 
export default function BpmnCopyPaste(bpmnFactory, eventBus, moddleCopy) {
 
  eventBus.on('copyPaste.copyElement', LOW_PRIORITY, function(context) {
    var descriptor = context.descriptor,
        element = context.element;
 
    var businessObject = descriptor.oldBusinessObject = getBusinessObject(element);
 
    descriptor.type = element.type;
 
    copyProperties(businessObject, descriptor, 'name');
 
    descriptor.di = {};
 
    // fill and stroke will be set to DI
    copyProperties(businessObject.di, descriptor.di, [
      'fill',
      'stroke'
    ]);
 
    copyProperties(businessObject.di, descriptor, 'isExpanded');
 
    if (isLabel(descriptor)) {
      return descriptor;
    }
 
    // default sequence flow
    if (businessObject.default) {
      descriptor.default = businessObject.default.id;
    }
  });
 
  eventBus.on('moddleCopy.canCopyProperty', function(context) {
    var parent = context.parent,
        property = context.property,
        propertyName = context.propertyName;
 
    if (is(parent, 'bpmn:Participant') &&
      is(property, 'bpmn:Process') &&
      propertyName === 'processRef') {
      return bpmnFactory.create('bpmn:Process');
    }
  });
 
  var references;
 
  function resolveReferences(descriptor) {
    var businessObject = getBusinessObject(descriptor);
 
    // default sequence flows
    if (descriptor.default) {
      references[ descriptor.default ] = {
        element: businessObject,
        property: 'default'
      };
    }
 
    references = omit(references, reduce(references, function(array, reference, key) {
      var element = reference.element,
          property = reference.property;
 
      if (key === descriptor.id) {
        element[ property ] = businessObject;
 
        array.push(descriptor.id);
      }
 
      return array;
    }, []));
  }
 
  eventBus.on('copyPaste.pasteElements', function() {
    references = {};
  });
 
  eventBus.on('copyPaste.pasteElement', function(context) {
    var cache = context.cache,
        descriptor = context.descriptor,
        oldBusinessObject = descriptor.oldBusinessObject,
        newBusinessObject;
 
    // do NOT copy business object if external label
    if (isLabel(descriptor)) {
      descriptor.businessObject = getBusinessObject(cache[ descriptor.labelTarget ]);
 
      return;
    }
 
    newBusinessObject = bpmnFactory.create(oldBusinessObject.$type);
 
    descriptor.businessObject = moddleCopy.copyElement(
      oldBusinessObject,
      newBusinessObject
    );
 
    // resolve references e.g. default sequence flow
    resolveReferences(descriptor);
 
    copyProperties(descriptor, newBusinessObject, [
      'isExpanded',
      'name'
    ]);
 
    removeProperties(descriptor, 'oldBusinessObject');
  });
 
}
 
 
BpmnCopyPaste.$inject = [
  'bpmnFactory',
  'eventBus',
  'moddleCopy'
];
 
// helpers //////////
 
function isLabel(element) {
  return !!element.labelTarget;
}