Fix wheel scrolling on LiveGrid content
[infodrom/rico3] / ricoClient / js / rico2pro.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 Prototype=='undefined') throw('This version of Rico requires the Prototype library');
16
17 var Rico = {
18   Lib: 'Prototype',
19   LibVersion: Prototype.Version,
20   extend: Object.extend,
21   tryFunctions: Try.these,
22   trim: function(s) { return s.strip(); },
23
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);
31   },
32
33   getJSON: function(xhr) { return xhr.responseJSON; },
34
35   select: function(selector, element) {
36     return element ? $(element).select(selector) : $$(selector);
37   },
38     
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};
45   },
46
47   eventHandle: function(object, method) {
48     return object[method].bindAsEventListener(object);
49   },
50
51   addClass: Element.addClassName,
52   removeClass: Element.removeClassName,
53   hasClass: Element.hasClassName,
54
55   getStyle: Element.getStyle,
56   setStyle: Element.setStyle,
57   windowHeight: function() {
58     return document.viewport.getHeight();
59   },
60   windowWidth: function() {
61     return document.viewport.getWidth();
62   },
63   positionedOffset: function(element) {
64     return $(element).positionedOffset();
65   },
66   cumulativeOffset: function(element) {
67     return $(element).cumulativeOffset();
68   },
69   docScrollLeft: function() {
70     return document.viewport.getScrollOffsets().left;
71   },
72   docScrollTop: function() {
73     return document.viewport.getScrollOffsets().top;
74   },
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);
79       return result;});
80   }
81 };
82
83 // Animation
84
85 Rico._animate=Class.create({
86   initialize: function(element,options,properties) {
87     this.element=$(element);
88     this.properties=[];
89     this.totSteps=(typeof options.duration =='number' ? options.duration : 500)/25;
90     this.options=options;
91     this.curStep=0;
92     var m,curval;
93     for (var p in properties) {
94       curval=this.element.getStyle(p);
95       switch (typeof curval) {
96         case 'string':
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] : ''});
99           }
100           break;
101         case 'number':
102           this.properties.push({property:p, vStart:curval, vEnd:parseFloat(properties[p]), units:''});
103           break;
104       }
105     }
106     this.px=new PeriodicalExecuter(this.processStep.bind(this),0.025);
107   },
108   
109   processStep: function() {
110     this.curStep++;
111     if (this.curStep >= this.totSteps) {
112       this.px.stop();
113       for (var i=0; i<this.properties.length; i++) {
114         this.setStyle(i,this.properties[i].vEnd);
115       }
116       if (this.options.onEnd) this.options.onEnd();
117     } else {
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);
120         this.setStyle(i,n);
121       }
122     }
123   },
124   
125   setStyle: function(idx, newVal) {
126     if (this.properties[idx].units) newVal+=this.properties[idx].units;
127     var styleParm={};
128     styleParm[this.properties[idx].property]=newVal;
129     this.element.setStyle(styleParm);
130   }
131 });
132
133 Rico.animate=function(element,options,properties) {
134   var a=new Rico._animate(element,options,properties);
135 };
136
137 Rico.fadeIn=function(element,duration,onEnd) {
138   new Rico._animate(element, {duration:duration, onEnd:onEnd}, {opacity:1.0})
139 };
140
141 Rico.fadeOut=function(element,duration,onEnd) {
142   new Rico._animate(element, {duration:duration, onEnd:onEnd}, {opacity:0.0})
143 };