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