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