# all files / ol/render/ box.js

92.45% Statements 49/53
66.67% Branches 4/6
100% Functions 7/7
92.45% Lines 49/53

    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

    1×

    1×
    1×
    1×

    1×

    167×

    167×
    167×
    167×

    167×

    167×

    167×

    1×

    1×
    1×

    1×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×

    1×
    7×

    7×
    7×
    6×

    1×
    3×
    3×
    3×
    3×

    1×
    3×

    3×

    3×
    3×
    3×
    3×

    3×

    3×
    3×
    3×

    1×
    4×

     // FIXME add rotation

    goog.provide('ol.render.Box');

    goog.require('goog.asserts');
    goog.require('ol.Disposable');
    goog.require('ol.geom.Polygon');

    /**
     * @constructor
     * @extends {ol.Disposable}
     * @param {string} className CSS class name.
     */
    ol.render.Box = function(className) {

      /**
       * @type {ol.geom.Polygon}
       * @private
       */
      this.geometry_ = null;

      /**
       * @type {HTMLDivElement}
       * @private
       */
      this.element_ = /** @type {HTMLDivElement} */ (document.createElement('div'));
      this.element_.style.position = 'absolute';
      this.element_.className = 'ol-box ' + className;

      /**
       * @private
       * @type {ol.Map}
       */
      this.map_ = null;

      /**
       * @private
       * @type {ol.Pixel}
       */
      this.startPixel_ = null;

      /**
       * @private
       * @type {ol.Pixel}
       */
      this.endPixel_ = null;

    };
    goog.inherits(ol.render.Box, ol.Disposable);

    /**
     * @inheritDoc
     */
    ol.render.Box.prototype.disposeInternal = function() {
      this.setMap(null);
    };

    /**
     * @private
     */
    ol.render.Box.prototype.render_ = function() {
      var startPixel = this.startPixel_;
      var endPixel = this.endPixel_;
      goog.asserts.assert(startPixel, 'this.startPixel_ must be truthy');
      goog.asserts.assert(endPixel, 'this.endPixel_ must be truthy');
      var px = 'px';
      var style = this.element_.style;
      style.left = Math.min(startPixel[0], endPixel[0]) + px;
      style.top = Math.min(startPixel[1], endPixel[1]) + px;
      style.width = Math.abs(endPixel[0] - startPixel[0]) + px;
      style.height = Math.abs(endPixel[1] - startPixel[1]) + px;
    };

    /**
     * @param {ol.Map} map Map.
     */
    ol.render.Box.prototype.setMap = function(map) {
      Iif (this.map_) {
        this.map_.getOverlayContainer().removeChild(this.element_);
        var style = this.element_.style;
        style.left = style.top = style.width = style.height = 'inherit';
      }
      this.map_ = map;
      if (this.map_) {
        this.map_.getOverlayContainer().appendChild(this.element_);
      }
    };

    /**
     * @param {ol.Pixel} startPixel Start pixel.
     * @param {ol.Pixel} endPixel End pixel.
     */
    ol.render.Box.prototype.setPixels = function(startPixel, endPixel) {
      this.startPixel_ = startPixel;
      this.endPixel_ = endPixel;
      this.createOrUpdateGeometry();
      this.render_();
    };

    /**
     * Creates or updates the cached geometry.
     */
    ol.render.Box.prototype.createOrUpdateGeometry = function() {
      goog.asserts.assert(this.startPixel_,
          'this.startPixel_ must be truthy');
      goog.asserts.assert(this.endPixel_,
          'this.endPixel_ must be truthy');
      goog.asserts.assert(this.map_, 'this.map_ must be truthy');
      var startPixel = this.startPixel_;
      var endPixel = this.endPixel_;
      var pixels = [
        startPixel,
        [startPixel[0], endPixel[1]],
        endPixel,
        [endPixel[0], startPixel[1]]
      ];
      var coordinates = pixels.map(this.map_.getCoordinateFromPixel, this.map_);
      // close the polygon
      coordinates[4] = coordinates[0].slice();
      Eif (!this.geometry_) {
        this.geometry_ = new ol.geom.Polygon([coordinates]);
      } else {
        this.geometry_.setCoordinates([coordinates]);
      }
    };

    /**
     * @return {ol.geom.Polygon} Geometry.
     */
    ol.render.Box.prototype.getGeometry = function() {
      return this.geometry_;
    };