alecpl
2008-10-09 e70b3b24fc8ac7b54a820ae87ce8f4af4043125e
commit | author | age
4e17e6 1 /*
T 2  +-----------------------------------------------------------------------+
3  | RoundCube common js library                                           |
4  |                                                                       |
5  | This file is part of the RoundCube web development suite              |
d39eec 6  | Copyright (C) 2005-2007, 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);
8b7f5a 52   this.ie7 = (this.dom && this.appver.indexOf('MSIE 7')>0);
4e17e6 53
T 54   this.mz = (this.dom && this.ver>=5);  // (this.dom && this.product=='Gecko')
55   this.ns = ((this.ver<5 && this.name=='Netscape') || (this.ver>=5 && this.vendor.indexOf('Netscape')>=0));
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
63   if(this.opera && window.RegExp)
64     this.vendver = (/opera(\s|\/)([0-9\.]+)/i.test(navigator.userAgent)) ? parseFloat(RegExp.$2) : -1;
65   else if(!this.vendver && this.safari)
42b113 66     this.vendver = (/(safari|applewebkit)\/([0-9]+)/i.test(this.agent)) ? parseInt(RegExp.$2) : 0;
4e17e6 67   else if((!this.vendver && this.mz) || this.agent.indexOf('Camino')>0)
T 68     this.vendver = (/rv:([0-9\.]+)/.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
69   else if(this.ie && window.RegExp)
70     this.vendver = (/msie\s+([0-9\.]+)/i.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
42b113 71   else if(this.konq && window.RegExp)
T 72     this.vendver = (/khtml\/([0-9\.]+)/i.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
73
4e17e6 74
T 75   // get real language out of safari's user agent
76   if(this.safari && (/;\s+([a-z]{2})-[a-z]{2}\)/i.test(this.agent)))
77     this.lang = RegExp.$1;
78
79   this.dhtml = ((this.ie4 && this.win) || this.ie5 || this.ie6 || this.ns4 || this.mz);
80   this.vml = (this.win && this.ie && this.dom && !this.opera);
81   this.pngalpha = (this.mz || (this.opera && this.vendver>=6) || (this.ie && this.mac && this.vendver>=5) ||
82                    (this.ie && this.win && this.vendver>=5.5) || this.safari);
83   this.opacity = (this.mz || (this.ie && this.vendver>=5.5 && !this.opera) || (this.safari && this.vendver>=100));
84   this.cookies = navigator.cookieEnabled;
a95e0e 85   
T 86   // test for XMLHTTP support
87   this.xmlhttp_test = function()
88     {
89     var activeX_test = new Function("try{var o=new ActiveXObject('Microsoft.XMLHTTP');return true;}catch(err){return false;}");
90     this.xmlhttp = (window.XMLHttpRequest || (window.ActiveXObject && activeX_test())) ? true : false;
91     return this.xmlhttp;
92     }
4e17e6 93   }
T 94
95
6b47de 96 // static functions for event handling
T 97 var rcube_event = {
98
a0ce2f 99 /**
T 100  * returns the event target element
101  */
102 get_target: function(e)
103 {
104   e = e || window.event;
105   return e && e.target ? e.target : e.srcElement;
106 },
107
108 /**
109  * returns the event key code
110  */
111 get_keycode: function(e)
112 {
113   e = e || window.event;
114   return e && e.keyCode ? e.keyCode : (e && e.which ? e.which : 0);
115 },
9bbb17 116
6b47de 117 /**
f89f03 118  * returns the event key code
T 119  */
120 get_button: function(e)
121 {
122   e = e || window.event;
123   return e && (typeof e.button != 'undefined') ? e.button : (e && e.which ? e.which : 0);
124 },
125
126 /**
6b47de 127  * returns modifier key (constants defined at top of file)
T 128  */
129 get_modifier: function(e)
130 {
131   var opcode = 0;
132   e = e || window.event;
133
134   if (bw.mac && e)
135     {
136     opcode += (e.metaKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
137     return opcode;    
138     }
139   if (e)
140     {
141     opcode += (e.ctrlKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
142     return opcode;
143     }
144 },
145
146 /**
147  * Return absolute mouse position of an event
148  */
149 get_mouse_pos: function(e)
150 {
151   if (!e) e = window.event;
152   var mX = (e.pageX) ? e.pageX : e.clientX;
153   var mY = (e.pageY) ? e.pageY : e.clientY;
154
155   if (document.body && document.all)
156   {
157     mX += document.body.scrollLeft;
158     mY += document.body.scrollTop;
159   }
160
161   return { x:mX, y:mY };
162 },
163
164 /**
165  * Add an object method as event listener to a certain element
166  */
167 add_listener: function(p)
168 {
169   if (!p.object || !p.method)  // not enough arguments
170     return;
171   if (!p.element)
172     p.element = document;
173
174   if (!p.object._rc_events)
175     p.object._rc_events = [];
176   
177   var key = p.event + '*' + p.method;
178   if (!p.object._rc_events[key])
179     p.object._rc_events[key] = function(e){ return p.object[p.method](e); };
180
181   if (p.element.addEventListener)
182     p.element.addEventListener(p.event, p.object._rc_events[key], false);
183   else if (p.element.attachEvent)
a265ab 184     {
T 185     // IE allows multiple events with the same function to be applied to the same object
186     // forcibly detach the event, then attach
187     p.element.detachEvent('on'+p.event, p.object._rc_events[key]);
6b47de 188     p.element.attachEvent('on'+p.event, p.object._rc_events[key]);
a265ab 189     }
6b47de 190   else
T 191     p.element['on'+p.event] = p.object._rc_events[key];
192 },
193
194 /**
195  * Remove event listener
196  */
197 remove_listener: function(p)
198 {
199   if (!p.element)
200     p.element = document;
201
202   var key = p.event + '*' + p.method;
203   if (p.object && p.object._rc_events && p.object._rc_events[key]) {
204     if (p.element.removeEventListener)
205       p.element.removeEventListener(p.event, p.object._rc_events[key], false);
206     else if (p.element.detachEvent)
207       p.element.detachEvent('on'+p.event, p.object._rc_events[key]);
208     else
209       p.element['on'+p.event] = null;
210   }
211 },
212
213 /**
214  * Prevent event propagation and bubbeling
215  */
216 cancel: function(evt)
217 {
218   var e = evt ? evt : window.event;
219   if (e.preventDefault)
220     e.preventDefault();
221   if (e.stopPropagation)
222     e.stopPropagation();
223
224   e.cancelBubble = true;
225   e.returnValue = false;
226   return false;
227 }
228
229 };
4e17e6 230
T 231
232 var rcube_layer_objects = new Array();
233
6b47de 234
T 235 /**
236  * RoundCube generic layer (floating box) class
237  *
238  * @constructor
239  */
4e17e6 240 function rcube_layer(id, attributes)
T 241   {
242   this.name = id;
243   
244   // create a new layer in the current document
245   this.create = function(arg)
246     {
247     var l = (arg.x) ? arg.x : 0;
248     var t = (arg.y) ? arg.y : 0;
249     var w = arg.width;
250     var h = arg.height;
251     var z = arg.zindex;
252     var vis = arg.vis;
253     var parent = arg.parent;
254     var obj;
255
256     obj = document.createElement('DIV');
e5686f 257
4e17e6 258     with(obj)
T 259       {
260       id = this.name;
261       with(style)
262         {
e5686f 263     position = 'absolute';
4e17e6 264         visibility = (vis) ? (vis==2) ? 'inherit' : 'visible' : 'hidden';
T 265         left = l+'px';
266         top = t+'px';
e5686f 267         if (w)
A 268       width = w.toString().match(/\%$/) ? w : w+'px';
269         if (h)
270       height = h.toString().match(/\%$/) ? h : h+'px';
4e17e6 271         if(z) zIndex = z;
e5686f 272     }
4e17e6 273       }
e5686f 274
A 275     if (parent)
276       parent.appendChild(obj);
277     else
278       document.body.appendChild(obj);
4e17e6 279
T 280     this.elm = obj;
281     };
282
283
284   // create new layer
285   if(attributes!=null)
286     {
287     this.create(attributes);
288     this.name = this.elm.id;
289     }
290   else  // just refer to the object
291     this.elm = document.getElementById(id);
292
293
294   if(!this.elm)
295     return false;
296
297
298   // ********* layer object properties *********
299
300   this.css = this.elm.style;
301   this.event = this.elm;
302   this.width = this.elm.offsetWidth;
303   this.height = this.elm.offsetHeight;
304   this.x = parseInt(this.elm.offsetLeft);
305   this.y = parseInt(this.elm.offsetTop);
306   this.visible = (this.css.visibility=='visible' || this.css.visibility=='show' || this.css.visibility=='inherit') ? true : false;
307
308   this.id = rcube_layer_objects.length;
309   this.obj = 'rcube_layer_objects['+this.id+']';
310   rcube_layer_objects[this.id] = this;
311
312
313   // ********* layer object methods *********
314
315
316   // move the layer to a specific position
317   this.move = function(x, y)
318     {
319     this.x = x;
320     this.y = y;
321     this.css.left = Math.round(this.x)+'px';
322     this.css.top = Math.round(this.y)+'px';
323     }
324
325
326   // move the layer for a specific step
327   this.shift = function(x,y)
328     {
329     x = Math.round(x*100)/100;
330     y = Math.round(y*100)/100;
331     this.move(this.x+x, this.y+y);
332     }
333
334
335   // change the layers width and height
336   this.resize = function(w,h)
337     {
338     this.css.width  = w+'px';
339     this.css.height = h+'px';
340     this.width = w;
341     this.height = h;
342     }
343
344
345   // cut the layer (top,width,height,left)
346   this.clip = function(t,w,h,l)
347     {
348     this.css.clip='rect('+t+' '+w+' '+h+' '+l+')';
349     this.clip_height = h;
350     this.clip_width = w;
351     }
352
353
354   // show or hide the layer
355   this.show = function(a)
356     {
357     if(a==1)
358       {
359       this.css.visibility = 'visible';
360       this.visible = true;
361       }
362     else if(a==2)
363       {
364       this.css.visibility = 'inherit';
365       this.visible = true;
366       }
367     else
368       {
369       this.css.visibility = 'hidden';
370       this.visible = false;
371       }
372     }
373
374
375   // write new content into a Layer
376   this.write = function(cont)
377     {
378     this.elm.innerHTML = cont;
379     }
380
381
382   // set the given color to the layer background
383   this.set_bgcolor = function(c)
384     {
385     if(!c || c=='#')
386       c = 'transparent';
387
388     this.css.backgroundColor = c;
389     }
390
391
392   // set the opacity of a layer to the given ammount (in %)
393   this.set_opacity = function(v)
394     {
395     if(!bw.opacity)
396       return;
397
398     var op = v<=1 ? Math.round(v*100) : parseInt(v);
399
400     if(bw.ie)
401       this.css.filter = 'alpha(opacity:'+op+')';
402     else if(bw.safari)
403       {
404       this.css.opacity = op/100;
405       this.css.KhtmlOpacity = op/100;
406       }
407     else if(bw.mz)
408       this.css.MozOpacity = op/100;
409     }
410   }
411
6b47de 412
10a699 413 // check if input is a valid email address
2c5762 414 // By Cal Henderson <cal@iamcal.com>
S 415 // http://code.iamcal.com/php/rfc822/
10a699 416 function rcube_check_email(input, inline)
T 417   {
418   if (input && window.RegExp)
419     {
f20cf0 420     var qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
T 421     var dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
422     var atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
423     var quoted_pair = '\\x5c[\\x00-\\x7f]';
424     var domain_literal = '\\x5b('+dtext+'|'+quoted_pair+')*\\x5d';
425     var quoted_string = '\\x22('+qtext+'|'+quoted_pair+')*\\x22';
426     var sub_domain = '('+atom+'|'+domain_literal+')';
427     var word = '('+atom+'|'+quoted_string+')';
428     var domain = sub_domain+'(\\x2e'+sub_domain+')*';
429     var local_part = word+'(\\x2e'+word+')*';
430     var addr_spec = local_part+'\\x40'+domain;
e581e7 431     var delim = '[,;\s\n]';
T 432     var reg1 = inline ? new RegExp('(^|<|'+delim+')'+addr_spec+'($|>|'+delim+')', 'i') : new RegExp('^'+addr_spec+'$', 'i');
2c5762 433     return reg1.test(input) ? true : false;
10a699 434     }
T 435   return false;
436   }
437   
4e17e6 438
T 439 // find a value in a specific array and returns the index
440 function find_in_array()
441   {
442   var args = find_in_array.arguments;
443   if(!args.length) return -1;
444
445   var haystack = typeof(args[0])=='object' ? args[0] : args.length>1 && typeof(args[1])=='object' ? args[1] : new Array();
446   var needle = typeof(args[0])!='object' ? args[0] : args.length>1 && typeof(args[1])!='object' ? args[1] : '';
447   var nocase = args.length==3 ? args[2] : false;
448
449   if(!haystack.length) return -1;
450
451   for(var i=0; i<haystack.length; i++)
452     if(nocase && haystack[i].toLowerCase()==needle.toLowerCase())
453       return i;
454     else if(haystack[i]==needle)
455       return i;
456
457   return -1;
458   }
459
460
4d4264 461 // make a string URL safe
T 462 function urlencode(str)
463 {
6b47de 464   return window.encodeURIComponent ? encodeURIComponent(str) : escape(str);
4d4264 465 }
T 466
467
4e17e6 468 // get any type of html objects by id/name
T 469 function rcube_find_object(id, d)
470   {
471   var n, f, obj, e;
472   if(!d) d = document;
473
474   if(d.getElementsByName && (e = d.getElementsByName(id)))
475     obj = e[0];
476   if(!obj && d.getElementById)
477     obj = d.getElementById(id);
478   if(!obj && d.all)
479     obj = d.all[id];
480
481   if(!obj && d.images.length)
482     obj = d.images[id];
483
484   if(!obj && d.forms.length)
485     for(f=0; f<d.forms.length; f++)
486       {
487       if(d.forms[f].name == id)
488         obj = d.forms[f];
489       else if(d.forms[f].elements[id])
490         obj = d.forms[f].elements[id];
491       }
492
493   if(!obj && d.layers)
494     {
495     if(d.layers[id]) obj = d.layers[id];
496     for(n=0; !obj && n<d.layers.length; n++)
86958f 497       obj = rcube_find_object(id, d.layers[n].document);
4e17e6 498     }
T 499
500   return obj;
501   }
502
503
504 // return the absolute position of an object within the document
e5686f 505 function rcube_get_object_pos(obj, relative)
4e17e6 506   {
T 507   if(typeof(obj)=='string')
86958f 508     obj = rcube_find_object(obj);
4e17e6 509
T 510   if(!obj) return {x:0, y:0};
511
512   var iX = (bw.layers) ? obj.x : obj.offsetLeft;
513   var iY = (bw.layers) ? obj.y : obj.offsetTop;
514
e5686f 515   if(!relative && (bw.ie || bw.mz))
4e17e6 516     {
T 517     var elm = obj.offsetParent;
518     while(elm && elm!=null)
519       {
f89f03 520       iX += elm.offsetLeft - (elm.parentNode && elm.parentNode.scrollLeft ? elm.parentNode.scrollLeft : 0);
T 521       iY += elm.offsetTop - (elm.parentNode && elm.parentNode.scrollTop ? elm.parentNode.scrollTop : 0);
4e17e6 522       elm = elm.offsetParent;
T 523       }
524     }
525
526   return {x:iX, y:iY};
527   }
a7d5c6 528
f89f03 529 // determine whether the mouse is over the given object or not
T 530 function rcube_mouse_is_over(ev, obj)
531 {
532   var mouse = rcube_event.get_mouse_pos(ev);
533   var pos = rcube_get_object_pos(obj);
534   
535   return ((mouse.x >= pos.x) && (mouse.x < (pos.x + obj.offsetWidth)) &&
536     (mouse.y >= pos.y) && (mouse.y < (pos.y + obj.offsetHeight)));
537 }
538
a7d5c6 539
T 540 /**
541  * Return the currently applied value of a css property
542  *
543  * @param {Object} html_element  Node reference
544  * @param {String} css_property  Property name to read in Javascript notation (eg. 'textAlign')
545  * @param {String} mozilla_equivalent  Equivalent property name in CSS notation (eg. 'text-align')
546  * @return CSS property value
547  * @type String
548  */
549 function get_elements_computed_style(html_element, css_property, mozilla_equivalent)
550   {
551   if (arguments.length==2)
552     mozilla_equivalent = css_property;
553
554   var el = html_element;
555   if (typeof(html_element)=='string')
86958f 556     el = rcube_find_object(html_element);
a7d5c6 557
T 558   if (el && el.currentStyle)
559     return el.currentStyle[css_property];
560   else if (el && document.defaultView && document.defaultView.getComputedStyle)
561     return document.defaultView.getComputedStyle(el, null).getPropertyValue(mozilla_equivalent);
562   else
563     return false;
564   }
dd53e2 565   
T 566
567 // cookie functions by GoogieSpell
568 function setCookie(name, value, expires, path, domain, secure)
569   {
570   var curCookie = name + "=" + escape(value) +
571       (expires ? "; expires=" + expires.toGMTString() : "") +
572       (path ? "; path=" + path : "") +
573       (domain ? "; domain=" + domain : "") +
574       (secure ? "; secure" : "");
575   document.cookie = curCookie;
576   }
a7d5c6 577
T 578 roundcube_browser.prototype.set_cookie = setCookie;
dd53e2 579
T 580 function getCookie(name)
581   {
582   var dc = document.cookie;
583   var prefix = name + "=";
584   var begin = dc.indexOf("; " + prefix);
585   if (begin == -1)
586     {
587     begin = dc.indexOf(prefix);
588     if (begin != 0) return null;
589     }
590   else
591     begin += 2;  
592   var end = document.cookie.indexOf(";", begin);
593   if (end == -1)
594     end = dc.length;
595   return unescape(dc.substring(begin + prefix.length, end));
596   }
597
a7d5c6 598 roundcube_browser.prototype.get_cookie = getCookie;
T 599
4e17e6 600
f11541 601 // tiny replacement for Firebox functionality
T 602 function rcube_console()
603 {
604   this.log = function(msg)
605   {
715553 606     box = rcube_find_object('console');
e5686f 607
715553 608     if (box)
e5686f 609       if (msg.charAt(msg.length-1)=='\n')
715553 610         box.value += msg+'--------------------------------------\n';
A 611       else
612         box.value += msg+'\n--------------------------------------\n';
f11541 613   };
715553 614
f11541 615   this.reset = function()
T 616   {
715553 617     box = rcube_find_object('console');
A 618     if (box)
619       box.value = '';
f11541 620   };
T 621 }
622
b4b081 623 var bw = new roundcube_browser();
f11541 624
T 625 if (!window.console)
626   console = new rcube_console();
b0dbf3 627
S 628
629 // Add escape() method to RegExp object
630 // http://dev.rubyonrails.org/changeset/7271
631 RegExp.escape = function(str)
632   {
633   return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
634   }
bafadd 635
A 636
637 // Make getElementById() case-sensitive on IE
638 if (bw.ie)
639   {
640   document._getElementById = document.getElementById;
641   document.getElementById = function(id)
642     {
6d89d6 643     var i = 0;
bafadd 644     var o = document._getElementById(id);
A 645
4c70d1 646     if (!o || o.id != id)
6d89d6 647       while ((o = document.all[i]) && o.id != id)
A 648         i++;
bafadd 649
A 650     return o;
651     }
652   }
f4b868 653
A 654
655 // Fire event on specified element
656 function exec_event(element,event)
657 {  
658   if (document.createEventObject) {
659     // dispatch for IE  
660     var evt = document.createEventObject();
661     return element.fireEvent('on'+event,evt)
662   }
663   else {  
664     // dispatch for firefox + others  
665     var evt = document.createEvent("HTMLEvents");
666     evt.initEvent(event, true, true); // event type,bubbling,cancelable
667     return !element.dispatchEvent(evt);
668    }
669 }