yllar
2006-12-16 77c28206a14b5bee3f3091f10cffd531bce5649c
commit | author | age
4e17e6 1 /*
T 2  +-----------------------------------------------------------------------+
3  | RoundCube common js library                                           |
4  |                                                                       |
5  | This file is part of the RoundCube web development suite              |
6b47de 6  | Copyright (C) 2005-2006, RoundCube Dev, - Switzerland                 |
30233b 7  | Licensed under the GNU GPL                                            |
4e17e6 8  |                                                                       |
T 9  +-----------------------------------------------------------------------+
10  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
11  +-----------------------------------------------------------------------+
5e3512 12  
T 13  $Id$
4e17e6 14 */
T 15
6b47de 16 // Constants
T 17 var CONTROL_KEY = 1;
18 var SHIFT_KEY = 2;
19 var CONTROL_SHIFT_KEY = 3;
4e17e6 20
6b47de 21
T 22 /**
23  * Default browser check class
24  * @construcotr
25  */
4e17e6 26 function roundcube_browser()
T 27   {
28   this.ver = parseFloat(navigator.appVersion);
29   this.appver = navigator.appVersion;
30   this.agent = navigator.userAgent;
31   this.name = navigator.appName;
32   this.vendor = navigator.vendor ? navigator.vendor : '';
33   this.vendver = navigator.vendorSub ? parseFloat(navigator.vendorSub) : 0;
34   this.product = navigator.product ? navigator.product : '';
35   this.platform = String(navigator.platform).toLowerCase();
36   this.lang = (navigator.language) ? navigator.language.substring(0,2) :
37               (navigator.browserLanguage) ? navigator.browserLanguage.substring(0,2) :
38               (navigator.systemLanguage) ? navigator.systemLanguage.substring(0,2) : 'en';
39
40   this.win = (this.platform.indexOf('win')>=0) ? true : false;
41   this.mac = (this.platform.indexOf('mac')>=0) ? true : false;
42   this.linux = (this.platform.indexOf('linux')>=0) ? true : false;
43   this.unix = (this.platform.indexOf('unix')>=0) ? true : false;
44
45   this.dom = document.getElementById ? true : false;
46   this.dom2 = (document.addEventListener && document.removeEventListener);
47
48   this.ie = (document.all) ? true : false;
49   this.ie4 = (this.ie && !this.dom);
50   this.ie5 = (this.dom && this.appver.indexOf('MSIE 5')>0);
51   this.ie6 = (this.dom && this.appver.indexOf('MSIE 6')>0);
52
53   this.mz = (this.dom && this.ver>=5);  // (this.dom && this.product=='Gecko')
54   this.ns = ((this.ver<5 && this.name=='Netscape') || (this.ver>=5 && this.vendor.indexOf('Netscape')>=0));
55   this.ns4 = (this.ns && parseInt(this.ver)==4);
56   this.ns6 = (this.ns && parseInt(this.vendver)==6);  // (this.mz && this.ns) ? true : false;
57   this.ns7 = (this.ns && parseInt(this.vendver)==7);  // this.agent.indexOf('Netscape/7')>0);
42b113 58   this.safari = (this.agent.toLowerCase().indexOf('safari')>0 || this.agent.toLowerCase().indexOf('applewebkit')>0);
T 59   this.konq   = (this.agent.toLowerCase().indexOf('konqueror')>0);
4e17e6 60
T 61   this.opera = (window.opera) ? true : false;
62   this.opera5 = (this.opera5 && this.agent.indexOf('Opera 5')>0) ? true : false;
63   this.opera6 = (this.opera && this.agent.indexOf('Opera 6')>0) ? true : false;
64   this.opera7 = (this.opera && this.agent.indexOf('Opera 7')>0) ? true : false;
65
66   if(this.opera && window.RegExp)
67     this.vendver = (/opera(\s|\/)([0-9\.]+)/i.test(navigator.userAgent)) ? parseFloat(RegExp.$2) : -1;
68   else if(!this.vendver && this.safari)
42b113 69     this.vendver = (/(safari|applewebkit)\/([0-9]+)/i.test(this.agent)) ? parseInt(RegExp.$2) : 0;
4e17e6 70   else if((!this.vendver && this.mz) || this.agent.indexOf('Camino')>0)
T 71     this.vendver = (/rv:([0-9\.]+)/.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
72   else if(this.ie && window.RegExp)
73     this.vendver = (/msie\s+([0-9\.]+)/i.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
42b113 74   else if(this.konq && window.RegExp)
T 75     this.vendver = (/khtml\/([0-9\.]+)/i.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
76
4e17e6 77
T 78   // get real language out of safari's user agent
79   if(this.safari && (/;\s+([a-z]{2})-[a-z]{2}\)/i.test(this.agent)))
80     this.lang = RegExp.$1;
81
82   this.dhtml = ((this.ie4 && this.win) || this.ie5 || this.ie6 || this.ns4 || this.mz);
83   this.layers = this.ns4;  // (document.layers);
84   this.div = (this.ie4 || this.dom);
85   this.vml = (this.win && this.ie && this.dom && !this.opera);
86   this.linkborder = (this.ie || this.mz);
87   this.rollover = (this.ver>=4 || (this.ns && this.ver>=3));  // (document.images) ? true : false;
88   this.pngalpha = (this.mz || (this.opera && this.vendver>=6) || (this.ie && this.mac && this.vendver>=5) ||
89                    (this.ie && this.win && this.vendver>=5.5) || this.safari);
90   this.opacity = (this.mz || (this.ie && this.vendver>=5.5 && !this.opera) || (this.safari && this.vendver>=100));
91   this.cookies = navigator.cookieEnabled;
a95e0e 92   
T 93   // test for XMLHTTP support
94   this.xmlhttp_test = function()
95     {
96     var activeX_test = new Function("try{var o=new ActiveXObject('Microsoft.XMLHTTP');return true;}catch(err){return false;}");
97     this.xmlhttp = (window.XMLHttpRequest || (window.ActiveXObject && activeX_test())) ? true : false;
98     return this.xmlhttp;
99     }
4e17e6 100   }
T 101
102
6b47de 103 // static functions for event handling
T 104 var rcube_event = {
105
106 /**
107  * returns modifier key (constants defined at top of file)
108  */
109 get_modifier: function(e)
110 {
111   var opcode = 0;
112   e = e || window.event;
113
114   if (bw.mac && e)
115     {
116     opcode += (e.metaKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
117     return opcode;    
118     }
119   if (e)
120     {
121     opcode += (e.ctrlKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
122     return opcode;
123     }
124 },
125
126 /**
127  * Return absolute mouse position of an event
128  */
129 get_mouse_pos: function(e)
130 {
131   if (!e) e = window.event;
132   var mX = (e.pageX) ? e.pageX : e.clientX;
133   var mY = (e.pageY) ? e.pageY : e.clientY;
134
135   if (document.body && document.all)
136   {
137     mX += document.body.scrollLeft;
138     mY += document.body.scrollTop;
139   }
140
141   return { x:mX, y:mY };
142 },
143
144 /**
145  * Add an object method as event listener to a certain element
146  */
147 add_listener: function(p)
148 {
149   if (!p.object || !p.method)  // not enough arguments
150     return;
151   if (!p.element)
152     p.element = document;
153
154   if (!p.object._rc_events)
155     p.object._rc_events = [];
156   
157   var key = p.event + '*' + p.method;
158   if (!p.object._rc_events[key])
159     p.object._rc_events[key] = function(e){ return p.object[p.method](e); };
160
161   if (p.element.addEventListener)
162     p.element.addEventListener(p.event, p.object._rc_events[key], false);
163   else if (p.element.attachEvent)
164     p.element.attachEvent('on'+p.event, p.object._rc_events[key]);
165   else
166     p.element['on'+p.event] = p.object._rc_events[key];
167 },
168
169 /**
170  * Remove event listener
171  */
172 remove_listener: function(p)
173 {
174   if (!p.element)
175     p.element = document;
176
177   var key = p.event + '*' + p.method;
178   if (p.object && p.object._rc_events && p.object._rc_events[key]) {
179     if (p.element.removeEventListener)
180       p.element.removeEventListener(p.event, p.object._rc_events[key], false);
181     else if (p.element.detachEvent)
182       p.element.detachEvent('on'+p.event, p.object._rc_events[key]);
183     else
184       p.element['on'+p.event] = null;
185   }
186 },
187
188 /**
189  * Prevent event propagation and bubbeling
190  */
191 cancel: function(evt)
192 {
193   var e = evt ? evt : window.event;
194   if (e.preventDefault)
195     e.preventDefault();
196   if (e.stopPropagation)
197     e.stopPropagation();
198
199   e.cancelBubble = true;
200   e.returnValue = false;
201   return false;
202 }
203
204 };
4e17e6 205
T 206
207 var rcube_layer_objects = new Array();
208
6b47de 209
T 210 /**
211  * RoundCube generic layer (floating box) class
212  *
213  * @constructor
214  */
4e17e6 215 function rcube_layer(id, attributes)
T 216   {
217   this.name = id;
218   
219   // create a new layer in the current document
220   this.create = function(arg)
221     {
222     var l = (arg.x) ? arg.x : 0;
223     var t = (arg.y) ? arg.y : 0;
224     var w = arg.width;
225     var h = arg.height;
226     var z = arg.zindex;
227     var vis = arg.vis;
228     var parent = arg.parent;
229     var obj;
230
231     obj = document.createElement('DIV');
232     with(obj)
233       {
234       id = this.name;
235       with(style)
236         {
237         position = 'absolute';
238         visibility = (vis) ? (vis==2) ? 'inherit' : 'visible' : 'hidden';
239         left = l+'px';
240         top = t+'px';
241         if(w) width = w+'px';
242         if(h) height = h+'px';
243         if(z) zIndex = z;
244         }
245       }
246       
247     if(parent) parent.appendChild(obj);
248     else document.body.appendChild(obj);
249
250     this.elm = obj;
251     };
252
253
254   // create new layer
255   if(attributes!=null)
256     {
257     this.create(attributes);
258     this.name = this.elm.id;
259     }
260   else  // just refer to the object
261     this.elm = document.getElementById(id);
262
263
264   if(!this.elm)
265     return false;
266
267
268   // ********* layer object properties *********
269
270   this.css = this.elm.style;
271   this.event = this.elm;
272   this.width = this.elm.offsetWidth;
273   this.height = this.elm.offsetHeight;
274   this.x = parseInt(this.elm.offsetLeft);
275   this.y = parseInt(this.elm.offsetTop);
276   this.visible = (this.css.visibility=='visible' || this.css.visibility=='show' || this.css.visibility=='inherit') ? true : false;
277
278   this.id = rcube_layer_objects.length;
279   this.obj = 'rcube_layer_objects['+this.id+']';
280   rcube_layer_objects[this.id] = this;
281
282
283   // ********* layer object methods *********
284
285
286   // move the layer to a specific position
287   this.move = function(x, y)
288     {
289     this.x = x;
290     this.y = y;
291     this.css.left = Math.round(this.x)+'px';
292     this.css.top = Math.round(this.y)+'px';
293     }
294
295
296   // move the layer for a specific step
297   this.shift = function(x,y)
298     {
299     x = Math.round(x*100)/100;
300     y = Math.round(y*100)/100;
301     this.move(this.x+x, this.y+y);
302     }
303
304
305   // change the layers width and height
306   this.resize = function(w,h)
307     {
308     this.css.width  = w+'px';
309     this.css.height = h+'px';
310     this.width = w;
311     this.height = h;
312     }
313
314
315   // cut the layer (top,width,height,left)
316   this.clip = function(t,w,h,l)
317     {
318     this.css.clip='rect('+t+' '+w+' '+h+' '+l+')';
319     this.clip_height = h;
320     this.clip_width = w;
321     }
322
323
324   // show or hide the layer
325   this.show = function(a)
326     {
327     if(a==1)
328       {
329       this.css.visibility = 'visible';
330       this.visible = true;
331       }
332     else if(a==2)
333       {
334       this.css.visibility = 'inherit';
335       this.visible = true;
336       }
337     else
338       {
339       this.css.visibility = 'hidden';
340       this.visible = false;
341       }
342     }
343
344
345   // write new content into a Layer
346   this.write = function(cont)
347     {
348     this.elm.innerHTML = cont;
349     }
350
351
352   // set the given color to the layer background
353   this.set_bgcolor = function(c)
354     {
355     if(!c || c=='#')
356       c = 'transparent';
357
358     this.css.backgroundColor = c;
359     }
360
361
362   // set the opacity of a layer to the given ammount (in %)
363   this.set_opacity = function(v)
364     {
365     if(!bw.opacity)
366       return;
367
368     var op = v<=1 ? Math.round(v*100) : parseInt(v);
369
370     if(bw.ie)
371       this.css.filter = 'alpha(opacity:'+op+')';
372     else if(bw.safari)
373       {
374       this.css.opacity = op/100;
375       this.css.KhtmlOpacity = op/100;
376       }
377     else if(bw.mz)
378       this.css.MozOpacity = op/100;
379     }
380   }
381
6b47de 382
10a699 383 // check if input is a valid email address
2c5762 384 // By Cal Henderson <cal@iamcal.com>
S 385 // http://code.iamcal.com/php/rfc822/
10a699 386 function rcube_check_email(input, inline)
T 387   {
388   if (input && window.RegExp)
389     {
2c5762 390     var no_ws_ctl    = "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]";
S 391     var alpha        = "[\\x41-\\x5a\\x61-\\x7a]";
392     var digit        = "[\\x30-\\x39]";
393     var cr        = "\\x0d";
394     var lf        = "\\x0a";
395     var crlf        = "(" + cr + lf + ")";
396
397     var obs_char    = "[\\x00-\\x09\\x0b\\x0c\\x0e-\\x7f]";
398     var obs_text    = "("+lf+"*"+cr+"*("+obs_char+lf+"*"+cr+"*)*)";
399     var text        = "([\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f]|"+obs_text+")";
400     var obs_qp        = "(\\x5c[\\x00-\\x7f])";
401     var quoted_pair    = "(\\x5c"+text+"|"+obs_qp+")";
402
403     var wsp        = "[\\x20\\x09]";
404     var obs_fws    = "("+wsp+"+("+crlf+wsp+"+)*)";
405     var fws        = "((("+wsp+"*"+crlf+")?"+wsp+"+)|"+obs_fws+")";
406     var ctext        = "("+no_ws_ctl+"|[\\x21-\\x27\\x2A-\\x5b\\x5d-\\x7e])";
407     var ccontent    = "("+ctext+"|"+quoted_pair+")";
408     var comment    = "(\\x28("+fws+"?"+ccontent+")*"+fws+"?\\x29)";
409     var cfws        = "(("+fws+"?"+comment+")*("+fws+"?"+comment+"|"+fws+"))";
410     var cfws        = fws+"*";
411
412     var atext        = "("+alpha+"|"+digit+"|[\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2e\\x3d\\x3f\\x5e\\x5f\\x60\\x7b-\\x7e])";
413     var atom        = "("+cfws+"?"+atext+"+"+cfws+"?)";
414
415     var qtext        = "("+no_ws_ctl+"|[\\x21\\x23-\\x5b\\x5d-\\x7e])";
416     var qcontent    = "("+qtext+"|"+quoted_pair+")";
417     var quoted_string    = "("+cfws+"?\\x22("+fws+"?"+qcontent+")*"+fws+"?\\x22"+cfws+"?)";
418     var word        = "("+atom+"|"+quoted_string+")";
419
420     var obs_local_part    = "("+word+"(\\x2e"+word+")*)";
421     var obs_domain    = "("+atom+"(\\x2e"+atom+")*)";
422
423     var dot_atom_text    = "("+atext+"+(\\x2e"+atext+"+)*)";
424     var dot_atom    = "("+cfws+"?"+dot_atom_text+cfws+"?)";
425
426     var dtext        = "("+no_ws_ctl+"|[\\x21-\\x5a\\x5e-\\x7e])";
427     var dcontent    = "("+dtext+"|"+quoted_pair+")";
428     var domain_literal    = "("+cfws+"?\\x5b("+fws+"?"+dcontent+")*"+fws+"?\\x5d"+cfws+"?)";
429
430     var local_part    = "("+dot_atom+"|"+quoted_string+"|"+obs_local_part+")";
431     var domain        = "("+dot_atom+"|"+domain_literal+"|"+obs_domain+")";
432     var addr_spec    = "("+local_part+"\\x40"+domain+")";
433
434     var reg1 = inline ? new RegExp(addr_spec, 'i') : new RegExp('^'+addr_spec+'$', 'i');
435     return reg1.test(input) ? true : false;
10a699 436     }
T 437   return false;
438   }
439   
4e17e6 440
T 441 // find a value in a specific array and returns the index
442 function find_in_array()
443   {
444   var args = find_in_array.arguments;
445   if(!args.length) return -1;
446
447   var haystack = typeof(args[0])=='object' ? args[0] : args.length>1 && typeof(args[1])=='object' ? args[1] : new Array();
448   var needle = typeof(args[0])!='object' ? args[0] : args.length>1 && typeof(args[1])!='object' ? args[1] : '';
449   var nocase = args.length==3 ? args[2] : false;
450
451   if(!haystack.length) return -1;
452
453   for(var i=0; i<haystack.length; i++)
454     if(nocase && haystack[i].toLowerCase()==needle.toLowerCase())
455       return i;
456     else if(haystack[i]==needle)
457       return i;
458
459   return -1;
460   }
461
462
4d4264 463 // make a string URL safe
T 464 function urlencode(str)
465 {
6b47de 466   return window.encodeURIComponent ? encodeURIComponent(str) : escape(str);
4d4264 467 }
T 468
469
4e17e6 470 // get any type of html objects by id/name
T 471 function rcube_find_object(id, d)
472   {
473   var n, f, obj, e;
474   if(!d) d = document;
475
476   if(d.getElementsByName && (e = d.getElementsByName(id)))
477     obj = e[0];
478   if(!obj && d.getElementById)
479     obj = d.getElementById(id);
480   if(!obj && d.all)
481     obj = d.all[id];
482
483   if(!obj && d.images.length)
484     obj = d.images[id];
485
486   if(!obj && d.forms.length)
487     for(f=0; f<d.forms.length; f++)
488       {
489       if(d.forms[f].name == id)
490         obj = d.forms[f];
491       else if(d.forms[f].elements[id])
492         obj = d.forms[f].elements[id];
493       }
494
495   if(!obj && d.layers)
496     {
497     if(d.layers[id]) obj = d.layers[id];
498     for(n=0; !obj && n<d.layers.length; n++)
499       obj = nex_get_object(id, d.layers[n].document);
500     }
501
502   return obj;
503   }
504
505
506 // return the absolute position of an object within the document
507 function rcube_get_object_pos(obj)
508   {
509   if(typeof(obj)=='string')
510     obj = nex_get_object(obj);
511
512   if(!obj) return {x:0, y:0};
513
514   var iX = (bw.layers) ? obj.x : obj.offsetLeft;
515   var iY = (bw.layers) ? obj.y : obj.offsetTop;
516
517   if(bw.ie || bw.mz)
518     {
519     var elm = obj.offsetParent;
520     while(elm && elm!=null)
521       {
522       iX += elm.offsetLeft;
523       iY += elm.offsetTop;
524       elm = elm.offsetParent;
525       }
526     }
527
528   if(bw.mac && bw.ie5) iX += document.body.leftMargin;
529   if(bw.mac && bw.ie5) iY += document.body.topMargin;
530
531   return {x:iX, y:iY};
532   }
dd53e2 533   
T 534
535 // cookie functions by GoogieSpell
536 function setCookie(name, value, expires, path, domain, secure)
537   {
538   var curCookie = name + "=" + escape(value) +
539       (expires ? "; expires=" + expires.toGMTString() : "") +
540       (path ? "; path=" + path : "") +
541       (domain ? "; domain=" + domain : "") +
542       (secure ? "; secure" : "");
543   document.cookie = curCookie;
544   }
545
546 function getCookie(name)
547   {
548   var dc = document.cookie;
549   var prefix = name + "=";
550   var begin = dc.indexOf("; " + prefix);
551   if (begin == -1)
552     {
553     begin = dc.indexOf(prefix);
554     if (begin != 0) return null;
555     }
556   else
557     begin += 2;  
558   var end = document.cookie.indexOf(";", begin);
559   if (end == -1)
560     end = dc.length;
561   return unescape(dc.substring(begin + prefix.length, end));
562   }
563
4e17e6 564
b4b081 565 var bw = new roundcube_browser();