all files / Github/bpmn-js/lib/features/replace/ BpmnReplace.js

98.68% Statements 75/76
96% Branches 72/75
100% Functions 9/9
98.68% Lines 75/76
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275                                                                              42×         42×   30×         12×     42× 20× 20×     22×                         1128×                           157×   157× 157×   157× 52× 42×   20×   20×         137×   137×         137× 137× 137×     137×   137× 2789×     2789× 70×         2719× 55×       2664×       2664×     2660× 31×     2629×     137×     137×         26× 12×       137×   55×   32×     23×           55× 27× 27×         137×       137×                   137×     137×                       58×     137×         11×     137×     137×   137× 116×     137×     1128×                         381×         96×   96× 32×               137× 3086×      
import {
  pick,
  assign,
  filter,
  has
} from 'min-dash';
 
import {
  is,
  getBusinessObject
} from '../../util/ModelUtil';
 
import {
  isAny
} from '../modeling/util/ModelingUtil';
 
import {
  isExpanded,
  isEventSubProcess
} from '../../util/DiUtil';
 
import {
  getProperties,
  IGNORED_PROPERTIES
} from '../../util/model/ModelCloneUtils';
 
import ModelCloneHelper from '../../util/model/ModelCloneHelper';
 
var CUSTOM_PROPERTIES = [
  'cancelActivity',
  'instantiate',
  'eventGatewayType',
  'triggeredByEvent',
  'isInterrupting'
];
 
 
function toggeling(element, target) {
 
  var oldCollapsed = (
    element && has(element, 'collapsed') ? element.collapsed : !isExpanded(element)
  );
 
  var targetCollapsed;
 
  if (target && (has(target, 'collapsed') || has(target, 'isExpanded'))) {
    // property is explicitly set so use it
    targetCollapsed = (
      has(target, 'collapsed') ? target.collapsed : !target.isExpanded
    );
  } else {
    // keep old state
    targetCollapsed = oldCollapsed;
  }
 
  if (oldCollapsed !== targetCollapsed) {
    element.collapsed = oldCollapsed;
    return true;
  }
 
  return false;
}
 
 
 
/**
 * This module takes care of replacing BPMN elements
 */
export default function BpmnReplace(
    bpmnFactory, elementFactory, replace,
    selection, modeling, eventBus
) {
 
  var helper = new ModelCloneHelper(eventBus, bpmnFactory);
 
  /**
   * Prepares a new business object for the replacement element
   * and triggers the replace operation.
   *
   * @param  {djs.model.Base} element
   * @param  {Object} target
   * @param  {Object} [hints]
   *
   * @return {djs.model.Base} the newly created element
   */
  function replaceElement(element, target, hints) {
 
    hints = hints || {};
 
    var type = target.type,
        oldBusinessObject = element.businessObject;
 
    if (isSubProcess(oldBusinessObject)) {
      if (type === 'bpmn:SubProcess') {
        if (toggeling(element, target)) {
          // expanding or collapsing process
          modeling.toggleCollapse(element);
 
          return element;
        }
      }
    }
 
    var newBusinessObject = bpmnFactory.create(type);
 
    var newElement = {
      type: type,
      businessObject: newBusinessObject
    };
 
    var elementProps = getProperties(oldBusinessObject.$descriptor),
        newElementProps = getProperties(newBusinessObject.$descriptor, true),
        copyProps = intersection(elementProps, newElementProps);
 
    // initialize special properties defined in target definition
    assign(newBusinessObject, pick(target, CUSTOM_PROPERTIES));
 
    var properties = filter(copyProps, function(property) {
      var propName = property.replace(/bpmn:/, '');
 
      // copying event definitions, unless we replace
      if (propName === 'eventDefinitions') {
        return hasEventDefinition(element, target.eventDefinitionType);
      }
 
      // retain loop characteristics if the target element
      // is not an event sub process
      if (propName === 'loopCharacteristics') {
        return !isEventSubProcess(newBusinessObject);
      }
 
      // so the applied properties from 'target' don't get lost
      Iif (property in newBusinessObject) {
        return false;
      }
 
      if (propName === 'processRef' && target.isExpanded === false) {
        return false;
      }
 
      if (propName === 'triggeredByEvent') {
        return false;
      }
 
      return IGNORED_PROPERTIES.indexOf(propName) === -1;
    });
 
    newBusinessObject = helper.clone(oldBusinessObject, newBusinessObject, properties);
 
    // initialize custom BPMN extensions
    if (target.eventDefinitionType) {
 
      // only initialize with new eventDefinition
      // if we did not set an event definition yet,
      // i.e. because we cloned it
      if (!hasEventDefinition(newBusinessObject, target.eventDefinitionType)) {
        newElement.eventDefinitionType = target.eventDefinitionType;
      }
    }
 
    if (is(oldBusinessObject, 'bpmn:Activity')) {
 
      if (isSubProcess(oldBusinessObject)) {
        // no toggeling, so keep old state
        newElement.isExpanded = isExpanded(oldBusinessObject);
      }
      // else if property is explicitly set, use it
      else if (target && has(target, 'isExpanded')) {
        newElement.isExpanded = target.isExpanded;
      }
 
      // TODO: need also to respect min/max Size
      // copy size, from an expanded subprocess to an expanded alternative subprocess
      // except bpmn:Task, because Task is always expanded
      if ((isExpanded(oldBusinessObject) && !is(oldBusinessObject, 'bpmn:Task')) && newElement.isExpanded) {
        newElement.width = element.width;
        newElement.height = element.height;
      }
    }
 
    // remove children if not expanding sub process
    if (isSubProcess(oldBusinessObject) && !isSubProcess(newBusinessObject)) {
      hints.moveChildren = false;
    }
 
    // transform collapsed/expanded pools
    if (is(oldBusinessObject, 'bpmn:Participant')) {
 
      // create expanded pool
      if (target.isExpanded === true) {
        newBusinessObject.processRef = bpmnFactory.create('bpmn:Process');
      } else {
        // remove children when transforming to collapsed pool
        hints.moveChildren = false;
      }
 
      // apply same width and default height
      newElement.width = element.width;
      newElement.height = elementFactory._getDefaultSize(newBusinessObject).height;
    }
 
    newBusinessObject.name = oldBusinessObject.name;
 
    // retain default flow's reference between inclusive <-> exclusive gateways and activities
    if (
      isAny(oldBusinessObject, [
        'bpmn:ExclusiveGateway',
        'bpmn:InclusiveGateway',
        'bpmn:Activity'
      ]) &&
      isAny(newBusinessObject, [
        'bpmn:ExclusiveGateway',
        'bpmn:InclusiveGateway',
        'bpmn:Activity'
      ])
    ) {
      newBusinessObject.default = oldBusinessObject.default;
    }
 
    if (
      target.host &&
      !is(oldBusinessObject, 'bpmn:BoundaryEvent') &&
      is(newBusinessObject, 'bpmn:BoundaryEvent')
    ) {
      newElement.host = target.host;
    }
 
    if ('fill' in oldBusinessObject.di || 'stroke' in oldBusinessObject.di) {
      assign(newElement, { colors: pick(oldBusinessObject.di, [ 'fill', 'stroke' ]) });
    }
 
    newElement = replace.replaceElement(element, newElement, hints);
 
    if (hints.select !== false) {
      selection.select(newElement);
    }
 
    return newElement;
  }
 
  this.replaceElement = replaceElement;
}
 
BpmnReplace.$inject = [
  'bpmnFactory',
  'elementFactory',
  'replace',
  'selection',
  'modeling',
  'eventBus'
];
 
 
function isSubProcess(bo) {
  return is(bo, 'bpmn:SubProcess');
}
 
function hasEventDefinition(element, type) {
 
  var bo = getBusinessObject(element);
 
  return type && bo.get('eventDefinitions').some(function(definition) {
    return is(definition, type);
  });
}
 
/**
 * Compute intersection between two arrays.
 */
function intersection(a1, a2) {
  return a1.filter(function(el) {
    return a2.indexOf(el) !== -1;
  });
}