# all files / ol/control/ rotatecontrol.js

71.43% Statements 45/63
41.18% Branches 14/34
50% Functions 2/4
71.43% Lines 45/63

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

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

    1×

    110×

    110×

    110×

    110×

    110×
    110×

    110×

    110×

    110×

    110×

    110×

    110×

    110×

    110×

    110×

    110×

    110×
    110×

    1×

    1×

    1×

    1×
    101×
    101×
    25×

    76×
    76×
    74×
    74×
    74×

    74×
    74×
    74×

    76×

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

    goog.require('goog.dom');
    goog.require('goog.dom.classlist');
    goog.require('goog.events');
    goog.require('goog.events.EventType');
    goog.require('ol');
    goog.require('ol.animation');
    goog.require('ol.control.Control');
    goog.require('ol.css');
    goog.require('ol.easing');

    /**
     * @classdesc
     * A button control to reset rotation to 0.
     * To style this control use css selector `.ol-rotate`. A `.ol-hidden` css
     * selector is added to the button when the rotation is 0.
     *
     * @constructor
     * @extends {ol.control.Control}
     * @param {olx.control.RotateOptions=} opt_options Rotate options.
     * @api stable
     */
    ol.control.Rotate = function(opt_options) {

      var options = opt_options ? opt_options : {};

      var className = options.className ?
          options.className : 'ol-rotate';

      var label = options.label ? options.label : '\u21E7';

      /**
       * @type {Element}
       * @private
       */
      this.label_ = null;

      Eif (goog.isString(label)) {
        this.label_ = goog.dom.createDom('SPAN',
            'ol-compass', label);
      } else {
        this.label_ = label;
        goog.dom.classlist.add(this.label_, 'ol-compass');
      }

      var tipLabel = options.tipLabel ? options.tipLabel : 'Reset rotation';

      var button = goog.dom.createDom('BUTTON', {
        'class': className + '-reset',
        'type' : 'button',
        'title': tipLabel
      }, this.label_);

      goog.events.listen(button, goog.events.EventType.CLICK,
          ol.control.Rotate.prototype.handleClick_, false, this);

      var cssClasses = className + ' ' + ol.css.CLASS_UNSELECTABLE + ' ' +
          ol.css.CLASS_CONTROL;
      var element = goog.dom.createDom('DIV', cssClasses, button);

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

      goog.base(this, {
        element: element,
        render: render,
        target: options.target
      });

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

      /**
       * @type {boolean}
       * @private
       */
      this.autoHide_ = options.autoHide !== undefined ? options.autoHide : true;

      /**
       * @private
       * @type {number|undefined}
       */
      this.rotation_ = undefined;

      Eif (this.autoHide_) {
        goog.dom.classlist.add(this.element, ol.css.CLASS_HIDDEN);
      }

    };
    goog.inherits(ol.control.Rotate, ol.control.Control);

    /**
     * @param {goog.events.BrowserEvent} event The event to handle
     * @private
     */
    ol.control.Rotate.prototype.handleClick_ = function(event) {
      event.preventDefault();
      this.resetNorth_();
    };

    /**
     * @private
     */
    ol.control.Rotate.prototype.resetNorth_ = function() {
      var map = this.getMap();
      var view = map.getView();
      if (!view) {
        // the map does not have a view, so we can't act
        // upon it
        return;
      }
      var currentRotation = view.getRotation();
      if (currentRotation !== undefined) {
        if (this.duration_ > 0) {
          currentRotation = currentRotation % (2 * Math.PI);
          if (currentRotation < -Math.PI) {
            currentRotation += 2 * Math.PI;
          }
          if (currentRotation > Math.PI) {
            currentRotation -= 2 * Math.PI;
          }
          map.beforeRender(ol.animation.rotate({
            rotation: currentRotation,
            duration: this.duration_,
            easing: ol.easing.easeOut
          }));
        }
        view.setRotation(0);
      }
    };

    /**
     * Update the rotate control element.
     * @param {ol.MapEvent} mapEvent Map event.
     * @this {ol.control.Rotate}
     * @api
     */
    ol.control.Rotate.render = function(mapEvent) {
      var frameState = mapEvent.frameState;
      if (!frameState) {
        return;
      }
      var rotation = frameState.viewState.rotation;
      if (rotation != this.rotation_) {
        var transform = 'rotate(' + rotation + 'rad)';
        Eif (this.autoHide_) {
          goog.dom.classlist.enable(
              this.element, ol.css.CLASS_HIDDEN, rotation === 0);
        }
        this.label_.style.msTransform = transform;
        this.label_.style.webkitTransform = transform;
        this.label_.style.transform = transform;
      }
      this.rotation_ = rotation;
    };