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