thomascube
2005-11-06 10a699759d4f106f29c077a6d65d3b8d212825e5
commit | author | age
4e17e6 1 /*
T 2  +-----------------------------------------------------------------------+
3  | RoundCube common js library                                           |
4  |                                                                       |
5  | This file is part of the RoundCube web development suite              |
6  | Copyright (C) 2005, RoundCube Dev, - Switzerland                      |
30233b 7  | Licensed under the GNU GPL                                            |
4e17e6 8  |                                                                       |
10a699 9  | Modified:2005/11/06 (roundcube)                                       |
4e17e6 10  |                                                                       |
T 11  +-----------------------------------------------------------------------+
12  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
13  +-----------------------------------------------------------------------+
14 */
15
16
17 // default browsercheck
18 function roundcube_browser()
19   {
20   this.ver = parseFloat(navigator.appVersion);
21   this.appver = navigator.appVersion;
22   this.agent = navigator.userAgent;
23   this.name = navigator.appName;
24   this.vendor = navigator.vendor ? navigator.vendor : '';
25   this.vendver = navigator.vendorSub ? parseFloat(navigator.vendorSub) : 0;
26   this.product = navigator.product ? navigator.product : '';
27   this.platform = String(navigator.platform).toLowerCase();
28   this.lang = (navigator.language) ? navigator.language.substring(0,2) :
29               (navigator.browserLanguage) ? navigator.browserLanguage.substring(0,2) :
30               (navigator.systemLanguage) ? navigator.systemLanguage.substring(0,2) : 'en';
31
32   this.win = (this.platform.indexOf('win')>=0) ? true : false;
33   this.mac = (this.platform.indexOf('mac')>=0) ? true : false;
34   this.linux = (this.platform.indexOf('linux')>=0) ? true : false;
35   this.unix = (this.platform.indexOf('unix')>=0) ? true : false;
36
37   this.dom = document.getElementById ? true : false;
38   this.dom2 = (document.addEventListener && document.removeEventListener);
39
40   this.ie = (document.all) ? true : false;
41   this.ie4 = (this.ie && !this.dom);
42   this.ie5 = (this.dom && this.appver.indexOf('MSIE 5')>0);
43   this.ie6 = (this.dom && this.appver.indexOf('MSIE 6')>0);
44
45   this.mz = (this.dom && this.ver>=5);  // (this.dom && this.product=='Gecko')
46   this.ns = ((this.ver<5 && this.name=='Netscape') || (this.ver>=5 && this.vendor.indexOf('Netscape')>=0));
47   this.ns4 = (this.ns && parseInt(this.ver)==4);
48   this.ns6 = (this.ns && parseInt(this.vendver)==6);  // (this.mz && this.ns) ? true : false;
49   this.ns7 = (this.ns && parseInt(this.vendver)==7);  // this.agent.indexOf('Netscape/7')>0);
42b113 50   this.safari = (this.agent.toLowerCase().indexOf('safari')>0 || this.agent.toLowerCase().indexOf('applewebkit')>0);
T 51   this.konq   = (this.agent.toLowerCase().indexOf('konqueror')>0);
4e17e6 52
T 53   this.opera = (window.opera) ? true : false;
54   this.opera5 = (this.opera5 && this.agent.indexOf('Opera 5')>0) ? true : false;
55   this.opera6 = (this.opera && this.agent.indexOf('Opera 6')>0) ? true : false;
56   this.opera7 = (this.opera && this.agent.indexOf('Opera 7')>0) ? true : false;
57
58   if(this.opera && window.RegExp)
59     this.vendver = (/opera(\s|\/)([0-9\.]+)/i.test(navigator.userAgent)) ? parseFloat(RegExp.$2) : -1;
60   else if(!this.vendver && this.safari)
42b113 61     this.vendver = (/(safari|applewebkit)\/([0-9]+)/i.test(this.agent)) ? parseInt(RegExp.$2) : 0;
4e17e6 62   else if((!this.vendver && this.mz) || this.agent.indexOf('Camino')>0)
T 63     this.vendver = (/rv:([0-9\.]+)/.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
64   else if(this.ie && window.RegExp)
65     this.vendver = (/msie\s+([0-9\.]+)/i.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
42b113 66   else if(this.konq && window.RegExp)
T 67     this.vendver = (/khtml\/([0-9\.]+)/i.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
68
4e17e6 69
T 70   // get real language out of safari's user agent
71   if(this.safari && (/;\s+([a-z]{2})-[a-z]{2}\)/i.test(this.agent)))
72     this.lang = RegExp.$1;
73
74   this.dhtml = ((this.ie4 && this.win) || this.ie5 || this.ie6 || this.ns4 || this.mz);
75   this.layers = this.ns4;  // (document.layers);
76   this.div = (this.ie4 || this.dom);
77   this.vml = (this.win && this.ie && this.dom && !this.opera);
78   this.linkborder = (this.ie || this.mz);
79   this.rollover = (this.ver>=4 || (this.ns && this.ver>=3));  // (document.images) ? true : false;
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
95
96
97 var rcube_layer_objects = new Array();
98
99 function rcube_layer(id, attributes)
100   {
101   this.name = id;
102   
103   // create a new layer in the current document
104   this.create = function(arg)
105     {
106     var l = (arg.x) ? arg.x : 0;
107     var t = (arg.y) ? arg.y : 0;
108     var w = arg.width;
109     var h = arg.height;
110     var z = arg.zindex;
111     var vis = arg.vis;
112     var parent = arg.parent;
113     var obj;
114
115     obj = document.createElement('DIV');
116     with(obj)
117       {
118       id = this.name;
119       with(style)
120         {
121         position = 'absolute';
122         visibility = (vis) ? (vis==2) ? 'inherit' : 'visible' : 'hidden';
123         left = l+'px';
124         top = t+'px';
125         if(w) width = w+'px';
126         if(h) height = h+'px';
127         if(z) zIndex = z;
128         }
129       }
130       
131     if(parent) parent.appendChild(obj);
132     else document.body.appendChild(obj);
133
134     this.elm = obj;
135     };
136
137
138   // create new layer
139   if(attributes!=null)
140     {
141     this.create(attributes);
142     this.name = this.elm.id;
143     }
144   else  // just refer to the object
145     this.elm = document.getElementById(id);
146
147
148   if(!this.elm)
149     return false;
150
151
152   // ********* layer object properties *********
153
154   this.css = this.elm.style;
155   this.event = this.elm;
156   this.width = this.elm.offsetWidth;
157   this.height = this.elm.offsetHeight;
158   this.x = parseInt(this.elm.offsetLeft);
159   this.y = parseInt(this.elm.offsetTop);
160   this.visible = (this.css.visibility=='visible' || this.css.visibility=='show' || this.css.visibility=='inherit') ? true : false;
161
162   this.id = rcube_layer_objects.length;
163   this.obj = 'rcube_layer_objects['+this.id+']';
164   rcube_layer_objects[this.id] = this;
165
166
167   // ********* layer object methods *********
168
169
170   // move the layer to a specific position
171   this.move = function(x, y)
172     {
173     this.x = x;
174     this.y = y;
175     this.css.left = Math.round(this.x)+'px';
176     this.css.top = Math.round(this.y)+'px';
177     }
178
179
180   // move the layer for a specific step
181   this.shift = function(x,y)
182     {
183     x = Math.round(x*100)/100;
184     y = Math.round(y*100)/100;
185     this.move(this.x+x, this.y+y);
186     }
187
188
189   // change the layers width and height
190   this.resize = function(w,h)
191     {
192     this.css.width  = w+'px';
193     this.css.height = h+'px';
194     this.width = w;
195     this.height = h;
196     }
197
198
199   // cut the layer (top,width,height,left)
200   this.clip = function(t,w,h,l)
201     {
202     this.css.clip='rect('+t+' '+w+' '+h+' '+l+')';
203     this.clip_height = h;
204     this.clip_width = w;
205     }
206
207
208   // show or hide the layer
209   this.show = function(a)
210     {
211     if(a==1)
212       {
213       this.css.visibility = 'visible';
214       this.visible = true;
215       }
216     else if(a==2)
217       {
218       this.css.visibility = 'inherit';
219       this.visible = true;
220       }
221     else
222       {
223       this.css.visibility = 'hidden';
224       this.visible = false;
225       }
226     }
227
228
229   // write new content into a Layer
230   this.write = function(cont)
231     {
232     this.elm.innerHTML = cont;
233     }
234
235
236   // set the given color to the layer background
237   this.set_bgcolor = function(c)
238     {
239     if(!c || c=='#')
240       c = 'transparent';
241
242     this.css.backgroundColor = c;
243     }
244
245
246   // set the opacity of a layer to the given ammount (in %)
247   this.set_opacity = function(v)
248     {
249     if(!bw.opacity)
250       return;
251
252     var op = v<=1 ? Math.round(v*100) : parseInt(v);
253
254     if(bw.ie)
255       this.css.filter = 'alpha(opacity:'+op+')';
256     else if(bw.safari)
257       {
258       this.css.opacity = op/100;
259       this.css.KhtmlOpacity = op/100;
260       }
261     else if(bw.mz)
262       this.css.MozOpacity = op/100;
263     }
264   }
265
266
10a699 267 // check if input is a valid email address
T 268 function rcube_check_email(input, inline)
269   {
270   if (input && window.RegExp)
271     {
272     var reg_str = '([a-z0-9][-a-z0-9\.\+_]*)\@([a-z0-9]([-a-z0-9][\.]?)*[a-z0-9]\.[a-z]{2,9})';
273     var reg1 = inline ? new RegExp(reg_str, 'i') : new RegExp('^'+reg_str+'$', 'i');
274     var reg2 = /[\._\-\@]{2}/;
275     return reg1.test(input) && !reg2.test(input) ? true : false;
276     }
277   return false;
278   }
279   
4e17e6 280
T 281 // find a value in a specific array and returns the index
282 function find_in_array()
283   {
284   var args = find_in_array.arguments;
285   if(!args.length) return -1;
286
287   var haystack = typeof(args[0])=='object' ? args[0] : args.length>1 && typeof(args[1])=='object' ? args[1] : new Array();
288   var needle = typeof(args[0])!='object' ? args[0] : args.length>1 && typeof(args[1])!='object' ? args[1] : '';
289   var nocase = args.length==3 ? args[2] : false;
290
291   if(!haystack.length) return -1;
292
293   for(var i=0; i<haystack.length; i++)
294     if(nocase && haystack[i].toLowerCase()==needle.toLowerCase())
295       return i;
296     else if(haystack[i]==needle)
297       return i;
298
299   return -1;
300   }
301
302
303 // get any type of html objects by id/name
304 function rcube_find_object(id, d)
305   {
306   var n, f, obj, e;
307   if(!d) d = document;
308
309   if(d.getElementsByName && (e = d.getElementsByName(id)))
310     obj = e[0];
311   if(!obj && d.getElementById)
312     obj = d.getElementById(id);
313   if(!obj && d.all)
314     obj = d.all[id];
315
316   if(!obj && d.images.length)
317     obj = d.images[id];
318
319   if(!obj && d.forms.length)
320     for(f=0; f<d.forms.length; f++)
321       {
322       if(d.forms[f].name == id)
323         obj = d.forms[f];
324       else if(d.forms[f].elements[id])
325         obj = d.forms[f].elements[id];
326       }
327
328   if(!obj && d.layers)
329     {
330     if(d.layers[id]) obj = d.layers[id];
331     for(n=0; !obj && n<d.layers.length; n++)
332       obj = nex_get_object(id, d.layers[n].document);
333     }
334
335   return obj;
336   }
337
338
339 // return the absolute position of an object within the document
340 function rcube_get_object_pos(obj)
341   {
342   if(typeof(obj)=='string')
343     obj = nex_get_object(obj);
344
345   if(!obj) return {x:0, y:0};
346
347   var iX = (bw.layers) ? obj.x : obj.offsetLeft;
348   var iY = (bw.layers) ? obj.y : obj.offsetTop;
349
350   if(bw.ie || bw.mz)
351     {
352     var elm = obj.offsetParent;
353     while(elm && elm!=null)
354       {
355       iX += elm.offsetLeft;
356       iY += elm.offsetTop;
357       elm = elm.offsetParent;
358       }
359     }
360
361   if(bw.mac && bw.ie5) iX += document.body.leftMargin;
362   if(bw.mac && bw.ie5) iY += document.body.topMargin;
363
364   return {x:iX, y:iY};
365   }
366
367 var bw = new roundcube_browser();