Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / core / src / dom / Element.static-more.js
1 /**
2  * @class Ext.core.Element
3  */
4 (function(){
5     var doc = document,
6         isCSS1 = doc.compatMode == "CSS1Compat",
7         ELEMENT = Ext.core.Element,
8         fly = function(el){
9             if (!_fly) {
10                 _fly = new Ext.core.Element.Flyweight();
11             }
12             _fly.dom = el;
13             return _fly;
14         }, _fly;
15
16     Ext.apply(ELEMENT, {
17         isAncestor : function(p, c) {
18             var ret = false;
19
20             p = Ext.getDom(p);
21             c = Ext.getDom(c);
22             if (p && c) {
23                 if (p.contains) {
24                     return p.contains(c);
25                 } else if (p.compareDocumentPosition) {
26                     return !!(p.compareDocumentPosition(c) & 16);
27                 } else {
28                     while ((c = c.parentNode)) {
29                         ret = c == p || ret;
30                     }
31                 }
32             }
33             return ret;
34         },
35
36         getViewWidth : function(full) {
37             return full ? ELEMENT.getDocumentWidth() : ELEMENT.getViewportWidth();
38         },
39
40         getViewHeight : function(full) {
41             return full ? ELEMENT.getDocumentHeight() : ELEMENT.getViewportHeight();
42         },
43
44         getDocumentHeight: function() {
45             return Math.max(!isCSS1 ? doc.body.scrollHeight : doc.documentElement.scrollHeight, ELEMENT.getViewportHeight());
46         },
47
48         getDocumentWidth: function() {
49             return Math.max(!isCSS1 ? doc.body.scrollWidth : doc.documentElement.scrollWidth, ELEMENT.getViewportWidth());
50         },
51
52         getViewportHeight: function(){
53             return Ext.isIE ?
54                    (Ext.isStrict ? doc.documentElement.clientHeight : doc.body.clientHeight) :
55                    self.innerHeight;
56         },
57
58         getViewportWidth : function() {
59             return (!Ext.isStrict && !Ext.isOpera) ? doc.body.clientWidth :
60                    Ext.isIE ? doc.documentElement.clientWidth : self.innerWidth;
61         },
62
63         getY : function(el) {
64             return ELEMENT.getXY(el)[1];
65         },
66
67         getX : function(el) {
68             return ELEMENT.getXY(el)[0];
69         },
70
71         getXY : function(el) {
72             var p,
73                 pe,
74                 b,
75                 bt,
76                 bl,
77                 dbd,
78                 x = 0,
79                 y = 0,
80                 scroll,
81                 hasAbsolute,
82                 bd = (doc.body || doc.documentElement),
83                 ret = [0,0];
84
85             el = Ext.getDom(el);
86
87             if(el != bd){
88                 hasAbsolute = fly(el).isStyle("position", "absolute");
89
90                 if (el.getBoundingClientRect) {
91                     b = el.getBoundingClientRect();
92                     scroll = fly(document).getScroll();
93                     ret = [Math.round(b.left + scroll.left), Math.round(b.top + scroll.top)];
94                 } else {
95                     p = el;
96
97                     while (p) {
98                         pe = fly(p);
99                         x += p.offsetLeft;
100                         y += p.offsetTop;
101
102                         hasAbsolute = hasAbsolute || pe.isStyle("position", "absolute");
103
104                         if (Ext.isGecko) {
105                             y += bt = parseInt(pe.getStyle("borderTopWidth"), 10) || 0;
106                             x += bl = parseInt(pe.getStyle("borderLeftWidth"), 10) || 0;
107
108                             if (p != el && !pe.isStyle('overflow','visible')) {
109                                 x += bl;
110                                 y += bt;
111                             }
112                         }
113                         p = p.offsetParent;
114                     }
115
116                     if (Ext.isSafari && hasAbsolute) {
117                         x -= bd.offsetLeft;
118                         y -= bd.offsetTop;
119                     }
120
121                     if (Ext.isGecko && !hasAbsolute) {
122                         dbd = fly(bd);
123                         x += parseInt(dbd.getStyle("borderLeftWidth"), 10) || 0;
124                         y += parseInt(dbd.getStyle("borderTopWidth"), 10) || 0;
125                     }
126
127                     p = el.parentNode;
128                     while (p && p != bd) {
129                         if (!Ext.isOpera || (p.tagName != 'TR' && !fly(p).isStyle("display", "inline"))) {
130                             x -= p.scrollLeft;
131                             y -= p.scrollTop;
132                         }
133                         p = p.parentNode;
134                     }
135                     ret = [x,y];
136                 }
137             }
138             return ret;
139         },
140
141         setXY : function(el, xy) {
142             (el = Ext.fly(el, '_setXY')).position();
143
144             var pts = el.translatePoints(xy),
145                 style = el.dom.style,
146                 pos;
147
148             for (pos in pts) {
149                 if (!isNaN(pts[pos])) {
150                     style[pos] = pts[pos] + "px";
151                 }
152             }
153         },
154
155         setX : function(el, x) {
156             ELEMENT.setXY(el, [x, false]);
157         },
158
159         setY : function(el, y) {
160             ELEMENT.setXY(el, [false, y]);
161         },
162
163         /**
164          * Serializes a DOM form into a url encoded string
165          * @param {Object} form The form
166          * @return {String} The url encoded form
167          */
168         serializeForm: function(form) {
169             var fElements = form.elements || (document.forms[form] || Ext.getDom(form)).elements,
170                 hasSubmit = false,
171                 encoder = encodeURIComponent,
172                 name,
173                 data = '',
174                 type,
175                 hasValue;
176
177             Ext.each(fElements, function(element){
178                 name = element.name;
179                 type = element.type;
180
181                 if (!element.disabled && name) {
182                     if (/select-(one|multiple)/i.test(type)) {
183                         Ext.each(element.options, function(opt){
184                             if (opt.selected) {
185                                 hasValue = opt.hasAttribute ? opt.hasAttribute('value') : opt.getAttributeNode('value').specified;
186                                 data += String.format("{0}={1}&", encoder(name), encoder(hasValue ? opt.value : opt.text));
187                             }
188                         });
189                     } else if (!(/file|undefined|reset|button/i.test(type))) {
190                         if (!(/radio|checkbox/i.test(type) && !element.checked) && !(type == 'submit' && hasSubmit)) {
191                             data += encoder(name) + '=' + encoder(element.value) + '&';
192                             hasSubmit = /submit/i.test(type);
193                         }
194                     }
195                 }
196             });
197             return data.substr(0, data.length - 1);
198         }
199     });
200 })();