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