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 // the Dojo native function has problems on IE
46 Rico.eventStop=function(ev) {
47 if (window.attachEvent) {
48 window.event.returnValue = false;
49 window.event.cancelBubble = true;
56 Rico.eventClient=function(ev) {
57 return {x:ev.pageX, y:ev.pageY};
60 Rico.eventHandle=function(object, method) {
61 return { object: object, method: method };
64 Rico.addClass=dojo.addClass;
65 Rico.removeClass=dojo.removeClass;
66 Rico.hasClass=dojo.hasClass;
68 Rico.getStyle=function(element, name) {
69 var camelCase = name.replace(/\-(\w)/g, function(all, letter){
70 return letter.toUpperCase();
72 return dojo.style(element,camelCase);
75 Rico.setStyle=dojo.style;
77 // tried to use dojo._abs - 1.3.0 was broken in webkit, nightlies broken on IE8
78 Rico.cumulativeOffset=function(element) {
79 // var offset=dojo._abs(element);
80 // return {top:offset.y, left:offset.x};
81 element=Rico.$(element);
82 var valueT = 0, valueL = 0;
84 valueT += element.offsetTop || 0;
85 valueL += element.offsetLeft || 0;
86 element = element.offsetParent;
88 return {left: valueL, top: valueT};
91 Rico.positionedOffset=function(element) {
92 element=Rico.$(element);
93 var p, valueT = 0, valueL = 0;
95 valueT += element.offsetTop || 0;
96 valueL += element.offsetLeft || 0;
97 element = element.offsetParent;
99 p = dojo.style(element,'position');
100 if (p == 'relative' || p == 'absolute') break;
103 return {left: valueL, top: valueT};
106 Rico.getDirectChildrenByTag=function(element, tagName) {
108 var allKids = element.childNodes;
109 tagName=tagName.toLowerCase();
110 for( var i = 0 ; i < allKids.length ; i++ ) {
111 if ( allKids[i] && allKids[i].tagName && allKids[i].tagName.toLowerCase() == tagName )
112 kids.push(allKids[i]);
117 // logic borrowed from Prototype
118 Rico._getWinDimension=function(D) {
119 if (this.isWebKit && !document.evaluate) {
120 // Safari <3.0 needs self.innerWidth/Height
121 return self['inner' + D];
122 } else if (this.isOpera && parseFloat(window.opera.version()) < 9.5) {
123 // Opera <9.5 needs document.body.clientWidth/Height
124 return document.body['client' + D]
126 return document.documentElement['client' + D];
130 Rico.windowHeight=function() {
131 return this._getWinDimension('Height');
134 Rico.windowWidth=function() {
135 return this._getWinDimension('Width');
138 Rico.docScrollLeft=function() {
139 return dojo._docScroll().x;
142 Rico.docScrollTop=function() {
143 return dojo._docScroll().y;
146 Rico.ajaxRequest=function(url,options) {
147 this.dojoSend(url,options);
150 Rico.ajaxRequest.prototype = {
151 dojoSend : function(url,options) {
152 this.onComplete=options.onComplete;
153 this.onSuccess=options.onSuccess;
154 this.onFailure=options.onFailure;
157 handle : function(response, ioArgs) { self.dojoComplete(response, ioArgs); },
158 error : function(response, ioArgs) { self.dojoError(response, ioArgs); },
159 load : function(response, ioArgs) { self.dojoLoad(response, ioArgs); },
161 content : options.parameters,
164 var method=options.method.toUpperCase();
165 dojo.xhr(method, dOptions, method=='POST');
168 dojoComplete : function(dataORerror, ioArgs) {
169 if (this.onComplete) this.onComplete(ioArgs.xhr);
172 dojoError : function(response, ioArgs) {
173 if (this.onFailure) this.onFailure(ioArgs.xhr);
176 dojoLoad : function(response, ioArgs) {
177 if (this.onSuccess) this.onSuccess(ioArgs.xhr);
181 Rico.getJSON=function(xhr) { return dojo.fromJson(xhr.responseText); };
183 Rico.ajaxSubmit=function(form,url,options) {
185 if (!options.method) options.method='post';
186 new Rico.ajaxRequest(url,options);
189 Rico.toQueryString=dojo.objectToQuery;
193 Rico.fadeIn=function(element,duration,onEnd) {
194 var a=dojo.fadeIn({node:element, duration:duration});
195 if (onEnd) dojo.connect(a,"onEnd",onEnd);
199 Rico.fadeOut=function(element,duration,onEnd) {
200 var a=dojo.fadeOut({node:element, duration:duration});
201 if (onEnd) dojo.connect(a,"onEnd",onEnd);
205 Rico.animate=function(element,options,properties) {
206 options.node=element;
207 options.properties=properties;
208 a=dojo.animateProperty(options);
212 Rico._bindLoadEvent();