# all files / ol/interaction/ dragpaninteraction.js

64.86% Statements 48/74
62.96% Branches 17/27
100% Functions 4/4
64.86% Lines 48/74

    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
    1701×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    116×

    116×

    116×

    116×

    116×

    116×

    116×

    1×

    1×
    1×

    1×

    1×
    1×

    1×

    1×

    1×
    57×
    57×
    57×
    57×

    57×
    57×
    57×

    1×
    60×
    57×
    57×
    57×
    57×
    57×

    57×
    57×

    57×
    57×

    57×
    57×

    3×

    1×
     goog.provide('ol.interaction.DragPan');

    goog.require('goog.asserts');
    goog.require('ol.Kinetic');
    goog.require('ol.Pixel');
    goog.require('ol.PreRenderFunction');
    goog.require('ol.ViewHint');
    goog.require('ol.coordinate');
    goog.require('ol.events.condition');
    goog.require('ol.interaction.Pointer');

    /**
     * @classdesc
     * Allows the user to pan the map by dragging the map.
     *
     * @constructor
     * @extends {ol.interaction.Pointer}
     * @param {olx.interaction.DragPanOptions=} opt_options Options.
     * @api stable
     */
    ol.interaction.DragPan = function(opt_options) {

      goog.base(this, {
        handleDownEvent: ol.interaction.DragPan.handleDownEvent_,
        handleDragEvent: ol.interaction.DragPan.handleDragEvent_,
        handleUpEvent: ol.interaction.DragPan.handleUpEvent_
      });

      var options = opt_options ? opt_options : {};

      /**
       * @private
       * @type {ol.Kinetic|undefined}
       */
      this.kinetic_ = options.kinetic;

      /**
       * @private
       * @type {?ol.PreRenderFunction}
       */
      this.kineticPreRenderFn_ = null;

      /**
       * @type {ol.Pixel}
       */
      this.lastCentroid = null;

      /**
       * @private
       * @type {ol.events.ConditionType}
       */
      this.condition_ = options.condition ?
          options.condition : ol.events.condition.noModifierKeys;

      /**
       * @private
       * @type {boolean}
       */
      this.noKinetic_ = false;

    };
    goog.inherits(ol.interaction.DragPan, ol.interaction.Pointer);

    /**
     * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
     * @this {ol.interaction.DragPan}
     * @private
     */
    ol.interaction.DragPan.handleDragEvent_ = function(mapBrowserEvent) {
      goog.asserts.assert(this.targetPointers.length >= 1,
          'the length of this.targetPointers should be more than 1');
      var centroid =
          ol.interaction.Pointer.centroid(this.targetPointers);
      Eif (this.kinetic_) {
        this.kinetic_.update(centroid[0], centroid[1]);
      }
      Iif (this.lastCentroid) {
        var deltaX = this.lastCentroid[0] - centroid[0];
        var deltaY = centroid[1] - this.lastCentroid[1];
        var map = mapBrowserEvent.map;
        var view = map.getView();
        var viewState = view.getState();
        var center = [deltaX, deltaY];
        ol.coordinate.scale(center, viewState.resolution);
        ol.coordinate.rotate(center, viewState.rotation);
        ol.coordinate.add(center, viewState.center);
        center = view.constrainCenter(center);
        map.render();
        view.setCenter(center);
      }
      this.lastCentroid = centroid;
    };

    /**
     * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
     * @return {boolean} Stop drag sequence?
     * @this {ol.interaction.DragPan}
     * @private
     */
    ol.interaction.DragPan.handleUpEvent_ = function(mapBrowserEvent) {
      var map = mapBrowserEvent.map;
      var view = map.getView();
      Eif (this.targetPointers.length === 0) {
        Iif (!this.noKinetic_ && this.kinetic_ && this.kinetic_.end()) {
          var distance = this.kinetic_.getDistance();
          var angle = this.kinetic_.getAngle();
          var center = view.getCenter();
          goog.asserts.assert(center !== undefined, 'center should be defined');
          this.kineticPreRenderFn_ = this.kinetic_.pan(center);
          map.beforeRender(this.kineticPreRenderFn_);
          var centerpx = map.getPixelFromCoordinate(center);
          var dest = map.getCoordinateFromPixel([
            centerpx[0] - distance * Math.cos(angle),
            centerpx[1] - distance * Math.sin(angle)
          ]);
          dest = view.constrainCenter(dest);
          view.setCenter(dest);
        }
        view.setHint(ol.ViewHint.INTERACTING, -1);
        map.render();
        return false;
      } else {
        this.lastCentroid = null;
        return true;
      }
    };

    /**
     * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
     * @return {boolean} Start drag sequence?
     * @this {ol.interaction.DragPan}
     * @private
     */
    ol.interaction.DragPan.handleDownEvent_ = function(mapBrowserEvent) {
      if (this.targetPointers.length > 0 && this.condition_(mapBrowserEvent)) {
        var map = mapBrowserEvent.map;
        var view = map.getView();
        this.lastCentroid = null;
        Eif (!this.handlingDownUpSequence) {
          view.setHint(ol.ViewHint.INTERACTING, 1);
        }
        map.render();
        Iif (this.kineticPreRenderFn_ &&
            map.removePreRenderFunction(this.kineticPreRenderFn_)) {
          view.setCenter(mapBrowserEvent.frameState.viewState.center);
          this.kineticPreRenderFn_ = null;
        }
        Eif (this.kinetic_) {
          this.kinetic_.begin();
        }
        // No kinetic as soon as more than one pointer on the screen is
        // detected. This is to prevent nasty pans after pinch.
        this.noKinetic_ = this.targetPointers.length > 1;
        return true;
      } else {
        return false;
      }
    };

    /**
     * @inheritDoc
     */
    ol.interaction.DragPan.prototype.shouldStopEvent = goog.functions.FALSE;