Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / src / ext-core / src / adapter / ext-base-dom.js
1 /*!
2  * Ext JS Library 3.2.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 (function(){
8         var doc = document,
9                 isCSS1 = doc.compatMode == "CSS1Compat",
10                 MAX = Math.max,         
11         ROUND = Math.round,
12                 PARSEINT = parseInt;
13                 
14         Ext.lib.Dom = {
15             isAncestor : function(p, c) {
16                     var ret = false;
17                         
18                         p = Ext.getDom(p);
19                         c = Ext.getDom(c);
20                         if (p && c) {
21                                 if (p.contains) {
22                                         return p.contains(c);
23                                 } else if (p.compareDocumentPosition) {
24                                         return !!(p.compareDocumentPosition(c) & 16);
25                                 } else {
26                                         while (c = c.parentNode) {
27                                                 ret = c == p || ret;                                    
28                                         }
29                                 }                   
30                         }       
31                         return ret;
32                 },
33                 
34         getViewWidth : function(full) {
35             return full ? this.getDocumentWidth() : this.getViewportWidth();
36         },
37
38         getViewHeight : function(full) {
39             return full ? this.getDocumentHeight() : this.getViewportHeight();
40         },
41
42         getDocumentHeight: function() {            
43             return MAX(!isCSS1 ? doc.body.scrollHeight : doc.documentElement.scrollHeight, this.getViewportHeight());
44         },
45
46         getDocumentWidth: function() {            
47             return MAX(!isCSS1 ? doc.body.scrollWidth : doc.documentElement.scrollWidth, this.getViewportWidth());
48         },
49
50         getViewportHeight: function(){
51                 return Ext.isIE ? 
52                            (Ext.isStrict ? doc.documentElement.clientHeight : doc.body.clientHeight) :
53                            self.innerHeight;
54         },
55
56         getViewportWidth : function() {
57                 return !Ext.isStrict && !Ext.isOpera ? doc.body.clientWidth :
58                            Ext.isIE ? doc.documentElement.clientWidth : self.innerWidth;
59         },
60         
61         getY : function(el) {
62             return this.getXY(el)[1];
63         },
64
65         getX : function(el) {
66             return this.getXY(el)[0];
67         },
68
69         getXY : function(el) {
70             var p, 
71                 pe, 
72                 b,
73                 bt, 
74                 bl,     
75                 dbd,            
76                 x = 0,
77                 y = 0, 
78                 scroll,
79                 hasAbsolute, 
80                 bd = (doc.body || doc.documentElement),
81                 ret = [0,0];
82                 
83             el = Ext.getDom(el);
84
85             if(el != bd){
86                     if (el.getBoundingClientRect) {
87                         b = el.getBoundingClientRect();
88                         scroll = fly(document).getScroll();
89                         ret = [ROUND(b.left + scroll.left), ROUND(b.top + scroll.top)];
90                     } else {  
91                             p = el;             
92                             hasAbsolute = fly(el).isStyle("position", "absolute");
93                 
94                             while (p) {
95                                     pe = fly(p);                
96                                 x += p.offsetLeft;
97                                 y += p.offsetTop;
98                 
99                                 hasAbsolute = hasAbsolute || pe.isStyle("position", "absolute");
100                                                 
101                                 if (Ext.isGecko) {                                  
102                                     y += bt = PARSEINT(pe.getStyle("borderTopWidth"), 10) || 0;
103                                     x += bl = PARSEINT(pe.getStyle("borderLeftWidth"), 10) || 0;        
104                 
105                                     if (p != el && !pe.isStyle('overflow','visible')) {
106                                         x += bl;
107                                         y += bt;
108                                     }
109                                 }
110                                 p = p.offsetParent;
111                             }
112                 
113                             if (Ext.isSafari && hasAbsolute) {
114                                 x -= bd.offsetLeft;
115                                 y -= bd.offsetTop;
116                             }
117                 
118                             if (Ext.isGecko && !hasAbsolute) {
119                                 dbd = fly(bd);
120                                 x += PARSEINT(dbd.getStyle("borderLeftWidth"), 10) || 0;
121                                 y += PARSEINT(dbd.getStyle("borderTopWidth"), 10) || 0;
122                             }
123                 
124                             p = el.parentNode;
125                             while (p && p != bd) {
126                                 if (!Ext.isOpera || (p.tagName != 'TR' && !fly(p).isStyle("display", "inline"))) {
127                                     x -= p.scrollLeft;
128                                     y -= p.scrollTop;
129                                 }
130                                 p = p.parentNode;
131                             }
132                             ret = [x,y];
133                     }
134                 }
135             return ret
136         },
137
138         setXY : function(el, xy) {
139             (el = Ext.fly(el, '_setXY')).position();
140             
141             var pts = el.translatePoints(xy),
142                 style = el.dom.style,
143                 pos;                    
144             
145             for (pos in pts) {              
146                     if(!isNaN(pts[pos])) style[pos] = pts[pos] + "px"
147             }
148         },
149
150         setX : function(el, x) {
151             this.setXY(el, [x, false]);
152         },
153
154         setY : function(el, y) {
155             this.setXY(el, [false, y]);
156         }
157     };
158 })();