3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
10 version = Prototype.Version.split('.'),
11 mouseEnterSupported = (parseInt(version[0], 10) >= 2) || (parseInt(version[1], 10) >= 7) || (parseInt(version[2], 10) >= 1),
13 elContains = function(parent, child) {
14 if(parent && parent.firstChild){
16 if(child === parent) {
19 child = child.parentNode;
20 if(child && (child.nodeType != 1)) {
27 checkRelatedTarget = function(e) {
28 return !elContains(e.currentTarget, Ext.lib.Event.getRelatedTarget(e));
32 getViewWidth : function(full){
33 return full ? this.getDocumentWidth() : this.getViewportWidth();
36 getViewHeight : function(full){
37 return full ? this.getDocumentHeight() : this.getViewportHeight();
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());
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());
50 getViewportHeight: function() { // missing from prototype?
51 var height = self.innerHeight;
52 var mode = document.compatMode;
54 if ( (mode || Ext.isIE) && !Ext.isOpera ) {
55 height = (mode == "CSS1Compat") ?
56 document.documentElement.clientHeight : // Standards
57 document.body.clientHeight; // Quirks
63 getViewportWidth: function() { // missing from prototype?
64 var width = self.innerWidth; // Safari
65 var mode = document.compatMode;
67 if (mode || Ext.isIE) { // IE, Gecko, Opera
68 width = (mode == "CSS1Compat") ?
69 document.documentElement.clientWidth : // Standards
70 document.body.clientWidth; // Quirks
75 isAncestor : function(p, c){ // missing from prototype?
83 } else if (p.compareDocumentPosition) {
84 return !!(p.compareDocumentPosition(c) & 16);
86 while (c = c.parentNode) {
94 getRegion : function(el){
95 return Ext.lib.Region.getRegion(el);
99 return this.getXY(el)[1];
103 return this.getXY(el)[0];
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);
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)];
123 var hasAbsolute = fly(el).getStyle("position") == "absolute";
130 if (!hasAbsolute && fly(p).getStyle("position") == "absolute") {
137 var bt = parseInt(pe.getStyle("borderTopWidth"), 10) || 0;
138 var bl = parseInt(pe.getStyle("borderLeftWidth"), 10) || 0;
145 if (p != el && pe.getStyle('overflow') != 'visible') {
153 if (Ext.isSafari && hasAbsolute) {
158 if (Ext.isGecko && !hasAbsolute) {
160 x += parseInt(dbd.getStyle("borderLeftWidth"), 10) || 0;
161 y += parseInt(dbd.getStyle("borderTopWidth"), 10) || 0;
165 while (p && p != bd) {
166 if (!Ext.isOpera || (p.tagName != 'TR' && fly(p).getStyle("display") != "inline")) {
175 setXY : function(el, xy){ // this initially used Position.cumulativeOffset but it is not accurate enough
176 el = Ext.fly(el, '_setXY');
178 var pts = el.translatePoints(xy);
180 el.dom.style.left = pts.left + "px";
183 el.dom.style.top = pts.top + "px";
187 setX : function(el, x){
188 this.setXY(el, [x, false]);
191 setY : function(el, y){
192 this.setXY(el, [false, y]);
197 getPageX : function(e){
198 return Event.pointerX(e.browserEvent || e);
201 getPageY : function(e){
202 return Event.pointerY(e.browserEvent || e);
206 e = e.browserEvent || e;
207 return [Event.pointerX(e), Event.pointerY(e)];
210 getTarget : function(e){
211 return Event.element(e.browserEvent || e);
214 resolveTextNode: Ext.isGecko ? function(node){
218 var s = HTMLElement.prototype.toString.call(node);
219 if(s == '[xpconnect wrapped native prototype]' || s == '[object XULElement]'){
222 return node.nodeType == 3 ? node.parentNode : node;
224 return node && node.nodeType == 3 ? node.parentNode : node;
227 getRelatedTarget: function(ev) { // missing from prototype?
228 ev = ev.browserEvent || ev;
229 var t = ev.relatedTarget;
231 if (ev.type == "mouseout") {
233 } else if (ev.type == "mouseover") {
238 return this.resolveTextNode(t);
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';
248 Event.observe(el, eventName, fn, false);
251 un : function(el, eventName, fn){
252 if((eventName == 'mouseenter' || eventName == 'mouseleave') && !mouseEnterSupported){
253 var item = mouseCache[el.id],
254 ev = item && item[eventName];
258 delete item[eventName];
259 eventName = (eventName == 'mouseenter') ? 'mouseover' : 'mouseout';
262 Event.stopObserving(el, eventName, fn, false);
265 purgeElement : function(el){
269 preventDefault : function(e){ // missing from prototype?
270 e = e.browserEvent || e;
271 if(e.preventDefault) {
274 e.returnValue = false;
278 stopPropagation : function(e){ // missing from prototype?
279 e = e.browserEvent || e;
280 if(e.stopPropagation) {
283 e.cancelBubble = true;
287 stopEvent : function(e){
288 Event.stop(e.browserEvent || e);
291 onAvailable : function(id, fn, scope){ // no equiv
292 var start = new Date(), iid;
294 if(start.getElapsed() > 10000){
297 var el = document.getElementById(id);
300 fn.call(scope||window, el);
303 iid = setInterval(f, 50);
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));
313 var createFailure = function(cb){
314 return cb.failure ? function(xhr){
315 cb.failure.call(cb.scope||window, createResponse(cb, xhr));
318 var createResponse = function(cb, xhr){
325 headerStr = xhr.getAllResponseHeaders();
326 Ext.each(headerStr.replace(/\r\n/g, '\n').split('\n'), function(v){
329 s = v.substr(0, t).toLowerCase();
330 if(v.charAt(t + 1) == ' '){
333 headerObj[s] = v.substr(t + 1);
339 responseText: xhr.responseText,
340 responseXML : xhr.responseXML,
341 argument: cb.argument,
343 statusText: xhr.statusText,
344 getResponseHeader : function(header){
345 return headerObj[header.toLowerCase()];
347 getAllResponseHeaders : function(){
353 request : function(method, uri, cb, data, options){
356 parameters: data || '',
358 onSuccess: createSuccess(cb),
359 onFailure: createFailure(cb)
362 var hs = options.headers;
364 o.requestHeaders = hs;
367 method = (method ? method : (options.method ? options.method : 'POST'));
368 if (!hs || !hs['Content-Type']){
369 o.contentType = 'text/xml';
371 o.postBody = options.xmlData;
374 if(options.jsonData){
375 method = (method ? method : (options.method ? options.method : 'POST'));
376 if (!hs || !hs['Content-Type']){
377 o.contentType = 'application/json';
379 o.postBody = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
383 new Ajax.Request(uri, o);
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:''),
391 onSuccess: createSuccess(cb),
392 onFailure: createFailure(cb)
396 isCallInProgress : function(trans){
400 abort : function(trans){
404 serializeForm : function(form){
405 return Form.serialize(form.dom||form);
411 Ext.lib.Anim = function(){
414 easeOut: function(pos) {
415 return 1-Math.pow(1-pos,2);
417 easeIn: function(pos) {
418 return 1-Math.pow(1-pos,2);
421 var createAnim = function(cb, scope){
423 stop : function(skipToLast){
424 this.effect.cancel();
427 isAnimated : function(){
428 return this.effect.state == 'running';
431 proxyCallback : function(){
432 Ext.callback(cb, scope);
437 scroll : function(el, args, duration, easing, cb, scope){
438 // not supported so scroll immediately?
439 var anim = createAnim(cb, scope);
441 if(typeof args.scroll.to[0] == 'number'){
442 el.scrollLeft = args.scroll.to[0];
444 if(typeof args.scroll.to[1] == 'number'){
445 el.scrollTop = args.scroll.to[1];
447 anim.proxyCallback();
451 motion : function(el, args, duration, easing, cb, scope){
452 return this.run(el, args, duration, easing, cb, scope);
455 color : function(el, args, duration, easing, cb, scope){
456 return this.run(el, args, duration, easing, cb, scope);
459 run : function(el, args, duration, easing, cb, scope, type){
462 switch(k){ // scriptaculous doesn't support, so convert these
464 var by, pts, e = Ext.fly(el, '_animrun');
466 if(by = args.points.by){
468 pts = e.translatePoints([xy[0]+by[0], xy[1]+by[1]]);
470 pts = e.translatePoints(args.points.to);
472 o.left = pts.left+'px';
473 o.top = pts.top+'px';
476 o.width = args.width.to+'px';
479 o.height = args.height.to+'px';
482 o.opacity = String(args.opacity.to);
485 o[k] = String(args[k].to);
489 var anim = createAnim(cb, scope);
490 anim.effect = new Effect.Morph(Ext.id(el), {
492 afterFinish: anim.proxyCallback,
493 transition: easings[easing] || Effect.Transitions.linear,
502 // all lib flyweight calls use their own flyweight to prevent collisions with developer flyweights
505 libFlyweight = new Ext.Element.Flyweight();
507 libFlyweight.dom = el;
511 Ext.lib.Region = function(t, r, b, l) {
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 );
529 getArea : function() {
530 return ( (this.bottom - this.top) * (this.right - this.left) );
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 );
539 if (b >= t && r >= l) {
540 return new Ext.lib.Region(t, r, b, l);
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 );
551 return new Ext.lib.Region(t, r, b, l);
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);
562 adjust : function(t, l, b, r){
571 Ext.lib.Region.getRegion = function(el) {
572 var p = Ext.lib.Dom.getXY(el);
575 var r = p[0] + el.offsetWidth;
576 var b = p[1] + el.offsetHeight;
579 return new Ext.lib.Region(t, r, b, l);
582 Ext.lib.Point = function(x, y) {
583 if (Ext.isArray(x)) {
587 this.x = this.right = this.left = this[0] = x;
588 this.y = this.top = this.bottom = this[1] = y;
591 Ext.lib.Point.prototype = new Ext.lib.Region();
596 function fnCleanUp() {
597 var p = Function.prototype;
598 delete p.createSequence;
600 delete p.createDelegate;
601 delete p.createCallback;
602 delete p.createInterceptor;
604 window.detachEvent("onunload", fnCleanUp);
606 window.attachEvent("onunload", fnCleanUp);