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 MooTools=='undefined') throw('This version of Rico requires the MooTools library');
19 LibVersion: MooTools.version,
20 extend: typeof($extend) != 'undefined' ? $extend : Object.append,
21 tryFunctions: typeof($try) != 'undefined' ? $try : Function.attempt,
22 trim: function(s) { return s.trim(); },
24 select: function(selector, element) {
25 return $(element || document).getElements(selector);
28 eventBind: function(element, eventName, handler) {
29 $(element).addEvent(eventName, handler);
32 eventUnbind: function(element, eventName, handler) {
33 $(element).removeEvent(eventName, handler);
36 eventHandle: function(object, method) {
37 return function(e) { object[method].call(object,e); };
40 eventElement: function(ev) {
44 eventClient: function(ev) {
48 eventKey: function(ev) {
52 eventStop: function(ev) {
56 eventLeftClick: function(ev) {
57 return !ev.rightClick;
60 addClass: function(element, className) {
61 return $(element).addClass(className);
64 removeClass: function(element, className) {
65 return $(element).removeClass(className);
68 hasClass: function(element, className) {
69 return $(element).hasClass(className);
72 getStyle: function(element, property) {
73 return $(element).getStyle(property);
76 setStyle: function(element, properties) {
77 return $(element).setStyles(properties);
81 * @returns available height, excluding scrollbar & margin
83 windowHeight: function() {
84 return Window.getSize().y;
88 * @returns available width, excluding scrollbar & margin
90 windowWidth: function() {
91 return Window.getSize().x;
94 _fixOffsets: function(o) {
95 return {top: o.y, left: o.x};
98 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 cumulativeOffset: function(element) {
113 return this._fixOffsets($(element).getPosition());
116 docScrollLeft: function() {
117 return Window.getScroll().x;
120 docScrollTop: function() {
121 return Window.getScroll().y;
124 getDirectChildrenByTag: function(element, tagName) {
125 return $(element).getChildren(tagName);
130 fadeIn: function(element,duration,onEnd) {
131 var a = new Fx.Tween(element, {duration:duration, onComplete:onEnd});
132 a.start('opacity', 1);
135 fadeOut: function(element,duration,onEnd) {
136 var a = new Fx.Tween(element, {duration:duration, onComplete:onEnd});
137 a.start('opacity', 0);
140 animate: function(element,options,properties) {
141 options.onComplete=options.onEnd;
142 var effect=new Fx.Morph(element,options);
143 effect.start(properties);
149 getJSON: function(xhr) { return JSON.decode(xhr.responseText,true); },
151 toQueryString: typeof(Hash) != 'undefined' ? Hash.toQueryString : Object.toQueryString,
153 ajaxRequest: function(url,options) {
154 this.mooSend(url,options);
158 Rico.ajaxRequest.prototype = {
159 mooSend : function(url,options) {
160 this.onSuccess=options.onSuccess;
161 this.onComplete=options.onComplete;
164 onComplete : function() { self.mooComplete(); },
165 onSuccess : function() { self.mooSuccess(); },
166 onFailure : options.onFailure,
167 method : options.method,
168 data : options.parameters,
171 this.mooRequest = new Request(mooOptions);
172 this.mooRequest.send();
175 mooSuccess : function() {
176 if (this.onSuccess) this.onSuccess(this.mooRequest.xhr);
179 mooComplete : function() {
180 if (this.onComplete) this.onComplete(this.mooRequest.xhr);
184 Rico.ajaxSubmit=function(form,url,options) {
185 options.parameters=$(form).toQueryString();
186 if (!options.method) options.method='post';
187 url=url || form.action;
188 new Rico.ajaxRequest(url,options);