Debian Codegraph @

dojox/html/metrics.js

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