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 | if(has("ie")){ |
---|
125 | f.onreadystatechange = function(){ |
---|
126 | if(f.contentWindow.document.readyState == "complete"){ |
---|
127 | f.onresize = f.contentWindow.parent[dojox._scopeName].html.metrics._fontresize; |
---|
128 | } |
---|
129 | }; |
---|
130 | }else{ |
---|
131 | f.onload = function(){ |
---|
132 | f.contentWindow.onresize = f.contentWindow.parent[dojox._scopeName].html.metrics._fontresize; |
---|
133 | }; |
---|
134 | } |
---|
135 | //The script tag is to work around a known firebug race condition. See comments in bug #9046 |
---|
136 | f.setAttribute("src", "javascript:'<html><head><script>if(\"loadFirebugConsole\" in window){window.loadFirebugConsole();}</script></head><body></body></html>'"); |
---|
137 | Window.body().appendChild(f); |
---|
138 | dhm.initOnFontResize = function(){}; |
---|
139 | }; |
---|
140 | |
---|
141 | dhm.onFontResize = function(){}; |
---|
142 | dhm._fontresize = function(){ |
---|
143 | dhm.onFontResize(); |
---|
144 | } |
---|
145 | |
---|
146 | UnloadUtil.addOnUnload(function(){ |
---|
147 | // destroy our font resize iframe if we have one |
---|
148 | var f = dhm._fontResizeNode; |
---|
149 | if(f){ |
---|
150 | if(has("ie") && f.onresize){ |
---|
151 | f.onresize = null; |
---|
152 | }else if(f.contentWindow && f.contentWindow.onresize){ |
---|
153 | f.contentWindow.onresize = null; |
---|
154 | } |
---|
155 | dhm._fontResizeNode = null; |
---|
156 | } |
---|
157 | }); |
---|
158 | |
---|
159 | ready(function(){ |
---|
160 | // getScrollbar metrics node |
---|
161 | try{ |
---|
162 | var n=Window.doc.createElement("div"); |
---|
163 | n.style.cssText = "top:0;left:0;width:100px;height:100px;overflow:scroll;position:absolute;visibility:hidden;"; |
---|
164 | Window.body().appendChild(n); |
---|
165 | scroll.w = n.offsetWidth - n.clientWidth; |
---|
166 | scroll.h = n.offsetHeight - n.clientHeight; |
---|
167 | Window.body().removeChild(n); |
---|
168 | //console.log("Scroll bar dimensions: ", scroll); |
---|
169 | delete n; |
---|
170 | }catch(e){} |
---|
171 | |
---|
172 | // text size poll setup |
---|
173 | if("fontSizeWatch" in kernel.config && !!kernel.config.fontSizeWatch){ |
---|
174 | dhm.initOnFontResize(); |
---|
175 | } |
---|
176 | }); |
---|
177 | return dhm; |
---|
178 | }); |
---|