'use strict';

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

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

var _index2 = _interopRequireDefault(_index);

var _events = require('../events.js');

var _events2 = _interopRequireDefault(_events);

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

var _eventtype2 = _interopRequireDefault(_eventtype);

var _control = require('../control/control.js');

var _control2 = _interopRequireDefault(_control);

var _css = require('../css.js');

var _css2 = _interopRequireDefault(_css);

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

/**
 * @classdesc
 * A button control which, when pressed, changes the map view to a specific
 * extent. To style this control use the css selector `.ol-zoom-extent`.
 *
 * @constructor
 * @extends {ol.control.Control}
 * @param {olx.control.ZoomToExtentOptions=} opt_options Options.
 * @api
 */
var _ol_control_ZoomToExtent_ = function (opt_options) {
  var options = opt_options ? opt_options : {};

  /**
   * @type {ol.Extent}
   * @protected
   */
  this.extent = options.extent ? options.extent : null;

  var className = options.className !== undefined ? options.className : 'ol-zoom-extent';

  var label = options.label !== undefined ? options.label : 'E';
  var tipLabel = options.tipLabel !== undefined ? options.tipLabel : 'Fit to extent';
  var button = document.createElement('button');
  button.setAttribute('type', 'button');
  button.title = tipLabel;
  button.appendChild(typeof label === 'string' ? document.createTextNode(label) : label);

  _events2.default.listen(button, _eventtype2.default.CLICK, this.handleClick_, this);

  var cssClasses = className + ' ' + _css2.default.CLASS_UNSELECTABLE + ' ' + _css2.default.CLASS_CONTROL;
  var element = document.createElement('div');
  element.className = cssClasses;
  element.appendChild(button);

  _control2.default.call(this, {
    element: element,
    target: options.target
  });
};

_index2.default.inherits(_ol_control_ZoomToExtent_, _control2.default);

/**
 * @param {Event} event The event to handle
 * @private
 */
_ol_control_ZoomToExtent_.prototype.handleClick_ = function (event) {
  event.preventDefault();
  this.handleZoomToExtent();
};

/**
 * @protected
 */
_ol_control_ZoomToExtent_.prototype.handleZoomToExtent = function () {
  var map = this.getMap();
  var view = map.getView();
  var extent = !this.extent ? view.getProjection().getExtent() : this.extent;
  view.fit(extent);
};
exports.default = _ol_control_ZoomToExtent_;
module.exports = exports['default'];