resolving svn conflicts
[infodrom/rico3] / ricoClient / js / rico2ext.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 Ext=='undefined') throw('This version of Rico requires the Ext-core library');
16
17
18 Rico.Lib='Ext-core';
19 Rico.LibVersion=Ext.version;
20 Rico.extend=Ext.apply;
21 Rico.trim=function(s) { return s.replace(Ext.DomQuery.trimRe,''); };
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
32 Rico.select=Ext.query;
33   
34 Rico.eventBind=function(element, eventName, handler) {
35   Ext.EventManager.addListener(element, eventName, handler.object[handler.method], handler.object);
36 };
37
38 Rico.eventUnbind=function(element, eventName, handler) {
39   Ext.EventManager.removeListener(element, eventName, handler.object[handler.method], handler.object);
40 };
41
42 Rico.eventHandle=function(object, method) {
43   return { object: object, method: method };
44 };
45
46 Rico.eventElement=function(ev) {
47   return ev.target;
48 };
49
50 Rico.eventClient=function(ev) {
51   return {x:ev.browserEvent.clientX, y:ev.browserEvent.clientY};
52 };
53
54 Rico.eventStop=function(ev) {
55   ev.stopEvent();
56 };
57
58 Rico.eventRelatedTarget=function(ev) {
59   return ev.getRelatedTarget();
60 };
61   
62 Rico.eventKey=function(ev) {
63   return ev.getKey();
64 };
65   
66 Rico.eventLeftClick=function(ev) {
67   return ev.button===0;
68 };
69   
70 Rico.addClass=function(element, className) {
71   return Ext.get(element).addClass(className);
72 };
73
74 Rico.removeClass=function(element, className) {
75   return Ext.get(element).removeClass(className);
76 };
77
78 Rico.hasClass=function(element, className) {
79   return Ext.get(element).hasClass(className);
80 };
81   
82 Rico.getStyle=function(element, property) {
83   return Ext.get(element).getStyle(property);
84 };
85 Rico.setStyle=function(element, properties) {
86   return Ext.get(element).setStyle(properties);
87 };
88
89 // logic borrowed from Prototype
90 // Ext.lib.Dom.getViewportWidth/Height includes scrollbar in Gecko browsers
91 Rico._getWinDimension=function(D) {
92   if (this.isWebKit && !document.evaluate) {
93     // Safari <3.0 needs self.innerWidth/Height
94     return self['inner' + D];
95   } else if (this.isOpera && parseFloat(window.opera.version()) < 9.5) {
96     // Opera <9.5 needs document.body.clientWidth/Height
97     return document.body['client' + D]
98   } else {
99     return document.documentElement['client' + D];
100   }
101 };
102
103 Rico.windowHeight=function() {
104   return this._getWinDimension('Height');
105 };
106
107 Rico.windowWidth=function() {
108   return this._getWinDimension('Width');
109 };
110
111 Rico.cumulativeOffset=function(element) {
112   element=Rico.$(element);
113   var valueT = 0, valueL = 0;
114   do {
115     valueT += element.offsetTop  || 0;
116     valueL += element.offsetLeft || 0;
117     element = element.offsetParent;
118   } while (element);
119   return {left: valueL, top: valueT};
120 };
121
122 Rico.positionedOffset=function(element) {
123   element=Rico.$(element);
124   var p, valueT = 0, valueL = 0;
125   do {
126     valueT += element.offsetTop  || 0;
127     valueL += element.offsetLeft || 0;
128     element = element.offsetParent;
129     if (element) {
130       p = Ext.get(element).getStyle('position');
131       if (p == 'relative' || p == 'absolute') break;
132     }
133   } while (element);
134   return {left: valueL, top: valueT};
135 };
136
137 Rico.docScrollLeft=function() {
138   return Ext.get(document).getScroll().left;
139 };
140
141 Rico.docScrollTop=function() {
142   return Ext.get(document).getScroll().top;
143 };
144
145 Rico.getDirectChildrenByTag=function(element, tagName) {
146   var kids = [];
147   var allKids = element.childNodes;
148   tagName=tagName.toLowerCase();
149   for( var i = 0 ; i < allKids.length ; i++ ) {
150     if ( allKids[i] && allKids[i].tagName && allKids[i].tagName.toLowerCase() == tagName )
151       kids.push(allKids[i]);
152   }
153   return kids;
154 };
155
156 Rico.ajaxRequest=function(url,options) {
157   var extOptions = {
158     success : options.onSuccess || options.onComplete,
159     failure : options.onFailure || options.onComplete,
160     method : options.method.toUpperCase(),
161     url : url,
162     form : options.form,
163     params : options.parameters
164   }
165   Ext.Ajax.request(extOptions);
166 }
167
168 Rico.getJSON=function(xhr) { return window["eval"]("(" + xhr.responseText + ")"); };
169
170 Rico.ajaxSubmit=function(form,url,options) {
171   options.form=form;
172   if (!options.method) options.method='post';
173   Rico.ajaxRequest(url,options);
174 }
175 Rico.toQueryString=Ext.urlEncode;
176
177 // Animation
178
179 Rico.fadeIn=function(element,duration,onEnd) {
180   Ext.get(element).fadeIn({duration:duration/1000.0, callback: onEnd});
181 };
182
183 Rico.fadeOut=function(element,duration,onEnd) {
184   Ext.get(element).fadeOut({duration:duration/1000.0, callback: onEnd});
185 };
186
187 Rico.animate=function(element,options,properties) {
188   var opts={};
189   opts.callback=options.onEnd;
190   opts.duration=options.duration/1000.0;
191   opts.width=properties.width;
192   opts.height=properties.height;
193   opts.x=properties.left;
194   opts.y=properties.top;
195   opts.opacity=properties.opacity;
196   Ext.get(element).shift(opts);
197 };