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