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 Prototype=='undefined') throw('This version of Rico requires the Prototype library');
19 LibVersion: Prototype.Version,
20 extend: Object.extend,
21 tryFunctions: Try.these,
22 trim: function(s) { return s.strip(); },
24 toQueryString: Object.toQueryString,
25 ajaxRequest: Ajax.Request,
26 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 getJSON: function(xhr) { return xhr.responseJSON; },
35 select: function(selector, element) {
36 return element ? $(element).select(selector) : $$(selector);
39 eventBind: Event.observe,
40 eventUnbind: Event.stopObserving,
41 eventElement: Event.element,
42 eventStop: Event.stop,
43 eventClient: function(ev) {
44 return {x:ev.clientX, y:ev.clientY};
47 eventHandle: function(object, method) {
48 return object[method].bindAsEventListener(object);
51 addClass: Element.addClassName,
52 removeClass: Element.removeClassName,
53 hasClass: Element.hasClassName,
55 getStyle: Element.getStyle,
56 setStyle: Element.setStyle,
57 windowHeight: function() {
58 return document.viewport.getHeight();
60 windowWidth: function() {
61 return document.viewport.getWidth();
63 positionedOffset: function(element) {
64 return $(element).positionedOffset();
66 cumulativeOffset: function(element) {
67 return $(element).cumulativeOffset();
69 docScrollLeft: function() {
70 return document.viewport.getScrollOffsets().left;
72 docScrollTop: function() {
73 return document.viewport.getScrollOffsets().top;
75 getDirectChildrenByTag: function(element, tagName) {
76 tagName=tagName.toLowerCase();
77 return $(element).childElements().inject([],function(result,child) {
78 if (child.tagName && child.tagName.toLowerCase()==tagName) result.push(child);
85 Rico._animate=Class.create({
86 initialize: function(element,options,properties) {
87 this.element=$(element);
89 this.totSteps=(typeof options.duration =='number' ? options.duration : 500)/25;
93 for (var p in properties) {
94 curval=this.element.getStyle(p);
95 switch (typeof curval) {
97 if (m=curval.match(/(-?\d+\.?\d*)([a-zA-Z]*)$/)) {
98 this.properties.push({property:p, vStart:parseFloat(m[1]), vEnd:parseFloat(properties[p]), units:m.length > 2 ? m[2] : ''});
102 this.properties.push({property:p, vStart:curval, vEnd:parseFloat(properties[p]), units:''});
106 this.px=new PeriodicalExecuter(this.processStep.bind(this),0.025);
109 processStep: function() {
111 if (this.curStep >= this.totSteps) {
113 for (var i=0; i<this.properties.length; i++) {
114 this.setStyle(i,this.properties[i].vEnd);
116 if (this.options.onEnd) this.options.onEnd();
118 for (var i=0; i<this.properties.length; i++) {
119 var n=this.properties[i].vStart + (this.curStep / this.totSteps) * (this.properties[i].vEnd - this.properties[i].vStart);
125 setStyle: function(idx, newVal) {
126 if (this.properties[idx].units) newVal+=this.properties[idx].units;
128 styleParm[this.properties[idx].property]=newVal;
129 this.element.setStyle(styleParm);
133 Rico.animate=function(element,options,properties) {
134 var a=new Rico._animate(element,options,properties);
137 Rico.fadeIn=function(element,duration,onEnd) {
138 new Rico._animate(element, {duration:duration, onEnd:onEnd}, {opacity:1.0})
141 Rico.fadeOut=function(element,duration,onEnd) {
142 new Rico._animate(element, {duration:duration, onEnd:onEnd}, {opacity:0.0})