Fix #6913 - add more documentation to code
[roojs1] / Roo / lib / Dom.js
1 /**
2  * @class Roo.lib.Dom
3  * @licence LGPL
4  * @static
5  * 
6  * Dom utils (from YIU afaik)
7  *
8  * 
9  **/
10 Roo.lib.Dom = {
11     /**
12      * Get the view width
13      * @param {Boolean} full True will get the full document, otherwise it's the view width
14      * @return {Number} The width
15      */
16      
17     getViewWidth : function(full) {
18         return full ? this.getDocumentWidth() : this.getViewportWidth();
19     },
20     /**
21      * Get the view height
22      * @param {Boolean} full True will get the full document, otherwise it's the view height
23      * @return {Number} The height
24      */
25     getViewHeight : function(full) {
26         return full ? this.getDocumentHeight() : this.getViewportHeight();
27     },
28     /**
29      * Get the Full Document height 
30      * @return {Number} The height
31      */
32     getDocumentHeight: function() {
33         var scrollHeight = (document.compatMode != "CSS1Compat") ? document.body.scrollHeight : document.documentElement.scrollHeight;
34         return Math.max(scrollHeight, this.getViewportHeight());
35     },
36     /**
37      * Get the Full Document width
38      * @return {Number} The width
39      */
40     getDocumentWidth: function() {
41         var scrollWidth = (document.compatMode != "CSS1Compat") ? document.body.scrollWidth : document.documentElement.scrollWidth;
42         return Math.max(scrollWidth, this.getViewportWidth());
43     },
44     /**
45      * Get the Window Viewport height
46      * @return {Number} The height
47      */
48     getViewportHeight: function() {
49         var height = self.innerHeight;
50         var mode = document.compatMode;
51
52         if ((mode || Roo.isIE) && !Roo.isOpera) {
53             height = (mode == "CSS1Compat") ?
54                      document.documentElement.clientHeight :
55                      document.body.clientHeight;
56         }
57
58         return height;
59     },
60     /**
61      * Get the Window Viewport width
62      * @return {Number} The width
63      */
64     getViewportWidth: function() {
65         var width = self.innerWidth;
66         var mode = document.compatMode;
67
68         if (mode || Roo.isIE) {
69             width = (mode == "CSS1Compat") ?
70                     document.documentElement.clientWidth :
71                     document.body.clientWidth;
72         }
73         return width;
74     },
75
76     isAncestor : function(p, c) {
77         p = Roo.getDom(p);
78         c = Roo.getDom(c);
79         if (!p || !c) {
80             return false;
81         }
82
83         if (p.contains && !Roo.isSafari) {
84             return p.contains(c);
85         } else if (p.compareDocumentPosition) {
86             return !!(p.compareDocumentPosition(c) & 16);
87         } else {
88             var parent = c.parentNode;
89             while (parent) {
90                 if (parent == p) {
91                     return true;
92                 }
93                 else if (!parent.tagName || parent.tagName.toUpperCase() == "HTML") {
94                     return false;
95                 }
96                 parent = parent.parentNode;
97             }
98             return false;
99         }
100     },
101
102     getRegion : function(el) {
103         return Roo.lib.Region.getRegion(el);
104     },
105
106     getY : function(el) {
107         return this.getXY(el)[1];
108     },
109
110     getX : function(el) {
111         return this.getXY(el)[0];
112     },
113
114     getXY : function(el) {
115         var p, pe, b, scroll, bd = document.body;
116         el = Roo.getDom(el);
117         var fly = Roo.lib.AnimBase.fly;
118         if (el.getBoundingClientRect) {
119             b = el.getBoundingClientRect();
120             scroll = fly(document).getScroll();
121             return [b.left + scroll.left, b.top + scroll.top];
122         }
123         var x = 0, y = 0;
124
125         p = el;
126
127         var hasAbsolute = fly(el).getStyle("position") == "absolute";
128
129         while (p) {
130
131             x += p.offsetLeft;
132             y += p.offsetTop;
133
134             if (!hasAbsolute && fly(p).getStyle("position") == "absolute") {
135                 hasAbsolute = true;
136             }
137
138             if (Roo.isGecko) {
139                 pe = fly(p);
140
141                 var bt = parseInt(pe.getStyle("borderTopWidth"), 10) || 0;
142                 var bl = parseInt(pe.getStyle("borderLeftWidth"), 10) || 0;
143
144
145                 x += bl;
146                 y += bt;
147
148
149                 if (p != el && pe.getStyle('overflow') != 'visible') {
150                     x += bl;
151                     y += bt;
152                 }
153             }
154             p = p.offsetParent;
155         }
156
157         if (Roo.isSafari && hasAbsolute) {
158             x -= bd.offsetLeft;
159             y -= bd.offsetTop;
160         }
161
162         if (Roo.isGecko && !hasAbsolute) {
163             var dbd = fly(bd);
164             x += parseInt(dbd.getStyle("borderLeftWidth"), 10) || 0;
165             y += parseInt(dbd.getStyle("borderTopWidth"), 10) || 0;
166         }
167
168         p = el.parentNode;
169         while (p && p != bd) {
170             if (!Roo.isOpera || (p.tagName != 'TR' && fly(p).getStyle("display") != "inline")) {
171                 x -= p.scrollLeft;
172                 y -= p.scrollTop;
173             }
174             p = p.parentNode;
175         }
176         return [x, y];
177     },
178  
179   
180
181
182     setXY : function(el, xy) {
183         el = Roo.fly(el, '_setXY');
184         el.position();
185         var pts = el.translatePoints(xy);
186         if (xy[0] !== false) {
187             el.dom.style.left = pts.left + "px";
188         }
189         if (xy[1] !== false) {
190             el.dom.style.top = pts.top + "px";
191         }
192     },
193
194     setX : function(el, x) {
195         this.setXY(el, [x, false]);
196     },
197
198     setY : function(el, y) {
199         this.setXY(el, [false, y]);
200     }
201 };