2 * Copyright (c) 2009-2011 Matt Brown
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 if (typeof glow=='undefined') throw('This version of Rico requires the glow library');
19 LibVersion: glow.VERSION,
20 extend: glow.lang.apply,
22 tryFunctions: function() {
23 for (var i=0; i<arguments.length; i++) {
25 return arguments[i]();
30 _g: function(element) {
31 if (typeof element=='string')
32 element = document.getElementById(element);
33 return glow.dom.get(element);
36 select: function(selector, element) {
37 return element ? this._g(element).get(selector) : glow.dom.get(selector);
40 eventBind: function(element, eventName, handler) {
41 handler.id=glow.events.addListener(Rico.$(element), eventName, handler.object[handler.method], handler.object);
44 eventUnbind: function(element, eventName, handler) {
45 glow.events.removeListener(handler.id);
48 eventHandle: function(object, method) {
49 return { object: object, method: method };
52 eventElement: function(ev) {
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};
61 eventStop: function(ev) {
66 eventKey: function(ev) {
70 eventLeftClick: function(ev) {
74 addClass: function(element, className) {
75 return this._g(element).addClass(className);
78 removeClass: function(element, className) {
79 return this._g(element).removeClass(className);
82 hasClass: function(element, className) {
83 return this._g(element).hasClass(className);
87 ran into bugs on FF and Safari with native Glow function
88 getStyle=function(element, property) {
89 return this._g(element).css(property);
92 Use a modified version of Prototype's method
94 getStyle: function(element, style) {
95 element = Rico.$(element);
96 var camelCase = style.replace(/\-(\w)/g, function(all, letter){
97 return letter.toUpperCase();
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;
109 if (style == 'opacity') return value ? parseFloat(value) : 1.0;
110 return value == 'auto' ? null : value;
114 setStyle: function(element, properties) {
115 var elem=this._g(element);
116 for (var prop in properties) {
117 elem.css(prop,properties[prop])
122 * @returns available height, excluding scrollbar & margin
124 windowHeight: function() {
125 return glow.dom.get(window).height();
129 * @returns available width, excluding scrollbar & margin
131 windowWidth: function() {
132 return glow.dom.get(window).width();
135 positionedOffset: function(element) {
136 var p, valueT = 0, valueL = 0;
138 valueT += element.offsetTop || 0;
139 valueL += element.offsetLeft || 0;
140 element = element.offsetParent;
142 p = glow.dom.get(element).css('position');
143 if (p == 'relative' || p == 'absolute') break;
146 return {left: valueL, top: valueT};
149 cumulativeOffset: function(element) {
150 return this._g(element).offset();
153 _docElement: function() {
154 return (document.compatMode && document.compatMode.indexOf("CSS")!=-1) ? document.documentElement : document.getElementsByTagName("body")[0];
157 docScrollLeft: function() {
158 return Rico._docElement.scrollLeft || window.pageXOffset || 0;
161 docScrollTop: function() {
162 return Rico._docElement.scrollTop || window.pageYOffset || 0;
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; });
172 fadeIn: function(element,duration,onEnd) {
173 glow.anim.fadeIn(this._g(element), duration/1000.0, {onComplete:onEnd});
176 fadeOut: function(element,duration,onEnd) {
177 glow.anim.fadeOut(this._g(element), duration/1000.0, {onComplete:onEnd});
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);
189 toQueryString: glow.data.encodeUrl,
191 getJSON: function(xhr) { return glow.data.decodeJson(xhr.responseText); },
193 ajaxRequest=function(url,options) {
194 this.glowSend(url,options);
198 Rico.ajaxRequest.prototype = {
199 glowSend : function(url,options) {
200 this.onComplete=options.onComplete;
201 this.onSuccess=options.onSuccess;
202 this.onFailure=options.onFailure;
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);
210 glow.net.get(url+'?'+glow.data.encodeUrl(options.parameters),options);
214 glowError : function(response) {
215 if (this.onFailure) this.onFailure(response);
216 if (this.onComplete) this.onComplete(response.nativeResponse);
219 glowLoad : function(response) {
220 if (this.onSuccess) this.onSuccess(response.nativeResponse);
221 if (this.onComplete) this.onComplete(response.nativeResponse);
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);