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