# all files / ol/control/ zoomslidercontrol.js

58.97% Statements 69/117
31.82% Branches 7/22
25% Functions 3/12
58.97% Lines 69/117

    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
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336

    1×

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

    1×

    6×

    6×

    6×

    6×

    6×

    6×

    6×
    6×

    6×

    6×
    6×

    6×

    6×

    6×

    6×

    6×

    6×

    6×

    1×

    1×

    1×
    6×
    6×
    6×

    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×

    1×

    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     // FIXME should possibly show tooltip when dragging?

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

    goog.require('goog.asserts');
    goog.require('goog.dom');
    goog.require('goog.events');
    goog.require('goog.events.Event');
    goog.require('goog.events.EventType');
    goog.require('goog.fx.DragEvent');
    goog.require('goog.fx.Dragger');
    goog.require('goog.fx.Dragger.EventType');
    goog.require('goog.math.Rect');
    goog.require('goog.style');
    goog.require('ol.Size');
    goog.require('ol.ViewHint');
    goog.require('ol.animation');
    goog.require('ol.control.Control');
    goog.require('ol.css');
    goog.require('ol.easing');
    goog.require('ol.math');

    /**
     * @classdesc
     * A slider type of control for zooming.
     *
     * Example:
     *
     *     map.addControl(new ol.control.ZoomSlider());
     *
     * @constructor
     * @extends {ol.control.Control}
     * @param {olx.control.ZoomSliderOptions=} opt_options Zoom slider options.
     * @api stable
     */
    ol.control.ZoomSlider = function(opt_options) {

      var options = opt_options ? opt_options : {};

      /**
       * Will hold the current resolution of the view.
       *
       * @type {number|undefined}
       * @private
       */
      this.currentResolution_ = undefined;

      /**
       * The direction of the slider. Will be determined from actual display of the
       * container and defaults to ol.control.ZoomSlider.direction.VERTICAL.
       *
       * @type {ol.control.ZoomSlider.direction}
       * @private
       */
      this.direction_ = ol.control.ZoomSlider.direction.VERTICAL;

      /**
       * The calculated thumb size (border box plus margins).  Set when initSlider_
       * is called.
       * @type {ol.Size}
       * @private
       */
      this.thumbSize_ = null;

      /**
       * Whether the slider is initialized.
       * @type {boolean}
       * @private
       */
      this.sliderInitialized_ = false;

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

      var className = options.className ? options.className : 'ol-zoomslider';
      var thumbElement = goog.dom.createDom('BUTTON', {
        'type': 'button',
        'class': className + '-thumb ' + ol.css.CLASS_UNSELECTABLE
      });
      var containerElement = goog.dom.createDom('DIV',
          [className, ol.css.CLASS_UNSELECTABLE, ol.css.CLASS_CONTROL],
          thumbElement);

      /**
       * @type {goog.fx.Dragger}
       * @private
       */
      this.dragger_ = new goog.fx.Dragger(thumbElement);
      this.registerDisposable(this.dragger_);

      goog.events.listen(this.dragger_, goog.fx.Dragger.EventType.START,
          this.handleDraggerStart_, false, this);
      goog.events.listen(this.dragger_, goog.fx.Dragger.EventType.DRAG,
          this.handleDraggerDrag_, false, this);
      goog.events.listen(this.dragger_, goog.fx.Dragger.EventType.END,
          this.handleDraggerEnd_, false, this);

      goog.events.listen(containerElement, goog.events.EventType.CLICK,
          this.handleContainerClick_, false, this);
      goog.events.listen(thumbElement, goog.events.EventType.CLICK,
          goog.events.Event.stopPropagation);

      var render = options.render ? options.render : ol.control.ZoomSlider.render;

      goog.base(this, {
        element: containerElement,
        render: render
      });
    };
    goog.inherits(ol.control.ZoomSlider, ol.control.Control);

    /**
     * The enum for available directions.
     *
     * @enum {number}
     */
    ol.control.ZoomSlider.direction = {
      VERTICAL: 0,
      HORIZONTAL: 1
    };

    /**
     * @inheritDoc
     */
    ol.control.ZoomSlider.prototype.setMap = function(map) {
      goog.base(this, 'setMap', map);
      Eif (map) {
        map.render();
      }
    };

    /**
     * Initializes the slider element. This will determine and set this controls
     * direction_ and also constrain the dragging of the thumb to always be within
     * the bounds of the container.
     *
     * @private
     */
    ol.control.ZoomSlider.prototype.initSlider_ = function() {
      var container = this.element;
      var containerSize = goog.style.getSize(container);

      var thumb = goog.dom.getFirstElementChild(container);
      var thumbMargins = goog.style.getMarginBox(thumb);
      var thumbBorderBoxSize = goog.style.getBorderBoxSize(thumb);
      var thumbWidth = thumbBorderBoxSize.width +
          thumbMargins.right + thumbMargins.left;
      var thumbHeight = thumbBorderBoxSize.height +
          thumbMargins.top + thumbMargins.bottom;
      this.thumbSize_ = [thumbWidth, thumbHeight];

      var width = containerSize.width - thumbWidth;
      var height = containerSize.height - thumbHeight;

      var limits;
      Eif (containerSize.width > containerSize.height) {
        this.direction_ = ol.control.ZoomSlider.direction.HORIZONTAL;
        limits = new goog.math.Rect(0, 0, width, 0);
      } else {
        this.direction_ = ol.control.ZoomSlider.direction.VERTICAL;
        limits = new goog.math.Rect(0, 0, 0, height);
      }
      this.dragger_.setLimits(limits);
      this.sliderInitialized_ = true;
    };

    /**
     * Update the zoomslider element.
     * @param {ol.MapEvent} mapEvent Map event.
     * @this {ol.control.ZoomSlider}
     * @api
     */
    ol.control.ZoomSlider.render = function(mapEvent) {
      if (!mapEvent.frameState) {
        return;
      }
      goog.asserts.assert(mapEvent.frameState.viewState,
          'viewState should be defined');
      if (!this.sliderInitialized_) {
        this.initSlider_();
      }
      var res = mapEvent.frameState.viewState.resolution;
      if (res !== this.currentResolution_) {
        this.currentResolution_ = res;
        this.setThumbPosition_(res);
      }
    };

    /**
     * @param {goog.events.BrowserEvent} browserEvent The browser event to handle.
     * @private
     */
    ol.control.ZoomSlider.prototype.handleContainerClick_ = function(browserEvent) {
      var map = this.getMap();
      var view = map.getView();
      var currentResolution = view.getResolution();
      goog.asserts.assert(currentResolution,
          'currentResolution should be defined');
      map.beforeRender(ol.animation.zoom({
        resolution: currentResolution,
        duration: this.duration_,
        easing: ol.easing.easeOut
      }));
      var relativePosition = this.getRelativePosition_(
          browserEvent.offsetX - this.thumbSize_[0] / 2,
          browserEvent.offsetY - this.thumbSize_[1] / 2);
      var resolution = this.getResolutionForPosition_(relativePosition);
      view.setResolution(view.constrainResolution(resolution));
    };

    /**
     * Handle dragger start events.
     * @param {goog.fx.DragEvent} event The drag event.
     * @private
     */
    ol.control.ZoomSlider.prototype.handleDraggerStart_ = function(event) {
      this.getMap().getView().setHint(ol.ViewHint.INTERACTING, 1);
    };

    /**
     * Handle dragger drag events.
     *
     * @param {goog.fx.DragEvent} event The drag event.
     * @private
     */
    ol.control.ZoomSlider.prototype.handleDraggerDrag_ = function(event) {
      var relativePosition = this.getRelativePosition_(event.left, event.top);
      this.currentResolution_ = this.getResolutionForPosition_(relativePosition);
      this.getMap().getView().setResolution(this.currentResolution_);
    };

    /**
     * Handle dragger end events.
     * @param {goog.fx.DragEvent} event The drag event.
     * @private
     */
    ol.control.ZoomSlider.prototype.handleDraggerEnd_ = function(event) {
      var map = this.getMap();
      var view = map.getView();
      view.setHint(ol.ViewHint.INTERACTING, -1);
      goog.asserts.assert(this.currentResolution_,
          'this.currentResolution_ should be defined');
      map.beforeRender(ol.animation.zoom({
        resolution: this.currentResolution_,
        duration: this.duration_,
        easing: ol.easing.easeOut
      }));
      var resolution = view.constrainResolution(this.currentResolution_);
      view.setResolution(resolution);
    };

    /**
     * Positions the thumb inside its container according to the given resolution.
     *
     * @param {number} res The res.
     * @private
     */
    ol.control.ZoomSlider.prototype.setThumbPosition_ = function(res) {
      var position = this.getPositionForResolution_(res);
      var dragger = this.dragger_;
      var thumb = goog.dom.getFirstElementChild(this.element);

      if (this.direction_ == ol.control.ZoomSlider.direction.HORIZONTAL) {
        var left = dragger.limits.left + dragger.limits.width * position;
        goog.style.setPosition(thumb, left);
      } else {
        var top = dragger.limits.top + dragger.limits.height * position;
        goog.style.setPosition(thumb, dragger.limits.left, top);
      }
    };

    /**
     * Calculates the relative position of the thumb given x and y offsets.  The
     * relative position scales from 0 to 1.  The x and y offsets are assumed to be
     * in pixel units within the dragger limits.
     *
     * @param {number} x Pixel position relative to the left of the slider.
     * @param {number} y Pixel position relative to the top of the slider.
     * @return {number} The relative position of the thumb.
     * @private
     */
    ol.control.ZoomSlider.prototype.getRelativePosition_ = function(x, y) {
      var draggerLimits = this.dragger_.limits;
      var amount;
      if (this.direction_ === ol.control.ZoomSlider.direction.HORIZONTAL) {
        amount = (x - draggerLimits.left) / draggerLimits.width;
      } else {
        amount = (y - draggerLimits.top) / draggerLimits.height;
      }
      return ol.math.clamp(amount, 0, 1);
    };

    /**
     * Calculates the corresponding resolution of the thumb given its relative
     * position (where 0 is the minimum and 1 is the maximum).
     *
     * @param {number} position The relative position of the thumb.
     * @return {number} The corresponding resolution.
     * @private
     */
    ol.control.ZoomSlider.prototype.getResolutionForPosition_ = function(position) {
      var fn = this.getMap().getView().getResolutionForValueFunction();
      return fn(1 - position);
    };

    /**
     * Determines the relative position of the slider for the given resolution.  A
     * relative position of 0 corresponds to the minimum view resolution.  A
     * relative position of 1 corresponds to the maximum view resolution.
     *
     * @param {number} res The resolution.
     * @return {number} The relative position value (between 0 and 1).
     * @private
     */
    ol.control.ZoomSlider.prototype.getPositionForResolution_ = function(res) {
      var fn = this.getMap().getView().getValueForResolutionFunction();
      return 1 - fn(res);
    };