]> git.donarmstrong.com Git - roundcube.git/blob - program/js/common.js
Imported Upstream version 0.3.1
[roundcube.git] / program / js / common.js
1 var CONTROL_KEY=1;
2 var SHIFT_KEY=2;
3 var CONTROL_SHIFT_KEY=3;
4 function roundcube_browser(){
5 this.ver=parseFloat(navigator.appVersion);
6 this.appver=navigator.appVersion;
7 this.agent=navigator.userAgent;
8 this.name=navigator.appName;
9 this.vendor=navigator.vendor?navigator.vendor:"";
10 this.vendver=navigator.vendorSub?parseFloat(navigator.vendorSub):0;
11 this.product=navigator.product?navigator.product:"";
12 this.platform=String(navigator.platform).toLowerCase();
13 this.lang=(navigator.language)?navigator.language.substring(0,2):(navigator.browserLanguage)?navigator.browserLanguage.substring(0,2):(navigator.systemLanguage)?navigator.systemLanguage.substring(0,2):"en";
14 this.win=(this.platform.indexOf("win")>=0)?true:false;
15 this.mac=(this.platform.indexOf("mac")>=0)?true:false;
16 this.linux=(this.platform.indexOf("linux")>=0)?true:false;
17 this.unix=(this.platform.indexOf("unix")>=0)?true:false;
18 this.dom=document.getElementById?true:false;
19 this.dom2=(document.addEventListener&&document.removeEventListener);
20 this.ie=(document.all)?true:false;
21 this.ie4=(this.ie&&!this.dom);
22 this.ie5=(this.dom&&this.appver.indexOf("MSIE 5")>0);
23 this.ie8=(this.dom&&this.appver.indexOf("MSIE 8")>0);
24 this.ie7=(this.dom&&this.appver.indexOf("MSIE 7")>0);
25 this.ie6=(this.dom&&!this.ie8&&!this.ie7&&this.appver.indexOf("MSIE 6")>0);
26 this.mz=(this.dom&&this.ver>=5);
27 this.ns=((this.ver<5&&this.name=="Netscape")||(this.ver>=5&&this.vendor.indexOf("Netscape")>=0));
28 this.ns6=(this.ns&&parseInt(this.vendver)==6);
29 this.ns7=(this.ns&&parseInt(this.vendver)==7);
30 this.safari=(this.agent.toLowerCase().indexOf("safari")>0||this.agent.toLowerCase().indexOf("applewebkit")>0);
31 this.konq=(this.agent.toLowerCase().indexOf("konqueror")>0);
32 this.opera=(window.opera)?true:false;
33 if(this.opera&&window.RegExp){
34 this.vendver=(/opera(\s|\/)([0-9\.]+)/i.test(navigator.userAgent))?parseFloat(RegExp.$2):-1;
35 }else{
36 if(!this.vendver&&this.safari){
37 this.vendver=(/(safari|applewebkit)\/([0-9]+)/i.test(this.agent))?parseInt(RegExp.$2):0;
38 }else{
39 if((!this.vendver&&this.mz)||this.agent.indexOf("Camino")>0){
40 this.vendver=(/rv:([0-9\.]+)/.test(this.agent))?parseFloat(RegExp.$1):0;
41 }else{
42 if(this.ie&&window.RegExp){
43 this.vendver=(/msie\s+([0-9\.]+)/i.test(this.agent))?parseFloat(RegExp.$1):0;
44 }else{
45 if(this.konq&&window.RegExp){
46 this.vendver=(/khtml\/([0-9\.]+)/i.test(this.agent))?parseFloat(RegExp.$1):0;
47 }
48 }
49 }
50 }
51 }
52 if(this.safari&&(/;\s+([a-z]{2})-[a-z]{2}\)/i.test(this.agent))){
53 this.lang=RegExp.$1;
54 }
55 this.dhtml=((this.ie4&&this.win)||this.ie5||this.ie6||this.ns4||this.mz);
56 this.vml=(this.win&&this.ie&&this.dom&&!this.opera);
57 this.pngalpha=(this.mz||(this.opera&&this.vendver>=6)||(this.ie&&this.mac&&this.vendver>=5)||(this.ie&&this.win&&this.vendver>=5.5)||this.safari);
58 this.opacity=(this.mz||(this.ie&&this.vendver>=5.5&&!this.opera)||(this.safari&&this.vendver>=100));
59 this.cookies=navigator.cookieEnabled;
60 this.xmlhttp_test=function(){
61 var _1=new Function("try{var o=new ActiveXObject('Microsoft.XMLHTTP');return true;}catch(err){return false;}");
62 this.xmlhttp=(window.XMLHttpRequest||(window.ActiveXObject&&_1()))?true:false;
63 return this.xmlhttp;
64 };
65 };
66 var rcube_event={get_target:function(e){
67 e=e||window.event;
68 return e&&e.target?e.target:e.srcElement;
69 },get_keycode:function(e){
70 e=e||window.event;
71 return e&&e.keyCode?e.keyCode:(e&&e.which?e.which:0);
72 },get_button:function(e){
73 e=e||window.event;
74 return e&&(typeof e.button!="undefined")?e.button:(e&&e.which?e.which:0);
75 },get_modifier:function(e){
76 var _6=0;
77 e=e||window.event;
78 if(bw.mac&&e){
79 _6+=(e.metaKey&&CONTROL_KEY)+(e.shiftKey&&SHIFT_KEY);
80 return _6;
81 }
82 if(e){
83 _6+=(e.ctrlKey&&CONTROL_KEY)+(e.shiftKey&&SHIFT_KEY);
84 return _6;
85 }
86 },get_mouse_pos:function(e){
87 if(!e){
88 e=window.event;
89 }
90 var mX=(e.pageX)?e.pageX:e.clientX;
91 var mY=(e.pageY)?e.pageY:e.clientY;
92 if(document.body&&document.all){
93 mX+=document.body.scrollLeft;
94 mY+=document.body.scrollTop;
95 }
96 if(e._offset){
97 mX+=e._offset.left;
98 mY+=e._offset.top;
99 }
100 return {x:mX,y:mY};
101 },add_listener:function(p){
102 if(!p.object||!p.method){
103 return;
104 }
105 if(!p.element){
106 p.element=document;
107 }
108 if(!p.object._rc_events){
109 p.object._rc_events=[];
110 }
111 var _b=p.event+"*"+p.method;
112 if(!p.object._rc_events[_b]){
113 p.object._rc_events[_b]=function(e){
114 return p.object[p.method](e);
115 };
116 }
117 if(p.element.addEventListener){
118 p.element.addEventListener(p.event,p.object._rc_events[_b],false);
119 }else{
120 if(p.element.attachEvent){
121 p.element.detachEvent("on"+p.event,p.object._rc_events[_b]);
122 p.element.attachEvent("on"+p.event,p.object._rc_events[_b]);
123 }else{
124 p.element["on"+p.event]=p.object._rc_events[_b];
125 }
126 }
127 },remove_listener:function(p){
128 if(!p.element){
129 p.element=document;
130 }
131 var _e=p.event+"*"+p.method;
132 if(p.object&&p.object._rc_events&&p.object._rc_events[_e]){
133 if(p.element.removeEventListener){
134 p.element.removeEventListener(p.event,p.object._rc_events[_e],false);
135 }else{
136 if(p.element.detachEvent){
137 p.element.detachEvent("on"+p.event,p.object._rc_events[_e]);
138 }else{
139 p.element["on"+p.event]=null;
140 }
141 }
142 }
143 },cancel:function(_f){
144 var e=_f?_f:window.event;
145 if(e.preventDefault){
146 e.preventDefault();
147 }
148 if(e.stopPropagation){
149 e.stopPropagation();
150 }
151 e.cancelBubble=true;
152 e.returnValue=false;
153 return false;
154 }};
155 function rcube_event_engine(){
156 this._events={};
157 };
158 rcube_event_engine.prototype={addEventListener:function(evt,_12,obj){
159 if(!this._events){
160 this._events={};
161 }
162 if(!this._events[evt]){
163 this._events[evt]=[];
164 }
165 var e={func:_12,obj:obj?obj:window};
166 this._events[evt][this._events[evt].length]=e;
167 },removeEventListener:function(evt,_16,obj){
168 if(typeof obj=="undefined"){
169 obj=window;
170 }
171 for(var h,i=0;this._events&&this._events[evt]&&i<this._events[evt].length;i++){
172 if((h=this._events[evt][i])&&h.func==_16&&h.obj==obj){
173 this._events[evt][i]=null;
174 }
175 }
176 },triggerEvent:function(evt,e){
177 var ret,h;
178 if(typeof e=="undefined"){
179 e=this;
180 }else{
181 if(typeof e=="object"){
182 e.event=evt;
183 }
184 }
185 if(this._events&&this._events[evt]&&!this._event_exec){
186 this._event_exec=true;
187 for(var i=0;i<this._events[evt].length;i++){
188 if((h=this._events[evt][i])){
189 if(typeof h.func=="function"){
190 ret=h.func.call?h.func.call(h.obj,e):h.func(e);
191 }else{
192 if(typeof h.obj[h.func]=="function"){
193 ret=h.obj[h.func](e);
194 }
195 }
196 if(typeof ret!="undefined"&&!ret){
197 break;
198 }
199 }
200 }
201 }
202 this._event_exec=false;
203 return ret;
204 }};
205 function rcube_layer(id,_20){
206 this.name=id;
207 this.create=function(arg){
208 var l=(arg.x)?arg.x:0;
209 var t=(arg.y)?arg.y:0;
210 var w=arg.width;
211 var h=arg.height;
212 var z=arg.zindex;
213 var vis=arg.vis;
214 var _28=arg.parent;
215 var obj;
216 obj=document.createElement("DIV");
217 with(obj){
218 id=this.name;
219 with(style){
220 position="absolute";
221 visibility=(vis)?(vis==2)?"inherit":"visible":"hidden";
222 left=l+"px";
223 top=t+"px";
224 if(w){
225 width=w.toString().match(/\%$/)?w:w+"px";
226 }
227 if(h){
228 height=h.toString().match(/\%$/)?h:h+"px";
229 }
230 if(z){
231 zIndex=z;
232 }
233 }
234 }
235 if(_28){
236 _28.appendChild(obj);
237 }else{
238 document.body.appendChild(obj);
239 }
240 this.elm=obj;
241 };
242 if(_20!=null){
243 this.create(_20);
244 this.name=this.elm.id;
245 }else{
246 this.elm=document.getElementById(id);
247 }
248 if(!this.elm){
249 return false;
250 }
251 this.css=this.elm.style;
252 this.event=this.elm;
253 this.width=this.elm.offsetWidth;
254 this.height=this.elm.offsetHeight;
255 this.x=parseInt(this.elm.offsetLeft);
256 this.y=parseInt(this.elm.offsetTop);
257 this.visible=(this.css.visibility=="visible"||this.css.visibility=="show"||this.css.visibility=="inherit")?true:false;
258 this.move=function(x,y){
259 this.x=x;
260 this.y=y;
261 this.css.left=Math.round(this.x)+"px";
262 this.css.top=Math.round(this.y)+"px";
263 };
264 this.resize=function(w,h){
265 this.css.width=w+"px";
266 this.css.height=h+"px";
267 this.width=w;
268 this.height=h;
269 };
270 this.show=function(a){
271 if(a==1){
272 this.css.visibility="visible";
273 this.visible=true;
274 }else{
275 if(a==2){
276 this.css.visibility="inherit";
277 this.visible=true;
278 }else{
279 this.css.visibility="hidden";
280 this.visible=false;
281 }
282 }
283 };
284 this.write=function(_2f){
285 this.elm.innerHTML=_2f;
286 };
287 };
288 function rcube_check_email(_30,_31){
289 if(_30&&window.RegExp){
290 var _32="[^\\x0d\\x22\\x5c\\x80-\\xff]";
291 var _33="[^\\x0d\\x5b-\\x5d\\x80-\\xff]";
292 var _34="[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+";
293 var _35="\\x5c[\\x00-\\x7f]";
294 var _36="\\x5b("+_33+"|"+_35+")*\\x5d";
295 var _37="\\x22("+_32+"|"+_35+")*\\x22";
296 var _38="("+_34+"|"+_36+")";
297 var _39="("+_34+"|"+_37+")";
298 var _3a=_38+"(\\x2e"+_38+")*";
299 var _3b=_39+"(\\x2e"+_39+")*";
300 var _3c=_3b+"\\x40"+_3a;
301 var _3d="[,;s\n]";
302 var _3e=_31?new RegExp("(^|<|"+_3d+")"+_3c+"($|>|"+_3d+")","i"):new RegExp("^"+_3c+"$","i");
303 return _3e.test(_30)?true:false;
304 }
305 return false;
306 };
307 function find_in_array(){
308 var _3f=find_in_array.arguments;
309 if(!_3f.length){
310 return -1;
311 }
312 var _40=typeof (_3f[0])=="object"?_3f[0]:_3f.length>1&&typeof (_3f[1])=="object"?_3f[1]:new Array();
313 var _41=typeof (_3f[0])!="object"?_3f[0]:_3f.length>1&&typeof (_3f[1])!="object"?_3f[1]:"";
314 var _42=_3f.length==3?_3f[2]:false;
315 if(!_40.length){
316 return -1;
317 }
318 for(var i=0;i<_40.length;i++){
319 if(_42&&_40[i].toLowerCase()==_41.toLowerCase()){
320 return i;
321 }else{
322 if(_40[i]==_41){
323 return i;
324 }
325 }
326 }
327 return -1;
328 };
329 function urlencode(str){
330 return window.encodeURIComponent?encodeURIComponent(str):escape(str);
331 };
332 function rcube_find_object(id,d){
333 var n,f,obj,e;
334 if(!d){
335 d=document;
336 }
337 if(d.getElementsByName&&(e=d.getElementsByName(id))){
338 obj=e[0];
339 }
340 if(!obj&&d.getElementById){
341 obj=d.getElementById(id);
342 }
343 if(!obj&&d.all){
344 obj=d.all[id];
345 }
346 if(!obj&&d.images.length){
347 obj=d.images[id];
348 }
349 if(!obj&&d.forms.length){
350 for(f=0;f<d.forms.length;f++){
351 if(d.forms[f].name==id){
352 obj=d.forms[f];
353 }else{
354 if(d.forms[f].elements[id]){
355 obj=d.forms[f].elements[id];
356 }
357 }
358 }
359 }
360 if(!obj&&d.layers){
361 if(d.layers[id]){
362 obj=d.layers[id];
363 }
364 for(n=0;!obj&&n<d.layers.length;n++){
365 obj=rcube_find_object(id,d.layers[n].document);
366 }
367 }
368 return obj;
369 };
370 function rcube_mouse_is_over(ev,obj){
371 var _4d=rcube_event.get_mouse_pos(ev);
372 var pos=$(obj).offset();
373 return ((_4d.x>=pos.left)&&(_4d.x<(pos.left+obj.offsetWidth))&&(_4d.y>=pos.top)&&(_4d.y<(pos.top+obj.offsetHeight)));
374 };
375 function setCookie(_4f,_50,_51,_52,_53,_54){
376 var _55=_4f+"="+escape(_50)+(_51?"; expires="+_51.toGMTString():"")+(_52?"; path="+_52:"")+(_53?"; domain="+_53:"")+(_54?"; secure":"");
377 document.cookie=_55;
378 };
379 roundcube_browser.prototype.set_cookie=setCookie;
380 function getCookie(_56){
381 var dc=document.cookie;
382 var _58=_56+"=";
383 var _59=dc.indexOf("; "+_58);
384 if(_59==-1){
385 _59=dc.indexOf(_58);
386 if(_59!=0){
387 return null;
388 }
389 }else{
390 _59+=2;
391 }
392 var end=document.cookie.indexOf(";",_59);
393 if(end==-1){
394 end=dc.length;
395 }
396 return unescape(dc.substring(_59+_58.length,end));
397 };
398 roundcube_browser.prototype.get_cookie=getCookie;
399 function rcube_console(){
400 this.log=function(msg){
401 var box=rcube_find_object("dbgconsole");
402 if(box){
403 if(msg.charAt(msg.length-1)=="\n"){
404 msg+="--------------------------------------\n";
405 }else{
406 msg+="\n--------------------------------------\n";
407 }
408 if(bw.konq){
409 box.innerText+=msg;
410 box.value=box.innerText;
411 }else{
412 box.value+=msg;
413 }
414 }
415 };
416 this.reset=function(){
417 var box=rcube_find_object("dbgconsole");
418 if(box){
419 box.innerText=box.value="";
420 }
421 };
422 };
423 var bw=new roundcube_browser();
424 if(!window.console){
425 console=new rcube_console();
426 }
427 RegExp.escape=function(str){
428 return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g,"\\$1");
429 };
430 if(bw.ie){
431 document._getElementById=document.getElementById;
432 document.getElementById=function(id){
433 var i=0;
434 var o=document._getElementById(id);
435 if(!o||o.id!=id){
436 while((o=document.all[i])&&o.id!=id){
437 i++;
438 }
439 }
440 return o;
441 };
442 }
443