2 * Copyright (c) 2009 Matt Brown
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
5 * file except in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11 * either express or implied. See the License for the specific language governing permissions
12 * and limitations under the License.
15 if (typeof Ext=='undefined') throw('This version of Rico requires the Ext-core library');
19 Rico.LibVersion=Ext.version;
20 Rico.extend=Ext.apply;
21 Rico.trim=function(s) { return s.replace(Ext.DomQuery.trimRe,''); };
22 Rico.tryFunctions = function() {
23 for (var i=0; i<arguments.length; i++) {
25 return arguments[i]();
32 Rico.select=Ext.query;
34 Rico.eventBind=function(element, eventName, handler) {
35 Ext.EventManager.addListener(element, eventName, handler.object[handler.method], handler.object);
38 Rico.eventUnbind=function(element, eventName, handler) {
39 Ext.EventManager.removeListener(element, eventName, handler.object[handler.method], handler.object);
42 Rico.eventHandle=function(object, method) {
43 return { object: object, method: method };
46 Rico.eventElement=function(ev) {
50 Rico.eventClient=function(ev) {
51 return {x:ev.browserEvent.clientX, y:ev.browserEvent.clientY};
54 Rico.eventStop=function(ev) {
58 Rico.eventRelatedTarget=function(ev) {
59 return ev.getRelatedTarget();
62 Rico.eventKey=function(ev) {
66 Rico.eventLeftClick=function(ev) {
70 Rico.addClass=function(element, className) {
71 return Ext.get(element).addClass(className);
74 Rico.removeClass=function(element, className) {
75 return Ext.get(element).removeClass(className);
78 Rico.hasClass=function(element, className) {
79 return Ext.get(element).hasClass(className);
82 Rico.getStyle=function(element, property) {
83 return Ext.get(element).getStyle(property);
85 Rico.setStyle=function(element, properties) {
86 return Ext.get(element).setStyle(properties);
89 // logic borrowed from Prototype
90 // Ext.lib.Dom.getViewportWidth/Height includes scrollbar in Gecko browsers
91 Rico._getWinDimension=function(D) {
92 if (this.isWebKit && !document.evaluate) {
93 // Safari <3.0 needs self.innerWidth/Height
94 return self['inner' + D];
95 } else if (this.isOpera && parseFloat(window.opera.version()) < 9.5) {
96 // Opera <9.5 needs document.body.clientWidth/Height
97 return document.body['client' + D]
99 return document.documentElement['client' + D];
103 Rico.windowHeight=function() {
104 return this._getWinDimension('Height');
107 Rico.windowWidth=function() {
108 return this._getWinDimension('Width');
111 Rico.cumulativeOffset=function(element) {
112 element=Rico.$(element);
113 var valueT = 0, valueL = 0;
115 valueT += element.offsetTop || 0;
116 valueL += element.offsetLeft || 0;
117 element = element.offsetParent;
119 return {left: valueL, top: valueT};
122 Rico.positionedOffset=function(element) {
123 element=Rico.$(element);
124 var p, valueT = 0, valueL = 0;
126 valueT += element.offsetTop || 0;
127 valueL += element.offsetLeft || 0;
128 element = element.offsetParent;
130 p = Ext.get(element).getStyle('position');
131 if (p == 'relative' || p == 'absolute') break;
134 return {left: valueL, top: valueT};
137 Rico.docScrollLeft=function() {
138 return Ext.get(document).getScroll().left;
141 Rico.docScrollTop=function() {
142 return Ext.get(document).getScroll().top;
145 Rico.getDirectChildrenByTag=function(element, tagName) {
147 var allKids = element.childNodes;
148 tagName=tagName.toLowerCase();
149 for( var i = 0 ; i < allKids.length ; i++ ) {
150 if ( allKids[i] && allKids[i].tagName && allKids[i].tagName.toLowerCase() == tagName )
151 kids.push(allKids[i]);
156 Rico.ajaxRequest=function(url,options) {
158 success : options.onSuccess || options.onComplete,
159 failure : options.onFailure || options.onComplete,
160 method : options.method.toUpperCase(),
163 params : options.parameters
165 Ext.Ajax.request(extOptions);
168 Rico.getJSON=function(xhr) { return window["eval"]("(" + xhr.responseText + ")"); };
170 Rico.ajaxSubmit=function(form,url,options) {
172 if (!options.method) options.method='post';
173 Rico.ajaxRequest(url,options);
175 Rico.toQueryString=Ext.urlEncode;
179 Rico.fadeIn=function(element,duration,onEnd) {
180 Ext.get(element).fadeIn({duration:duration/1000.0, callback: onEnd});
183 Rico.fadeOut=function(element,duration,onEnd) {
184 Ext.get(element).fadeOut({duration:duration/1000.0, callback: onEnd});
187 Rico.animate=function(element,options,properties) {
189 opts.callback=options.onEnd;
190 opts.duration=options.duration/1000.0;
191 opts.width=properties.width;
192 opts.height=properties.height;
193 opts.x=properties.left;
194 opts.y=properties.top;
195 opts.opacity=properties.opacity;
196 Ext.get(element).shift(opts);