]> git.donarmstrong.com Git - roundcube.git/blob - program/js/common.js
Imported Upstream version 0.1~rc1~dfsg
[roundcube.git] / program / js / common.js
1 /*
2  +-----------------------------------------------------------------------+
3  | RoundCube common js library                                           |
4  |                                                                       |
5  | This file is part of the RoundCube web development suite              |
6  | Copyright (C) 2005-2006, RoundCube Dev, - Switzerland                 |
7  | Licensed under the GNU GPL                                            |
8  |                                                                       |
9  +-----------------------------------------------------------------------+
10  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
11  +-----------------------------------------------------------------------+
12  
13  $Id: common.js 543 2007-04-28 18:07:12Z thomasb $
14 */
15
16 // Constants
17 var CONTROL_KEY = 1;
18 var SHIFT_KEY = 2;
19 var CONTROL_SHIFT_KEY = 3;
20
21
22 /**
23  * Default browser check class
24  * @construcotr
25  */
26 function roundcube_browser()
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);
58   this.safari = (this.agent.toLowerCase().indexOf('safari')>0 || this.agent.toLowerCase().indexOf('applewebkit')>0);
59   this.konq   = (this.agent.toLowerCase().indexOf('konqueror')>0);
60
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)
69     this.vendver = (/(safari|applewebkit)\/([0-9]+)/i.test(this.agent)) ? parseInt(RegExp.$2) : 0;
70   else if((!this.vendver && this.mz) || this.agent.indexOf('Camino')>0)
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;
74   else if(this.konq && window.RegExp)
75     this.vendver = (/khtml\/([0-9\.]+)/i.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
76
77
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;
92   
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     }
100   }
101
102
103 // static functions for event handling
104 var rcube_event = {
105
106  /**
107   * returns the event key code
108   */
109  get_keycode: function(e)
110  {
111    e = e || window.event;
112    return e && e.keyCode ? e.keyCode : (e && e.which ? e.which : 0);
113  },
114
115 /**
116  * returns modifier key (constants defined at top of file)
117  */
118 get_modifier: function(e)
119 {
120   var opcode = 0;
121   e = e || window.event;
122
123   if (bw.mac && e)
124     {
125     opcode += (e.metaKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
126     return opcode;    
127     }
128   if (e)
129     {
130     opcode += (e.ctrlKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
131     return opcode;
132     }
133 },
134
135 /**
136  * Return absolute mouse position of an event
137  */
138 get_mouse_pos: function(e)
139 {
140   if (!e) e = window.event;
141   var mX = (e.pageX) ? e.pageX : e.clientX;
142   var mY = (e.pageY) ? e.pageY : e.clientY;
143
144   if (document.body && document.all)
145   {
146     mX += document.body.scrollLeft;
147     mY += document.body.scrollTop;
148   }
149
150   return { x:mX, y:mY };
151 },
152
153 /**
154  * Add an object method as event listener to a certain element
155  */
156 add_listener: function(p)
157 {
158   if (!p.object || !p.method)  // not enough arguments
159     return;
160   if (!p.element)
161     p.element = document;
162
163   if (!p.object._rc_events)
164     p.object._rc_events = [];
165   
166   var key = p.event + '*' + p.method;
167   if (!p.object._rc_events[key])
168     p.object._rc_events[key] = function(e){ return p.object[p.method](e); };
169
170   if (p.element.addEventListener)
171     p.element.addEventListener(p.event, p.object._rc_events[key], false);
172   else if (p.element.attachEvent)
173     p.element.attachEvent('on'+p.event, p.object._rc_events[key]);
174   else
175     p.element['on'+p.event] = p.object._rc_events[key];
176 },
177
178 /**
179  * Remove event listener
180  */
181 remove_listener: function(p)
182 {
183   if (!p.element)
184     p.element = document;
185
186   var key = p.event + '*' + p.method;
187   if (p.object && p.object._rc_events && p.object._rc_events[key]) {
188     if (p.element.removeEventListener)
189       p.element.removeEventListener(p.event, p.object._rc_events[key], false);
190     else if (p.element.detachEvent)
191       p.element.detachEvent('on'+p.event, p.object._rc_events[key]);
192     else
193       p.element['on'+p.event] = null;
194   }
195 },
196
197 /**
198  * Prevent event propagation and bubbeling
199  */
200 cancel: function(evt)
201 {
202   var e = evt ? evt : window.event;
203   if (e.preventDefault)
204     e.preventDefault();
205   if (e.stopPropagation)
206     e.stopPropagation();
207
208   e.cancelBubble = true;
209   e.returnValue = false;
210   return false;
211 }
212
213 };
214
215
216 var rcube_layer_objects = new Array();
217
218
219 /**
220  * RoundCube generic layer (floating box) class
221  *
222  * @constructor
223  */
224 function rcube_layer(id, attributes)
225   {
226   this.name = id;
227   
228   // create a new layer in the current document
229   this.create = function(arg)
230     {
231     var l = (arg.x) ? arg.x : 0;
232     var t = (arg.y) ? arg.y : 0;
233     var w = arg.width;
234     var h = arg.height;
235     var z = arg.zindex;
236     var vis = arg.vis;
237     var parent = arg.parent;
238     var obj;
239
240     obj = document.createElement('DIV');
241     with(obj)
242       {
243       id = this.name;
244       with(style)
245         {
246         position = 'absolute';
247         visibility = (vis) ? (vis==2) ? 'inherit' : 'visible' : 'hidden';
248         left = l+'px';
249         top = t+'px';
250         if(w) width = w+'px';
251         if(h) height = h+'px';
252         if(z) zIndex = z;
253         }
254       }
255       
256     if(parent) parent.appendChild(obj);
257     else document.body.appendChild(obj);
258
259     this.elm = obj;
260     };
261
262
263   // create new layer
264   if(attributes!=null)
265     {
266     this.create(attributes);
267     this.name = this.elm.id;
268     }
269   else  // just refer to the object
270     this.elm = document.getElementById(id);
271
272
273   if(!this.elm)
274     return false;
275
276
277   // ********* layer object properties *********
278
279   this.css = this.elm.style;
280   this.event = this.elm;
281   this.width = this.elm.offsetWidth;
282   this.height = this.elm.offsetHeight;
283   this.x = parseInt(this.elm.offsetLeft);
284   this.y = parseInt(this.elm.offsetTop);
285   this.visible = (this.css.visibility=='visible' || this.css.visibility=='show' || this.css.visibility=='inherit') ? true : false;
286
287   this.id = rcube_layer_objects.length;
288   this.obj = 'rcube_layer_objects['+this.id+']';
289   rcube_layer_objects[this.id] = this;
290
291
292   // ********* layer object methods *********
293
294
295   // move the layer to a specific position
296   this.move = function(x, y)
297     {
298     this.x = x;
299     this.y = y;
300     this.css.left = Math.round(this.x)+'px';
301     this.css.top = Math.round(this.y)+'px';
302     }
303
304
305   // move the layer for a specific step
306   this.shift = function(x,y)
307     {
308     x = Math.round(x*100)/100;
309     y = Math.round(y*100)/100;
310     this.move(this.x+x, this.y+y);
311     }
312
313
314   // change the layers width and height
315   this.resize = function(w,h)
316     {
317     this.css.width  = w+'px';
318     this.css.height = h+'px';
319     this.width = w;
320     this.height = h;
321     }
322
323
324   // cut the layer (top,width,height,left)
325   this.clip = function(t,w,h,l)
326     {
327     this.css.clip='rect('+t+' '+w+' '+h+' '+l+')';
328     this.clip_height = h;
329     this.clip_width = w;
330     }
331
332
333   // show or hide the layer
334   this.show = function(a)
335     {
336     if(a==1)
337       {
338       this.css.visibility = 'visible';
339       this.visible = true;
340       }
341     else if(a==2)
342       {
343       this.css.visibility = 'inherit';
344       this.visible = true;
345       }
346     else
347       {
348       this.css.visibility = 'hidden';
349       this.visible = false;
350       }
351     }
352
353
354   // write new content into a Layer
355   this.write = function(cont)
356     {
357     this.elm.innerHTML = cont;
358     }
359
360
361   // set the given color to the layer background
362   this.set_bgcolor = function(c)
363     {
364     if(!c || c=='#')
365       c = 'transparent';
366
367     this.css.backgroundColor = c;
368     }
369
370
371   // set the opacity of a layer to the given ammount (in %)
372   this.set_opacity = function(v)
373     {
374     if(!bw.opacity)
375       return;
376
377     var op = v<=1 ? Math.round(v*100) : parseInt(v);
378
379     if(bw.ie)
380       this.css.filter = 'alpha(opacity:'+op+')';
381     else if(bw.safari)
382       {
383       this.css.opacity = op/100;
384       this.css.KhtmlOpacity = op/100;
385       }
386     else if(bw.mz)
387       this.css.MozOpacity = op/100;
388     }
389   }
390
391
392 // check if input is a valid email address
393 // By Cal Henderson <cal@iamcal.com>
394 // http://code.iamcal.com/php/rfc822/
395 function rcube_check_email(input, inline)
396   {
397   if (input && window.RegExp)
398     {
399     var no_ws_ctl    = "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]";
400     var alpha        = "[\\x41-\\x5a\\x61-\\x7a]";
401     var digit        = "[\\x30-\\x39]";
402     var cr        = "\\x0d";
403     var lf        = "\\x0a";
404     var crlf        = "(" + cr + lf + ")";
405
406     var obs_char    = "[\\x00-\\x09\\x0b\\x0c\\x0e-\\x7f]";
407     var obs_text    = "("+lf+"*"+cr+"*("+obs_char+lf+"*"+cr+"*)*)";
408     var text        = "([\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f]|"+obs_text+")";
409     var obs_qp        = "(\\x5c[\\x00-\\x7f])";
410     var quoted_pair    = "(\\x5c"+text+"|"+obs_qp+")";
411
412     var wsp        = "[\\x20\\x09]";
413     var obs_fws    = "("+wsp+"+("+crlf+wsp+"+)*)";
414     var fws        = "((("+wsp+"*"+crlf+")?"+wsp+"+)|"+obs_fws+")";
415     var ctext        = "("+no_ws_ctl+"|[\\x21-\\x27\\x2A-\\x5b\\x5d-\\x7e])";
416     var ccontent    = "("+ctext+"|"+quoted_pair+")";
417     var comment    = "(\\x28("+fws+"?"+ccontent+")*"+fws+"?\\x29)";
418     var cfws        = "(("+fws+"?"+comment+")*("+fws+"?"+comment+"|"+fws+"))";
419     var cfws        = fws+"*";
420
421     var atext        = "("+alpha+"|"+digit+"|[\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2e\\x3d\\x3f\\x5e\\x5f\\x60\\x7b-\\x7e])";
422     var atom        = "("+cfws+"?"+atext+"+"+cfws+"?)";
423
424     var qtext        = "("+no_ws_ctl+"|[\\x21\\x23-\\x5b\\x5d-\\x7e])";
425     var qcontent    = "("+qtext+"|"+quoted_pair+")";
426     var quoted_string    = "("+cfws+"?\\x22("+fws+"?"+qcontent+")*"+fws+"?\\x22"+cfws+"?)";
427     var word        = "("+atom+"|"+quoted_string+")";
428
429     var obs_local_part    = "("+word+"(\\x2e"+word+")*)";
430     var obs_domain    = "("+atom+"(\\x2e"+atom+")*)";
431
432     var dot_atom_text    = "("+atext+"+(\\x2e"+atext+"+)*)";
433     var dot_atom    = "("+cfws+"?"+dot_atom_text+cfws+"?)";
434
435     var dtext        = "("+no_ws_ctl+"|[\\x21-\\x5a\\x5e-\\x7e])";
436     var dcontent    = "("+dtext+"|"+quoted_pair+")";
437     var domain_literal    = "("+cfws+"?\\x5b("+fws+"?"+dcontent+")*"+fws+"?\\x5d"+cfws+"?)";
438
439     var local_part    = "("+dot_atom+"|"+quoted_string+"|"+obs_local_part+")";
440     var domain        = "("+dot_atom+"|"+domain_literal+"|"+obs_domain+")";
441     var addr_spec    = "("+local_part+"\\x40"+domain+")";
442
443     var reg1 = inline ? new RegExp(addr_spec, 'i') : new RegExp('^'+addr_spec+'$', 'i');
444     return reg1.test(input) ? true : false;
445     }
446   return false;
447   }
448   
449
450 // find a value in a specific array and returns the index
451 function find_in_array()
452   {
453   var args = find_in_array.arguments;
454   if(!args.length) return -1;
455
456   var haystack = typeof(args[0])=='object' ? args[0] : args.length>1 && typeof(args[1])=='object' ? args[1] : new Array();
457   var needle = typeof(args[0])!='object' ? args[0] : args.length>1 && typeof(args[1])!='object' ? args[1] : '';
458   var nocase = args.length==3 ? args[2] : false;
459
460   if(!haystack.length) return -1;
461
462   for(var i=0; i<haystack.length; i++)
463     if(nocase && haystack[i].toLowerCase()==needle.toLowerCase())
464       return i;
465     else if(haystack[i]==needle)
466       return i;
467
468   return -1;
469   }
470
471
472 // make a string URL safe
473 function urlencode(str)
474 {
475   return window.encodeURIComponent ? encodeURIComponent(str) : escape(str);
476 }
477
478
479 // get any type of html objects by id/name
480 function rcube_find_object(id, d)
481   {
482   var n, f, obj, e;
483   if(!d) d = document;
484
485   if(d.getElementsByName && (e = d.getElementsByName(id)))
486     obj = e[0];
487   if(!obj && d.getElementById)
488     obj = d.getElementById(id);
489   if(!obj && d.all)
490     obj = d.all[id];
491
492   if(!obj && d.images.length)
493     obj = d.images[id];
494
495   if(!obj && d.forms.length)
496     for(f=0; f<d.forms.length; f++)
497       {
498       if(d.forms[f].name == id)
499         obj = d.forms[f];
500       else if(d.forms[f].elements[id])
501         obj = d.forms[f].elements[id];
502       }
503
504   if(!obj && d.layers)
505     {
506     if(d.layers[id]) obj = d.layers[id];
507     for(n=0; !obj && n<d.layers.length; n++)
508       obj = rcube_find_object(id, d.layers[n].document);
509     }
510
511   return obj;
512   }
513
514
515 // return the absolute position of an object within the document
516 function rcube_get_object_pos(obj)
517   {
518   if(typeof(obj)=='string')
519     obj = rcube_find_object(obj);
520
521   if(!obj) return {x:0, y:0};
522
523   var iX = (bw.layers) ? obj.x : obj.offsetLeft;
524   var iY = (bw.layers) ? obj.y : obj.offsetTop;
525
526   if(bw.ie || bw.mz)
527     {
528     var elm = obj.offsetParent;
529     while(elm && elm!=null)
530       {
531       iX += elm.offsetLeft;
532       iY += elm.offsetTop;
533       elm = elm.offsetParent;
534       }
535     }
536
537   //if(bw.mac && bw.ie5) iX += document.body.leftMargin;
538   //if(bw.mac && bw.ie5) iY += document.body.topMargin;
539
540   return {x:iX, y:iY};
541   }
542
543
544 /**
545  * Return the currently applied value of a css property
546  *
547  * @param {Object} html_element  Node reference
548  * @param {String} css_property  Property name to read in Javascript notation (eg. 'textAlign')
549  * @param {String} mozilla_equivalent  Equivalent property name in CSS notation (eg. 'text-align')
550  * @return CSS property value
551  * @type String
552  */
553 function get_elements_computed_style(html_element, css_property, mozilla_equivalent)
554   {
555   if (arguments.length==2)
556     mozilla_equivalent = css_property;
557
558   var el = html_element;
559   if (typeof(html_element)=='string')
560     el = rcube_find_object(html_element);
561
562   if (el && el.currentStyle)
563     return el.currentStyle[css_property];
564   else if (el && document.defaultView && document.defaultView.getComputedStyle)
565     return document.defaultView.getComputedStyle(el, null).getPropertyValue(mozilla_equivalent);
566   else
567     return false;
568   }
569   
570
571 // cookie functions by GoogieSpell
572 function setCookie(name, value, expires, path, domain, secure)
573   {
574   var curCookie = name + "=" + escape(value) +
575       (expires ? "; expires=" + expires.toGMTString() : "") +
576       (path ? "; path=" + path : "") +
577       (domain ? "; domain=" + domain : "") +
578       (secure ? "; secure" : "");
579   document.cookie = curCookie;
580   }
581
582 roundcube_browser.prototype.set_cookie = setCookie;
583
584 function getCookie(name)
585   {
586   var dc = document.cookie;
587   var prefix = name + "=";
588   var begin = dc.indexOf("; " + prefix);
589   if (begin == -1)
590     {
591     begin = dc.indexOf(prefix);
592     if (begin != 0) return null;
593     }
594   else
595     begin += 2;  
596   var end = document.cookie.indexOf(";", begin);
597   if (end == -1)
598     end = dc.length;
599   return unescape(dc.substring(begin + prefix.length, end));
600   }
601
602 roundcube_browser.prototype.get_cookie = getCookie;
603
604
605 // tiny replacement for Firebox functionality
606 function rcube_console()
607 {
608   this.box = rcube_find_object('console');
609   
610   this.log = function(msg)
611   {
612     if (this.box)
613       this.box.value += str+'\n--------------------------------------\n';
614   };
615   
616   this.reset = function()
617   {
618     if (this.box)
619       this.box.value = '';
620   };
621 }
622
623 var bw = new roundcube_browser();
624
625 if (!window.console)
626   console = new rcube_console();