Most base libraries now loaded from googleapis. Changes to the way LiveGridForms...
[infodrom/rico3] / ricoClient / js / rico2moo.js
1 /**
2   *  Copyright (c) 2009, 2010 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 MooTools=='undefined') throw('This version of Rico requires the MooTools library');
16
17
18 Rico.Lib='MooTools';
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(); };
23
24 Rico.select=function(selector, element) {
25   return $(element || document).getElements(selector);
26 };
27   
28 Rico.eventBind=function(element, eventName, handler) {
29   $(element).addEvent(eventName, handler);
30 };
31
32 Rico.eventUnbind=function(element, eventName, handler) {
33   $(element).removeEvent(eventName, handler);
34 };
35
36 Rico.eventHandle=function(object, method) {
37   return function(e) { object[method].call(object,e); };
38 };
39
40 Rico.eventElement=function(ev) {
41   return ev.target;
42 };
43
44 Rico.eventClient=function(ev) {
45   return ev.client;
46 };
47
48 Rico.eventKey=function(ev) {
49   return ev.code;
50 };
51
52 Rico.eventStop=function(ev) {
53   ev.stop();
54 };
55
56 Rico.eventLeftClick=function(ev) {
57   return !ev.rightClick;
58 };
59   
60 Rico.addClass=function(element, className) {
61   return $(element).addClass(className);
62 };
63
64 Rico.removeClass=function(element, className) {
65   return $(element).removeClass(className);
66 };
67
68 Rico.hasClass=function(element, className) {
69   return $(element).hasClass(className);
70 };
71   
72 Rico.getStyle=function(element, property) {
73   return $(element).getStyle(property);
74 };
75
76 Rico.setStyle=function(element, properties) {
77   return $(element).setStyles(properties);
78 };
79
80 /**
81  * @returns available height, excluding scrollbar & margin
82  */
83 Rico.windowHeight=function() {
84   return Window.getSize().y;
85 };
86
87 /**
88  * @returns available width, excluding scrollbar & margin
89  */
90 Rico.windowWidth=function() {
91   return Window.getSize().x;
92 };
93
94 Rico._fixOffsets=function(o) {
95   return {top: o.y, left: o.x};
96 }
97
98 Rico.positionedOffset=function(element) {
99   var p, valueT = 0, valueL = 0;
100   do {
101     valueT += element.offsetTop  || 0;
102     valueL += element.offsetLeft || 0;
103     element = element.offsetParent;
104     if (element) {
105       p = $(element).getStyle('position');
106       if (p == 'relative' || p == 'absolute') break;
107     }
108   } while (element);
109   return {left: valueL, top: valueT};
110 };
111
112 Rico.cumulativeOffset=function(element) {
113   return this._fixOffsets($(element).getPosition());
114 };
115
116 Rico.docScrollLeft=function() {
117   return Window.getScroll().x;
118 };
119
120 Rico.docScrollTop=function() {
121   return Window.getScroll().y;
122 };
123
124 Rico.getDirectChildrenByTag=function(element, tagName) {
125   return $(element).getChildren(tagName);
126 };
127
128 Rico.ajaxRequest=function(url,options) {
129   this.mooSend(url,options);
130 }
131
132 Rico.ajaxRequest.prototype = {
133   mooSend : function(url,options) {
134     this.onSuccess=options.onSuccess;
135     this.onComplete=options.onComplete;
136     var mooOptions = {
137       onComplete : Rico.bind(this,'mooComplete'),
138       onSuccess : Rico.bind(this,'mooSuccess'),
139       onFailure : options.onFailure,
140       method : options.method,
141       data : options.parameters,
142       url : url
143     }
144     this.mooRequest = new Request(mooOptions);
145     this.mooRequest.send();
146   },
147   
148   mooSuccess : function() {
149     if (this.onSuccess) this.onSuccess(this.mooRequest.xhr);
150   },
151   
152   mooComplete : function() {
153     if (this.onComplete) this.onComplete(this.mooRequest.xhr);
154   }
155 }
156
157 Rico.getJSON=function(xhr) { return JSON.decode(xhr.responseText,true); };
158
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);
164 }
165 Rico.toQueryString=typeof(Hash) != 'undefined' ? Hash.toQueryString : Object.toQueryString;
166
167 // Animation
168
169 Rico.fadeIn=function(element,duration,onEnd) {
170   var a = new Fx.Tween(element, {duration:duration, onComplete:onEnd});
171   a.start('opacity', 1);
172 };
173
174 Rico.fadeOut=function(element,duration,onEnd) {
175   var a = new Fx.Tween(element, {duration:duration, onComplete:onEnd});
176   a.start('opacity', 0);
177 };
178
179 Rico.animate=function(element,options,properties) {
180   options.onComplete=options.onEnd;
181   var effect=new Fx.Morph(element,options);
182   effect.start(properties);
183   return effect;
184 };