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