2 +-----------------------------------------------------------------------+
3 | RoundCube common js library |
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 |
9 +-----------------------------------------------------------------------+
10 | Author: Thomas Bruederli <roundcube@gmail.com> |
11 +-----------------------------------------------------------------------+
13 $Id: common.js 543 2007-04-28 18:07:12Z thomasb $
19 var CONTROL_SHIFT_KEY = 3;
23 * Default browser check class
26 function roundcube_browser()
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';
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;
45 this.dom = document.getElementById ? true : false;
46 this.dom2 = (document.addEventListener && document.removeEventListener);
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);
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);
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;
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;
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;
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;
93 // test for XMLHTTP support
94 this.xmlhttp_test = function()
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;
103 // static functions for event handling
107 * returns the event key code
109 get_keycode: function(e)
111 e = e || window.event;
112 return e && e.keyCode ? e.keyCode : (e && e.which ? e.which : 0);
116 * returns modifier key (constants defined at top of file)
118 get_modifier: function(e)
121 e = e || window.event;
125 opcode += (e.metaKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
130 opcode += (e.ctrlKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
136 * Return absolute mouse position of an event
138 get_mouse_pos: function(e)
140 if (!e) e = window.event;
141 var mX = (e.pageX) ? e.pageX : e.clientX;
142 var mY = (e.pageY) ? e.pageY : e.clientY;
144 if (document.body && document.all)
146 mX += document.body.scrollLeft;
147 mY += document.body.scrollTop;
150 return { x:mX, y:mY };
154 * Add an object method as event listener to a certain element
156 add_listener: function(p)
158 if (!p.object || !p.method) // not enough arguments
161 p.element = document;
163 if (!p.object._rc_events)
164 p.object._rc_events = [];
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); };
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]);
175 p.element['on'+p.event] = p.object._rc_events[key];
179 * Remove event listener
181 remove_listener: function(p)
184 p.element = document;
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]);
193 p.element['on'+p.event] = null;
198 * Prevent event propagation and bubbeling
200 cancel: function(evt)
202 var e = evt ? evt : window.event;
203 if (e.preventDefault)
205 if (e.stopPropagation)
208 e.cancelBubble = true;
209 e.returnValue = false;
216 var rcube_layer_objects = new Array();
220 * RoundCube generic layer (floating box) class
224 function rcube_layer(id, attributes)
228 // create a new layer in the current document
229 this.create = function(arg)
231 var l = (arg.x) ? arg.x : 0;
232 var t = (arg.y) ? arg.y : 0;
237 var parent = arg.parent;
240 obj = document.createElement('DIV');
246 position = 'absolute';
247 visibility = (vis) ? (vis==2) ? 'inherit' : 'visible' : 'hidden';
250 if(w) width = w+'px';
251 if(h) height = h+'px';
256 if(parent) parent.appendChild(obj);
257 else document.body.appendChild(obj);
266 this.create(attributes);
267 this.name = this.elm.id;
269 else // just refer to the object
270 this.elm = document.getElementById(id);
277 // ********* layer object properties *********
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;
287 this.id = rcube_layer_objects.length;
288 this.obj = 'rcube_layer_objects['+this.id+']';
289 rcube_layer_objects[this.id] = this;
292 // ********* layer object methods *********
295 // move the layer to a specific position
296 this.move = function(x, y)
300 this.css.left = Math.round(this.x)+'px';
301 this.css.top = Math.round(this.y)+'px';
305 // move the layer for a specific step
306 this.shift = function(x,y)
308 x = Math.round(x*100)/100;
309 y = Math.round(y*100)/100;
310 this.move(this.x+x, this.y+y);
314 // change the layers width and height
315 this.resize = function(w,h)
317 this.css.width = w+'px';
318 this.css.height = h+'px';
324 // cut the layer (top,width,height,left)
325 this.clip = function(t,w,h,l)
327 this.css.clip='rect('+t+' '+w+' '+h+' '+l+')';
328 this.clip_height = h;
333 // show or hide the layer
334 this.show = function(a)
338 this.css.visibility = 'visible';
343 this.css.visibility = 'inherit';
348 this.css.visibility = 'hidden';
349 this.visible = false;
354 // write new content into a Layer
355 this.write = function(cont)
357 this.elm.innerHTML = cont;
361 // set the given color to the layer background
362 this.set_bgcolor = function(c)
367 this.css.backgroundColor = c;
371 // set the opacity of a layer to the given ammount (in %)
372 this.set_opacity = function(v)
377 var op = v<=1 ? Math.round(v*100) : parseInt(v);
380 this.css.filter = 'alpha(opacity:'+op+')';
383 this.css.opacity = op/100;
384 this.css.KhtmlOpacity = op/100;
387 this.css.MozOpacity = op/100;
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)
397 if (input && window.RegExp)
399 var no_ws_ctl = "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]";
400 var alpha = "[\\x41-\\x5a\\x61-\\x7a]";
401 var digit = "[\\x30-\\x39]";
404 var crlf = "(" + cr + lf + ")";
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+")";
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+"))";
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+"?)";
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+")";
429 var obs_local_part = "("+word+"(\\x2e"+word+")*)";
430 var obs_domain = "("+atom+"(\\x2e"+atom+")*)";
432 var dot_atom_text = "("+atext+"+(\\x2e"+atext+"+)*)";
433 var dot_atom = "("+cfws+"?"+dot_atom_text+cfws+"?)";
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+"?)";
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+")";
443 var reg1 = inline ? new RegExp(addr_spec, 'i') : new RegExp('^'+addr_spec+'$', 'i');
444 return reg1.test(input) ? true : false;
450 // find a value in a specific array and returns the index
451 function find_in_array()
453 var args = find_in_array.arguments;
454 if(!args.length) return -1;
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;
460 if(!haystack.length) return -1;
462 for(var i=0; i<haystack.length; i++)
463 if(nocase && haystack[i].toLowerCase()==needle.toLowerCase())
465 else if(haystack[i]==needle)
472 // make a string URL safe
473 function urlencode(str)
475 return window.encodeURIComponent ? encodeURIComponent(str) : escape(str);
479 // get any type of html objects by id/name
480 function rcube_find_object(id, d)
485 if(d.getElementsByName && (e = d.getElementsByName(id)))
487 if(!obj && d.getElementById)
488 obj = d.getElementById(id);
492 if(!obj && d.images.length)
495 if(!obj && d.forms.length)
496 for(f=0; f<d.forms.length; f++)
498 if(d.forms[f].name == id)
500 else if(d.forms[f].elements[id])
501 obj = d.forms[f].elements[id];
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);
515 // return the absolute position of an object within the document
516 function rcube_get_object_pos(obj)
518 if(typeof(obj)=='string')
519 obj = rcube_find_object(obj);
521 if(!obj) return {x:0, y:0};
523 var iX = (bw.layers) ? obj.x : obj.offsetLeft;
524 var iY = (bw.layers) ? obj.y : obj.offsetTop;
528 var elm = obj.offsetParent;
529 while(elm && elm!=null)
531 iX += elm.offsetLeft;
533 elm = elm.offsetParent;
537 //if(bw.mac && bw.ie5) iX += document.body.leftMargin;
538 //if(bw.mac && bw.ie5) iY += document.body.topMargin;
545 * Return the currently applied value of a css property
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
553 function get_elements_computed_style(html_element, css_property, mozilla_equivalent)
555 if (arguments.length==2)
556 mozilla_equivalent = css_property;
558 var el = html_element;
559 if (typeof(html_element)=='string')
560 el = rcube_find_object(html_element);
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);
571 // cookie functions by GoogieSpell
572 function setCookie(name, value, expires, path, domain, secure)
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;
582 roundcube_browser.prototype.set_cookie = setCookie;
584 function getCookie(name)
586 var dc = document.cookie;
587 var prefix = name + "=";
588 var begin = dc.indexOf("; " + prefix);
591 begin = dc.indexOf(prefix);
592 if (begin != 0) return null;
596 var end = document.cookie.indexOf(";", begin);
599 return unescape(dc.substring(begin + prefix.length, end));
602 roundcube_browser.prototype.get_cookie = getCookie;
605 // tiny replacement for Firebox functionality
606 function rcube_console()
608 this.box = rcube_find_object('console');
610 this.log = function(msg)
613 this.box.value += str+'\n--------------------------------------\n';
616 this.reset = function()
623 var bw = new roundcube_browser();
626 console = new rcube_console();