# all files / ol/control/ zoomtoextent.js

80.65% Statements 25/31
42.86% Branches 6/14
33.33% Functions 1/3
80.65% Lines 25/31

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

    1×
    1×
    1×
    1×
    1×

    1×
    1×

    1×

    1×

    1×
    1×

    1×
    1×
    1×
    1×

    1×

    1×

    1×
    1×
    1×

    1×

    1×

    1×

    1×

     goog.provide('ol.control.ZoomToExtent');

    goog.require('ol');
    goog.require('ol.events');
    goog.require('ol.events.EventType');
    goog.require('ol.control.Control');
    goog.require('ol.css');

    /**
     * @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
     */
    ol.control.ZoomToExtent = function(opt_options) {
      var options = opt_options ? opt_options : {};

      /**
       * @type {ol.Extent}
       * @private
       */
      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
      );

      ol.events.listen(button, ol.events.EventType.CLICK,
          this.handleClick_, this);

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

      ol.control.Control.call(this, {
        element: element,
        target: options.target
      });
    };
    ol.inherits(ol.control.ZoomToExtent, ol.control.Control);

    /**
     * @param {Event} event The event to handle
     * @private
     */
    ol.control.ZoomToExtent.prototype.handleClick_ = function(event) {
      event.preventDefault();
      this.handleZoomToExtent_();
    };

    /**
     * @private
     */
    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);
    };