# all files / diagram-js/lib/features/auto-resize/ AutoResize.js

86.32% Statements 82/95
70.37% Branches 38/54
80% Functions 12/15
86.32% Lines 82/95

    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
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290

    84×

    84×
    84×
    84×

    84×

    84×
    6×
    6×
    6×
    6×

    6×

    6×

    84×
    33×
    33×
    33×

    33×

    33×
    1×

    32×
    49×

    32×

    34×

    34×

    84×

    84×
    50×
    50×
    50×
    50×

    50×

    50×
    50×

    1×

    1×

    1×

    41×
    41×

    41×
    41×

    41×

    41×
    6×

    41×
    5×

    41×
    20×

    41×
    11×

    41×

    1×

    123×
    34×

    123×

    123×
    82×

    41×

    41×
    8×

    33×

    33×

    33×

    33×
    33×

    1×
    41×

    1×
    41×

    1×

    41×

    33×

    33×
    33×

    33×
    6×

    33×
    20×

    33×
    11×

    33×
    5×

    33×
     import inherits from 'inherits';

    import { getBBox as getBoundingBox } from '../../util/Elements';

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

    import {
      assign,
      flatten,
      find,
      forEach,
      groupBy,
      isArray,
      matchPattern,
      pick,
      values
    } from 'min-dash';

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

    /**
     * An auto resize component that takes care of expanding a parent element
     * if child elements are created or moved close the parents edge.
     *
     * @param {EventBus} eventBus
     * @param {ElementRegistry} elementRegistry
     * @param {Modeling} modeling
     * @param {Rules} rules
     */
    export default function AutoResize(eventBus, elementRegistry, modeling, rules) {

      CommandInterceptor.call(this, eventBus);

      this._elementRegistry = elementRegistry;
      this._modeling = modeling;
      this._rules = rules;

      var self = this;

      this.postExecuted([ 'shape.create' ], function(event) {
        var context = event.context,
            hints = context.hints || {},
            shape = context.shape,
            parent = context.parent || context.newParent;

        Iif (hints.autoResize === false) {
          return;
        }

        self._expand([ shape ], parent);
      });

      this.postExecuted([ 'elements.move' ], function(event) {
        var context = event.context,
            elements = flatten(values(context.closure.topLevel)),
            hints = context.hints;

        var autoResize = hints ? hints.autoResize : true;

        if (autoResize === false) {
          return;
        }

        var expandings = groupBy(elements, function(element) {
          return element.parent.id;
        });

        forEach(expandings, function(elements, parentId) {

          // optionally filter elements to be considered when resizing
          Iif (isArray(autoResize)) {
            elements = elements.filter(function(element) {
              return find(autoResize, matchPattern({ id: element.id }));
            });
          }

          self._expand(elements, parentId);
        });
      });

      this.postExecuted([ 'shape.toggleCollapse' ], function(event) {
        var context = event.context,
            hints = context.hints,
            shape = context.shape;

        if (hints && hints.autoResize === false) {
          return;
        }

        if (shape.collapsed) {
          return;
        }

        self._expand(shape.children || [], shape);
      });

      this.postExecuted([ 'shape.resize' ], function(event) {
        var context = event.context,
            hints = context.hints,
            shape = context.shape,
            parent = shape.parent;

        Iif (hints && hints.autoResize === false) {
          return;
        }

        Eif (parent) {
          self._expand([ shape ], parent);
        }
      });

    }

    AutoResize.$inject = [
      'eventBus',
      'elementRegistry',
      'modeling',
      'rules'
    ];

    inherits(AutoResize, CommandInterceptor);

    /**
     * Calculate the new bounds of the target shape, given
     * a number of elements have been moved or added into the parent.
     *
     * This method considers the current size, the added elements as well as
     * the provided padding for the new bounds.
     *
     * @param {Array<djs.model.Shape>} elements
     * @param {djs.model.Shape} target
     */
    AutoResize.prototype._getOptimalBounds = function(elements, target) {

      var offset = this.getOffset(target),
          padding = this.getPadding(target);

      var elementsTrbl = asTRBL(getBoundingBox(elements)),
          targetTrbl = asTRBL(target);

      var newTrbl = {};

      if (elementsTrbl.top - targetTrbl.top < padding.top) {
        newTrbl.top = elementsTrbl.top - offset.top;
      }

      if (elementsTrbl.left - targetTrbl.left < padding.left) {
        newTrbl.left = elementsTrbl.left - offset.left;
      }

      if (targetTrbl.right - elementsTrbl.right < padding.right) {
        newTrbl.right = elementsTrbl.right + offset.right;
      }

      if (targetTrbl.bottom - elementsTrbl.bottom < padding.bottom) {
        newTrbl.bottom = elementsTrbl.bottom + offset.bottom;
      }

      return asBounds(assign({}, targetTrbl, newTrbl));
    };

    /**
     * Expand the target shape respecting rules, offset and padding
     *
     * @param {Array<djs.model.Shape>} elements
     * @param {djs.model.Shape|String} target|targetId
     */
    AutoResize.prototype._expand = function(elements, target) {

      if (typeof target === 'string') {
        target = this._elementRegistry.get(target);
      }

      var allowed = this._rules.allowed('element.autoResize', {
        elements: elements,
        target: target
      });

      if (!allowed) {
        return;
      }

      // calculate the new bounds
      var newBounds = this._getOptimalBounds(elements, target);

      if (!boundsChanged(newBounds, target)) {
        return;
      }

      var resizeDirections = getResizeDirections(pick(target, [ 'x', 'y', 'width', 'height' ]), newBounds);

      // resize the parent shape
      this.resize(target, newBounds, {
        autoResize: resizeDirections
      });

      var parent = target.parent;

      // recursively expand parent elements
      Eif (parent) {
        this._expand([ target ], parent);
      }
    };

    /**
     * Get the amount to expand the given shape in each direction.
     *
     * @param {djs.model.Shape} shape
     *
     * @return {TRBL}
     */
    AutoResize.prototype.getOffset = function(shape) {
      return { top: 60, bottom: 60, left: 100, right: 100 };
    };

    /**
     * Get the activation threshold for each side for which
     * resize triggers.
     *
     * @param {djs.model.Shape} shape
     *
     * @return {TRBL}
     */
    AutoResize.prototype.getPadding = function(shape) {
      return { top: 2, bottom: 2, left: 15, right: 15 };
    };

    /**
     * Perform the actual resize operation.
     *
     * @param {djs.model.Shape} shape
     * @param {Bounds} newBounds
     * @param {Object} [hints]
     * @param {string} [hints.autoResize]
     */
    AutoResize.prototype.resize = function(shape, newBounds, hints) {
      this._modeling.resizeShape(shape, newBounds, null, hints);
    };

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

    /**
     * Get directions of resize as {n|w|s|e} e.g. "nw".
     *
     * @param {Bounds} oldBounds
     * @param {Bounds} newBounds
     *
     * @returns {string} Resize directions as {n|w|s|e}.
     */
    function getResizeDirections(oldBounds, newBounds) {
      var directions = '';

      oldBounds = asTRBL(oldBounds);
      newBounds = asTRBL(newBounds);

      if (oldBounds.top > newBounds.top) {
        directions = directions.concat('n');
      }

      if (oldBounds.right < newBounds.right) {
        directions = directions.concat('w');
      }

      if (oldBounds.bottom < newBounds.bottom) {
        directions = directions.concat('s');
      }

      if (oldBounds.left > newBounds.left) {
        directions = directions.concat('e');
      }

      return directions;
    }