[483] | 1 | define(["dojo/_base/kernel","dojo/_base/lang", "dojo/_base/sniff", "dojo/ready", "dojo/_base/unload", |
---|
| 2 | "dojo/_base/window", "dojo/dom-geometry"], |
---|
| 3 | function(kernel,lang,has,ready,UnloadUtil,Window,DOMGeom){ |
---|
| 4 | var dhm = lang.getObject("dojox.html.metrics",true); |
---|
| 5 | var dojox = lang.getObject("dojox"); |
---|
| 6 | |
---|
| 7 | // derived from Morris John's emResized measurer |
---|
| 8 | dhm.getFontMeasurements = function(){ |
---|
| 9 | // summary: |
---|
| 10 | // Returns an object that has pixel equivilents of standard font size values. |
---|
| 11 | var heights = { |
---|
| 12 | '1em':0, '1ex':0, '100%':0, '12pt':0, '16px':0, 'xx-small':0, 'x-small':0, |
---|
| 13 | 'small':0, 'medium':0, 'large':0, 'x-large':0, 'xx-large':0 |
---|
| 14 | }; |
---|
| 15 | |
---|
| 16 | if(has("ie")){ |
---|
| 17 | // we do a font-size fix if and only if one isn't applied already. |
---|
| 18 | // NOTE: If someone set the fontSize on the HTML Element, this will kill it. |
---|
| 19 | Window.doc.documentElement.style.fontSize="100%"; |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | // set up the measuring node. |
---|
| 23 | var div=Window.doc.createElement("div"); |
---|
| 24 | var ds = div.style; |
---|
| 25 | ds.position="absolute"; |
---|
| 26 | ds.left="-100px"; |
---|
| 27 | ds.top="0"; |
---|
| 28 | ds.width="30px"; |
---|
| 29 | ds.height="1000em"; |
---|
| 30 | ds.borderWidth="0"; |
---|
| 31 | ds.margin="0"; |
---|
| 32 | ds.padding="0"; |
---|
| 33 | ds.outline="0"; |
---|
| 34 | ds.lineHeight="1"; |
---|
| 35 | ds.overflow="hidden"; |
---|
| 36 | Window.body().appendChild(div); |
---|
| 37 | |
---|
| 38 | // do the measurements. |
---|
| 39 | for(var p in heights){ |
---|
| 40 | ds.fontSize = p; |
---|
| 41 | heights[p] = Math.round(div.offsetHeight * 12/16) * 16/12 / 1000; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | Window.body().removeChild(div); |
---|
| 45 | div = null; |
---|
| 46 | return heights; // object |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | var fontMeasurements = null; |
---|
| 50 | |
---|
| 51 | dhm.getCachedFontMeasurements = function(recalculate){ |
---|
| 52 | if(recalculate || !fontMeasurements){ |
---|
| 53 | fontMeasurements = dhm.getFontMeasurements(); |
---|
| 54 | } |
---|
| 55 | return fontMeasurements; |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | var measuringNode = null, empty = {}; |
---|
| 59 | dhm.getTextBox = function(/* String */ text, /* Object */ style, /* String? */ className){ |
---|
| 60 | var m, s; |
---|
| 61 | if(!measuringNode){ |
---|
| 62 | m = measuringNode = Window.doc.createElement("div"); |
---|
| 63 | // Container that we can set contraints on so that it doesn't |
---|
| 64 | // trigger a scrollbar. |
---|
| 65 | var c = Window.doc.createElement("div"); |
---|
| 66 | c.appendChild(m); |
---|
| 67 | s = c.style; |
---|
| 68 | s.overflow='scroll'; |
---|
| 69 | s.position = "absolute"; |
---|
| 70 | s.left = "0px"; |
---|
| 71 | s.top = "-10000px"; |
---|
| 72 | s.width = "1px"; |
---|
| 73 | s.height = "1px"; |
---|
| 74 | s.visibility = "hidden"; |
---|
| 75 | s.borderWidth = "0"; |
---|
| 76 | s.margin = "0"; |
---|
| 77 | s.padding = "0"; |
---|
| 78 | s.outline = "0"; |
---|
| 79 | Window.body().appendChild(c); |
---|
| 80 | }else{ |
---|
| 81 | m = measuringNode; |
---|
| 82 | } |
---|
| 83 | // reset styles |
---|
| 84 | m.className = ""; |
---|
| 85 | s = m.style; |
---|
| 86 | s.borderWidth = "0"; |
---|
| 87 | s.margin = "0"; |
---|
| 88 | s.padding = "0"; |
---|
| 89 | s.outline = "0"; |
---|
| 90 | // set new style |
---|
| 91 | if(arguments.length > 1 && style){ |
---|
| 92 | for(var i in style){ |
---|
| 93 | if(i in empty){ continue; } |
---|
| 94 | s[i] = style[i]; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | // set classes |
---|
| 98 | if(arguments.length > 2 && className){ |
---|
| 99 | m.className = className; |
---|
| 100 | } |
---|
| 101 | // take a measure |
---|
| 102 | m.innerHTML = text; |
---|
| 103 | var box = DOMGeom.position(m); |
---|
| 104 | // position doesn't report right (reports 1, since parent is 1) |
---|
| 105 | // So we have to look at the scrollWidth to get the real width |
---|
| 106 | // Height is right. |
---|
| 107 | box.w = m.parentNode.scrollWidth; |
---|
| 108 | return box; |
---|
| 109 | }; |
---|
| 110 | |
---|
| 111 | // determine the scrollbar sizes on load. |
---|
| 112 | var scroll={ w:16, h:16 }; |
---|
| 113 | dhm.getScrollbar=function(){ return { w:scroll.w, h:scroll.h }; }; |
---|
| 114 | |
---|
| 115 | dhm._fontResizeNode = null; |
---|
| 116 | |
---|
| 117 | dhm.initOnFontResize = function(interval){ |
---|
| 118 | var f = dhm._fontResizeNode = Window.doc.createElement("iframe"); |
---|
| 119 | var fs = f.style; |
---|
| 120 | fs.position = "absolute"; |
---|
| 121 | fs.width = "5em"; |
---|
| 122 | fs.height = "10em"; |
---|
| 123 | fs.top = "-10000px"; |
---|
| 124 | fs.display = "none"; |
---|
| 125 | if(has("ie")){ |
---|
| 126 | f.onreadystatechange = function(){ |
---|
| 127 | if(f.contentWindow.document.readyState == "complete"){ |
---|
| 128 | f.onresize = f.contentWindow.parent[dojox._scopeName].html.metrics._fontresize; |
---|
| 129 | } |
---|
| 130 | }; |
---|
| 131 | }else{ |
---|
| 132 | f.onload = function(){ |
---|
| 133 | f.contentWindow.onresize = f.contentWindow.parent[dojox._scopeName].html.metrics._fontresize; |
---|
| 134 | }; |
---|
| 135 | } |
---|
| 136 | //The script tag is to work around a known firebug race condition. See comments in bug #9046 |
---|
| 137 | f.setAttribute("src", "javascript:'<html><head><script>if(\"loadFirebugConsole\" in window){window.loadFirebugConsole();}</script></head><body></body></html>'"); |
---|
| 138 | Window.body().appendChild(f); |
---|
| 139 | dhm.initOnFontResize = function(){}; |
---|
| 140 | }; |
---|
| 141 | |
---|
| 142 | dhm.onFontResize = function(){}; |
---|
| 143 | dhm._fontresize = function(){ |
---|
| 144 | dhm.onFontResize(); |
---|
| 145 | }; |
---|
| 146 | |
---|
| 147 | UnloadUtil.addOnUnload(function(){ |
---|
| 148 | // destroy our font resize iframe if we have one |
---|
| 149 | var f = dhm._fontResizeNode; |
---|
| 150 | if(f){ |
---|
| 151 | if(has("ie") && f.onresize){ |
---|
| 152 | f.onresize = null; |
---|
| 153 | }else if(f.contentWindow && f.contentWindow.onresize){ |
---|
| 154 | f.contentWindow.onresize = null; |
---|
| 155 | } |
---|
| 156 | dhm._fontResizeNode = null; |
---|
| 157 | } |
---|
| 158 | }); |
---|
| 159 | |
---|
| 160 | ready(function(){ |
---|
| 161 | // getScrollbar metrics node |
---|
| 162 | try{ |
---|
| 163 | var n=Window.doc.createElement("div"); |
---|
| 164 | n.style.cssText = "top:0;left:0;width:100px;height:100px;overflow:scroll;position:absolute;visibility:hidden;"; |
---|
| 165 | Window.body().appendChild(n); |
---|
| 166 | scroll.w = n.offsetWidth - n.clientWidth; |
---|
| 167 | scroll.h = n.offsetHeight - n.clientHeight; |
---|
| 168 | Window.body().removeChild(n); |
---|
| 169 | //console.log("Scroll bar dimensions: ", scroll); |
---|
| 170 | delete n; |
---|
| 171 | }catch(e){} |
---|
| 172 | |
---|
| 173 | // text size poll setup |
---|
| 174 | if("fontSizeWatch" in kernel.config && !!kernel.config.fontSizeWatch){ |
---|
| 175 | dhm.initOnFontResize(); |
---|
| 176 | } |
---|
| 177 | }); |
---|
| 178 | return dhm; |
---|
| 179 | }); |
---|