# all files / Github/diagram-js/lib/features/resize/ ResizeHandles.js

43.08% Statements 28/65
28.57% Branches 4/14
60% Functions 6/10
43.08% Lines 28/65

    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

    1×
    1×
    1×

    1×

    151×
    151×

    151×

    151×
    228×

    228×

    228×
    7×

    151×
    127×

    127×

    1×

    1×

    1×

    1×
    7×

    7×
    7×

    1×
    317×

    317×

    1×
    317×

    1×

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

    var HANDLE_OFFSET = -2,
        HANDLE_SIZE = 5,
        HANDLE_HIT_SIZE = 20;

    var CLS_RESIZER = 'djs-resizer';

    import {
      append as svgAppend,
      attr as svgAttr,
      classes as svgClasses,
      clear as svgClear,
      create as svgCreate
    } from 'tiny-svg';

    import {
      event as domEvent
    } from 'min-dom';

    import {
      isPrimaryButton
    } from '../../util/Mouse';

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

    import {
      transform
    } from '../../util/SvgTransformUtil';

    /**
     * This component is responsible for adding resize handles.
     *
     * @param {EventBus} eventBus
     * @param {Canvas} canvas
     * @param {Selection} selection
     * @param {Resize} resize
     */
    export default function ResizeHandles(eventBus, canvas, selection, resize) {

      this._resize = resize;
      this._canvas = canvas;

      var self = this;

      eventBus.on('selection.changed', function(e) {
        var newSelection = e.newSelection;

        // remove old selection markers
        self.removeResizers();

        // add new selection markers ONLY if single selection
        if (newSelection.length === 1) {
          forEach(newSelection, bind(self.addResizer, self));
        }
      });

      eventBus.on('shape.changed', function(e) {
        var shape = e.element;

        Iif (selection.isSelected(shape)) {
          self.removeResizers();

          self.addResizer(shape);
        }
      });
    }

    ResizeHandles.prototype.makeDraggable = function(element, gfx, direction) {
      var resize = this._resize;

      function startResize(event) {
        // only trigger on left mouse button
        if (isPrimaryButton(event)) {
          resize.activate(event, element, direction);
        }
      }

      domEvent.bind(gfx, 'mousedown', startResize);
      domEvent.bind(gfx, 'touchstart', startResize);
    };

    ResizeHandles.prototype._createResizer = function(element, x, y, rotation, direction) {
      var resizersParent = this._getResizersParent();

      var group = svgCreate('g');
      svgClasses(group).add(CLS_RESIZER);
      svgClasses(group).add(CLS_RESIZER + '-' + element.id);
      svgClasses(group).add(CLS_RESIZER + '-' + direction);

      svgAppend(resizersParent, group);

      var origin = -HANDLE_SIZE + HANDLE_OFFSET;

      // Create four drag indicators on the outline
      var visual = svgCreate('rect');
      svgAttr(visual, {
        x: origin,
        y: origin,
        width: HANDLE_SIZE,
        height: HANDLE_SIZE
      });
      svgClasses(visual).add(CLS_RESIZER + '-visual');

      svgAppend(group, visual);

      var hit = svgCreate('rect');
      svgAttr(hit, {
        x: origin,
        y: origin,
        width: HANDLE_HIT_SIZE,
        height: HANDLE_HIT_SIZE
      });
      svgClasses(hit).add(CLS_RESIZER + '-hit');

      svgAppend(group, hit);

      transform(group, x, y, rotation);

      return group;
    };

    ResizeHandles.prototype.createResizer = function(element, direction) {
      var resizer;

      var trbl = asTRBL(element);

      if (direction === 'nw') {
        resizer = this._createResizer(element, trbl.left, trbl.top, 0, direction);
      } else if (direction === 'ne') {
        resizer = this._createResizer(element, trbl.right, trbl.top, 90, direction);
      } else if (direction === 'se') {
        resizer = this._createResizer(element, trbl.right, trbl.bottom, 180, direction);
      } else {
        resizer = this._createResizer(element, trbl.left, trbl.bottom, 270, direction);
      }

      this.makeDraggable(element, resizer, direction);
    };

    // resize handles implementation ///////////////////////////////

    /**
     * Add resizers for a given element.
     *
     * @param {djs.model.Shape} shape
     */
    ResizeHandles.prototype.addResizer = function(shape) {
      var resize = this._resize;

      Eif (!resize.canResize({ shape: shape })) {
        return;
      }

      this.createResizer(shape, 'nw');
      this.createResizer(shape, 'ne');
      this.createResizer(shape, 'se');
      this.createResizer(shape, 'sw');
    };

    /**
     * Remove all resizers
     */
    ResizeHandles.prototype.removeResizers = function() {
      var resizersParent = this._getResizersParent();

      svgClear(resizersParent);
    };

    ResizeHandles.prototype._getResizersParent = function() {
      return this._canvas.getLayer('resizers');
    };

    ResizeHandles.$inject = [
      'eventBus',
      'canvas',
      'selection',
      'resize'
    ];