all files / Github/bpmn-js/lib/features/modeling/behavior/ ReplaceElementBehaviour.js

98.04% Statements 50/51
95.45% Branches 21/22
100% Functions 7/7
98.04% Lines 50/51
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                                              1128×   1128× 1128× 1128× 1128×   1128×   406× 406× 406× 406×   406× 564×     564×         406× 17×     406×   406× 18×         1128×   137× 137× 137× 137×     137× 19×   19×         1128× 137× 137× 137×   137× 137×           37× 37× 37× 37×   37×   21×       21×   21×     21×   21×   21×       37× 20×                      
import inherits from 'inherits';
 
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
 
import {
  forEach
} from 'min-dash';
 
import {
  isEventSubProcess
} from '../../../util/DiUtil';
 
import { is } from '../../../util/ModelUtil';
 
 
/**
 * Defines the behaviour of what happens to the elements inside a container
 * that morphs into another BPMN element
 */
export default function ReplaceElementBehaviour(
    eventBus, bpmnReplace, bpmnRules,
    elementRegistry, selection, modeling) {
 
  CommandInterceptor.call(this, eventBus);
 
  this._bpmnReplace = bpmnReplace;
  this._elementRegistry = elementRegistry;
  this._selection = selection;
  this._modeling = modeling;
 
  this.postExecuted([ 'elements.move' ], 500, function(event) {
 
    var context = event.context,
        target = context.newParent,
        newHost = context.newHost,
        elements = [];
 
    forEach(context.closure.topLevel, function(topLevelElements) {
      Iif (isEventSubProcess(topLevelElements)) {
        elements = elements.concat(topLevelElements.children);
      } else {
        elements = elements.concat(topLevelElements);
      }
    });
 
    // Change target to host when the moving element is a `bpmn:BoundaryEvent`
    if (elements.length === 1 && newHost) {
      target = newHost;
    }
 
    var canReplace = bpmnRules.canReplace(elements, target);
 
    if (canReplace) {
      this.replaceElements(elements, canReplace.replacements, newHost);
    }
  }, this);
 
  // update attachments if the host is replaced
  this.postExecute([ 'shape.replace' ], 1500, function(e) {
 
    var context = e.context,
        oldShape = context.oldShape,
        newShape = context.newShape,
        attachers = oldShape.attachers,
        canReplace;
 
    if (attachers && attachers.length) {
      canReplace = bpmnRules.canReplace(attachers, newShape);
 
      this.replaceElements(attachers, canReplace.replacements);
    }
 
  }, this);
 
  this.postExecuted([ 'shape.replace' ], 1500, function(e) {
    var context = e.context,
        oldShape = context.oldShape,
        newShape = context.newShape;
 
    modeling.unclaimId(oldShape.businessObject.id, oldShape.businessObject);
    modeling.updateProperties(newShape, { id: oldShape.id });
  });
}
 
inherits(ReplaceElementBehaviour, CommandInterceptor);
 
 
ReplaceElementBehaviour.prototype.replaceElements = function(elements, newElements, newHost) {
  var elementRegistry = this._elementRegistry,
      bpmnReplace = this._bpmnReplace,
      selection = this._selection,
      modeling = this._modeling;
 
  forEach(newElements, function(replacement) {
 
    var newElement = {
      type: replacement.newElementType
    };
 
    var oldElement = elementRegistry.get(replacement.oldElementId);
 
    if (newHost && is(oldElement, 'bpmn:BoundaryEvent')) {
      modeling.updateAttachment(oldElement, null);
    }
 
    var idx = elements.indexOf(oldElement);
 
    elements[idx] = bpmnReplace.replaceElement(oldElement, newElement, { select: false });
 
    if (newHost && is(elements[idx], 'bpmn:BoundaryEvent')) {
      modeling.updateAttachment(elements[idx], newHost);
    }
  });
 
  if (newElements) {
    selection.select(elements);
  }
};
 
ReplaceElementBehaviour.$inject = [
  'eventBus',
  'bpmnReplace',
  'bpmnRules',
  'elementRegistry',
  'selection',
  'modeling'
];