Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / src / adapter / jquery-bridge.js
1 /*!
2  * Ext JS Library 3.2.2
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 if(typeof jQuery == "undefined"){
8     throw "Unable to load Ext, jQuery not found.";
9 }
10
11 (function(){
12 var libFlyweight;
13
14 Ext.lib.Dom = {
15     getViewWidth : function(full){
16         // jQuery doesn't report full window size on document query, so max both
17         return full ? Math.max(jQuery(document).width(),jQuery(window).width()) : jQuery(window).width();
18     },
19
20     getViewHeight : function(full){
21         // jQuery doesn't report full window size on document query, so max both
22         return full ? Math.max(jQuery(document).height(),jQuery(window).height()) : jQuery(window).height();
23     },
24
25     isAncestor : function(p, c){
26         var ret = false;
27
28         p = Ext.getDom(p);
29         c = Ext.getDom(c);
30         if (p && c) {
31             if (p.contains) {
32                 return p.contains(c);
33             } else if (p.compareDocumentPosition) {
34                 return !!(p.compareDocumentPosition(c) & 16);
35             } else {
36                 while (c = c.parentNode) {
37                     ret = c == p || ret;
38                 }
39             }
40         }
41         return ret;
42     },
43
44     getRegion : function(el){
45         return Ext.lib.Region.getRegion(el);
46     },
47
48     //////////////////////////////////////////////////////////////////////////////////////
49     // Use of jQuery.offset() removed to promote consistent behavior across libs.
50     // JVS 05/23/07
51     //////////////////////////////////////////////////////////////////////////////////////
52
53     getY : function(el){
54         return this.getXY(el)[1];
55     },
56
57     getX : function(el){
58         return this.getXY(el)[0];
59     },
60
61     getXY : function(el) {
62         var p, pe, b, scroll, bd = (document.body || document.documentElement);
63         el = Ext.getDom(el);
64
65         if(el == bd){
66             return [0, 0];
67         }
68
69         if (el.getBoundingClientRect) {
70             b = el.getBoundingClientRect();
71             scroll = fly(document).getScroll();
72             return [Math.round(b.left + scroll.left), Math.round(b.top + scroll.top)];
73         }
74         var x = 0, y = 0;
75
76         p = el;
77
78         var hasAbsolute = fly(el).getStyle("position") == "absolute";
79
80         while (p) {
81
82             x += p.offsetLeft;
83             y += p.offsetTop;
84
85             if (!hasAbsolute && fly(p).getStyle("position") == "absolute") {
86                 hasAbsolute = true;
87             }
88
89             if (Ext.isGecko) {
90                 pe = fly(p);
91
92                 var bt = parseInt(pe.getStyle("borderTopWidth"), 10) || 0;
93                 var bl = parseInt(pe.getStyle("borderLeftWidth"), 10) || 0;
94
95
96                 x += bl;
97                 y += bt;
98
99
100                 if (p != el && pe.getStyle('overflow') != 'visible') {
101                     x += bl;
102                     y += bt;
103                 }
104             }
105             p = p.offsetParent;
106         }
107
108         if (Ext.isSafari && hasAbsolute) {
109             x -= bd.offsetLeft;
110             y -= bd.offsetTop;
111         }
112
113         if (Ext.isGecko && !hasAbsolute) {
114             var dbd = fly(bd);
115             x += parseInt(dbd.getStyle("borderLeftWidth"), 10) || 0;
116             y += parseInt(dbd.getStyle("borderTopWidth"), 10) || 0;
117         }
118
119         p = el.parentNode;
120         while (p && p != bd) {
121             if (!Ext.isOpera || (p.tagName != 'TR' && fly(p).getStyle("display") != "inline")) {
122                 x -= p.scrollLeft;
123                 y -= p.scrollTop;
124             }
125             p = p.parentNode;
126         }
127         return [x, y];
128     },
129
130     setXY : function(el, xy){
131         el = Ext.fly(el, '_setXY');
132         el.position();
133         var pts = el.translatePoints(xy);
134         if(xy[0] !== false){
135             el.dom.style.left = pts.left + "px";
136         }
137         if(xy[1] !== false){
138             el.dom.style.top = pts.top + "px";
139         }
140     },
141
142     setX : function(el, x){
143         this.setXY(el, [x, false]);
144     },
145
146     setY : function(el, y){
147         this.setXY(el, [false, y]);
148     }
149 };
150
151 // all lib flyweight calls use their own flyweight to prevent collisions with developer flyweights
152 function fly(el){
153     if(!libFlyweight){
154         libFlyweight = new Ext.Element.Flyweight();
155     }
156     libFlyweight.dom = el;
157     return libFlyweight;
158 }
159 Ext.lib.Event = {
160     getPageX : function(e){
161         e = e.browserEvent || e;
162         return e.pageX;
163     },
164
165     getPageY : function(e){
166         e = e.browserEvent || e;
167         return e.pageY;
168     },
169
170     getXY : function(e){
171         e = e.browserEvent || e;
172         return [e.pageX, e.pageY];
173     },
174
175     getTarget : function(e){
176         return e.target;
177     },
178
179     // all Ext events will go through event manager which provides scoping
180     on : function(el, eventName, fn, scope, override){
181         jQuery(el).bind(eventName, fn);
182     },
183
184     un : function(el, eventName, fn){
185         jQuery(el).unbind(eventName, fn);
186     },
187
188     purgeElement : function(el){
189         jQuery(el).unbind();
190     },
191
192     preventDefault : function(e){
193         e = e.browserEvent || e;
194         if(e.preventDefault){
195             e.preventDefault();
196         }else{
197             e.returnValue = false;
198         }
199     },
200
201     stopPropagation : function(e){
202         e = e.browserEvent || e;
203         if(e.stopPropagation){
204             e.stopPropagation();
205         }else{
206             e.cancelBubble = true;
207         }
208     },
209
210     stopEvent : function(e){
211         this.preventDefault(e);
212         this.stopPropagation(e);
213     },
214
215     onAvailable : function(id, fn, scope){
216         var start = new Date();
217         var f = function(){
218             if(start.getElapsed() > 10000){
219                 clearInterval(iid);
220             }
221             var el = document.getElementById(id);
222             if(el){
223                 clearInterval(iid);
224                 fn.call(scope||window, el);
225             }
226         };
227         var iid = setInterval(f, 50);
228     },
229
230     resolveTextNode: Ext.isGecko ? function(node){
231         if(!node){
232             return;
233         }
234         var s = HTMLElement.prototype.toString.call(node);
235         if(s == '[xpconnect wrapped native prototype]' || s == '[object XULElement]'){
236             return;
237         }
238         return node.nodeType == 3 ? node.parentNode : node;
239     } : function(node){
240         return node && node.nodeType == 3 ? node.parentNode : node;
241     },
242
243     getRelatedTarget: function(ev) {
244         ev = ev.browserEvent || ev;
245         var t = ev.relatedTarget;
246         if (!t) {
247             if (ev.type == "mouseout") {
248                 t = ev.toElement;
249             } else if (ev.type == "mouseover") {
250                 t = ev.fromElement;
251             }
252         }
253
254         return this.resolveTextNode(t);
255     }
256 };
257
258 Ext.lib.Ajax = function(){
259     var createComplete = function(cb){
260          return function(xhr, status){
261             if((status == 'error' || status == 'timeout') && cb.failure){
262                 cb.failure.call(cb.scope||window, createResponse(cb, xhr));
263             }else if(cb.success){
264                 cb.success.call(cb.scope||window, createResponse(cb, xhr));
265             }
266          };
267     };
268
269     var createResponse = function(cb, xhr){
270         var headerObj = {},
271             headerStr,
272             t,
273             s;
274
275         try {
276             headerStr = xhr.getAllResponseHeaders();
277             Ext.each(headerStr.replace(/\r\n/g, '\n').split('\n'), function(v){
278                 t = v.indexOf(':');
279                 if(t >= 0){
280                     s = v.substr(0, t).toLowerCase();
281                     if(v.charAt(t + 1) == ' '){
282                         ++t;
283                     }
284                     headerObj[s] = v.substr(t + 1);
285                 }
286             });
287         } catch(e) {}
288
289         return {
290             responseText: xhr.responseText,
291             responseXML : xhr.responseXML,
292             argument: cb.argument,
293             status: xhr.status,
294             statusText: xhr.statusText,
295             getResponseHeader : function(header){return headerObj[header.toLowerCase()];},
296             getAllResponseHeaders : function(){return headerStr}
297         };
298     };
299     return {
300         request : function(method, uri, cb, data, options){
301             var o = {
302                 type: method,
303                 url: uri,
304                 data: data,
305                 timeout: cb.timeout,
306                 complete: createComplete(cb)
307             };
308
309             if(options){
310                 var hs = options.headers;
311                 if(options.xmlData){
312                     o.data = options.xmlData;
313                     o.processData = false;
314                     o.type = (method ? method : (options.method ? options.method : 'POST'));
315                     if (!hs || !hs['Content-Type']){
316                         o.contentType = 'text/xml';
317                     }
318                 }else if(options.jsonData){
319                     o.data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
320                     o.processData = false;
321                     o.type = (method ? method : (options.method ? options.method : 'POST'));
322                     if (!hs || !hs['Content-Type']){
323                         o.contentType = 'application/json';
324                     }
325                 }
326                 if(hs){
327                     o.beforeSend = function(xhr){
328                         for(var h in hs){
329                             if(hs.hasOwnProperty(h)){
330                                 xhr.setRequestHeader(h, hs[h]);
331                             }
332                         }
333                     }
334                 }
335             }
336             jQuery.ajax(o);
337         },
338
339         formRequest : function(form, uri, cb, data, isUpload, sslUri){
340             jQuery.ajax({
341                 type: Ext.getDom(form).method ||'POST',
342                 url: uri,
343                 data: jQuery(form).serialize()+(data?'&'+data:''),
344                 timeout: cb.timeout,
345                 complete: createComplete(cb)
346             });
347         },
348
349         isCallInProgress : function(trans){
350             return false;
351         },
352
353         abort : function(trans){
354             return false;
355         },
356
357         serializeForm : function(form){
358             return jQuery(form.dom||form).serialize();
359         }
360     };
361 }();
362
363 Ext.lib.Anim = function(){
364     var createAnim = function(cb, scope){
365         var animated = true;
366         return {
367             stop : function(skipToLast){
368                 // do nothing
369             },
370
371             isAnimated : function(){
372                 return animated;
373             },
374
375             proxyCallback : function(){
376                 animated = false;
377                 Ext.callback(cb, scope);
378             }
379         };
380     };
381     return {
382         scroll : function(el, args, duration, easing, cb, scope){
383             // scroll anim not supported so just scroll immediately
384             var anim = createAnim(cb, scope);
385             el = Ext.getDom(el);
386             if(typeof args.scroll.to[0] == 'number'){
387                 el.scrollLeft = args.scroll.to[0];
388             }
389             if(typeof args.scroll.to[1] == 'number'){
390                 el.scrollTop = args.scroll.to[1];
391             }
392             anim.proxyCallback();
393             return anim;
394         },
395
396         motion : function(el, args, duration, easing, cb, scope){
397             return this.run(el, args, duration, easing, cb, scope);
398         },
399
400         color : function(el, args, duration, easing, cb, scope){
401             // color anim not supported, so execute callback immediately
402             var anim = createAnim(cb, scope);
403             anim.proxyCallback();
404             return anim;
405         },
406
407         run : function(el, args, duration, easing, cb, scope, type){
408             var anim = createAnim(cb, scope), e = Ext.fly(el, '_animrun');
409             var o = {};
410             for(var k in args){
411                 switch(k){   // jquery doesn't support, so convert
412                     case 'points':
413                         var by, pts;
414                         e.position();
415                         if(by = args.points.by){
416                             var xy = e.getXY();
417                             pts = e.translatePoints([xy[0]+by[0], xy[1]+by[1]]);
418                         }else{
419                             pts = e.translatePoints(args.points.to);
420                         }
421                         o.left = pts.left;
422                         o.top = pts.top;
423                         if(!parseInt(e.getStyle('left'), 10)){ // auto bug
424                             e.setLeft(0);
425                         }
426                         if(!parseInt(e.getStyle('top'), 10)){
427                             e.setTop(0);
428                         }
429                         if(args.points.from){
430                             e.setXY(args.points.from);
431                         }
432                     break;
433                     case 'width':
434                         o.width = args.width.to;
435                         if (args.width.from)
436                             e.setWidth(args.width.from);
437                     break;
438                     case 'height':
439                         o.height = args.height.to;
440                         if (args.height.from)
441                             e.setHeight(args.height.from);
442                     break;
443                     case 'opacity':
444                         o.opacity = args.opacity.to;
445                         if (args.opacity.from)
446                             e.setOpacity(args.opacity.from);
447                     break;
448                     case 'left':
449                         o.left = args.left.to;
450                         if (args.left.from)
451                             e.setLeft(args.left.from);
452                     break;
453                     case 'top':
454                         o.top = args.top.to;
455                         if (args.top.from)
456                             e.setTop(args.top.from);
457                     break;
458                         // jQuery can't handle callback, scope, and xy arguments, so break here
459                     case 'callback':
460                     case 'scope':
461                     case 'xy':
462                     break;
463
464                     default:
465                         o[k] = args[k].to;
466                         if (args[k].from)
467                             e.setStyle(k, args[k].from);
468                     break;
469                 }
470             }
471             // TODO: find out about easing plug in?
472             jQuery(el).animate(o, duration*1000, undefined, anim.proxyCallback);
473             return anim;
474         }
475     };
476 }();
477
478
479 Ext.lib.Region = function(t, r, b, l) {
480     this.top = t;
481     this[1] = t;
482     this.right = r;
483     this.bottom = b;
484     this.left = l;
485     this[0] = l;
486 };
487
488 Ext.lib.Region.prototype = {
489     contains : function(region) {
490         return ( region.left   >= this.left   &&
491                  region.right  <= this.right  &&
492                  region.top    >= this.top    &&
493                  region.bottom <= this.bottom    );
494
495     },
496
497     getArea : function() {
498         return ( (this.bottom - this.top) * (this.right - this.left) );
499     },
500
501     intersect : function(region) {
502         var t = Math.max( this.top,    region.top    );
503         var r = Math.min( this.right,  region.right  );
504         var b = Math.min( this.bottom, region.bottom );
505         var l = Math.max( this.left,   region.left   );
506
507         if (b >= t && r >= l) {
508             return new Ext.lib.Region(t, r, b, l);
509         } else {
510             return null;
511         }
512     },
513     union : function(region) {
514         var t = Math.min( this.top,    region.top    );
515         var r = Math.max( this.right,  region.right  );
516         var b = Math.max( this.bottom, region.bottom );
517         var l = Math.min( this.left,   region.left   );
518
519         return new Ext.lib.Region(t, r, b, l);
520     },
521
522     constrainTo : function(r) {
523             this.top = this.top.constrain(r.top, r.bottom);
524             this.bottom = this.bottom.constrain(r.top, r.bottom);
525             this.left = this.left.constrain(r.left, r.right);
526             this.right = this.right.constrain(r.left, r.right);
527             return this;
528     },
529
530     adjust : function(t, l, b, r){
531         this.top += t;
532         this.left += l;
533         this.right += r;
534         this.bottom += b;
535         return this;
536     }
537 };
538
539 Ext.lib.Region.getRegion = function(el) {
540     var p = Ext.lib.Dom.getXY(el);
541
542     var t = p[1];
543     var r = p[0] + el.offsetWidth;
544     var b = p[1] + el.offsetHeight;
545     var l = p[0];
546
547     return new Ext.lib.Region(t, r, b, l);
548 };
549
550 Ext.lib.Point = function(x, y) {
551    if (Ext.isArray(x)) {
552       y = x[1];
553       x = x[0];
554    }
555     this.x = this.right = this.left = this[0] = x;
556     this.y = this.top = this.bottom = this[1] = y;
557 };
558
559 Ext.lib.Point.prototype = new Ext.lib.Region();
560
561 // prevent IE leaks
562 if(Ext.isIE) {
563     function fnCleanUp() {
564         var p = Function.prototype;
565         delete p.createSequence;
566         delete p.defer;
567         delete p.createDelegate;
568         delete p.createCallback;
569         delete p.createInterceptor;
570
571         window.detachEvent("onunload", fnCleanUp);
572     }
573     window.attachEvent("onunload", fnCleanUp);
574 }
575 })();