# all files / ol/interaction/ keyboardpan.js

100% Statements 39/39
84% Branches 21/25
100% Functions 3/3
100% Lines 39/39

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

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

    1×

    305×

    305×

    305×
    4×

    305×

    305×

    305×

    1×

    1×
    313×
    313×
    4×
    4×
    4×

    4×
    4×
    4×
    4×
    4×
    1×
    3×
    1×
    2×
    1×

    1×

    4×
    4×
    4×
    4×
    4×

    313×

     goog.provide('ol.interaction.KeyboardPan');

    goog.require('ol');
    goog.require('ol.coordinate');
    goog.require('ol.events.EventType');
    goog.require('ol.events.KeyCode');
    goog.require('ol.events.condition');
    goog.require('ol.interaction.Interaction');

    /**
     * @classdesc
     * Allows the user to pan the map using keyboard arrows.
     * Note that, although this interaction is by default included in maps,
     * the keys can only be used when browser focus is on the element to which
     * the keyboard events are attached. By default, this is the map div,
     * though you can change this with the `keyboardEventTarget` in
     * {@link ol.Map}. `document` never loses focus but, for any other element,
     * focus will have to be on, and returned to, this element if the keys are to
     * function.
     * See also {@link ol.interaction.KeyboardZoom}.
     *
     * @constructor
     * @extends {ol.interaction.Interaction}
     * @param {olx.interaction.KeyboardPanOptions=} opt_options Options.
     * @api
     */
    ol.interaction.KeyboardPan = function(opt_options) {

      ol.interaction.Interaction.call(this, {
        handleEvent: ol.interaction.KeyboardPan.handleEvent
      });

      var options = opt_options || {};

      /**
       * @private
       * @param {ol.MapBrowserEvent} mapBrowserEvent Browser event.
       * @return {boolean} Combined condition result.
       */
      this.defaultCondition_ = function(mapBrowserEvent) {
        return ol.events.condition.noModifierKeys(mapBrowserEvent) &&
          ol.events.condition.targetNotEditable(mapBrowserEvent);
      };

      /**
       * @private
       * @type {ol.EventsConditionType}
       */
      this.condition_ = options.condition !== undefined ?
        options.condition : this.defaultCondition_;

      /**
       * @private
       * @type {number}
       */
      this.duration_ = options.duration !== undefined ? options.duration : 100;

      /**
       * @private
       * @type {number}
       */
      this.pixelDelta_ = options.pixelDelta !== undefined ?
        options.pixelDelta : 128;

    };
    ol.inherits(ol.interaction.KeyboardPan, ol.interaction.Interaction);

    /**
     * Handles the {@link ol.MapBrowserEvent map browser event} if it was a
     * `KeyEvent`, and decides the direction to pan to (if an arrow key was
     * pressed).
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} `false` to stop event propagation.
     * @this {ol.interaction.KeyboardPan}
     * @api
     */
    ol.interaction.KeyboardPan.handleEvent = function(mapBrowserEvent) {
      var stopEvent = false;
      if (mapBrowserEvent.type == ol.events.EventType.KEYDOWN) {
        var keyEvent = mapBrowserEvent.originalEvent;
        var keyCode = keyEvent.keyCode;
        Eif (this.condition_(mapBrowserEvent) &&
            (keyCode == ol.events.KeyCode.DOWN ||
            keyCode == ol.events.KeyCode.LEFT ||
            keyCode == ol.events.KeyCode.RIGHT ||
            keyCode == ol.events.KeyCode.UP)) {
          var map = mapBrowserEvent.map;
          var view = map.getView();
          var mapUnitsDelta = view.getResolution() * this.pixelDelta_;
          var deltaX = 0, deltaY = 0;
          if (keyCode == ol.events.KeyCode.DOWN) {
            deltaY = -mapUnitsDelta;
          } else if (keyCode == ol.events.KeyCode.LEFT) {
            deltaX = -mapUnitsDelta;
          } else if (keyCode == ol.events.KeyCode.RIGHT) {
            deltaX = mapUnitsDelta;
          } else {
            deltaY = mapUnitsDelta;
          }
          var delta = [deltaX, deltaY];
          ol.coordinate.rotate(delta, view.getRotation());
          ol.interaction.Interaction.pan(view, delta, this.duration_);
          mapBrowserEvent.preventDefault();
          stopEvent = true;
        }
      }
      return !stopEvent;
    };