2 * Copyright (c) 2009 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 Prototype=='undefined') throw('This version of Rico requires the Prototype library');
18 Rico.LibVersion=Prototype.Version;
19 Rico.extend=Object.extend;
20 Rico.tryFunctions=Try.these;
21 Rico.trim=function(s) { return s.strip(); };
23 Rico.toQueryString=Object.toQueryString;
24 Rico.ajaxRequest=Ajax.Request;
26 Rico.ajaxSubmit=function(form,url,options) {
27 options.parameters=Form.serialize(form);
28 if (!options.method) options.method='post';
29 url=url || form.action;
30 new Ajax.Request(url,options);
33 Rico.getJSON=function(xhr) { return xhr.responseJSON; };
35 Rico.select=function(selector, element) {
36 return element ? $(element).select(selector) : $$(selector);
39 Rico.eventBind=Event.observe;
40 Rico.eventUnbind=Event.stopObserving;
41 Rico.eventElement=Event.element;
42 Rico.eventStop=Event.stop;
43 Rico.eventClient=function(ev) {
44 return {x:ev.clientX, y:ev.clientY};
47 Rico.eventHandle=function(object, method) {
48 return object[method].bindAsEventListener(object);
51 Rico.addClass=Element.addClassName;
52 Rico.removeClass=Element.removeClassName;
53 Rico.hasClass=Element.hasClassName;
55 Rico.getStyle=Element.getStyle;
56 Rico.setStyle=Element.setStyle;
57 Rico.windowHeight=function() {
58 return document.viewport.getHeight();
60 Rico.windowWidth=function() {
61 return document.viewport.getWidth();
63 Rico.positionedOffset=function(element) {
64 return $(element).positionedOffset();
66 Rico.cumulativeOffset=function(element) {
67 return $(element).cumulativeOffset();
70 Rico.docScrollLeft=function() {
71 return document.viewport.getScrollOffsets().left;
74 Rico.docScrollTop=function() {
75 return document.viewport.getScrollOffsets().top;
78 Rico.getDirectChildrenByTag=function(element, tagName) {
79 tagName=tagName.toLowerCase();
80 return $(element).childElements().inject([],function(result,child) {
81 if (child.tagName && child.tagName.toLowerCase()==tagName) result.push(child);
88 Rico._animate=Class.create({
89 initialize: function(element,options,properties) {
90 this.element=$(element);
92 this.totSteps=(typeof options.duration =='number' ? options.duration : 500)/25;
96 for (var p in properties) {
97 curval=this.element.getStyle(p);
98 switch (typeof curval) {
100 if (m=curval.match(/(-?\d+\.?\d*)([a-zA-Z]*)$/)) {
101 this.properties.push({property:p, vStart:parseFloat(m[1]), vEnd:parseFloat(properties[p]), units:m.length > 2 ? m[2] : ''});
105 this.properties.push({property:p, vStart:curval, vEnd:parseFloat(properties[p]), units:''});
109 this.px=new PeriodicalExecuter(this.processStep.bind(this),0.025);
112 processStep: function() {
114 if (this.curStep >= this.totSteps) {
116 for (var i=0; i<this.properties.length; i++) {
117 this.setStyle(i,this.properties[i].vEnd);
119 if (this.options.onEnd) this.options.onEnd();
121 for (var i=0; i<this.properties.length; i++) {
122 var n=this.properties[i].vStart + (this.curStep / this.totSteps) * (this.properties[i].vEnd - this.properties[i].vStart);
128 setStyle: function(idx, newVal) {
129 if (this.properties[idx].units) newVal+=this.properties[idx].units;
131 styleParm[this.properties[idx].property]=newVal;
132 this.element.setStyle(styleParm);
136 Rico.animate=function(element,options,properties) {
137 var a=new Rico._animate(element,options,properties);
140 Rico.fadeIn=function(element,duration,onEnd) {
141 new Rico._animate(element, {duration:duration, onEnd:onEnd}, {opacity:1.0})
144 Rico.fadeOut=function(element,duration,onEnd) {
145 new Rico._animate(element, {duration:duration, onEnd:onEnd}, {opacity:0.0})