all files / ol/interaction/ dragzoominteraction.js

100% Statements 36/36
91.67% Branches 11/12
100% Functions 2/2
100% Lines 36/36
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                                   158×   158×             158×           158×   158×                                                                    
goog.provide('ol.interaction.DragZoom');
 
goog.require('goog.asserts');
goog.require('ol.animation');
goog.require('ol.easing');
goog.require('ol.events.condition');
goog.require('ol.extent');
goog.require('ol.interaction.DragBox');
 
 
/**
 * @classdesc
 * Allows the user to zoom the map by clicking and dragging on the map,
 * normally combined with an {@link ol.events.condition} that limits
 * it to when a key, shift by default, is held down.
 *
 * To change the style of the box, use CSS and the `.ol-dragzoom` selector, or
 * your custom one configured with `className`.
 *
 * @constructor
 * @extends {ol.interaction.DragBox}
 * @param {olx.interaction.DragZoomOptions=} opt_options Options.
 * @api stable
 */
ol.interaction.DragZoom = function(opt_options) {
  var options = opt_options ? opt_options : {};
 
  var condition = options.condition ?
      options.condition : ol.events.condition.shiftKeyOnly;
 
  /**
   * @private
   * @type {number}
   */
  this.duration_ = options.duration !== undefined ? options.duration : 200;
 
  /**
   * @private
   * @type {boolean}
   */
  this.out_ = options.out !== undefined ? options.out : false;
 
  goog.base(this, {
    condition: condition,
    className: options.className || 'ol-dragzoom'
  });
 
};
goog.inherits(ol.interaction.DragZoom, ol.interaction.DragBox);
 
 
/**
 * @inheritDoc
 */
ol.interaction.DragZoom.prototype.onBoxEnd = function() {
  var map = this.getMap();
 
  var view = map.getView();
  goog.asserts.assert(view, 'map must have view');
 
  var size = map.getSize();
  goog.asserts.assert(size !== undefined, 'size should be defined');
 
  var extent = this.getGeometry().getExtent();
 
  if (this.out_) {
    var mapExtent = view.calculateExtent(size);
    var boxPixelExtent = ol.extent.createOrUpdateFromCoordinates([
      map.getPixelFromCoordinate(ol.extent.getBottomLeft(extent)),
      map.getPixelFromCoordinate(ol.extent.getTopRight(extent))]);
    var factor = view.getResolutionForExtent(boxPixelExtent, size);
 
    ol.extent.scaleFromCenter(mapExtent, 1 / factor);
    extent = mapExtent;
  }
 
  var resolution = view.constrainResolution(
      view.getResolutionForExtent(extent, size));
 
  var currentResolution = view.getResolution();
  goog.asserts.assert(currentResolution !== undefined, 'res should be defined');
 
  var currentCenter = view.getCenter();
  goog.asserts.assert(currentCenter !== undefined, 'center should be defined');
 
  map.beforeRender(ol.animation.zoom({
    resolution: currentResolution,
    duration: this.duration_,
    easing: ol.easing.easeOut
  }));
  map.beforeRender(ol.animation.pan({
    source: currentCenter,
    duration: this.duration_,
    easing: ol.easing.easeOut
  }));
 
  view.setCenter(ol.extent.getCenter(extent));
  view.setResolution(resolution);
};