# all files / ol/interaction/ interaction.js

46.67% Statements 28/60
0% Branches 0/29
45.45% Functions 5/11
46.67% Lines 28/60

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

    1×
    1×
    1×
    1×
    1×

    1×

    1×

    1120×

    1120×

    1120×

    1120×

    1×

    1×
    2143×

    1×
    103×

    1×
    1131×

    1×
    1118×

    1×

    1×

    1×

    1×

    1×

    1×

     // FIXME factor out key precondition (shift et. al)

    goog.provide('ol.interaction.Interaction');
    goog.provide('ol.interaction.InteractionProperty');

    goog.require('ol');
    goog.require('ol.MapBrowserEvent');
    goog.require('ol.Object');
    goog.require('ol.animation');
    goog.require('ol.easing');

    /**
     * @enum {string}
     */
    ol.interaction.InteractionProperty = {
      ACTIVE: 'active'
    };

    /**
     * @classdesc
     * Abstract base class; normally only used for creating subclasses and not
     * instantiated in apps.
     * User actions that change the state of the map. Some are similar to controls,
     * but are not associated with a DOM element.
     * For example, {@link ol.interaction.KeyboardZoom} is functionally the same as
     * {@link ol.control.Zoom}, but triggered by a keyboard event not a button
     * element event.
     * Although interactions do not have a DOM element, some of them do render
     * vectors and so are visible on the screen.
     *
     * @constructor
     * @param {olx.interaction.InteractionOptions} options Options.
     * @extends {ol.Object}
     * @api
     */
    ol.interaction.Interaction = function(options) {

      goog.base(this);

      /**
       * @private
       * @type {ol.Map}
       */
      this.map_ = null;

      this.setActive(true);

      /**
       * @type {function(ol.MapBrowserEvent):boolean}
       */
      this.handleEvent = options.handleEvent;

    };
    goog.inherits(ol.interaction.Interaction, ol.Object);

    /**
     * Return whether the interaction is currently active.
     * @return {boolean} `true` if the interaction is active, `false` otherwise.
     * @observable
     * @api
     */
    ol.interaction.Interaction.prototype.getActive = function() {
      return /** @type {boolean} */ (
          this.get(ol.interaction.InteractionProperty.ACTIVE));
    };

    /**
     * Get the map associated with this interaction.
     * @return {ol.Map} Map.
     * @api
     */
    ol.interaction.Interaction.prototype.getMap = function() {
      return this.map_;
    };

    /**
     * Activate or deactivate the interaction.
     * @param {boolean} active Active.
     * @observable
     * @api
     */
    ol.interaction.Interaction.prototype.setActive = function(active) {
      this.set(ol.interaction.InteractionProperty.ACTIVE, active);
    };

    /**
     * Remove the interaction from its current map and attach it to the new map.
     * Subclasses may set up event handlers to get notified about changes to
     * the map here.
     * @param {ol.Map} map Map.
     */
    ol.interaction.Interaction.prototype.setMap = function(map) {
      this.map_ = map;
    };

    /**
     * @param {ol.Map} map Map.
     * @param {ol.View} view View.
     * @param {ol.Coordinate} delta Delta.
     * @param {number=} opt_duration Duration.
     */
    ol.interaction.Interaction.pan = function(map, view, delta, opt_duration) {
      var currentCenter = view.getCenter();
      if (currentCenter) {
        if (opt_duration && opt_duration > 0) {
          map.beforeRender(ol.animation.pan({
            source: currentCenter,
            duration: opt_duration,
            easing: ol.easing.linear
          }));
        }
        var center = view.constrainCenter(
            [currentCenter[0] + delta[0], currentCenter[1] + delta[1]]);
        view.setCenter(center);
      }
    };

    /**
     * @param {ol.Map} map Map.
     * @param {ol.View} view View.
     * @param {number|undefined} rotation Rotation.
     * @param {ol.Coordinate=} opt_anchor Anchor coordinate.
     * @param {number=} opt_duration Duration.
     */
    ol.interaction.Interaction.rotate =
        function(map, view, rotation, opt_anchor, opt_duration) {
      rotation = view.constrainRotation(rotation, 0);
      ol.interaction.Interaction.rotateWithoutConstraints(
          map, view, rotation, opt_anchor, opt_duration);
    };

    /**
     * @param {ol.Map} map Map.
     * @param {ol.View} view View.
     * @param {number|undefined} rotation Rotation.
     * @param {ol.Coordinate=} opt_anchor Anchor coordinate.
     * @param {number=} opt_duration Duration.
     */
    ol.interaction.Interaction.rotateWithoutConstraints =
        function(map, view, rotation, opt_anchor, opt_duration) {
      if (rotation !== undefined) {
        var currentRotation = view.getRotation();
        var currentCenter = view.getCenter();
        if (currentRotation !== undefined && currentCenter &&
            opt_duration && opt_duration > 0) {
          map.beforeRender(ol.animation.rotate({
            rotation: currentRotation,
            duration: opt_duration,
            easing: ol.easing.easeOut
          }));
          if (opt_anchor) {
            map.beforeRender(ol.animation.pan({
              source: currentCenter,
              duration: opt_duration,
              easing: ol.easing.easeOut
            }));
          }
        }
        view.rotate(rotation, opt_anchor);
      }
    };

    /**
     * @param {ol.Map} map Map.
     * @param {ol.View} view View.
     * @param {number|undefined} resolution Resolution to go to.
     * @param {ol.Coordinate=} opt_anchor Anchor coordinate.
     * @param {number=} opt_duration Duration.
     * @param {number=} opt_direction Zooming direction; > 0 indicates
     *     zooming out, in which case the constraints system will select
     *     the largest nearest resolution; < 0 indicates zooming in, in
     *     which case the constraints system will select the smallest
     *     nearest resolution; == 0 indicates that the zooming direction
     *     is unknown/not relevant, in which case the constraints system
     *     will select the nearest resolution. If not defined 0 is
     *     assumed.
     */
    ol.interaction.Interaction.zoom =
        function(map, view, resolution, opt_anchor, opt_duration, opt_direction) {
      resolution = view.constrainResolution(resolution, 0, opt_direction);
      ol.interaction.Interaction.zoomWithoutConstraints(
          map, view, resolution, opt_anchor, opt_duration);
    };

    /**
     * @param {ol.Map} map Map.
     * @param {ol.View} view View.
     * @param {number} delta Delta from previous zoom level.
     * @param {ol.Coordinate=} opt_anchor Anchor coordinate.
     * @param {number=} opt_duration Duration.
     */
    ol.interaction.Interaction.zoomByDelta =
        function(map, view, delta, opt_anchor, opt_duration) {
      var currentResolution = view.getResolution();
      var resolution = view.constrainResolution(currentResolution, delta, 0);
      ol.interaction.Interaction.zoomWithoutConstraints(
          map, view, resolution, opt_anchor, opt_duration);
    };

    /**
     * @param {ol.Map} map Map.
     * @param {ol.View} view View.
     * @param {number|undefined} resolution Resolution to go to.
     * @param {ol.Coordinate=} opt_anchor Anchor coordinate.
     * @param {number=} opt_duration Duration.
     */
    ol.interaction.Interaction.zoomWithoutConstraints =
        function(map, view, resolution, opt_anchor, opt_duration) {
      if (resolution) {
        var currentResolution = view.getResolution();
        var currentCenter = view.getCenter();
        if (currentResolution !== undefined && currentCenter &&
            resolution !== currentResolution &&
            opt_duration && opt_duration > 0) {
          map.beforeRender(ol.animation.zoom({
            resolution: currentResolution,
            duration: opt_duration,
            easing: ol.easing.easeOut
          }));
          if (opt_anchor) {
            map.beforeRender(ol.animation.pan({
              source: currentCenter,
              duration: opt_duration,
              easing: ol.easing.easeOut
            }));
          }
        }
        if (opt_anchor) {
          var center = view.calculateCenterZoom(resolution, opt_anchor);
          view.setCenter(center);
        }
        view.setResolution(resolution);
      }
    };