8ed4278280843615472bf19d69cf6e54b257b133
[infodrom/rico3] / ricoClient / js / rico2glo.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 glow=='undefined') throw('This version of Rico requires the glow library');
16
17
18 Rico.Lib='Glow';
19 Rico.LibVersion=glow.VERSION;
20 Rico.extend=glow.lang.apply;
21 Rico.trim=glow.lang.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 Rico._g=function(element) {
31   if (typeof element=='string')
32     element = document.getElementById(element);
33   return glow.dom.get(element);
34 };
35
36 Rico.select=function(selector, element) {
37   return element ? this._g(element).get(selector) : glow.dom.get(selector);
38 };
39   
40 Rico.eventBind=function(element, eventName, handler) {
41   handler.id=glow.events.addListener(Rico.$(element), eventName, handler.object[handler.method], handler.object);
42 };
43
44 Rico.eventUnbind=function(element, eventName, handler) {
45   glow.events.removeListener(handler.id);
46 };
47
48 Rico.eventHandle=function(object, method) {
49   return { object: object, method: method };
50 };
51
52 Rico.eventElement=function(ev) {
53   return ev.source;
54 };
55
56 Rico.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 Rico.eventStop=function(ev) {
62   ev.preventDefault();
63   ev.stopPropagation();
64 };
65
66 Rico.eventKey=function(ev) {
67   return ev.keyCode;
68 };
69
70 Rico.eventLeftClick=function(ev) {
71   return ev.button==0;
72 };
73   
74 Rico.addClass=function(element, className) {
75   return this._g(element).addClass(className);
76 };
77
78 Rico.removeClass=function(element, className) {
79   return this._g(element).removeClass(className);
80 };
81
82 Rico.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 Rico.getStyle=function(element, property) {
89   return this._g(element).css(property);
90 };
91
92 Use a modified version of Prototype's method
93 */
94 Rico.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 Rico.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 Rico.windowHeight=function() {
125   return glow.dom.get(window).height();
126 };
127
128 /**
129  * @returns available width, excluding scrollbar & margin
130  */
131 Rico.windowWidth=function() {
132   return glow.dom.get(window).width();
133 };
134
135 Rico.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 Rico.cumulativeOffset=function(element) {
150   return this._g(element).offset();
151 };
152
153 Rico._docElement=function() {
154   return (document.compatMode && document.compatMode.indexOf("CSS")!=-1) ? document.documentElement : document.getElementsByTagName("body")[0];
155 },
156
157 Rico.docScrollLeft=function() {
158   return Rico._docElement.scrollLeft || window.pageXOffset || 0;
159 };
160
161 Rico.docScrollTop=function() {
162   return Rico._docElement.scrollTop || window.pageYOffset || 0;
163 };
164
165 Rico.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 Rico.getJSON=function(xhr) { return glow.data.decodeJson(xhr.responseText); };
171
172 Rico.ajaxRequest=function(url,options) {
173   this.glowSend(url,options);
174 }
175
176 Rico.ajaxRequest.prototype = {
177   glowSend : function(url,options) {
178     this.onComplete=options.onComplete;
179     this.onSuccess=options.onSuccess;
180     this.onFailure=options.onFailure;
181     var self=this;
182     options.onLoad=function(response) { self.glowLoad(response); };
183     options.onError=function(response) { self.glowError(response); };
184     options.useCache=true;
185     if (options.method.toLowerCase()=='post') {
186       glow.net.post(url,options.parameters,options);
187     } else {
188       glow.net.get(url+'?'+glow.data.encodeUrl(options.parameters),options);
189     }
190   },
191   
192   glowError : function(response) {
193     if (this.onFailure) this.onFailure(response);
194     if (this.onComplete) this.onComplete(response.nativeResponse);
195   },
196   
197   glowLoad : function(response) {
198     if (this.onSuccess) this.onSuccess(response.nativeResponse);
199     if (this.onComplete) this.onComplete(response.nativeResponse);
200   }
201 }
202
203 Rico.ajaxSubmit=function(form,url,options) {
204   options.parameters=glow.data.encodeUrl(this._g(form).val());
205   if (!options.method) options.method='post';
206   url=url || form.action;
207   new Rico.ajaxRequest(url,options);
208 }
209 Rico.toQueryString=glow.data.encodeUrl;
210
211 // Animation
212
213 Rico.fadeIn=function(element,duration,onEnd) {
214   glow.anim.fadeIn(this._g(element), duration/1000.0, {onComplete:onEnd});
215 };
216
217 Rico.fadeOut=function(element,duration,onEnd) {
218   glow.anim.fadeOut(this._g(element), duration/1000.0, {onComplete:onEnd});
219 };
220
221 Rico.animate=function(element,options,properties) {
222   var effect=glow.anim.css(this._g(element), options.duration/1000.0, properties);
223   glow.events.addListener(effect, "complete", options.onEnd);
224   effect.start();
225   return effect;
226 };
227
228 Rico._bindLoadEvent();