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 dojo=='undefined') throw('This version of Rico requires the Dojo library');
18 Rico.LibVersion=dojo.version.toString();
19 Rico.extend=dojo.mixin;
22 Rico.tryFunctions = function() {
23 for (var i=0; i<arguments.length; i++) {
25 return arguments[i]();
31 Rico.select=dojo.query;
33 Rico.eventBind=function(element, eventName, handler) {
34 handler.connection=dojo.connect(Rico.$(element), eventName, handler.object, handler.method);
37 Rico.eventUnbind=function(element, eventName, handler) {
38 dojo.disconnect(handler.connection);
41 Rico.eventElement=function(ev) {
45 Rico.eventStop=dojo.stopEvent;
47 Rico.eventClient=function(ev) {
48 return {x:ev.pageX, y:ev.pageY};
51 Rico.eventHandle=function(object, method) {
52 return { object: object, method: method };
55 Rico.addClass=dojo.addClass;
56 Rico.removeClass=dojo.removeClass;
57 Rico.hasClass=dojo.hasClass;
59 Rico.getStyle=function(element, name) {
60 var camelCase = name.replace(/\-(\w)/g, function(all, letter){
61 return letter.toUpperCase();
63 return dojo.style(element,camelCase);
66 Rico.setStyle=dojo.style;
68 // tried to use dojo._abs - 1.3.0 was broken in webkit, nightlies broken on IE8
69 Rico.cumulativeOffset=function(element) {
70 // var offset=dojo._abs(element);
71 // return {top:offset.y, left:offset.x};
72 element=Rico.$(element);
73 var valueT = 0, valueL = 0;
75 valueT += element.offsetTop || 0;
76 valueL += element.offsetLeft || 0;
77 element = element.offsetParent;
79 return {left: valueL, top: valueT};
82 Rico.positionedOffset=function(element) {
83 element=Rico.$(element);
84 var p, valueT = 0, valueL = 0;
86 valueT += element.offsetTop || 0;
87 valueL += element.offsetLeft || 0;
88 element = element.offsetParent;
90 p = dojo.style(element,'position');
91 if (p == 'relative' || p == 'absolute') break;
94 return {left: valueL, top: valueT};
97 Rico.getDirectChildrenByTag=function(element, tagName) {
99 var allKids = element.childNodes;
100 tagName=tagName.toLowerCase();
101 for( var i = 0 ; i < allKids.length ; i++ ) {
102 if ( allKids[i] && allKids[i].tagName && allKids[i].tagName.toLowerCase() == tagName )
103 kids.push(allKids[i]);
108 // logic borrowed from Prototype
109 Rico._getWinDimension=function(D) {
110 if (this.isWebKit && !document.evaluate) {
111 // Safari <3.0 needs self.innerWidth/Height
112 return self['inner' + D];
113 } else if (this.isOpera && parseFloat(window.opera.version()) < 9.5) {
114 // Opera <9.5 needs document.body.clientWidth/Height
115 return document.body['client' + D]
117 return document.documentElement['client' + D];
121 Rico.windowHeight=function() {
122 return this._getWinDimension('Height');
125 Rico.windowWidth=function() {
126 return this._getWinDimension('Width');
129 Rico.docScrollLeft=function() {
130 return dojo._docScroll().x;
133 Rico.docScrollTop=function() {
134 return dojo._docScroll().y;
137 Rico.ajaxRequest=function(url,options) {
138 this.dojoSend(url,options);
141 Rico.ajaxRequest.prototype = {
142 dojoSend : function(url,options) {
143 this.onComplete=options.onComplete;
144 this.onSuccess=options.onSuccess;
145 this.onFailure=options.onFailure;
147 handle : Rico.bind(this,'dojoComplete'),
148 error : Rico.bind(this,'dojoError'),
149 load : Rico.bind(this,'dojoLoad'),
151 content : options.parameters,
154 var method=options.method.toUpperCase();
155 dojo.xhr(method, dOptions, method=='POST');
158 dojoComplete : function(dataORerror, ioArgs) {
159 if (this.onComplete) this.onComplete(ioArgs.xhr);
162 dojoError : function(response, ioArgs) {
163 if (this.onFailure) this.onFailure(ioArgs.xhr);
166 dojoLoad : function(response, ioArgs) {
167 if (this.onSuccess) this.onSuccess(ioArgs.xhr);
171 Rico.getJSON=function(xhr) { return dojo.fromJson(xhr.responseText); };
173 Rico.ajaxSubmit=function(form,url,options) {
175 if (!options.method) options.method='post';
176 new Rico.ajaxRequest(url,options);
179 Rico.toQueryString=dojo.objectToQuery;
183 Rico.fadeIn=function(element,duration,onEnd) {
184 var a=dojo.fadeIn({node:element, duration:duration});
185 if (onEnd) dojo.connect(a,"onEnd",onEnd);
189 Rico.fadeOut=function(element,duration,onEnd) {
190 var a=dojo.fadeOut({node:element, duration:duration});
191 if (onEnd) dojo.connect(a,"onEnd",onEnd);
195 Rico.animate=function(element,options,properties) {
196 options.node=element;
197 options.properties=properties;
198 a=dojo.animateProperty(options);