# all files / diagram-js/lib/features/resize/ Resize.js

96.36% Statements 53/55
66.67% Branches 16/24
100% Functions 11/11
96.36% Lines 53/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
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248

    1×

    153×
    153×

    153×

    20×
    20×
    20×

    20×

    20×

    20×

    20×

    10×

    10×

    10×
    8×

    2×
    2×

    2×

    2×
    2×
    2×

    2×

    2×

    2×

    2×

    153×
    10×

    153×
    20×

    20×

    153×
    2×

    1×
    29×

    29×

    29×

    1×
    10×

    10×
    10×

    10×

    10×

    10×

    10×

    10×

    1×
    2×
    2×

    2×

    2×

    2×

    1×

    2×

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

    import {
      resizeBounds,
      ensureConstraints,
      computeChildrenBBox,
      getMinResizeBounds
    } from './ResizeUtil';

    import {
      asTRBL,
      roundBounds
    } from '../../layout/LayoutUtil';

    var DEFAULT_MIN_WIDTH = 10;

    /**
     * A component that provides resizing of shapes on the canvas.
     *
     * The following components are part of shape resize:
     *
     *  * adding resize handles,
     *  * creating a visual during resize
     *  * checking resize rules
     *  * committing a change once finished
     *
     *
     * ## Customizing
     *
     * It's possible to customize the resizing behaviour by intercepting 'resize.start'
     * and providing the following parameters through the 'context':
     *
     *   * minDimensions ({ width, height }): minimum shape dimensions
     *
     *   * childrenBoxPadding ({ left, top, bottom, right } || number):
     *     gap between the minimum bounding box and the container
     *
     * f.ex:
     *
     * ```javascript
     * eventBus.on('resize.start', 1500, function(event) {
     *   var context = event.context,
     *
     *  context.minDimensions = { width: 140, height: 120 };
     *
     *  // Passing general padding
     *  context.childrenBoxPadding = 30;
     *
     *  // Passing padding to a specific side
     *  context.childrenBoxPadding.left = 20;
     * });
     * ```
     */
    export default function Resize(eventBus, rules, modeling, dragging) {

      this._dragging = dragging;
      this._rules = rules;

      var self = this;

      /**
       * Handle resize move by specified delta.
       *
       * @param {Object} context
       * @param {Point} delta
       */
      function handleMove(context, delta) {

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

        context.delta = delta;

        newBounds = resizeBounds(shape, direction, delta);

        // ensure constraints during resize
        context.newBounds = ensureConstraints(newBounds, resizeConstraints);

        // update + cache executable state
        context.canExecute = self.canResize(context);
      }

      /**
       * Handle resize start.
       *
       * @param  {Object} context
       */
      function handleStart(context) {

        var resizeConstraints = context.resizeConstraints,
            // evaluate minBounds for backwards compatibility
            minBounds = context.minBounds;

        if (resizeConstraints !== undefined) {
          return;
        }

        Eif (minBounds === undefined) {
          minBounds = self.computeMinResizeBox(context);
        }

        context.resizeConstraints = {
          min: asTRBL(minBounds)
        };
      }

      /**
       * Handle resize end.
       *
       * @param  {Object} context
       */
      function handleEnd(context) {
        var shape = context.shape,
            canExecute = context.canExecute,
            newBounds = context.newBounds;

        Eif (canExecute) {

          // ensure we have actual pixel values for new bounds
          // (important when zoom level was > 1 during move)
          newBounds = roundBounds(newBounds);

          Iif (!boundsChanged(shape, newBounds)) {

            // no resize necessary
            return;
          }

          // perform the actual resize
          modeling.resizeShape(shape, newBounds);
        }
      }

      eventBus.on('resize.start', function(event) {
        handleStart(event.context);
      });

      eventBus.on('resize.move', function(event) {
        var delta = {
          x: event.dx,
          y: event.dy
        };

        handleMove(event.context, delta);
      });

      eventBus.on('resize.end', function(event) {
        handleEnd(event.context);
      });

    }

    Resize.prototype.canResize = function(context) {
      var rules = this._rules;

      var ctx = pick(context, [ 'newBounds', 'shape', 'delta', 'direction' ]);

      return rules.allowed('shape.resize', ctx);
    };

    /**
     * Activate a resize operation.
     *
     * You may specify additional contextual information and must specify a
     * resize direction during activation of the resize event.
     *
     * @param {MouseEvent} event
     * @param {djs.model.Shape} shape
     * @param {Object|String} contextOrDirection
     */
    Resize.prototype.activate = function(event, shape, contextOrDirection) {
      var dragging = this._dragging,
          context,
          direction;

      Eif (typeof contextOrDirection === 'string') {
        contextOrDirection = {
          direction: contextOrDirection
        };
      }

      context = assign({ shape: shape }, contextOrDirection);

      direction = context.direction;

      Iif (!direction) {
        throw new Error('must provide a direction (nw|se|ne|sw)');
      }

      var referencePoint = {
        x: /w/.test(direction) ? shape.x : shape.x + shape.width,
        y: /n/.test(direction) ? shape.y : shape.y + shape.height
      };

      dragging.init(event, referencePoint, 'resize', {
        autoActivate: true,
        cursor: 'resize-' + (/nw|se/.test(direction) ? 'nwse' : 'nesw'),
        data: {
          shape: shape,
          context: context
        }
      });
    };

    Resize.prototype.computeMinResizeBox = function(context) {
      var shape = context.shape,
          direction = context.direction,
          minDimensions,
          childrenBounds;

      minDimensions = context.minDimensions || {
        width: DEFAULT_MIN_WIDTH,
        height: DEFAULT_MIN_WIDTH
      };

      // get children bounds
      childrenBounds = computeChildrenBBox(shape, context.childrenBoxPadding);

      // get correct minimum bounds from given resize direction
      // basically ensures that the minBounds is max(childrenBounds, minDimensions)
      return getMinResizeBounds(direction, shape, minDimensions, childrenBounds);
    };

    Resize.$inject = [
      'eventBus',
      'rules',
      'modeling',
      'dragging'
    ];

    // helpers //////////

    function boundsChanged(shape, newBounds) {
      return shape.x !== newBounds.x ||
        shape.y !== newBounds.y ||
        shape.width !== newBounds.width ||
        shape.height !== newBounds.height;
    }