resolving svn conflicts
[infodrom/rico3] / ricoClient / js / rico2jqu.js
1 /**
2   *  Copyright (c) 2009 Matt Brown
3   *
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
6   *
7   *         http://www.apache.org/licenses/LICENSE-2.0
8   *
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.
13   **/
14
15 if (typeof jQuery=='undefined') throw('This version of Rico requires the jQuery library');
16
17
18 Rico.Lib='jQuery';
19 Rico.LibVersion=jQuery().jquery;
20 Rico.extend=jQuery.extend;
21 Rico.trim=jQuery.trim;
22 Rico.tryFunctions = function() {
23   for (var i=0; i<arguments.length; i++) {
24                 try {
25                         return arguments[i]();
26                 } catch(e){}
27         }
28         return null;
29 };
30
31
32 Rico._j=function(element) {
33   if (typeof element=='string')
34     element = document.getElementById(element);
35   return jQuery(element);
36 };
37   
38 Rico.select=function(selector, element) {
39   return element ? this._j(element).find(selector) : jQuery(selector);
40 };
41   
42 Rico.eventBind=function(element, eventName, handler) {
43   this._j(element).bind(eventName, handler);
44 };
45
46 Rico.eventUnbind=function(element, eventName, handler) {
47   this._j(element).unbind(eventName, handler);
48 };
49
50 Rico.eventHandle=function(object, method) {
51   return function(e) {
52     return object[method].call(object,e);
53   }
54 };
55
56 Rico.eventElement=function(ev) {
57   return ev.target;
58 };
59
60 Rico.eventClient=function(ev) {
61   return {x:ev.clientX, y:ev.clientY};
62 };
63
64 Rico.eventStop=function(ev) {
65   ev.preventDefault();
66   ev.stopPropagation();
67 };
68   
69 Rico.addClass=function(element, className) {
70   return this._j(element).addClass(className);
71 };
72
73 Rico.removeClass=function(element, className) {
74   return this._j(element).removeClass(className);
75 };
76
77 Rico.hasClass=function(element, className) {
78   return this._j(element).hasClass(className);
79 };
80   
81 Rico.getStyle=function(element, property) {
82   return this._j(element).css(property);
83 };
84 Rico.setStyle=function(element, properties) {
85   return this._j(element).css(properties);
86 };
87
88 /**
89  * @returns available height, excluding scrollbar & margin
90  */
91 Rico.windowHeight=function() {
92   return jQuery(window).height();
93 };
94
95 /**
96  * @returns available width, excluding scrollbar & margin
97  */
98 Rico.windowWidth=function() {
99   return jQuery(window).width();
100 };
101
102 Rico.positionedOffset=function(element) {
103   return this._j(element).position();
104 };
105
106 Rico.cumulativeOffset=function(element) {
107   return this._j(element).offset();
108 };
109
110 Rico.docScrollLeft=function() {
111   return jQuery('html').scrollLeft();
112 };
113
114 Rico.docScrollTop=function() {
115   return jQuery('html').scrollTop();
116 };
117
118 Rico.getDirectChildrenByTag=function(element, tagName) {
119   return this._j(element).children(tagName);
120 };
121
122 Rico.ajaxRequest=function(url,options) {
123   this.jSend(url,options);
124 }
125
126 Rico.ajaxRequest.prototype = {
127   jSend: function(url,options) {
128     this.onSuccess=options.onSuccess;
129     var jOptions = {
130       complete : options.onComplete,
131       error: options.onFailure,
132       success: Rico.bind(this,'jSuccess'),
133       type : options.method.toUpperCase(),
134       url : url,
135       data : options.parameters
136     }
137     this.xhr=jQuery.ajax(jOptions);
138   },
139   
140   jSuccess: function() {
141     if (this.onSuccess) this.onSuccess(this.xhr);
142   }
143 }
144
145 Rico.getJSON=function(xhr) { return jQuery.httpData(xhr,'json'); };
146
147 Rico.ajaxSubmit=function(form,url,options) {
148   options.parameters=this._j(form).serialize();
149   if (!options.method) options.method='post';
150   url=url || form.action;
151   new Rico.ajaxRequest(url,options);
152 }
153 Rico.toQueryString=jQuery.param;
154
155 // Animation
156
157 Rico.fadeIn=function(element,duration,onEnd) {
158   this._j(element).fadeIn(duration,onEnd);
159 };
160
161 Rico.fadeOut=function(element,duration,onEnd) {
162   this._j(element).fadeOut(duration,onEnd);
163 };
164
165 Rico.animate=function(element,options,properties) {
166   options.complete=options.onEnd;
167   this._j(element).animate(properties,options);
168 };