Fix wheel scrolling on LiveGrid content
[infodrom/rico3] / ricoClient / js / rico2doj.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 dojo=='undefined') throw('This version of Rico requires the Dojo library');
16
17 var Rico = {
18   Lib: 'dojo',
19   LibVersion: dojo.version.toString(),
20   extend: dojo.mixin,
21   trim: dojo.trim,
22
23   tryFunctions: function() {
24     for (var i=0; i<arguments.length; i++) {
25       try {
26         return arguments[i]();
27       } catch(e){}
28     }
29     return null;
30   },
31
32   select: dojo.query,
33
34   eventBind: function(element, eventName, handler) {
35     handler.connection=dojo.connect(Rico.$(element), eventName, handler.object, handler.method);
36   },
37
38   eventUnbind: function(element, eventName, handler) {
39     dojo.disconnect(handler.connection);
40   },
41
42   eventElement: function(ev) {
43     return ev.target;
44   },
45
46   eventStop: dojo.stopEvent,
47
48   eventClient: function(ev) {
49     return {x:ev.pageX, y:ev.pageY};
50   },
51
52   eventHandle: function(object, method) {
53     return { object: object, method: method };
54   },
55
56   addClass: dojo.addClass,
57   removeClass: dojo.removeClass,
58   hasClass: dojo.hasClass,
59
60   getStyle: function(element, name) {
61     var camelCase = name.replace(/\-(\w)/g, function(all, letter){
62       return letter.toUpperCase();
63     });
64     return dojo.style(element,camelCase);
65   },
66
67   setStyle: dojo.style,
68
69   // tried to use dojo._abs - 1.3.0 was broken in webkit, nightlies broken on IE8
70   cumulativeOffset: function(element) {
71   //  var offset=dojo._abs(element);
72   //  return {top:offset.y, left:offset.x};
73     element=Rico.$(element);
74     var valueT = 0, valueL = 0;
75     do {
76       valueT += element.offsetTop  || 0;
77       valueL += element.offsetLeft || 0;
78       element = element.offsetParent;
79     } while (element);
80     return {left: valueL, top: valueT};
81   },
82
83   positionedOffset: function(element) {
84     element=Rico.$(element);
85     var p, valueT = 0, valueL = 0;
86     do {
87       valueT += element.offsetTop  || 0;
88       valueL += element.offsetLeft || 0;
89       element = element.offsetParent;
90       if (element) {
91         p = dojo.style(element,'position');
92         if (p == 'relative' || p == 'absolute') break;
93       }
94     } while (element);
95     return {left: valueL, top: valueT};
96   },
97
98   getDirectChildrenByTag: function(element, tagName) {
99     var kids = [];
100     var allKids = element.childNodes;
101     tagName=tagName.toLowerCase();
102     for( var i = 0 ; i < allKids.length ; i++ ) {
103       if ( allKids[i] && allKids[i].tagName && allKids[i].tagName.toLowerCase() == tagName )
104         kids.push(allKids[i]);
105     }
106     return kids;
107   },
108
109   // logic borrowed from Prototype
110   _getWinDimension: function(D) {
111     if (this.isWebKit && !document.evaluate) {
112       // Safari <3.0 needs self.innerWidth/Height
113       return self['inner' + D];
114     } else if (this.isOpera && parseFloat(window.opera.version()) < 9.5) {
115       // Opera <9.5 needs document.body.clientWidth/Height
116       return document.body['client' + D]
117     } else {
118       return document.documentElement['client' + D];
119     }
120   },
121
122   windowHeight: function() {
123     return this._getWinDimension('Height');
124   },
125
126   windowWidth: function() {
127     return this._getWinDimension('Width');
128   },
129
130   docScrollLeft: function() {
131     return dojo._docScroll().x;
132   },
133
134   docScrollTop: function() {
135     return dojo._docScroll().y;
136   },
137
138   // Animation
139
140   fadeIn: function(element,duration,onEnd) {
141     var a=dojo.fadeIn({node:element, duration:duration});
142     if (onEnd) dojo.connect(a,"onEnd",onEnd);
143     a.play();
144   },
145
146   fadeOut: function(element,duration,onEnd) {
147     var a=dojo.fadeOut({node:element, duration:duration});
148     if (onEnd) dojo.connect(a,"onEnd",onEnd);
149     a.play();
150   },
151
152   animate: function(element,options,properties) {
153     options.node=element;
154     options.properties=properties;
155     a=dojo.animateProperty(options);
156     a.play();
157   },
158
159   // AJAX
160
161   toQueryString: dojo.objectToQuery,
162
163   getJSON: function(xhr) { return dojo.fromJson(xhr.responseText); },
164
165   ajaxRequest: function(url,options) {
166     this.dojoSend(url,options);
167   }
168 };
169
170 Rico.ajaxRequest.prototype = {
171   dojoSend : function(url,options) {
172     this.onComplete=options.onComplete;
173     this.onSuccess=options.onSuccess;
174     this.onFailure=options.onFailure;
175     var self=this;
176     var dOptions = {
177       handle : function(response, ioArgs) { self.dojoComplete(response, ioArgs); },
178       error : function(response, ioArgs) { self.dojoError(response, ioArgs); },
179       load : function(response, ioArgs) { self.dojoLoad(response, ioArgs); },
180       url : url,
181       content : options.parameters,
182       form : options.form
183     }
184     var method=options.method.toUpperCase();
185     dojo.xhr(method, dOptions, method=='POST');
186   },
187
188   dojoComplete : function(dataORerror, ioArgs) {
189     if (this.onComplete) this.onComplete(ioArgs.xhr);
190   },
191
192   dojoError : function(response, ioArgs) {
193     if (this.onFailure) this.onFailure(ioArgs.xhr);
194   },
195
196   dojoLoad : function(response, ioArgs) {
197     if (this.onSuccess) this.onSuccess(ioArgs.xhr);
198   }
199 };
200
201 Rico.ajaxSubmit=function(form,url,options) {
202   options.form=form;
203   if (!options.method) options.method='post';
204   new Rico.ajaxRequest(url,options);
205 };