Fix #6912 - issue with parsing Roo.lib.Dom
[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     getDocumentHeight: function() {
30         var scrollHeight = (document.compatMode != "CSS1Compat") ? document.body.scrollHeight : document.documentElement.scrollHeight;
31         return Math.max(scrollHeight, this.getViewportHeight());
32     },
33
34     getDocumentWidth: function() {
35         var scrollWidth = (document.compatMode != "CSS1Compat") ? document.body.scrollWidth : document.documentElement.scrollWidth;
36         return Math.max(scrollWidth, this.getViewportWidth());
37     },
38
39     getViewportHeight: function() {
40         var height = self.innerHeight;
41         var mode = document.compatMode;
42
43         if ((mode || Roo.isIE) && !Roo.isOpera) {
44             height = (mode == "CSS1Compat") ?
45                      document.documentElement.clientHeight :
46                      document.body.clientHeight;
47         }
48
49         return height;
50     },
51
52     getViewportWidth: function() {
53         var width = self.innerWidth;
54         var mode = document.compatMode;
55
56         if (mode || Roo.isIE) {
57             width = (mode == "CSS1Compat") ?
58                     document.documentElement.clientWidth :
59                     document.body.clientWidth;
60         }
61         return width;
62     },
63
64     isAncestor : function(p, c) {
65         p = Roo.getDom(p);
66         c = Roo.getDom(c);
67         if (!p || !c) {
68             return false;
69         }
70
71         if (p.contains && !Roo.isSafari) {
72             return p.contains(c);
73         } else if (p.compareDocumentPosition) {
74             return !!(p.compareDocumentPosition(c) & 16);
75         } else {
76             var parent = c.parentNode;
77             while (parent) {
78                 if (parent == p) {
79                     return true;
80                 }
81                 else if (!parent.tagName || parent.tagName.toUpperCase() == "HTML") {
82                     return false;
83                 }
84                 parent = parent.parentNode;
85             }
86             return false;
87         }
88     },
89
90     getRegion : function(el) {
91         return Roo.lib.Region.getRegion(el);
92     },
93
94     getY : function(el) {
95         return this.getXY(el)[1];
96     },
97
98     getX : function(el) {
99         return this.getXY(el)[0];
100     },
101
102     getXY : function(el) {
103         var p, pe, b, scroll, bd = document.body;
104         el = Roo.getDom(el);
105         var fly = Roo.lib.AnimBase.fly;
106         if (el.getBoundingClientRect) {
107             b = el.getBoundingClientRect();
108             scroll = fly(document).getScroll();
109             return [b.left + scroll.left, b.top + scroll.top];
110         }
111         var x = 0, y = 0;
112
113         p = el;
114
115         var hasAbsolute = fly(el).getStyle("position") == "absolute";
116
117         while (p) {
118
119             x += p.offsetLeft;
120             y += p.offsetTop;
121
122             if (!hasAbsolute && fly(p).getStyle("position") == "absolute") {
123                 hasAbsolute = true;
124             }
125
126             if (Roo.isGecko) {
127                 pe = fly(p);
128
129                 var bt = parseInt(pe.getStyle("borderTopWidth"), 10) || 0;
130                 var bl = parseInt(pe.getStyle("borderLeftWidth"), 10) || 0;
131
132
133                 x += bl;
134                 y += bt;
135
136
137                 if (p != el && pe.getStyle('overflow') != 'visible') {
138                     x += bl;
139                     y += bt;
140                 }
141             }
142             p = p.offsetParent;
143         }
144
145         if (Roo.isSafari && hasAbsolute) {
146             x -= bd.offsetLeft;
147             y -= bd.offsetTop;
148         }
149
150         if (Roo.isGecko && !hasAbsolute) {
151             var dbd = fly(bd);
152             x += parseInt(dbd.getStyle("borderLeftWidth"), 10) || 0;
153             y += parseInt(dbd.getStyle("borderTopWidth"), 10) || 0;
154         }
155
156         p = el.parentNode;
157         while (p && p != bd) {
158             if (!Roo.isOpera || (p.tagName != 'TR' && fly(p).getStyle("display") != "inline")) {
159                 x -= p.scrollLeft;
160                 y -= p.scrollTop;
161             }
162             p = p.parentNode;
163         }
164         return [x, y];
165     },
166  
167   
168
169
170     setXY : function(el, xy) {
171         el = Roo.fly(el, '_setXY');
172         el.position();
173         var pts = el.translatePoints(xy);
174         if (xy[0] !== false) {
175             el.dom.style.left = pts.left + "px";
176         }
177         if (xy[1] !== false) {
178             el.dom.style.top = pts.top + "px";
179         }
180     },
181
182     setX : function(el, x) {
183         this.setXY(el, [x, false]);
184     },
185
186     setY : function(el, y) {
187         this.setXY(el, [false, y]);
188     }
189 };