# all files / diagram-js/lib/navigation/zoomscroll/ ZoomScroll.js

47.14% Statements 33/70
30% Branches 9/30
36.36% Functions 4/11
47.14% Lines 33/70

    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

    1×

    1×
    1×

    1×

    1×

    58×

    58×

    58×
    58×

    58×

    58×
    58×

    58×

    58×
    58×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    58×
    58×

    58×

    58×

    58×

    58×

    58×

    58×

    1×
    58×

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

    import {
      getStepSize,
      cap
    } from './ZoomUtil';

    import {
      log10
    } from '../../util/Math';

    import {
      bind
    } from 'min-dash';

    var sign = Math.sign || function(n) {
      return n >= 0 ? 1 : -1;
    };

    var RANGE = { min: 0.2, max: 4 },
        NUM_STEPS = 10;

    var DELTA_THRESHOLD = 0.1;

    var DEFAULT_SCALE = 0.75;

    /**
     * An implementation of zooming and scrolling within the
     * {@link Canvas} via the mouse wheel.
     *
     * Mouse wheel zooming / scrolling may be disabled using
     * the {@link toggle(enabled)} method.
     *
     * @param {Object} [config]
     * @param {Boolean} [config.enabled=true] default enabled state
     * @param {Number} [config.scale=.75] scroll sensivity
     * @param {EventBus} eventBus
     * @param {Canvas} canvas
     */
    export default function ZoomScroll(config, eventBus, canvas) {

      config = config || {};

      this._enabled = false;

      this._canvas = canvas;
      this._container = canvas._container;

      this._handleWheel = bind(this._handleWheel, this);

      this._totalDelta = 0;
      this._scale = config.scale || DEFAULT_SCALE;

      var self = this;

      eventBus.on('canvas.init', function(e) {
        self._init(config.enabled !== false);
      });
    }

    ZoomScroll.$inject = [
      'config.zoomScroll',
      'eventBus',
      'canvas'
    ];

    ZoomScroll.prototype.scroll = function scroll(delta) {
      this._canvas.scroll(delta);
    };

    ZoomScroll.prototype.reset = function reset() {
      this._canvas.zoom('fit-viewport');
    };

    /**
     * Zoom depending on delta.
     *
     * @param {number} delta
     * @param {Object} position
     */
    ZoomScroll.prototype.zoom = function zoom(delta, position) {

      // zoom with half the step size of stepZoom
      var stepSize = getStepSize(RANGE, NUM_STEPS * 2);

      // add until threshold reached
      this._totalDelta += delta;

      if (Math.abs(this._totalDelta) > DELTA_THRESHOLD) {
        this._zoom(delta, position, stepSize);

        // reset
        this._totalDelta = 0;
      }
    };

    ZoomScroll.prototype._handleWheel = function handleWheel(event) {
      // event is already handled by '.djs-scrollable'
      if (domClosest(event.target, '.djs-scrollable', true)) {
        return;
      }

      var element = this._container;

      event.preventDefault();

      // pinch to zoom is mapped to wheel + ctrlKey = true
      // in modern browsers (!)

      var isZoom = event.ctrlKey;

      var isHorizontalScroll = event.shiftKey;

      var factor = -1 * this._scale,
          delta;

      if (isZoom) {
        factor *= event.deltaMode === 0 ? 0.020 : 0.32;
      } else {
        factor *= event.deltaMode === 0 ? 1.0 : 16.0;
      }

      if (isZoom) {
        var elementRect = element.getBoundingClientRect();

        var offset = {
          x: event.clientX - elementRect.left,
          y: event.clientY - elementRect.top
        };

        delta = (
          Math.sqrt(
            Math.pow(event.deltaY, 2) +
            Math.pow(event.deltaX, 2)
          ) * sign(event.deltaY) * factor
        );

        // zoom in relative to diagram {x,y} coordinates
        this.zoom(delta, offset);
      } else {

        if (isHorizontalScroll) {
          delta = {
            dx: factor * event.deltaY,
            dy: 0
          };
        } else {
          delta = {
            dx: factor * event.deltaX,
            dy: factor * event.deltaY
          };
        }

        this.scroll(delta);
      }
    };

    /**
     * Zoom with fixed step size.
     *
     * @param {number} delta - Zoom delta (1 for zooming in, -1 for out).
     * @param {Object} position
     */
    ZoomScroll.prototype.stepZoom = function stepZoom(delta, position) {

      var stepSize = getStepSize(RANGE, NUM_STEPS);

      this._zoom(delta, position, stepSize);
    };

    /**
     * Zoom in/out given a step size.
     *
     * @param {number} delta
     * @param {Object} position
     * @param {number} stepSize
     */
    ZoomScroll.prototype._zoom = function(delta, position, stepSize) {
      var canvas = this._canvas;

      var direction = delta > 0 ? 1 : -1;

      var currentLinearZoomLevel = log10(canvas.zoom());

      // snap to a proximate zoom step
      var newLinearZoomLevel = Math.round(currentLinearZoomLevel / stepSize) * stepSize;

      // increase or decrease one zoom step in the given direction
      newLinearZoomLevel += stepSize * direction;

      // calculate the absolute logarithmic zoom level based on the linear zoom level
      // (e.g. 2 for an absolute x2 zoom)
      var newLogZoomLevel = Math.pow(10, newLinearZoomLevel);

      canvas.zoom(cap(RANGE, newLogZoomLevel), position);
    };

    /**
     * Toggle the zoom scroll ability via mouse wheel.
     *
     * @param  {Boolean} [newEnabled] new enabled state
     */
    ZoomScroll.prototype.toggle = function toggle(newEnabled) {

      var element = this._container;
      var handleWheel = this._handleWheel;

      var oldEnabled = this._enabled;

      Iif (typeof newEnabled === 'undefined') {
        newEnabled = !oldEnabled;
      }

      // only react on actual changes
      Eif (oldEnabled !== newEnabled) {

        // add or remove wheel listener based on
        // changed enabled state
        domEvent[newEnabled ? 'bind' : 'unbind'](element, 'wheel', handleWheel, false);
      }

      this._enabled = newEnabled;

      return newEnabled;
    };

    ZoomScroll.prototype._init = function(newEnabled) {
      this.toggle(newEnabled);
    };