Moved rico.js to minsrc and is now part of rico_min.js. Files are now sent to the...
[infodrom/rico3] / ricoClient / js / rico2glo.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 glow=='undefined') throw('This version of Rico requires the glow library');
16
17 var Rico = {
18   Lib: 'Glow',
19   LibVersion: glow.VERSION,
20   extend: glow.lang.apply,
21   trim: glow.lang.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   _g: function(element) {
31     if (typeof element=='string')
32       element = document.getElementById(element);
33     return glow.dom.get(element);
34   },
35
36   select: function(selector, element) {
37     return element ? this._g(element).get(selector) : glow.dom.get(selector);
38   },
39
40   eventBind: function(element, eventName, handler) {
41     handler.id=glow.events.addListener(Rico.$(element), eventName, handler.object[handler.method], handler.object);
42   },
43
44   eventUnbind: function(element, eventName, handler) {
45     glow.events.removeListener(handler.id);
46   },
47
48   eventHandle: function(object, method) {
49     return { object: object, method: method };
50   },
51
52   eventElement: function(ev) {
53     return ev.source;
54   },
55
56   eventClient: function(ev) {
57     return {x:ev.pageX - document.body.scrollLeft - document.documentElement.scrollLeft,
58             y:ev.pageY - document.body.scrollTop - document.documentElement.scrollTop};
59   },
60
61   eventStop: function(ev) {
62     ev.preventDefault();
63     ev.stopPropagation();
64   },
65
66   eventKey: function(ev) {
67     return ev.keyCode;
68   },
69
70   eventLeftClick: function(ev) {
71     return ev.button==0;
72   },
73
74   addClass: function(element, className) {
75     return this._g(element).addClass(className);
76   },
77
78   removeClass: function(element, className) {
79     return this._g(element).removeClass(className);
80   },
81
82   hasClass: function(element, className) {
83     return this._g(element).hasClass(className);
84   },
85
86 /*
87 ran into bugs on FF and Safari with native Glow function
88 getStyle=function(element, property) {
89   return this._g(element).css(property);
90 };
91
92 Use a modified version of Prototype's method
93 */
94   getStyle: function(element, style) {
95     element = Rico.$(element);
96     var camelCase = style.replace(/\-(\w)/g, function(all, letter){
97       return letter.toUpperCase();
98     });
99     style = style == 'float' ? 'cssFloat' : camelCase;
100     var value = element.style[style];
101     if (!value || value == 'auto') {
102       if (element.currentStyle) {
103         value=element.currentStyle[style];
104       } else if (document.defaultView) {
105         var css = document.defaultView.getComputedStyle(element, null);
106         value = css ? css[style] : null;
107       }
108     }
109     if (style == 'opacity') return value ? parseFloat(value) : 1.0;
110     return value == 'auto' ? null : value;
111   },
112
113
114   setStyle: function(element, properties) {
115     var elem=this._g(element);
116     for (var prop in properties) {
117       elem.css(prop,properties[prop])
118     }
119   },
120
121   /**
122    * @returns available height, excluding scrollbar & margin
123    */
124   windowHeight: function() {
125     return glow.dom.get(window).height();
126   },
127
128   /**
129    * @returns available width, excluding scrollbar & margin
130    */
131   windowWidth: function() {
132     return glow.dom.get(window).width();
133   },
134
135   positionedOffset: function(element) {
136     var p, valueT = 0, valueL = 0;
137     do {
138       valueT += element.offsetTop  || 0;
139       valueL += element.offsetLeft || 0;
140       element = element.offsetParent;
141       if (element) {
142         p = glow.dom.get(element).css('position');
143         if (p == 'relative' || p == 'absolute') break;
144       }
145     } while (element);
146     return {left: valueL, top: valueT};
147   },
148
149   cumulativeOffset: function(element) {
150     return this._g(element).offset();
151   },
152
153   _docElement: function() {
154     return (document.compatMode && document.compatMode.indexOf("CSS")!=-1) ? document.documentElement : document.getElementsByTagName("body")[0];
155   },
156
157   docScrollLeft: function() {
158     return Rico._docElement.scrollLeft || window.pageXOffset || 0;
159   },
160
161   docScrollTop: function() {
162     return Rico._docElement.scrollTop || window.pageYOffset || 0;
163   },
164
165   getDirectChildrenByTag: function(element, tagName) {
166     tagName=tagName.toLowerCase();
167     return this._g(element).children().filter(function(i) { return this.tagName && this.tagName.toLowerCase()==tagName; });
168   },
169
170   // Animation
171
172   fadeIn: function(element,duration,onEnd) {
173     glow.anim.fadeIn(this._g(element), duration/1000.0, {onComplete:onEnd});
174   },
175
176   fadeOut: function(element,duration,onEnd) {
177     glow.anim.fadeOut(this._g(element), duration/1000.0, {onComplete:onEnd});
178   };
179
180   animate: function(element,options,properties) {
181     var effect=glow.anim.css(this._g(element), options.duration/1000.0, properties);
182     glow.events.addListener(effect, "complete", options.onEnd);
183     effect.start();
184     return effect;
185   },
186
187   // AJAX
188
189   toQueryString: glow.data.encodeUrl,
190
191   getJSON: function(xhr) { return glow.data.decodeJson(xhr.responseText); },
192
193   ajaxRequest=function(url,options) {
194     this.glowSend(url,options);
195   }
196 };
197
198 Rico.ajaxRequest.prototype = {
199   glowSend : function(url,options) {
200     this.onComplete=options.onComplete;
201     this.onSuccess=options.onSuccess;
202     this.onFailure=options.onFailure;
203     var self=this;
204     options.onLoad=function(response) { self.glowLoad(response); };
205     options.onError=function(response) { self.glowError(response); };
206     options.useCache=true;
207     if (options.method.toLowerCase()=='post') {
208       glow.net.post(url,options.parameters,options);
209     } else {
210       glow.net.get(url+'?'+glow.data.encodeUrl(options.parameters),options);
211     }
212   },
213
214   glowError : function(response) {
215     if (this.onFailure) this.onFailure(response);
216     if (this.onComplete) this.onComplete(response.nativeResponse);
217   },
218
219   glowLoad : function(response) {
220     if (this.onSuccess) this.onSuccess(response.nativeResponse);
221     if (this.onComplete) this.onComplete(response.nativeResponse);
222   }
223 };
224
225 Rico.ajaxSubmit=function(form,url,options) {
226   options.parameters=glow.data.encodeUrl(this._g(form).val());
227   if (!options.method) options.method='post';
228   url=url || form.action;
229   new Rico.ajaxRequest(url,options);
230 };