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 glow=='undefined') throw('This version of Rico requires the glow library');
19 Rico.LibVersion=glow.VERSION;
20 Rico.extend=glow.lang.apply;
21 Rico.trim=glow.lang.trim;
22 Rico.tryFunctions = function() {
23 for (var i=0; i<arguments.length; i++) {
25 return arguments[i]();
30 Rico._g=function(element) {
31 if (typeof element=='string')
32 element = document.getElementById(element);
33 return glow.dom.get(element);
36 Rico.select=function(selector, element) {
37 return element ? this._g(element).get(selector) : glow.dom.get(selector);
40 Rico.eventBind=function(element, eventName, handler) {
41 handler.id=glow.events.addListener(Rico.$(element), eventName, handler.object[handler.method], handler.object);
44 Rico.eventUnbind=function(element, eventName, handler) {
45 glow.events.removeListener(handler.id);
48 Rico.eventHandle=function(object, method) {
49 return { object: object, method: method };
52 Rico.eventElement=function(ev) {
56 Rico.eventClient=function(ev) {
57 return {x:ev.pageX - document.body.scrollLeft - document.documentElement.scrollLeft,
58 y:ev.pageY - document.body.scrollTop - document.documentElement.scrollTop};
61 Rico.eventStop=function(ev) {
66 Rico.eventKey=function(ev) {
70 Rico.eventLeftClick=function(ev) {
74 Rico.addClass=function(element, className) {
75 return this._g(element).addClass(className);
78 Rico.removeClass=function(element, className) {
79 return this._g(element).removeClass(className);
82 Rico.hasClass=function(element, className) {
83 return this._g(element).hasClass(className);
87 ran into bugs on FF and Safari with native Glow function
88 Rico.getStyle=function(element, property) {
89 return this._g(element).css(property);
92 Use a modified version of Prototype's method
94 Rico.getStyle=function(element, style) {
95 element = Rico.$(element);
96 var camelCase = style.replace(/\-(\w)/g, function(all, letter){
97 return letter.toUpperCase();
99 style = style == 'float' ? 'cssFloat' : camelCase;
100 var value = element.style[style];
101 if (!value || value == 'auto') {
102 if (element.currentStyle) {
103 value=element.currentStyle[style];
104 } else if (document.defaultView) {
105 var css = document.defaultView.getComputedStyle(element, null);
106 value = css ? css[style] : null;
109 if (style == 'opacity') return value ? parseFloat(value) : 1.0;
110 return value == 'auto' ? null : value;
114 Rico.setStyle=function(element, properties) {
115 var elem=this._g(element);
116 for (var prop in properties) {
117 elem.css(prop,properties[prop])
122 * @returns available height, excluding scrollbar & margin
124 Rico.windowHeight=function() {
125 return glow.dom.get(window).height();
129 * @returns available width, excluding scrollbar & margin
131 Rico.windowWidth=function() {
132 return glow.dom.get(window).width();
135 Rico.positionedOffset=function(element) {
136 var p, valueT = 0, valueL = 0;
138 valueT += element.offsetTop || 0;
139 valueL += element.offsetLeft || 0;
140 element = element.offsetParent;
142 p = glow.dom.get(element).css('position');
143 if (p == 'relative' || p == 'absolute') break;
146 return {left: valueL, top: valueT};
149 Rico.cumulativeOffset=function(element) {
150 return this._g(element).offset();
153 Rico._docElement=function() {
154 return (document.compatMode && document.compatMode.indexOf("CSS")!=-1) ? document.documentElement : document.getElementsByTagName("body")[0];
157 Rico.docScrollLeft=function() {
158 return Rico._docElement.scrollLeft || window.pageXOffset || 0;
161 Rico.docScrollTop=function() {
162 return Rico._docElement.scrollTop || window.pageYOffset || 0;
165 Rico.getDirectChildrenByTag=function(element, tagName) {
166 tagName=tagName.toLowerCase();
167 return this._g(element).children().filter(function(i) { return this.tagName && this.tagName.toLowerCase()==tagName; });
170 Rico.getJSON=function(xhr) { return glow.data.decodeJson(xhr.responseText); };
172 Rico.ajaxRequest=function(url,options) {
173 this.glowSend(url,options);
176 Rico.ajaxRequest.prototype = {
177 glowSend : function(url,options) {
178 this.onComplete=options.onComplete;
179 this.onSuccess=options.onSuccess;
180 this.onFailure=options.onFailure;
181 options.onLoad=Rico.bind(this,'glowLoad');
182 options.onError=Rico.bind(this,'glowError');
183 options.useCache=true;
184 if (options.method.toLowerCase()=='post') {
185 glow.net.post(url,options.parameters,options);
187 glow.net.get(url+'?'+glow.data.encodeUrl(options.parameters),options);
191 glowError : function(response) {
192 if (this.onFailure) this.onFailure(response);
193 if (this.onComplete) this.onComplete(response.nativeResponse);
196 glowLoad : function(response) {
197 if (this.onSuccess) this.onSuccess(response.nativeResponse);
198 if (this.onComplete) this.onComplete(response.nativeResponse);
202 Rico.ajaxSubmit=function(form,url,options) {
203 options.parameters=glow.data.encodeUrl(this._g(form).val());
204 if (!options.method) options.method='post';
205 url=url || form.action;
206 new Rico.ajaxRequest(url,options);
208 Rico.toQueryString=glow.data.encodeUrl;
212 Rico.fadeIn=function(element,duration,onEnd) {
213 glow.anim.fadeIn(this._g(element), duration/1000.0, {onComplete:onEnd});
216 Rico.fadeOut=function(element,duration,onEnd) {
217 glow.anim.fadeOut(this._g(element), duration/1000.0, {onComplete:onEnd});
220 Rico.animate=function(element,options,properties) {
221 var effect=glow.anim.css(this._g(element), options.duration/1000.0, properties);
222 glow.events.addListener(effect, "complete", options.onEnd);