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 jQuery=='undefined') throw('This version of Rico requires the jQuery library');
19 LibVersion: jQuery().jquery,
20 extend: jQuery.extend,
22 tryFunctions: function() {
23 for (var i=0; i<arguments.length; i++) {
25 return arguments[i]();
31 _j: function(element) {
32 if (typeof element=='string')
33 element = document.getElementById(element);
34 return jQuery(element);
37 select: function(selector, element) {
38 return element ? this._j(element).find(selector) : jQuery(selector);
41 eventBind: function(element, eventName, handler) {
42 this._j(element).bind(eventName, handler);
45 eventUnbind: function(element, eventName, handler) {
46 this._j(element).unbind(eventName, handler);
49 eventHandle: function(object, method) {
51 return object[method].call(object,e);
55 eventElement: function(ev) {
59 eventClient: function(ev) {
60 return {x:ev.clientX, y:ev.clientY};
63 eventStop: function(ev) {
68 addClass: function(element, className) {
69 var j=this._j(element);
70 if (!j.hasClass(className)) j.addClass(className);
74 removeClass: function(element, className) {
75 return this._j(element).removeClass(className);
78 hasClass: function(element, className) {
79 return this._j(element).hasClass(className);
82 getStyle: function(element, property) {
83 return this._j(element).css(property);
85 setStyle: function(element, properties) {
86 return this._j(element).css(properties);
90 * @returns available height, excluding scrollbar & margin
92 windowHeight: function() {
93 return jQuery(window).height();
97 * @returns available width, excluding scrollbar & margin
99 windowWidth: function() {
100 return jQuery(window).width();
103 positionedOffset: function(element) {
104 return this._j(element).position();
107 cumulativeOffset: function(element) {
108 return this._j(element).offset();
111 docScrollLeft: function() {
112 return jQuery('html').scrollLeft();
115 docScrollTop: function() {
116 return jQuery('html').scrollTop();
119 getDirectChildrenByTag: function(element, tagName) {
120 return this._j(element).children(tagName);
123 toQueryString: jQuery.param,
127 fadeIn: function(element,duration,onEnd) {
128 this._j(element).fadeIn(duration,onEnd);
131 fadeOut: function(element,duration,onEnd) {
132 this._j(element).fadeOut(duration,onEnd);
135 animate: function(element,options,properties) {
136 options.complete=options.onEnd;
137 this._j(element).animate(properties,options);
140 getJSON: jQuery.httpData ? function(xhr) { return jQuery.httpData(xhr,'json'); } : function(xhr) { return jQuery.parseJSON(xhr.responseText); },
142 ajaxRequest: function(url,options) {
143 this.jSend(url,options);
147 Rico.ajaxRequest.prototype = {
148 jSend: function(url,options) {
149 this.onSuccess=options.onSuccess;
152 complete : options.onComplete,
153 error: options.onFailure,
154 success: function() { self.jSuccess(); },
155 type : options.method.toUpperCase(),
157 data : options.parameters
159 this.xhr=jQuery.ajax(jOptions);
162 jSuccess: function() {
163 if (this.onSuccess) this.onSuccess(this.xhr);
167 Rico.ajaxSubmit=function(form,url,options) {
168 options.parameters=this._j(form).serialize();
169 if (!options.method) options.method='post';
170 url=url || form.action;
171 new Rico.ajaxRequest(url,options);