'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _index = require('../index.js');

var _index2 = _interopRequireDefault(_index);

var _coordinate = require('../coordinate.js');

var _coordinate2 = _interopRequireDefault(_coordinate);

var _eventtype = require('../events/eventtype.js');

var _eventtype2 = _interopRequireDefault(_eventtype);

var _keycode = require('../events/keycode.js');

var _keycode2 = _interopRequireDefault(_keycode);

var _condition = require('../events/condition.js');

var _condition2 = _interopRequireDefault(_condition);

var _interaction = require('../interaction/interaction.js');

var _interaction2 = _interopRequireDefault(_interaction);

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

/**
 * @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
 */
var _ol_interaction_KeyboardPan_ = function (opt_options) {

  _interaction2.default.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 _condition2.default.noModifierKeys(mapBrowserEvent) && _condition2.default.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;
};

_index2.default.inherits(_ol_interaction_KeyboardPan_, _interaction2.default);

/**
 * 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 == _eventtype2.default.KEYDOWN) {
    var keyEvent = mapBrowserEvent.originalEvent;
    var keyCode = keyEvent.keyCode;
    if (this.condition_(mapBrowserEvent) && (keyCode == _keycode2.default.DOWN || keyCode == _keycode2.default.LEFT || keyCode == _keycode2.default.RIGHT || keyCode == _keycode2.default.UP)) {
      var map = mapBrowserEvent.map;
      var view = map.getView();
      var mapUnitsDelta = view.getResolution() * this.pixelDelta_;
      var deltaX = 0,
          deltaY = 0;
      if (keyCode == _keycode2.default.DOWN) {
        deltaY = -mapUnitsDelta;
      } else if (keyCode == _keycode2.default.LEFT) {
        deltaX = -mapUnitsDelta;
      } else if (keyCode == _keycode2.default.RIGHT) {
        deltaX = mapUnitsDelta;
      } else {
        deltaY = mapUnitsDelta;
      }
      var delta = [deltaX, deltaY];
      _coordinate2.default.rotate(delta, view.getRotation());
      _interaction2.default.pan(view, delta, this.duration_);
      mapBrowserEvent.preventDefault();
      stopEvent = true;
    }
  }
  return !stopEvent;
};
exports.default = _ol_interaction_KeyboardPan_;
module.exports = exports['default'];