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