# all files / diagram-js/lib/features/replace/ Replace.js

100% Statements 11/11
83.33% Branches 5/6
100% Functions 2/2
100% Lines 11/11

    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
    411×

    1189×

    1×

    1×

    138×

    138×

    138×

    138×
    138×

    138×

    138×

     var round = Math.round;

    /**
     * Service that allow replacing of elements.
     */
    export default function Replace(modeling) {

      this._modeling = modeling;
    }

    Replace.$inject = [ 'modeling' ];

    /**
     * @param {Element} oldElement - Element to be replaced
     * @param {Object}  newElementData - Containing information about the new Element, for example height, width, type.
     * @param {Object}  options - Custom options that will be attached to the context. It can be used to inject data
     *                            that is needed in the command chain. For example it could be used in
     *                            eventbus.on('commandStack.shape.replace.postExecute') to change shape attributes after
     *                            shape creation.
     */
    Replace.prototype.replaceElement = function(oldElement, newElementData, options) {

      var modeling = this._modeling;

      var newElement = null;

      Iif (oldElement.waypoints) {
        // TODO
        // modeling.replaceConnection
      } else {
        // set center of element for modeling API
        // if no new width / height is given use old elements size
        newElementData.x = round(oldElement.x + (newElementData.width || oldElement.width) / 2);
        newElementData.y = round(oldElement.y + (newElementData.height || oldElement.height) / 2);

        newElement = modeling.replaceShape(oldElement, newElementData, options);
      }

      return newElement;
    };