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

89.29% Statements 25/28
81.25% Branches 13/16
100% Functions 6/6
89.29% Lines 25/28

    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

    1128×

    1×

    1×
    317×
    317×
    317×

    317×

    317×

    317×

    317×

    317×

    317×

    1×

    308×
    308×

    308×

    308×
    28×

    308×
    26×

    1×

    33×
    33×

    33×

    33×

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

    import {
      getResizedSourceAnchor,
      getResizedTargetAnchor
    } from './helper/AnchorsHelper';

    /**
     * A handler that implements reversible resizing of shapes.
     *
     * @param {Modeling} modeling
     */
    export default function ResizeShapeHandler(modeling) {
      this._modeling = modeling;
    }

    ResizeShapeHandler.$inject = [ 'modeling' ];

    /**
     * {
     *   shape: {....}
     *   newBounds: {
     *     width:  20,
     *     height: 40,
     *     x:       5,
     *     y:      10
     *   }
     *
     * }
     */
    ResizeShapeHandler.prototype.execute = function(context) {
      var shape = context.shape,
          newBounds = context.newBounds,
          minBounds = context.minBounds;

      Iif (newBounds.x === undefined || newBounds.y === undefined ||
          newBounds.width === undefined || newBounds.height === undefined) {
        throw new Error('newBounds must have {x, y, width, height} properties');
      }

      Iif (minBounds && (newBounds.width < minBounds.width
        || newBounds.height < minBounds.height)) {
        throw new Error('width and height cannot be less than minimum height and width');
      } else Iif (!minBounds
        && newBounds.width < 10 || newBounds.height < 10) {
        throw new Error('width and height cannot be less than 10px');
      }

      // save old bbox in context
      context.oldBounds = {
        width:  shape.width,
        height: shape.height,
        x:      shape.x,
        y:      shape.y
      };

      // update shape
      assign(shape, {
        width:  newBounds.width,
        height: newBounds.height,
        x:      newBounds.x,
        y:      newBounds.y
      });

      return shape;
    };

    ResizeShapeHandler.prototype.postExecute = function(context) {

      var shape = context.shape,
          oldBounds = context.oldBounds;

      var modeling = this._modeling;

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

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

    };

    ResizeShapeHandler.prototype.revert = function(context) {

      var shape = context.shape,
          oldBounds = context.oldBounds;

      // restore previous bbox
      assign(shape, {
        width:  oldBounds.width,
        height: oldBounds.height,
        x:      oldBounds.x,
        y:      oldBounds.y
      });

      return shape;
    };