]> git.wh0rd.org - tt-rss.git/blame - lib/dojo/dom-style.js.uncompressed.js
modify dojo rebuild script to remove uncompressed files
[tt-rss.git] / lib / dojo / dom-style.js.uncompressed.js
CommitLineData
f0cfe83e
AD
1define("dojo/dom-style", ["./sniff", "./dom"], function(has, dom){
2 // module:
3 // dojo/dom-style
4
5 // =============================
6 // Style Functions
7 // =============================
8
9 // getComputedStyle drives most of the style code.
10 // Wherever possible, reuse the returned object.
11 //
12 // API functions below that need to access computed styles accept an
13 // optional computedStyle parameter.
14 // If this parameter is omitted, the functions will call getComputedStyle themselves.
15 // This way, calling code can access computedStyle once, and then pass the reference to
16 // multiple API functions.
17
18 // Although we normally eschew argument validation at this
19 // level, here we test argument 'node' for (duck)type,
20 // by testing nodeType, ecause 'document' is the 'parentNode' of 'body'
21 // it is frequently sent to this function even
22 // though it is not Element.
23 var getComputedStyle, style = {
24 // summary:
25 // This module defines the core dojo DOM style API.
26 };
27 if(has("webkit")){
28 getComputedStyle = function(/*DomNode*/ node){
29 var s;
30 if(node.nodeType == 1){
31 var dv = node.ownerDocument.defaultView;
32 s = dv.getComputedStyle(node, null);
33 if(!s && node.style){
34 node.style.display = "";
35 s = dv.getComputedStyle(node, null);
36 }
37 }
38 return s || {};
39 };
40 }else if(has("ie") && (has("ie") < 9 || has("quirks"))){
41 getComputedStyle = function(node){
42 // IE (as of 7) doesn't expose Element like sane browsers
43 // currentStyle can be null on IE8!
44 return node.nodeType == 1 /* ELEMENT_NODE*/ && node.currentStyle ? node.currentStyle : {};
45 };
46 }else{
47 getComputedStyle = function(node){
48 return node.nodeType == 1 /* ELEMENT_NODE*/ ?
49 node.ownerDocument.defaultView.getComputedStyle(node, null) : {};
50 };
51 }
52 style.getComputedStyle = getComputedStyle;
53 /*=====
54 style.getComputedStyle = function(node){
55 // summary:
56 // Returns a "computed style" object.
57 //
58 // description:
59 // Gets a "computed style" object which can be used to gather
60 // information about the current state of the rendered node.
61 //
62 // Note that this may behave differently on different browsers.
63 // Values may have different formats and value encodings across
64 // browsers.
65 //
66 // Note also that this method is expensive. Wherever possible,
67 // reuse the returned object.
68 //
69 // Use the dojo.style() method for more consistent (pixelized)
70 // return values.
71 //
72 // node: DOMNode
73 // A reference to a DOM node. Does NOT support taking an
74 // ID string for speed reasons.
75 // example:
76 // | dojo.getComputedStyle(dojo.byId('foo')).borderWidth;
77 //
78 // example:
79 // Reusing the returned object, avoiding multiple lookups:
80 // | var cs = dojo.getComputedStyle(dojo.byId("someNode"));
81 // | var w = cs.width, h = cs.height;
82 return; // CSS2Properties
83 };
84 =====*/
85
86 var toPixel;
87 if(!has("ie")){
88 toPixel = function(element, value){
89 // style values can be floats, client code may want
90 // to round for integer pixels.
91 return parseFloat(value) || 0;
92 };
93 }else{
94 toPixel = function(element, avalue){
95 if(!avalue){ return 0; }
96 // on IE7, medium is usually 4 pixels
97 if(avalue == "medium"){ return 4; }
98 // style values can be floats, client code may
99 // want to round this value for integer pixels.
100 if(avalue.slice && avalue.slice(-2) == 'px'){ return parseFloat(avalue); }
101 var s = element.style, rs = element.runtimeStyle, cs = element.currentStyle,
102 sLeft = s.left, rsLeft = rs.left;
103 rs.left = cs.left;
104 try{
105 // 'avalue' may be incompatible with style.left, which can cause IE to throw
106 // this has been observed for border widths using "thin", "medium", "thick" constants
107 // those particular constants could be trapped by a lookup
108 // but perhaps there are more
109 s.left = avalue;
110 avalue = s.pixelLeft;
111 }catch(e){
112 avalue = 0;
113 }
114 s.left = sLeft;
115 rs.left = rsLeft;
116 return avalue;
117 };
118 }
119 style.toPixelValue = toPixel;
120 /*=====
121 style.toPixelValue = function(node, value){
122 // summary:
123 // converts style value to pixels on IE or return a numeric value.
124 // node: DOMNode
125 // value: String
126 // returns: Number
127 };
128 =====*/
129
130 // FIXME: there opacity quirks on FF that we haven't ported over. Hrm.
131
132 var astr = "DXImageTransform.Microsoft.Alpha";
133 var af = function(n, f){
134 try{
135 return n.filters.item(astr);
136 }catch(e){
137 return f ? {} : null;
138 }
139 };
140
141 var _getOpacity =
142 has("ie") < 9 || (has("ie") < 10 && has("quirks")) ? function(node){
143 try{
144 return af(node).Opacity / 100; // Number
145 }catch(e){
146 return 1; // Number
147 }
148 } :
149 function(node){
150 return getComputedStyle(node).opacity;
151 };
152
153 var _setOpacity =
154 has("ie") < 9 || (has("ie") < 10 && has("quirks")) ? function(/*DomNode*/ node, /*Number*/ opacity){
155 var ov = opacity * 100, opaque = opacity == 1;
156 node.style.zoom = opaque ? "" : 1;
157
158 if(!af(node)){
159 if(opaque){
160 return opacity;
161 }
162 node.style.filter += " progid:" + astr + "(Opacity=" + ov + ")";
163 }else{
164 af(node, 1).Opacity = ov;
165 }
166
167 // on IE7 Alpha(Filter opacity=100) makes text look fuzzy so disable it altogether (bug #2661),
168 //but still update the opacity value so we can get a correct reading if it is read later.
169 af(node, 1).Enabled = !opaque;
170
171 if(node.tagName.toLowerCase() == "tr"){
172 for(var td = node.firstChild; td; td = td.nextSibling){
173 if(td.tagName.toLowerCase() == "td"){
174 _setOpacity(td, opacity);
175 }
176 }
177 }
178 return opacity;
179 } :
180 function(node, opacity){
181 return node.style.opacity = opacity;
182 };
183
184 var _pixelNamesCache = {
185 left: true, top: true
186 };
187 var _pixelRegExp = /margin|padding|width|height|max|min|offset/; // |border
188 function _toStyleValue(node, type, value){
189 //TODO: should we really be doing string case conversion here? Should we cache it? Need to profile!
190 type = type.toLowerCase();
191 if(has("ie")){
192 if(value == "auto"){
193 if(type == "height"){ return node.offsetHeight; }
194 if(type == "width"){ return node.offsetWidth; }
195 }
196 if(type == "fontweight"){
197 switch(value){
198 case 700: return "bold";
199 case 400:
200 default: return "normal";
201 }
202 }
203 }
204 if(!(type in _pixelNamesCache)){
205 _pixelNamesCache[type] = _pixelRegExp.test(type);
206 }
207 return _pixelNamesCache[type] ? toPixel(node, value) : value;
208 }
209
210 var _floatStyle = has("ie") ? "styleFloat" : "cssFloat",
211 _floatAliases = {"cssFloat": _floatStyle, "styleFloat": _floatStyle, "float": _floatStyle};
212
213 // public API
214
215 style.get = function getStyle(/*DOMNode|String*/ node, /*String?*/ name){
216 // summary:
217 // Accesses styles on a node.
218 // description:
219 // Getting the style value uses the computed style for the node, so the value
220 // will be a calculated value, not just the immediate node.style value.
221 // Also when getting values, use specific style names,
222 // like "borderBottomWidth" instead of "border" since compound values like
223 // "border" are not necessarily reflected as expected.
224 // If you want to get node dimensions, use `dojo.marginBox()`,
225 // `dojo.contentBox()` or `dojo.position()`.
226 // node: DOMNode|String
227 // id or reference to node to get style for
228 // name: String?
229 // the style property to get
230 // example:
231 // Passing only an ID or node returns the computed style object of
232 // the node:
233 // | dojo.getStyle("thinger");
234 // example:
235 // Passing a node and a style property returns the current
236 // normalized, computed value for that property:
237 // | dojo.getStyle("thinger", "opacity"); // 1 by default
238
239 var n = dom.byId(node), l = arguments.length, op = (name == "opacity");
240 if(l == 2 && op){
241 return _getOpacity(n);
242 }
243 name = _floatAliases[name] || name;
244 var s = style.getComputedStyle(n);
245 return (l == 1) ? s : _toStyleValue(n, name, s[name] || n.style[name]); /* CSS2Properties||String||Number */
246 };
247
248 style.set = function setStyle(/*DOMNode|String*/ node, /*String|Object*/ name, /*String?*/ value){
249 // summary:
250 // Sets styles on a node.
251 // node: DOMNode|String
252 // id or reference to node to set style for
253 // name: String|Object
254 // the style property to set in DOM-accessor format
255 // ("borderWidth", not "border-width") or an object with key/value
256 // pairs suitable for setting each property.
257 // value: String?
258 // If passed, sets value on the node for style, handling
259 // cross-browser concerns. When setting a pixel value,
260 // be sure to include "px" in the value. For instance, top: "200px".
261 // Otherwise, in some cases, some browsers will not apply the style.
262 //
263 // example:
264 // Passing a node, a style property, and a value changes the
265 // current display of the node and returns the new computed value
266 // | dojo.setStyle("thinger", "opacity", 0.5); // == 0.5
267 //
268 // example:
269 // Passing a node, an object-style style property sets each of the values in turn and returns the computed style object of the node:
270 // | dojo.setStyle("thinger", {
271 // | "opacity": 0.5,
272 // | "border": "3px solid black",
273 // | "height": "300px"
274 // | });
275 //
276 // example:
277 // When the CSS style property is hyphenated, the JavaScript property is camelCased.
278 // font-size becomes fontSize, and so on.
279 // | dojo.setStyle("thinger",{
280 // | fontSize:"14pt",
281 // | letterSpacing:"1.2em"
282 // | });
283 //
284 // example:
285 // dojo/NodeList implements .style() using the same syntax, omitting the "node" parameter, calling
286 // dojo.style() on every element of the list. See: `dojo.query()` and `dojo/NodeList`
287 // | dojo.query(".someClassName").style("visibility","hidden");
288 // | // or
289 // | dojo.query("#baz > div").style({
290 // | opacity:0.75,
291 // | fontSize:"13pt"
292 // | });
293
294 var n = dom.byId(node), l = arguments.length, op = (name == "opacity");
295 name = _floatAliases[name] || name;
296 if(l == 3){
297 return op ? _setOpacity(n, value) : n.style[name] = value; // Number
298 }
299 for(var x in name){
300 style.set(node, x, name[x]);
301 }
302 return style.getComputedStyle(n);
303 };
304
305 return style;
306});