Upgrade to ExtJS 3.3.0 - Released 10/06/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.3.0
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], 10) >= 2) || (parseInt(version[1], 10) >= 7) || (parseInt(version[2], 10) >= 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){
353                 return headerObj[header.toLowerCase()];
354             },
355             getAllResponseHeaders : function(){
356                 return headerStr;
357             }
358         };
359     };
360     return {
361         request : function(method, uri, cb, data, options){
362             var o = {
363                 method: method,
364                 parameters: data || '',
365                 timeout: cb.timeout,
366                 onSuccess: createSuccess(cb),
367                 onFailure: createFailure(cb)
368             };
369             if(options){
370                 var hs = options.headers;
371                 if(hs){
372                     o.requestHeaders = hs;
373                 }
374                 if(options.xmlData){
375                     method = (method ? method : (options.method ? options.method : 'POST'));
376                     if (!hs || !hs['Content-Type']){
377                         o.contentType = 'text/xml';
378                     }
379                     o.postBody = options.xmlData;
380                     delete o.parameters;
381                 }
382                 if(options.jsonData){
383                     method = (method ? method : (options.method ? options.method : 'POST'));
384                     if (!hs || !hs['Content-Type']){
385                         o.contentType = 'application/json';
386                     }
387                     o.postBody = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
388                     delete o.parameters;
389                 }
390             }
391             new Ajax.Request(uri, o);
392         },
393
394         formRequest : function(form, uri, cb, data, isUpload, sslUri){
395             new Ajax.Request(uri, {
396                 method: Ext.getDom(form).method ||'POST',
397                 parameters: Form.serialize(form)+(data?'&'+data:''),
398                 timeout: cb.timeout,
399                 onSuccess: createSuccess(cb),
400                 onFailure: createFailure(cb)
401             });
402         },
403
404         isCallInProgress : function(trans){
405             return false;
406         },
407
408         abort : function(trans){
409             return false;
410         },
411
412         serializeForm : function(form){
413             return Form.serialize(form.dom||form);
414         }
415     };
416 }();
417
418
419 Ext.lib.Anim = function(){
420
421     var easings = {
422         easeOut: function(pos) {
423             return 1-Math.pow(1-pos,2);
424         },
425         easeIn: function(pos) {
426             return 1-Math.pow(1-pos,2);
427         }
428     };
429     var createAnim = function(cb, scope){
430         return {
431             stop : function(skipToLast){
432                 this.effect.cancel();
433             },
434
435             isAnimated : function(){
436                 return this.effect.state == 'running';
437             },
438
439             proxyCallback : function(){
440                 Ext.callback(cb, scope);
441             }
442         };
443     };
444     return {
445         scroll : function(el, args, duration, easing, cb, scope){
446             // not supported so scroll immediately?
447             var anim = createAnim(cb, scope);
448             el = Ext.getDom(el);
449             if(typeof args.scroll.to[0] == 'number'){
450                 el.scrollLeft = args.scroll.to[0];
451             }
452             if(typeof args.scroll.to[1] == 'number'){
453                 el.scrollTop = args.scroll.to[1];
454             }
455             anim.proxyCallback();
456             return anim;
457         },
458
459         motion : function(el, args, duration, easing, cb, scope){
460             return this.run(el, args, duration, easing, cb, scope);
461         },
462
463         color : function(el, args, duration, easing, cb, scope){
464             return this.run(el, args, duration, easing, cb, scope);
465         },
466
467         run : function(el, args, duration, easing, cb, scope, type){
468             var o = {};
469             for(var k in args){
470                 switch(k){   // scriptaculous doesn't support, so convert these
471                     case 'points':
472                         var by, pts, e = Ext.fly(el, '_animrun');
473                         e.position();
474                         if(by = args.points.by){
475                             var xy = e.getXY();
476                             pts = e.translatePoints([xy[0]+by[0], xy[1]+by[1]]);
477                         }else{
478                             pts = e.translatePoints(args.points.to);
479                         }
480                         o.left = pts.left+'px';
481                         o.top = pts.top+'px';
482                     break;
483                     case 'width':
484                         o.width = args.width.to+'px';
485                     break;
486                     case 'height':
487                         o.height = args.height.to+'px';
488                     break;
489                     case 'opacity':
490                         o.opacity = String(args.opacity.to);
491                     break;
492                     default:
493                         o[k] = String(args[k].to);
494                     break;
495                 }
496             }
497             var anim = createAnim(cb, scope);
498             anim.effect = new Effect.Morph(Ext.id(el), {
499                 duration: duration,
500                 afterFinish: anim.proxyCallback,
501                 transition: easings[easing] || Effect.Transitions.linear,
502                 style: o
503             });
504             return anim;
505         }
506     };
507 }();
508
509
510 // all lib flyweight calls use their own flyweight to prevent collisions with developer flyweights
511 function fly(el){
512     if(!libFlyweight){
513         libFlyweight = new Ext.Element.Flyweight();
514     }
515     libFlyweight.dom = el;
516     return libFlyweight;
517 }
518
519 Ext.lib.Region = function(t, r, b, l) {
520     this.top = t;
521     this[1] = t;
522     this.right = r;
523     this.bottom = b;
524     this.left = l;
525     this[0] = l;
526 };
527
528 Ext.lib.Region.prototype = {
529     contains : function(region) {
530         return ( region.left   >= this.left   &&
531                  region.right  <= this.right  &&
532                  region.top    >= this.top    &&
533                  region.bottom <= this.bottom    );
534
535     },
536
537     getArea : function() {
538         return ( (this.bottom - this.top) * (this.right - this.left) );
539     },
540
541     intersect : function(region) {
542         var t = Math.max( this.top,    region.top    );
543         var r = Math.min( this.right,  region.right  );
544         var b = Math.min( this.bottom, region.bottom );
545         var l = Math.max( this.left,   region.left   );
546
547         if (b >= t && r >= l) {
548             return new Ext.lib.Region(t, r, b, l);
549         } else {
550             return null;
551         }
552     },
553     union : function(region) {
554         var t = Math.min( this.top,    region.top    );
555         var r = Math.max( this.right,  region.right  );
556         var b = Math.max( this.bottom, region.bottom );
557         var l = Math.min( this.left,   region.left   );
558
559         return new Ext.lib.Region(t, r, b, l);
560     },
561
562     constrainTo : function(r) {
563             this.top = this.top.constrain(r.top, r.bottom);
564             this.bottom = this.bottom.constrain(r.top, r.bottom);
565             this.left = this.left.constrain(r.left, r.right);
566             this.right = this.right.constrain(r.left, r.right);
567             return this;
568     },
569
570     adjust : function(t, l, b, r){
571         this.top += t;
572         this.left += l;
573         this.right += r;
574         this.bottom += b;
575         return this;
576     }
577 };
578
579 Ext.lib.Region.getRegion = function(el) {
580     var p = Ext.lib.Dom.getXY(el);
581
582     var t = p[1];
583     var r = p[0] + el.offsetWidth;
584     var b = p[1] + el.offsetHeight;
585     var l = p[0];
586
587     return new Ext.lib.Region(t, r, b, l);
588 };
589
590 Ext.lib.Point = function(x, y) {
591    if (Ext.isArray(x)) {
592       y = x[1];
593       x = x[0];
594    }
595     this.x = this.right = this.left = this[0] = x;
596     this.y = this.top = this.bottom = this[1] = y;
597 };
598
599 Ext.lib.Point.prototype = new Ext.lib.Region();
600
601
602 // prevent IE leaks
603 if(Ext.isIE) {
604     function fnCleanUp() {
605         var p = Function.prototype;
606         delete p.createSequence;
607         delete p.defer;
608         delete p.createDelegate;
609         delete p.createCallback;
610         delete p.createInterceptor;
611
612         window.detachEvent("onunload", fnCleanUp);
613     }
614     window.attachEvent("onunload", fnCleanUp);
615 }
616 })();</pre>    
617 </body>
618 </html>