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