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