Requires

Provides

Element.Measure.js

Extends the Element native object to include methods useful in measuring dimensions.

License:
MIT-style license
Authors:
Aaron Newton
  1. 28
  2. 29
  3. 30
  4. 31
  5. 32
  6. 33
  7. 34
  8. 35
  9. 36
  10. 37
  11. 38
  12. 39
  13. 40
  14. 41
  15. 42
  16. 43
  17. 44
  18. 45
  19. 46
  20. 47
  21. 48
  22. 49
  23. 50
  24. 51
  25. 52
  26. 53
  27. 54
  28. 55
  29. 56
  30. 57
  31. 58
  32. 59
  33. 60
  34. 61
  35. 62
  36. 63
  37. 64
  38. 65
  39. 66
  40. 67
  41. 68
  42. 69
  43. 70
  44. 71
  45. 72
  46. 73
  47. 74
  48. 75
  49. 76
  50. 77
  51. 78
  52. 79
  53. 80
  54. 81
  55. 82
  56. 83
  57. 84
  58. 85
  59. 86
  60. 87
  61. 88
  62. 89
  63. 90
  64. 91
  65. 92
  66. 93
  67. 94
  68. 95
  69. 96
  70. 97
  71. 98
  72. 99
  73. 100
  74. 101
  75. 102
  76. 103
  77. 104
  78. 105
  79. 106
  80. 107
  81. 108
  82. 109
  83. 110
  84. 111
  85. 112
  86. 113
  87. 114
  88. 115
  89. 116
  90. 117
  91. 118
  92. 119
  93. 120
(function(){ var getStylesList = function(styles, planes){ var list = []; Object.each(planes, function(directions){ Object.each(directions, function(edge){ styles.each(function(style){ if (style == 'border') list.push(style + '-' + edge + '-width'); else list.push(style + '-' + edge); }); }); }); return list; }; var calculateEdgeSize = function(edge, styles){ var total = 0; Object.each(styles, function(value, style){ if (style.test(edge)) total += value.toInt(); }); return total; }; Element.implement({ measure: function(fn){ var visibility = function(el){ return !!(!el || el.offsetHeight || el.offsetWidth); }; if (visibility(this)) return fn.apply(this); var parent = this.getParent(), restorers = [], toMeasure = []; while (!visibility(parent) && parent != document.body){ toMeasure.push(parent.expose()); parent = parent.getParent(); } var restore = this.expose(); var result = fn.apply(this); restore(); toMeasure.each(function(restore){ restore(); }); return result; }, expose: function(){ if (this.getStyle('display') != 'none') return function(){}; var before = this.style.cssText; this.setStyles({ display: 'block', position: 'absolute', visibility: 'hidden' }); return function(){ this.style.cssText = before; }.bind(this); }, getDimensions: function(options){ options = Object.merge({computeSize: false}, options); var dim = {}; var getSize = function(el, options){ return (options.computeSize) ? el.getComputedSize(options) : el.getSize(); }; var parent = this.getParent('body'); if (parent && this.getStyle('display') == 'none'){ dim = this.measure(function(){ return getSize(this, options); }); } else if (parent){ try { //safari sometimes crashes here, so catch it dim = getSize(this, options); }catch(e){} } else { dim = {x: 0, y: 0}; } return Object.append(dim, (dim.x || dim.x === 0) ? { width: dim.x, height: dim.y } : { x: dim.width, y: dim.height } ); }, getComputedSize: function(options){

<1.2compat> legacy support for my stupid spelling error

  1. 123
if (options && options.plains) options.planes = options.plains;

</1.2compat>

  1. 126
  2. 127
  3. 128
  4. 129
  5. 130
  6. 131
  7. 132
  8. 133
  9. 134
  10. 135
  11. 136
  12. 137
  13. 138
  14. 139
  15. 140
  16. 141
  17. 142
  18. 143
  19. 144
  20. 145
  21. 146
  22. 147
  23. 148
  24. 149
  25. 150
  26. 151
  27. 152
  28. 153
  29. 154
  30. 155
  31. 156
  32. 157
  33. 158
  34. 159
  35. 160
  36. 161
  37. 162
  38. 163
  39. 164
  40. 165
  41. 166
  42. 167
  43. 168
  44. 169
  45. 170
  46. 171
  47. 172
  48. 173
options = Object.merge({ styles: ['padding','border'], planes: { height: ['top','bottom'], width: ['left','right'] }, mode: 'both' }, options); var styles = {}, size = {width: 0, height: 0}; switch (options.mode){ case 'vertical': delete size.width; delete options.planes.width; break; case 'horizontal': delete size.height; delete options.planes.height; break; } getStylesList(options.styles, options.planes).each(function(style){ styles[style] = this.getStyle(style).toInt(); }, this); Object.each(options.planes, function(edges, plane){ var capitalized = plane.capitalize(); styles[plane] = this.getStyle(plane).toInt(); size['total' + capitalized] = styles[plane]; edges.each(function(edge){ var edgesize = calculateEdgeSize(edge, styles); size['computed' + edge.capitalize()] = edgesize; size['total' + capitalized] += edgesize; }); }, this); return Object.append(size, styles); } }); })();