Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / docs / source / prototype-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 (function(){
16
17 var libFlyweight,
18     version = Prototype.Version.split('.'),
19     mouseEnterSupported = (parseInt(version[0]) >= 2) || (parseInt(version[1]) >= 7) || (parseInt(version[2]) >= 1),
20     mouseCache = {},
21     elContains = function(parent, child) {
22        if(parent && parent.firstChild){
23          while(child) {
24             if(child === parent) {
25                 return true;
26             }
27             child = child.parentNode;
28             if(child && (child.nodeType != 1)) {
29                 child = null;
30             }
31           }
32         }
33         return false;
34     },
35     checkRelatedTarget = function(e) {
36         return !elContains(e.currentTarget, Ext.lib.Event.getRelatedTarget(e));
37     };
38
39 Ext.lib.Dom = {
40     getViewWidth : function(full){
41         return full ? this.getDocumentWidth() : this.getViewportWidth();
42     },
43
44     getViewHeight : function(full){
45         return full ? this.getDocumentHeight() : this.getViewportHeight();
46     },
47
48     getDocumentHeight: function() { // missing from prototype?
49         var scrollHeight = (document.compatMode != "CSS1Compat") ? document.body.scrollHeight : document.documentElement.scrollHeight;
50         return Math.max(scrollHeight, this.getViewportHeight());
51     },
52
53     getDocumentWidth: function() { // missing from prototype?
54         var scrollWidth = (document.compatMode != "CSS1Compat") ? document.body.scrollWidth : document.documentElement.scrollWidth;
55         return Math.max(scrollWidth, this.getViewportWidth());
56     },
57
58     getViewportHeight: function() { // missing from prototype?
59         var height = self.innerHeight;
60         var mode = document.compatMode;
61
62         if ( (mode || Ext.isIE) && !Ext.isOpera ) {
63             height = (mode == "CSS1Compat") ?
64                     document.documentElement.clientHeight : // Standards
65                     document.body.clientHeight; // Quirks
66         }
67
68         return height;
69     },
70
71     getViewportWidth: function() { // missing from prototype?
72         var width = self.innerWidth;  // Safari
73         var mode = document.compatMode;
74
75         if (mode || Ext.isIE) { // IE, Gecko, Opera
76             width = (mode == "CSS1Compat") ?
77                     document.documentElement.clientWidth : // Standards
78                     document.body.clientWidth; // Quirks
79         }
80         return width;
81     },
82
83     isAncestor : function(p, c){ // missing from prototype?
84         var ret = false;
85
86         p = Ext.getDom(p);
87         c = Ext.getDom(c);
88         if (p && c) {
89             if (p.contains) {
90                 return p.contains(c);
91             } else if (p.compareDocumentPosition) {
92                 return !!(p.compareDocumentPosition(c) & 16);
93             } else {
94                 while (c = c.parentNode) {
95                     ret = c == p || ret;
96                 }
97             }
98         }
99         return ret;
100     },
101
102     getRegion : function(el){
103         return Ext.lib.Region.getRegion(el);
104     },
105
106     getY : function(el){
107         return this.getXY(el)[1];
108     },
109
110     getX : function(el){
111         return this.getXY(el)[0];
112     },
113
114     getXY : function(el){ // this initially used Position.cumulativeOffset but it is not accurate enough
115         var p, pe, b, scroll, bd = (document.body || document.documentElement);
116         el = Ext.getDom(el);
117
118         if(el == bd){
119             return [0, 0];
120         }
121
122         if (el.getBoundingClientRect) {
123             b = el.getBoundingClientRect();
124             scroll = fly(document).getScroll();
125             return [Math.round(b.left + scroll.left), Math.round(b.top + scroll.top)];
126         }
127         var x = 0, y = 0;
128
129         p = el;
130
131         var hasAbsolute = fly(el).getStyle("position") == "absolute";
132
133         while (p) {
134
135             x += p.offsetLeft;
136             y += p.offsetTop;
137
138             if (!hasAbsolute && fly(p).getStyle("position") == "absolute") {
139                 hasAbsolute = true;
140             }
141
142             if (Ext.isGecko) {
143                 pe = fly(p);
144
145                 var bt = parseInt(pe.getStyle("borderTopWidth"), 10) || 0;
146                 var bl = parseInt(pe.getStyle("borderLeftWidth"), 10) || 0;
147
148
149                 x += bl;
150                 y += bt;
151
152
153                 if (p != el && pe.getStyle('overflow') != 'visible') {
154                     x += bl;
155                     y += bt;
156                 }
157             }
158             p = p.offsetParent;
159         }
160
161         if (Ext.isSafari && hasAbsolute) {
162             x -= bd.offsetLeft;
163             y -= bd.offsetTop;
164         }
165
166         if (Ext.isGecko && !hasAbsolute) {
167             var dbd = fly(bd);
168             x += parseInt(dbd.getStyle("borderLeftWidth"), 10) || 0;
169             y += parseInt(dbd.getStyle("borderTopWidth"), 10) || 0;
170         }
171
172         p = el.parentNode;
173         while (p && p != bd) {
174             if (!Ext.isOpera || (p.tagName != 'TR' && fly(p).getStyle("display") != "inline")) {
175                 x -= p.scrollLeft;
176                 y -= p.scrollTop;
177             }
178             p = p.parentNode;
179         }
180         return [x, y];
181     },
182
183     setXY : function(el, xy){ // this initially used Position.cumulativeOffset but it is not accurate enough
184         el = Ext.fly(el, '_setXY');
185         el.position();
186         var pts = el.translatePoints(xy);
187         if(xy[0] !== false){
188             el.dom.style.left = pts.left + "px";
189         }
190         if(xy[1] !== false){
191             el.dom.style.top = pts.top + "px";
192         }
193     },
194
195     setX : function(el, x){
196         this.setXY(el, [x, false]);
197     },
198
199     setY : function(el, y){
200         this.setXY(el, [false, y]);
201     }
202 };
203
204 Ext.lib.Event = {
205     getPageX : function(e){
206         return Event.pointerX(e.browserEvent || e);
207     },
208
209     getPageY : function(e){
210         return Event.pointerY(e.browserEvent || e);
211     },
212
213     getXY : function(e){
214         e = e.browserEvent || e;
215         return [Event.pointerX(e), Event.pointerY(e)];
216     },
217
218     getTarget : function(e){
219         return Event.element(e.browserEvent || e);
220     },
221
222     resolveTextNode: Ext.isGecko ? function(node){
223         if(!node){
224             return;
225         }
226         var s = HTMLElement.prototype.toString.call(node);
227         if(s == '[xpconnect wrapped native prototype]' || s == '[object XULElement]'){
228             return;
229         }
230         return node.nodeType == 3 ? node.parentNode : node;
231     } : function(node){
232         return node && node.nodeType == 3 ? node.parentNode : node;
233     },
234
235     getRelatedTarget: function(ev) { // missing from prototype?
236         ev = ev.browserEvent || ev;
237         var t = ev.relatedTarget;
238         if (!t) {
239             if (ev.type == "mouseout") {
240                 t = ev.toElement;
241             } else if (ev.type == "mouseover") {
242                 t = ev.fromElement;
243             }
244         }
245
246         return this.resolveTextNode(t);
247     },
248
249     on : function(el, eventName, fn){
250         if((eventName == 'mouseenter' || eventName == 'mouseleave') && !mouseEnterSupported){
251             var item = mouseCache[el.id] || (mouseCache[el.id] = {});
252             item[eventName] = fn;
253             fn = fn.createInterceptor(checkRelatedTarget);
254             eventName = (eventName == 'mouseenter') ? 'mouseover' : 'mouseout';
255         }
256         Event.observe(el, eventName, fn, false);
257     },
258
259     un : function(el, eventName, fn){
260         if((eventName == 'mouseenter' || eventName == 'mouseleave') && !mouseEnterSupported){
261             var item = mouseCache[el.id],
262                 ev = item && item[eventName];
263
264             if(ev){
265                 fn = ev.fn;
266                 delete item[eventName];
267                 eventName = (eventName == 'mouseenter') ? 'mouseover' : 'mouseout';
268             }
269         }
270         Event.stopObserving(el, eventName, fn, false);
271     },
272
273     purgeElement : function(el){
274         // no equiv?
275     },
276
277     preventDefault : function(e){   // missing from prototype?
278         e = e.browserEvent || e;
279         if(e.preventDefault) {
280             e.preventDefault();
281         } else {
282             e.returnValue = false;
283         }
284     },
285
286     stopPropagation : function(e){   // missing from prototype?
287         e = e.browserEvent || e;
288         if(e.stopPropagation) {
289             e.stopPropagation();
290         } else {
291             e.cancelBubble = true;
292         }
293     },
294
295     stopEvent : function(e){
296         Event.stop(e.browserEvent || e);
297     },
298
299     onAvailable : function(id, fn, scope){  // no equiv
300         var start = new Date(), iid;
301         var f = function(){
302             if(start.getElapsed() > 10000){
303                 clearInterval(iid);
304             }
305             var el = document.getElementById(id);
306             if(el){
307                 clearInterval(iid);
308                 fn.call(scope||window, el);
309             }
310         };
311         iid = setInterval(f, 50);
312     }
313 };
314
315 Ext.lib.Ajax = function(){
316     var createSuccess = function(cb){
317          return cb.success ? function(xhr){
318             cb.success.call(cb.scope||window, createResponse(cb, xhr));
319          } : Ext.emptyFn;
320     };
321     var createFailure = function(cb){
322          return cb.failure ? function(xhr){
323             cb.failure.call(cb.scope||window, createResponse(cb, xhr));
324          } : Ext.emptyFn;
325     };
326     var createResponse = function(cb, xhr){
327         var headerObj = {},
328             headerStr,
329             t,
330             s;
331
332         try {
333             headerStr = xhr.getAllResponseHeaders();
334             Ext.each(headerStr.replace(/\r\n/g, '\n').split('\n'), function(v){
335                 t = v.indexOf(':');
336                 if(t >= 0){
337                     s = v.substr(0, t).toLowerCase();
338                     if(v.charAt(t + 1) == ' '){
339                         ++t;
340                     }
341                     headerObj[s] = v.substr(t + 1);
342                 }
343             });
344         } catch(e) {}
345
346         return {
347             responseText: xhr.responseText,
348             responseXML : xhr.responseXML,
349             argument: cb.argument,
350             status: xhr.status,
351             statusText: xhr.statusText,
352             getResponseHeader : function(header){return headerObj[header.toLowerCase()];},
353             getAllResponseHeaders : function(){return headerStr}
354         };
355     };
356     return {
357         request : function(method, uri, cb, data, options){
358             var o = {
359                 method: method,
360                 parameters: data || '',
361                 timeout: cb.timeout,
362                 onSuccess: createSuccess(cb),
363                 onFailure: createFailure(cb)
364             };
365             if(options){
366                 var hs = options.headers;
367                 if(hs){
368                     o.requestHeaders = hs;
369                 }
370                 if(options.xmlData){
371                     method = (method ? method : (options.method ? options.method : 'POST'));
372                     if (!hs || !hs['Content-Type']){
373                         o.contentType = 'text/xml';
374                     }
375                     o.postBody = options.xmlData;
376                     delete o.parameters;
377                 }
378                 if(options.jsonData){
379                     method = (method ? method : (options.method ? options.method : 'POST'));
380                     if (!hs || !hs['Content-Type']){
381                         o.contentType = 'application/json';
382                     }
383                     o.postBody = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
384                     delete o.parameters;
385                 }
386             }
387             new Ajax.Request(uri, o);
388         },
389
390         formRequest : function(form, uri, cb, data, isUpload, sslUri){
391             new Ajax.Request(uri, {
392                 method: Ext.getDom(form).method ||'POST',
393                 parameters: Form.serialize(form)+(data?'&'+data:''),
394                 timeout: cb.timeout,
395                 onSuccess: createSuccess(cb),
396                 onFailure: createFailure(cb)
397             });
398         },
399
400         isCallInProgress : function(trans){
401             return false;
402         },
403
404         abort : function(trans){
405             return false;
406         },
407
408         serializeForm : function(form){
409             return Form.serialize(form.dom||form);
410         }
411     };
412 }();
413
414
415 Ext.lib.Anim = function(){
416
417     var easings = {
418         easeOut: function(pos) {
419             return 1-Math.pow(1-pos,2);
420         },
421         easeIn: function(pos) {
422             return 1-Math.pow(1-pos,2);
423         }
424     };
425     var createAnim = function(cb, scope){
426         return {
427             stop : function(skipToLast){
428                 this.effect.cancel();
429             },
430
431             isAnimated : function(){
432                 return this.effect.state == 'running';
433             },
434
435             proxyCallback : function(){
436                 Ext.callback(cb, scope);
437             }
438         };
439     };
440     return {
441         scroll : function(el, args, duration, easing, cb, scope){
442             // not supported so scroll immediately?
443             var anim = createAnim(cb, scope);
444             el = Ext.getDom(el);
445             if(typeof args.scroll.to[0] == 'number'){
446                 el.scrollLeft = args.scroll.to[0];
447             }
448             if(typeof args.scroll.to[1] == 'number'){
449                 el.scrollTop = args.scroll.to[1];
450             }
451             anim.proxyCallback();
452             return anim;
453         },
454
455         motion : function(el, args, duration, easing, cb, scope){
456             return this.run(el, args, duration, easing, cb, scope);
457         },
458
459         color : function(el, args, duration, easing, cb, scope){
460             return this.run(el, args, duration, easing, cb, scope);
461         },
462
463         run : function(el, args, duration, easing, cb, scope, type){
464             var o = {};
465             for(var k in args){
466                 switch(k){   // scriptaculous doesn't support, so convert these
467                     case 'points':
468                         var by, pts, e = Ext.fly(el, '_animrun');
469                         e.position();
470                         if(by = args.points.by){
471                             var xy = e.getXY();
472                             pts = e.translatePoints([xy[0]+by[0], xy[1]+by[1]]);
473                         }else{
474                             pts = e.translatePoints(args.points.to);
475                         }
476                         o.left = pts.left+'px';
477                         o.top = pts.top+'px';
478                     break;
479                     case 'width':
480                         o.width = args.width.to+'px';
481                     break;
482                     case 'height':
483                         o.height = args.height.to+'px';
484                     break;
485                     case 'opacity':
486                         o.opacity = String(args.opacity.to);
487                     break;
488                     default:
489                         o[k] = String(args[k].to);
490                     break;
491                 }
492             }
493             var anim = createAnim(cb, scope);
494             anim.effect = new Effect.Morph(Ext.id(el), {
495                 duration: duration,
496                 afterFinish: anim.proxyCallback,
497                 transition: easings[easing] || Effect.Transitions.linear,
498                 style: o
499             });
500             return anim;
501         }
502     };
503 }();
504
505
506 // all lib flyweight calls use their own flyweight to prevent collisions with developer flyweights
507 function fly(el){
508     if(!libFlyweight){
509         libFlyweight = new Ext.Element.Flyweight();
510     }
511     libFlyweight.dom = el;
512     return libFlyweight;
513 }
514
515 Ext.lib.Region = function(t, r, b, l) {
516     this.top = t;
517     this[1] = t;
518     this.right = r;
519     this.bottom = b;
520     this.left = l;
521     this[0] = l;
522 };
523
524 Ext.lib.Region.prototype = {
525     contains : function(region) {
526         return ( region.left   >= this.left   &&
527                  region.right  <= this.right  &&
528                  region.top    >= this.top    &&
529                  region.bottom <= this.bottom    );
530
531     },
532
533     getArea : function() {
534         return ( (this.bottom - this.top) * (this.right - this.left) );
535     },
536
537     intersect : function(region) {
538         var t = Math.max( this.top,    region.top    );
539         var r = Math.min( this.right,  region.right  );
540         var b = Math.min( this.bottom, region.bottom );
541         var l = Math.max( this.left,   region.left   );
542
543         if (b >= t && r >= l) {
544             return new Ext.lib.Region(t, r, b, l);
545         } else {
546             return null;
547         }
548     },
549     union : function(region) {
550         var t = Math.min( this.top,    region.top    );
551         var r = Math.max( this.right,  region.right  );
552         var b = Math.max( this.bottom, region.bottom );
553         var l = Math.min( this.left,   region.left   );
554
555         return new Ext.lib.Region(t, r, b, l);
556     },
557
558     constrainTo : function(r) {
559             this.top = this.top.constrain(r.top, r.bottom);
560             this.bottom = this.bottom.constrain(r.top, r.bottom);
561             this.left = this.left.constrain(r.left, r.right);
562             this.right = this.right.constrain(r.left, r.right);
563             return this;
564     },
565
566     adjust : function(t, l, b, r){
567         this.top += t;
568         this.left += l;
569         this.right += r;
570         this.bottom += b;
571         return this;
572     }
573 };
574
575 Ext.lib.Region.getRegion = function(el) {
576     var p = Ext.lib.Dom.getXY(el);
577
578     var t = p[1];
579     var r = p[0] + el.offsetWidth;
580     var b = p[1] + el.offsetHeight;
581     var l = p[0];
582
583     return new Ext.lib.Region(t, r, b, l);
584 };
585
586 Ext.lib.Point = function(x, y) {
587    if (Ext.isArray(x)) {
588       y = x[1];
589       x = x[0];
590    }
591     this.x = this.right = this.left = this[0] = x;
592     this.y = this.top = this.bottom = this[1] = y;
593 };
594
595 Ext.lib.Point.prototype = new Ext.lib.Region();
596
597
598 // prevent IE leaks
599 if(Ext.isIE) {
600     function fnCleanUp() {
601         var p = Function.prototype;
602         delete p.createSequence;
603         delete p.defer;
604         delete p.createDelegate;
605         delete p.createCallback;
606         delete p.createInterceptor;
607
608         window.detachEvent("onunload", fnCleanUp);
609     }
610     window.attachEvent("onunload", fnCleanUp);
611 }
612 })();</pre>    
613 </body>
614 </html>