# all files / diagram-js/lib/features/modeling/cmd/ ReplaceShapeHandler.js

100% Statements 53/53
100% Branches 12/12
100% Functions 13/13
100% Lines 53/53

    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

    1189×
    1189×

    1×

    1×

    138×
    138×
    138×

    138×
    138×
    138×

    115×

    138×

    138×

    138×
    21×

    138×
    132×

    132×

    138×
    138×

    138×
    46×
    46×
    46×
    46×

    46×
    31×

    138×
    69×
    69×
    69×
    69×

    69×
    58×

    1×
    138×

    138×
    138×
    138×

    138×
    119×
    31×

    119×
    52×

    138×

    1×

    1×

    1×
    138×

    1×
    58×

    1×
    31×

     import { forEach } from 'min-dash';

    /**
     * A handler that implements reversible replacing of shapes.
     * Internally the old shape will be removed and the new shape will be added.
     *
     *
     * @class
     * @constructor
     *
     * @param {canvas} Canvas
     */
    export default function ReplaceShapeHandler(modeling, rules) {
      this._modeling = modeling;
      this._rules = rules;
    }

    ReplaceShapeHandler.$inject = [ 'modeling', 'rules' ];

    // api //////////////////////

    /**
     * Replaces a shape with an replacement Element.
     *
     * The newData object should contain type, x, y.
     *
     * If possible also the incoming/outgoing connection
     * will be restored.
     *
     * @param {Object} context
     */
    ReplaceShapeHandler.prototype.preExecute = function(context) {

      var self = this,
          modeling = this._modeling,
          rules = this._rules;

      var oldShape = context.oldShape,
          newData = context.newData,
          hints = context.hints,
          newShape;

      function canReconnect(type, source, target, connection) {
        return rules.allowed(type, {
          source: source,
          target: target,
          connection: connection
        });
      }

      // (1) place a new shape at the given position

      var position = {
        x: newData.x,
        y: newData.y
      };

      newShape = context.newShape =
        context.newShape ||
        self.createShape(newData, position, oldShape.parent, hints);

      // (2) update the host

      if (oldShape.host) {
        modeling.updateAttachment(newShape, oldShape.host);
      }

      // (3) adopt all children from the old shape

      var children;

      if (hints.moveChildren !== false) {
        children = oldShape.children.slice();

        modeling.moveElements(children, { x: 0, y: 0 }, newShape);
      }

      // (4) reconnect connections to the new shape (where allowed)

      var incoming = oldShape.incoming.slice(),
          outgoing = oldShape.outgoing.slice();

      forEach(incoming, function(connection) {
        var waypoints = connection.waypoints,
            docking = waypoints[waypoints.length - 1],
            source = connection.source,
            allowed = canReconnect('connection.reconnectEnd', source, newShape, connection);

        if (allowed) {
          self.reconnectEnd(connection, newShape, docking, { layoutConnection: hints.layoutConnection });
        }
      });

      forEach(outgoing, function(connection) {
        var waypoints = connection.waypoints,
            docking = waypoints[0],
            target = connection.target,
            allowed = canReconnect('connection.reconnectStart', newShape, target, connection);

        if (allowed) {
          self.reconnectStart(connection, newShape, docking, { layoutConnection: hints.layoutConnection });
        }

      });
    };

    ReplaceShapeHandler.prototype.postExecute = function(context) {
      var modeling = this._modeling;

      var oldShape = context.oldShape,
          newShape = context.newShape,
          layoutConnection = context.hints.layoutConnection;

      // do not layout if explicitly excluded
      if (layoutConnection !== false) {
        forEach(newShape.incoming, function(c) {
          modeling.layoutConnection(c, { endChanged: true });
        });

        forEach(newShape.outgoing, function(c) {
          modeling.layoutConnection(c, { startChanged: true });
        });
      }

      modeling.removeShape(oldShape);
    };

    ReplaceShapeHandler.prototype.execute = function(context) {};

    ReplaceShapeHandler.prototype.revert = function(context) {};

    ReplaceShapeHandler.prototype.createShape = function(shape, position, target, hints) {
      return this._modeling.createShape(shape, position, target, hints);
    };

    ReplaceShapeHandler.prototype.reconnectStart = function(connection, newSource, dockingPoint, hints) {
      this._modeling.reconnectStart(connection, newSource, dockingPoint, hints);
    };

    ReplaceShapeHandler.prototype.reconnectEnd = function(connection, newTarget, dockingPoint, hints) {
      this._modeling.reconnectEnd(connection, newTarget, dockingPoint, hints);
    };