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