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

97.56% Statements 40/41
62.5% Branches 5/8
87.5% Functions 7/8
97.56% Lines 40/41

    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

    1189×

    1189×

    1×

    1×

    905×
    905×
    905×
    905×
    905×

    905×

    905×
    905×

    905×

    905×

    905×

    1×

    828×
    828×
    828×

    828×

    828×

    175×
    2×

    175×
    19×

    828×
    175×

    1×

    129×
    129×
    129×
    129×

    129×

    129×

    129×

    1×

    175×
    175×

    175×

    1×

     import {
      assign,
      forEach,
      pick
    } from 'min-dash';

    import MoveHelper from './helper/MoveHelper';

    import {
      add as collectionAdd,
      remove as collectionRemove
    } from '../../../util/Collections';

    import {
      getMovedSourceAnchor,
      getMovedTargetAnchor
    } from './helper/AnchorsHelper';

    /**
     * A handler that implements reversible moving of shapes.
     */
    export default function MoveShapeHandler(modeling) {
      this._modeling = modeling;

      this._helper = new MoveHelper(modeling);
    }

    MoveShapeHandler.$inject = [ 'modeling' ];

    MoveShapeHandler.prototype.execute = function(context) {

      var shape = context.shape,
          delta = context.delta,
          newParent = context.newParent || shape.parent,
          newParentIndex = context.newParentIndex,
          oldParent = shape.parent;

      context.oldBounds = pick(shape, [ 'x', 'y', 'width', 'height']);

      // save old parent in context
      context.oldParent = oldParent;
      context.oldParentIndex = collectionRemove(oldParent.children, shape);

      // add to new parent at position
      collectionAdd(newParent.children, shape, newParentIndex);

      // update shape parent + position
      assign(shape, {
        parent: newParent,
        x: shape.x + delta.x,
        y: shape.y + delta.y
      });

      return shape;
    };

    MoveShapeHandler.prototype.postExecute = function(context) {

      var shape = context.shape,
          delta = context.delta,
          hints = context.hints;

      var modeling = this._modeling;

      if (hints.layout !== false) {

        forEach(shape.incoming, function(c) {
          modeling.layoutConnection(c, {
            connectionEnd: getMovedTargetAnchor(c, shape, delta)
          });
        });

        forEach(shape.outgoing, function(c) {
          modeling.layoutConnection(c, {
            connectionStart: getMovedSourceAnchor(c, shape, delta)
          });
        });
      }

      if (hints.recurse !== false) {
        this.moveChildren(context);
      }
    };

    MoveShapeHandler.prototype.revert = function(context) {

      var shape = context.shape,
          oldParent = context.oldParent,
          oldParentIndex = context.oldParentIndex,
          delta = context.delta;

      // restore previous location in old parent
      collectionAdd(oldParent.children, shape, oldParentIndex);

      // revert to old position and parent
      assign(shape, {
        parent: oldParent,
        x: shape.x - delta.x,
        y: shape.y - delta.y
      });

      return shape;
    };

    MoveShapeHandler.prototype.moveChildren = function(context) {

      var delta = context.delta,
          shape = context.shape;

      this._helper.moveRecursive(shape.children, delta, null);
    };

    MoveShapeHandler.prototype.getNewParent = function(context) {
      return context.newParent || context.shape.parent;
    };