# all files / Github/diagram-js/lib/features/grid-snapping/behavior/ ResizeBehavior.js

83.64% Statements 46/55
83.33% Branches 15/18
83.33% Functions 5/6
83.64% Lines 46/55

    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
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180

    74×

    74×

    74×

    74×
    20×
    20×
    20×

    20×
    15×

    5×
    5×

    5×
    5×

    1×

    1×

    1×

    1×
    5×
    3×

    5×
    4×

    5×

    1×
    3×
    3×
    3×

    3×

    3×

    3×

    1×
    1×

    1×

    3×

    3×

    1×
    4×
    4×
    4×

    4×

    4×

    4×

    1×
    1×

    1×

    4×

    4×
     import inherits from 'inherits';

    import CommandInterceptor from '../../../command/CommandInterceptor';

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

    /**
     * Integrates resizing with grid snapping.
     */
    export default function ResizeBehavior(eventBus, gridSnapping) {
      CommandInterceptor.call(this, eventBus);

      this._gridSnapping = gridSnapping;

      var self = this;

      this.preExecute('shape.resize', function(event) {
        var context = event.context,
            hints = context.hints || {},
            autoResize = hints.autoResize;

        if (!autoResize) {
          return;
        }

        var shape = context.shape,
            newBounds = context.newBounds;

        Eif (isString(autoResize)) {
          context.newBounds = self.snapComplex(newBounds, autoResize);
        } else {
          context.newBounds = self.snapSimple(shape, newBounds);
        }
      });
    }

    ResizeBehavior.$inject = [
      'eventBus',
      'gridSnapping',
      'modeling'
    ];

    inherits(ResizeBehavior, CommandInterceptor);

    /**
     * Snap width and height in relation to center.
     *
     * @param {djs.model.shape} shape
     * @param {Bounds} newBounds
     *
     * @returns {Bounds} Snapped bounds.
     */
    ResizeBehavior.prototype.snapSimple = function(shape, newBounds) {
      var gridSnapping = this._gridSnapping;

      newBounds.width = gridSnapping.snapValue(newBounds.width, {
        min: newBounds.width
      });

      newBounds.height = gridSnapping.snapValue(newBounds.height, {
        min: newBounds.height
      });

      newBounds.x = shape.x + (shape.width / 2) - (newBounds.width / 2);
      newBounds.y = shape.y + (shape.height / 2) - (newBounds.height / 2);

      return newBounds;
    };

    /**
     * Snap x, y, width and height according to given directions.
     *
     * @param {Bounds} newBounds
     * @param {string} directions - Directions as {n|w|s|e}.
     *
     * @returns {Bounds} Snapped bounds.
     */
    ResizeBehavior.prototype.snapComplex = function(newBounds, directions) {
      if (/w|e/.test(directions)) {
        newBounds = this.snapHorizontally(newBounds, directions);
      }

      if (/n|s/.test(directions)) {
        newBounds = this.snapVertically(newBounds, directions);
      }

      return newBounds;
    };

    /**
     * Snap in one or both directions horizontally.
     *
     * @param {Bounds} newBounds
     * @param {string} directions - Directions as {n|w|s|e}.
     *
     * @returns {Bounds} Snapped bounds.
     */
    ResizeBehavior.prototype.snapHorizontally = function(newBounds, directions) {
      var gridSnapping = this._gridSnapping,
          west = /w/.test(directions),
          east = /e/.test(directions);

      var snappedNewBounds = {};

      snappedNewBounds.width = gridSnapping.snapValue(newBounds.width, {
        min: newBounds.width
      });

      if (east) {

        // handle <we>
        Eif (west) {
          snappedNewBounds.x = gridSnapping.snapValue(newBounds.x, {
            max: newBounds.x
          });

          snappedNewBounds.width += gridSnapping.snapValue(newBounds.x - snappedNewBounds.x, {
            min: newBounds.x - snappedNewBounds.x
          });
        }

        // handle <e>
        else {
          newBounds.x = newBounds.x + newBounds.width - snappedNewBounds.width;
        }
      }

      // assign snapped x and width
      assign(newBounds, snappedNewBounds);

      return newBounds;
    };

    /**
     * Snap in one or both directions vertically.
     *
     * @param {Bounds} newBounds
     * @param {string} directions - Directions as {n|w|s|e}.
     *
     * @returns {Bounds} Snapped bounds.
     */
    ResizeBehavior.prototype.snapVertically = function(newBounds, directions) {
      var gridSnapping = this._gridSnapping,
          north = /n/.test(directions),
          south = /s/.test(directions);

      var snappedNewBounds = {};

      snappedNewBounds.height = gridSnapping.snapValue(newBounds.height, {
        min: newBounds.height
      });

      if (north) {

        // handle <ns>
        Eif (south) {
          snappedNewBounds.y = gridSnapping.snapValue(newBounds.y, {
            max: newBounds.y
          });

          snappedNewBounds.height += gridSnapping.snapValue(newBounds.y - snappedNewBounds.y, {
            min: newBounds.y - snappedNewBounds.y
          });
        }

        // handle <n>
        else {
          newBounds.y = newBounds.y + newBounds.height - snappedNewBounds.height;
        }
      }

      // assign snapped y and height
      assign(newBounds, snappedNewBounds);

      return newBounds;
    };