Moved rico.js to minsrc and is now part of rico_min.js. Files are now sent to the...
[infodrom/rico3] / ricoClient / js / rico2jqu.js
1 /**
2   *  Copyright (c) 2009-2011 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 var Rico = {
18   Lib: 'jQuery',
19   LibVersion: jQuery().jquery,
20   extend: jQuery.extend,
21   trim: jQuery.trim,
22   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   _j: function(element) {
32     if (typeof element=='string')
33       element = document.getElementById(element);
34     return jQuery(element);
35   },
36
37   select: function(selector, element) {
38     return element ? this._j(element).find(selector) : jQuery(selector);
39   },
40
41   eventBind: function(element, eventName, handler) {
42     this._j(element).bind(eventName, handler);
43   },
44
45   eventUnbind: function(element, eventName, handler) {
46     this._j(element).unbind(eventName, handler);
47   },
48
49   eventHandle: function(object, method) {
50     return function(e) {
51       return object[method].call(object,e);
52     }
53   },
54
55   eventElement: function(ev) {
56     return ev.target;
57   },
58
59   eventClient: function(ev) {
60     return {x:ev.clientX, y:ev.clientY};
61   },
62
63   eventStop: function(ev) {
64     ev.preventDefault();
65     ev.stopPropagation();
66   },
67
68   addClass: function(element, className) {
69     var j=this._j(element);
70     if (!j.hasClass(className)) j.addClass(className);
71     return j;
72   },
73
74   removeClass: function(element, className) {
75     return this._j(element).removeClass(className);
76   },
77
78   hasClass: function(element, className) {
79     return this._j(element).hasClass(className);
80   },
81
82   getStyle: function(element, property) {
83     return this._j(element).css(property);
84   },
85   setStyle: function(element, properties) {
86     return this._j(element).css(properties);
87   },
88
89   /**
90    * @returns available height, excluding scrollbar & margin
91    */
92   windowHeight: function() {
93     return jQuery(window).height();
94   },
95
96   /**
97    * @returns available width, excluding scrollbar & margin
98    */
99   windowWidth: function() {
100     return jQuery(window).width();
101   },
102
103   positionedOffset: function(element) {
104     return this._j(element).position();
105   },
106
107   cumulativeOffset: function(element) {
108     return this._j(element).offset();
109   },
110
111   docScrollLeft: function() {
112     return jQuery('html').scrollLeft();
113   },
114
115   docScrollTop: function() {
116     return jQuery('html').scrollTop();
117   },
118
119   getDirectChildrenByTag: function(element, tagName) {
120     return this._j(element).children(tagName);
121   },
122
123   toQueryString: jQuery.param,
124
125   // Animation
126
127   fadeIn: function(element,duration,onEnd) {
128     this._j(element).fadeIn(duration,onEnd);
129   },
130
131   fadeOut: function(element,duration,onEnd) {
132     this._j(element).fadeOut(duration,onEnd);
133   },
134
135   animate: function(element,options,properties) {
136     options.complete=options.onEnd;
137     this._j(element).animate(properties,options);
138   },
139
140   getJSON: jQuery.httpData ? function(xhr) { return jQuery.httpData(xhr,'json'); } : function(xhr) { return jQuery.parseJSON(xhr.responseText); },
141
142   ajaxRequest: function(url,options) {
143     this.jSend(url,options);
144   }
145 };
146
147 Rico.ajaxRequest.prototype = {
148   jSend: function(url,options) {
149     this.onSuccess=options.onSuccess;
150     var self=this;
151     var jOptions = {
152       complete : options.onComplete,
153       error: options.onFailure,
154       success: function() { self.jSuccess(); },
155       type : options.method.toUpperCase(),
156       url : url,
157       data : options.parameters
158     }
159     this.xhr=jQuery.ajax(jOptions);
160   },
161
162   jSuccess: function() {
163     if (this.onSuccess) this.onSuccess(this.xhr);
164   }
165 };
166
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);
172 };