# all files / ol/dom/ dom.js

27.27% Statements 24/88
11.11% Branches 4/36
33.33% Functions 3/9
27.27% Lines 24/88

    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

    1×
    1×

    1×
    1×
    1×
    1×
    1×

    1×
    294×
    294×
    96×

    294×
    96×

    294×

    1×
    1×
    1×

    1×
    1×
    1×

    1×

    1×

    1×

    1×

     // FIXME add tests for browser features (Modernizr?)

    goog.provide('ol.dom');
    goog.provide('ol.dom.BrowserFeature');

    goog.require('goog.asserts');
    goog.require('goog.dom');
    goog.require('goog.userAgent');
    goog.require('goog.vec.Mat4');
    goog.require('ol');

    /**
     * Create an html canvas element and returns its 2d context.
     * @param {number=} opt_width Canvas width.
     * @param {number=} opt_height Canvas height.
     * @return {CanvasRenderingContext2D}
     */
    ol.dom.createCanvasContext2D = function(opt_width, opt_height) {
      var canvas = goog.dom.createElement('CANVAS');
      if (opt_width) {
        canvas.width = opt_width;
      }
      if (opt_height) {
        canvas.height = opt_height;
      }
      return canvas.getContext('2d');
    };

    /**
     * Detect 2d transform.
     * Adapted from http://stackoverflow.com/q/5661671/130442
     * http://caniuse.com/#feat=transforms2d
     * @return {boolean}
     */
    ol.dom.canUseCssTransform = (function() {
      var canUseCssTransform;
      return function() {
        if (canUseCssTransform === undefined) {
          goog.asserts.assert(document.body,
              'document.body should not be null');
          if (!goog.global.getComputedStyle) {
            // this browser is ancient
            canUseCssTransform = false;
          } else {
            var el = goog.dom.createElement('P'),
                has2d,
                transforms = {
                  'webkitTransform': '-webkit-transform',
                  'OTransform': '-o-transform',
                  'msTransform': '-ms-transform',
                  'MozTransform': '-moz-transform',
                  'transform': 'transform'
                };
            document.body.appendChild(el);
            for (var t in transforms) {
              if (t in el.style) {
                el.style[t] = 'translate(1px,1px)';
                has2d = goog.global.getComputedStyle(el).getPropertyValue(
                    transforms[t]);
              }
            }
            goog.dom.removeNode(el);

            canUseCssTransform = (has2d && has2d !== 'none');
          }
        }
        return canUseCssTransform;
      };
    }());

    /**
     * Detect 3d transform.
     * Adapted from http://stackoverflow.com/q/5661671/130442
     * http://caniuse.com/#feat=transforms3d
     * @return {boolean}
     */
    ol.dom.canUseCssTransform3D = (function() {
      var canUseCssTransform3D;
      return function() {
        if (canUseCssTransform3D === undefined) {
          goog.asserts.assert(document.body,
              'document.body should not be null');
          if (!goog.global.getComputedStyle) {
            // this browser is ancient
            canUseCssTransform3D = false;
          } else {
            var el = goog.dom.createElement('P'),
                has3d,
                transforms = {
                  'webkitTransform': '-webkit-transform',
                  'OTransform': '-o-transform',
                  'msTransform': '-ms-transform',
                  'MozTransform': '-moz-transform',
                  'transform': 'transform'
                };
            document.body.appendChild(el);
            for (var t in transforms) {
              if (t in el.style) {
                el.style[t] = 'translate3d(1px,1px,1px)';
                has3d = goog.global.getComputedStyle(el).getPropertyValue(
                    transforms[t]);
              }
            }
            goog.dom.removeNode(el);

            canUseCssTransform3D = (has3d && has3d !== 'none');
          }
        }
        return canUseCssTransform3D;
      };
    }());

    /**
     * @param {Element} element Element.
     * @param {string} value Value.
     */
    ol.dom.setTransform = function(element, value) {
      var style = element.style;
      style.WebkitTransform = value;
      style.MozTransform = value;
      style.OTransform = value;
      style.msTransform = value;
      style.transform = value;

      // IE 9+ seems to assume transform-origin: 100% 100%; for some unknown reason
      if (goog.userAgent.IE && goog.userAgent.isVersionOrHigher('9.0')) {
        element.style.transformOrigin = '0 0';
      }
    };

    /**
     * @param {!Element} element Element.
     * @param {goog.vec.Mat4.Number} transform Matrix.
     * @param {number=} opt_precision Precision.
     */
    ol.dom.transformElement2D = function(element, transform, opt_precision) {
      // using matrix() causes gaps in Chrome and Firefox on Mac OS X, so prefer
      // matrix3d()
      var i;
      if (ol.dom.canUseCssTransform3D()) {
        var value3D;

        if (opt_precision !== undefined) {
          /** @type {Array.<string>} */
          var strings3D = new Array(16);
          for (i = 0; i < 16; ++i) {
            strings3D[i] = transform[i].toFixed(opt_precision);
          }
          value3D = strings3D.join(',');
        } else {
          value3D = transform.join(',');
        }
        ol.dom.setTransform(element, 'matrix3d(' + value3D + ')');
      } else if (ol.dom.canUseCssTransform()) {
        /** @type {Array.<number>} */
        var transform2D = [
          goog.vec.Mat4.getElement(transform, 0, 0),
          goog.vec.Mat4.getElement(transform, 1, 0),
          goog.vec.Mat4.getElement(transform, 0, 1),
          goog.vec.Mat4.getElement(transform, 1, 1),
          goog.vec.Mat4.getElement(transform, 0, 3),
          goog.vec.Mat4.getElement(transform, 1, 3)
        ];
        var value2D;
        if (opt_precision !== undefined) {
          /** @type {Array.<string>} */
          var strings2D = new Array(6);
          for (i = 0; i < 6; ++i) {
            strings2D[i] = transform2D[i].toFixed(opt_precision);
          }
          value2D = strings2D.join(',');
        } else {
          value2D = transform2D.join(',');
        }
        ol.dom.setTransform(element, 'matrix(' + value2D + ')');
      } else {
        element.style.left =
            Math.round(goog.vec.Mat4.getElement(transform, 0, 3)) + 'px';
        element.style.top =
            Math.round(goog.vec.Mat4.getElement(transform, 1, 3)) + 'px';

        // TODO: Add scaling here. This isn't quite as simple as multiplying
        // width/height, because that only changes the container size, not the
        // content size.
      }
    };

    /**
     * Get the current computed width for the given element including margin,
     * padding and border.
     * Equivalent to jQuery's `$(el).outerWidth(true)`.
     * @param {!Element} element Element.
     * @return {number}
     */
    ol.dom.outerWidth = function(element) {
      var width = element.offsetWidth;
      var style = element.currentStyle || window.getComputedStyle(element);
      width += parseInt(style.marginLeft, 10) + parseInt(style.marginRight, 10);

      return width;
    };

    /**
     * Get the current computed height for the given element including margin,
     * padding and border.
     * Equivalent to jQuery's `$(el).outerHeight(true)`.
     * @param {!Element} element Element.
     * @return {number}
     */
    ol.dom.outerHeight = function(element) {
      var height = element.offsetHeight;
      var style = element.currentStyle || window.getComputedStyle(element);
      height += parseInt(style.marginTop, 10) + parseInt(style.marginBottom, 10);

      return height;
    };