Upgrade to ExtJS 3.3.0 - Released 10/06/2010
[extjs.git] / src / adapter / jquery-bridge.js
1 /*!
2  * Ext JS Library 3.3.0
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){
296                 return headerObj[header.toLowerCase()];
297             },
298             getAllResponseHeaders : function(){
299                 return headerStr;
300             }
301         };
302     };
303     return {
304         request : function(method, uri, cb, data, options){
305             var o = {
306                 type: method,
307                 url: uri,
308                 data: data,
309                 timeout: cb.timeout,
310                 complete: createComplete(cb)
311             };
312
313             if(options){
314                 var hs = options.headers;
315                 if(options.xmlData){
316                     o.data = options.xmlData;
317                     o.processData = false;
318                     o.type = (method ? method : (options.method ? options.method : 'POST'));
319                     if (!hs || !hs['Content-Type']){
320                         o.contentType = 'text/xml';
321                     }
322                 }else if(options.jsonData){
323                     o.data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
324                     o.processData = false;
325                     o.type = (method ? method : (options.method ? options.method : 'POST'));
326                     if (!hs || !hs['Content-Type']){
327                         o.contentType = 'application/json';
328                     }
329                 }
330                 if(hs){
331                     o.beforeSend = function(xhr){
332                         for (var h in hs) {
333                             if (hs.hasOwnProperty(h)) {
334                                 xhr.setRequestHeader(h, hs[h]);
335                             }
336                         }
337                     };
338                 }
339             }
340             jQuery.ajax(o);
341         },
342
343         formRequest : function(form, uri, cb, data, isUpload, sslUri){
344             jQuery.ajax({
345                 type: Ext.getDom(form).method ||'POST',
346                 url: uri,
347                 data: jQuery(form).serialize()+(data?'&'+data:''),
348                 timeout: cb.timeout,
349                 complete: createComplete(cb)
350             });
351         },
352
353         isCallInProgress : function(trans){
354             return false;
355         },
356
357         abort : function(trans){
358             return false;
359         },
360
361         serializeForm : function(form){
362             return jQuery(form.dom||form).serialize();
363         }
364     };
365 }();
366
367 Ext.lib.Anim = function(){
368     var createAnim = function(cb, scope){
369         var animated = true;
370         return {
371             stop : function(skipToLast){
372                 // do nothing
373             },
374
375             isAnimated : function(){
376                 return animated;
377             },
378
379             proxyCallback : function(){
380                 animated = false;
381                 Ext.callback(cb, scope);
382             }
383         };
384     };
385     return {
386         scroll : function(el, args, duration, easing, cb, scope){
387             // scroll anim not supported so just scroll immediately
388             var anim = createAnim(cb, scope);
389             el = Ext.getDom(el);
390             if(typeof args.scroll.to[0] == 'number'){
391                 el.scrollLeft = args.scroll.to[0];
392             }
393             if(typeof args.scroll.to[1] == 'number'){
394                 el.scrollTop = args.scroll.to[1];
395             }
396             anim.proxyCallback();
397             return anim;
398         },
399
400         motion : function(el, args, duration, easing, cb, scope){
401             return this.run(el, args, duration, easing, cb, scope);
402         },
403
404         color : function(el, args, duration, easing, cb, scope){
405             // color anim not supported, so execute callback immediately
406             var anim = createAnim(cb, scope);
407             anim.proxyCallback();
408             return anim;
409         },
410
411         run : function(el, args, duration, easing, cb, scope, type){
412             var anim = createAnim(cb, scope), e = Ext.fly(el, '_animrun');
413             var o = {};
414             for(var k in args){
415                 switch(k){   // jquery doesn't support, so convert
416                     case 'points':
417                         var by, pts;
418                         e.position();
419                         if(by = args.points.by){
420                             var xy = e.getXY();
421                             pts = e.translatePoints([xy[0]+by[0], xy[1]+by[1]]);
422                         }else{
423                             pts = e.translatePoints(args.points.to);
424                         }
425                         o.left = pts.left;
426                         o.top = pts.top;
427                         if(!parseInt(e.getStyle('left'), 10)){ // auto bug
428                             e.setLeft(0);
429                         }
430                         if(!parseInt(e.getStyle('top'), 10)){
431                             e.setTop(0);
432                         }
433                         if(args.points.from){
434                             e.setXY(args.points.from);
435                         }
436                     break;
437                     case 'width':
438                         o.width = args.width.to;
439                         if (args.width.from)
440                             e.setWidth(args.width.from);
441                     break;
442                     case 'height':
443                         o.height = args.height.to;
444                         if (args.height.from)
445                             e.setHeight(args.height.from);
446                     break;
447                     case 'opacity':
448                         o.opacity = args.opacity.to;
449                         if (args.opacity.from)
450                             e.setOpacity(args.opacity.from);
451                     break;
452                     case 'left':
453                         o.left = args.left.to;
454                         if (args.left.from)
455                             e.setLeft(args.left.from);
456                     break;
457                     case 'top':
458                         o.top = args.top.to;
459                         if (args.top.from)
460                             e.setTop(args.top.from);
461                     break;
462                         // jQuery can't handle callback, scope, and xy arguments, so break here
463                     case 'callback':
464                     case 'scope':
465                     case 'xy':
466                     break;
467
468                     default:
469                         o[k] = args[k].to;
470                         if (args[k].from)
471                             e.setStyle(k, args[k].from);
472                     break;
473                 }
474             }
475             // TODO: find out about easing plug in?
476             jQuery(el).animate(o, duration*1000, undefined, anim.proxyCallback);
477             return anim;
478         }
479     };
480 }();
481
482
483 Ext.lib.Region = function(t, r, b, l) {
484     this.top = t;
485     this[1] = t;
486     this.right = r;
487     this.bottom = b;
488     this.left = l;
489     this[0] = l;
490 };
491
492 Ext.lib.Region.prototype = {
493     contains : function(region) {
494         return ( region.left   >= this.left   &&
495                  region.right  <= this.right  &&
496                  region.top    >= this.top    &&
497                  region.bottom <= this.bottom    );
498
499     },
500
501     getArea : function() {
502         return ( (this.bottom - this.top) * (this.right - this.left) );
503     },
504
505     intersect : function(region) {
506         var t = Math.max( this.top,    region.top    );
507         var r = Math.min( this.right,  region.right  );
508         var b = Math.min( this.bottom, region.bottom );
509         var l = Math.max( this.left,   region.left   );
510
511         if (b >= t && r >= l) {
512             return new Ext.lib.Region(t, r, b, l);
513         } else {
514             return null;
515         }
516     },
517     union : function(region) {
518         var t = Math.min( this.top,    region.top    );
519         var r = Math.max( this.right,  region.right  );
520         var b = Math.max( this.bottom, region.bottom );
521         var l = Math.min( this.left,   region.left   );
522
523         return new Ext.lib.Region(t, r, b, l);
524     },
525
526     constrainTo : function(r) {
527             this.top = this.top.constrain(r.top, r.bottom);
528             this.bottom = this.bottom.constrain(r.top, r.bottom);
529             this.left = this.left.constrain(r.left, r.right);
530             this.right = this.right.constrain(r.left, r.right);
531             return this;
532     },
533
534     adjust : function(t, l, b, r){
535         this.top += t;
536         this.left += l;
537         this.right += r;
538         this.bottom += b;
539         return this;
540     }
541 };
542
543 Ext.lib.Region.getRegion = function(el) {
544     var p = Ext.lib.Dom.getXY(el);
545
546     var t = p[1];
547     var r = p[0] + el.offsetWidth;
548     var b = p[1] + el.offsetHeight;
549     var l = p[0];
550
551     return new Ext.lib.Region(t, r, b, l);
552 };
553
554 Ext.lib.Point = function(x, y) {
555    if (Ext.isArray(x)) {
556       y = x[1];
557       x = x[0];
558    }
559     this.x = this.right = this.left = this[0] = x;
560     this.y = this.top = this.bottom = this[1] = y;
561 };
562
563 Ext.lib.Point.prototype = new Ext.lib.Region();
564
565 // prevent IE leaks
566 if(Ext.isIE) {
567     function fnCleanUp() {
568         var p = Function.prototype;
569         delete p.createSequence;
570         delete p.defer;
571         delete p.createDelegate;
572         delete p.createCallback;
573         delete p.createInterceptor;
574
575         window.detachEvent("onunload", fnCleanUp);
576     }
577     window.attachEvent("onunload", fnCleanUp);
578 }
579 })();