2 * Copyright (c) 2009, 2010 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 MooTools=='undefined') throw('This version of Rico requires the MooTools library');
19 Rico.LibVersion=MooTools.version;
20 Rico.extend=typeof($extend) != 'undefined' ? $extend : Object.append;
21 Rico.tryFunctions = typeof($try) != 'undefined' ? $try : Function.attempt;
22 Rico.trim=function(s) { return s.trim(); };
24 Rico.select=function(selector, element) {
25 return $(element || document).getElements(selector);
28 Rico.eventBind=function(element, eventName, handler) {
29 $(element).addEvent(eventName, handler);
32 Rico.eventUnbind=function(element, eventName, handler) {
33 $(element).removeEvent(eventName, handler);
36 Rico.eventHandle=function(object, method) {
37 return function(e) { object[method].call(object,e); };
40 Rico.eventElement=function(ev) {
44 Rico.eventClient=function(ev) {
48 Rico.eventKey=function(ev) {
52 Rico.eventStop=function(ev) {
56 Rico.eventLeftClick=function(ev) {
57 return !ev.rightClick;
60 Rico.addClass=function(element, className) {
61 return $(element).addClass(className);
64 Rico.removeClass=function(element, className) {
65 return $(element).removeClass(className);
68 Rico.hasClass=function(element, className) {
69 return $(element).hasClass(className);
72 Rico.getStyle=function(element, property) {
73 return $(element).getStyle(property);
76 Rico.setStyle=function(element, properties) {
77 return $(element).setStyles(properties);
81 * @returns available height, excluding scrollbar & margin
83 Rico.windowHeight=function() {
84 return Window.getSize().y;
88 * @returns available width, excluding scrollbar & margin
90 Rico.windowWidth=function() {
91 return Window.getSize().x;
94 Rico._fixOffsets=function(o) {
95 return {top: o.y, left: o.x};
98 Rico.positionedOffset=function(element) {
99 var p, valueT = 0, valueL = 0;
101 valueT += element.offsetTop || 0;
102 valueL += element.offsetLeft || 0;
103 element = element.offsetParent;
105 p = $(element).getStyle('position');
106 if (p == 'relative' || p == 'absolute') break;
109 return {left: valueL, top: valueT};
112 Rico.cumulativeOffset=function(element) {
113 return this._fixOffsets($(element).getPosition());
116 Rico.docScrollLeft=function() {
117 return Window.getScroll().x;
120 Rico.docScrollTop=function() {
121 return Window.getScroll().y;
124 Rico.getDirectChildrenByTag=function(element, tagName) {
125 return $(element).getChildren(tagName);
128 Rico.ajaxRequest=function(url,options) {
129 this.mooSend(url,options);
132 Rico.ajaxRequest.prototype = {
133 mooSend : function(url,options) {
134 this.onSuccess=options.onSuccess;
135 this.onComplete=options.onComplete;
137 onComplete : Rico.bind(this,'mooComplete'),
138 onSuccess : Rico.bind(this,'mooSuccess'),
139 onFailure : options.onFailure,
140 method : options.method,
141 data : options.parameters,
144 this.mooRequest = new Request(mooOptions);
145 this.mooRequest.send();
148 mooSuccess : function() {
149 if (this.onSuccess) this.onSuccess(this.mooRequest.xhr);
152 mooComplete : function() {
153 if (this.onComplete) this.onComplete(this.mooRequest.xhr);
157 Rico.getJSON=function(xhr) { return JSON.decode(xhr.responseText,true); };
159 Rico.ajaxSubmit=function(form,url,options) {
160 options.parameters=$(form).toQueryString();
161 if (!options.method) options.method='post';
162 url=url || form.action;
163 new Rico.ajaxRequest(url,options);
165 Rico.toQueryString=typeof(Hash) != 'undefined' ? Hash.toQueryString : Object.toQueryString;
169 Rico.fadeIn=function(element,duration,onEnd) {
170 var a = new Fx.Tween(element, {duration:duration, onComplete:onEnd});
171 a.start('opacity', 1);
174 Rico.fadeOut=function(element,duration,onEnd) {
175 var a = new Fx.Tween(element, {duration:duration, onComplete:onEnd});
176 a.start('opacity', 0);
179 Rico.animate=function(element,options,properties) {
180 options.onComplete=options.onEnd;
181 var effect=new Fx.Morph(element,options);
182 effect.start(properties);