2 * Copyright (c) 2009-2011 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');
19 LibVersion: dojo.version.toString(),
23 tryFunctions: function() {
24 for (var i=0; i<arguments.length; i++) {
26 return arguments[i]();
34 eventBind: function(element, eventName, handler) {
35 handler.connection=dojo.connect(Rico.$(element), eventName, handler.object, handler.method);
38 eventUnbind: function(element, eventName, handler) {
39 dojo.disconnect(handler.connection);
42 eventElement: function(ev) {
46 eventStop: dojo.stopEvent,
48 eventClient: function(ev) {
49 return {x:ev.pageX, y:ev.pageY};
52 eventHandle: function(object, method) {
53 return { object: object, method: method };
56 addClass: dojo.addClass,
57 removeClass: dojo.removeClass,
58 hasClass: dojo.hasClass,
60 getStyle: function(element, name) {
61 var camelCase = name.replace(/\-(\w)/g, function(all, letter){
62 return letter.toUpperCase();
64 return dojo.style(element,camelCase);
69 // tried to use dojo._abs - 1.3.0 was broken in webkit, nightlies broken on IE8
70 cumulativeOffset: function(element) {
71 // var offset=dojo._abs(element);
72 // return {top:offset.y, left:offset.x};
73 element=Rico.$(element);
74 var valueT = 0, valueL = 0;
76 valueT += element.offsetTop || 0;
77 valueL += element.offsetLeft || 0;
78 element = element.offsetParent;
80 return {left: valueL, top: valueT};
83 positionedOffset: function(element) {
84 element=Rico.$(element);
85 var p, valueT = 0, valueL = 0;
87 valueT += element.offsetTop || 0;
88 valueL += element.offsetLeft || 0;
89 element = element.offsetParent;
91 p = dojo.style(element,'position');
92 if (p == 'relative' || p == 'absolute') break;
95 return {left: valueL, top: valueT};
98 getDirectChildrenByTag: function(element, tagName) {
100 var allKids = element.childNodes;
101 tagName=tagName.toLowerCase();
102 for( var i = 0 ; i < allKids.length ; i++ ) {
103 if ( allKids[i] && allKids[i].tagName && allKids[i].tagName.toLowerCase() == tagName )
104 kids.push(allKids[i]);
109 // logic borrowed from Prototype
110 _getWinDimension: function(D) {
111 if (this.isWebKit && !document.evaluate) {
112 // Safari <3.0 needs self.innerWidth/Height
113 return self['inner' + D];
114 } else if (this.isOpera && parseFloat(window.opera.version()) < 9.5) {
115 // Opera <9.5 needs document.body.clientWidth/Height
116 return document.body['client' + D]
118 return document.documentElement['client' + D];
122 windowHeight: function() {
123 return this._getWinDimension('Height');
126 windowWidth: function() {
127 return this._getWinDimension('Width');
130 docScrollLeft: function() {
131 return dojo._docScroll().x;
134 docScrollTop: function() {
135 return dojo._docScroll().y;
140 fadeIn: function(element,duration,onEnd) {
141 var a=dojo.fadeIn({node:element, duration:duration});
142 if (onEnd) dojo.connect(a,"onEnd",onEnd);
146 fadeOut: function(element,duration,onEnd) {
147 var a=dojo.fadeOut({node:element, duration:duration});
148 if (onEnd) dojo.connect(a,"onEnd",onEnd);
152 animate: function(element,options,properties) {
153 options.node=element;
154 options.properties=properties;
155 a=dojo.animateProperty(options);
161 toQueryString: dojo.objectToQuery,
163 getJSON: function(xhr) { return dojo.fromJson(xhr.responseText); },
165 ajaxRequest: function(url,options) {
166 this.dojoSend(url,options);
170 Rico.ajaxRequest.prototype = {
171 dojoSend : function(url,options) {
172 this.onComplete=options.onComplete;
173 this.onSuccess=options.onSuccess;
174 this.onFailure=options.onFailure;
177 handle : function(response, ioArgs) { self.dojoComplete(response, ioArgs); },
178 error : function(response, ioArgs) { self.dojoError(response, ioArgs); },
179 load : function(response, ioArgs) { self.dojoLoad(response, ioArgs); },
181 content : options.parameters,
184 var method=options.method.toUpperCase();
185 dojo.xhr(method, dOptions, method=='POST');
188 dojoComplete : function(dataORerror, ioArgs) {
189 if (this.onComplete) this.onComplete(ioArgs.xhr);
192 dojoError : function(response, ioArgs) {
193 if (this.onFailure) this.onFailure(ioArgs.xhr);
196 dojoLoad : function(response, ioArgs) {
197 if (this.onSuccess) this.onSuccess(ioArgs.xhr);
201 Rico.ajaxSubmit=function(form,url,options) {
203 if (!options.method) options.method='post';
204 new Rico.ajaxRequest(url,options);