thomascube
2005-10-07 42b11351497ce67e96a0465c76694632cdfb3ecb
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  |                                                                       |
T 9  | Modified: 19.08.2005 (tbr)                                            |
10  |                                                                       |
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;
84   }
85
86
87
88
89 var rcube_layer_objects = new Array();
90
91 function rcube_layer(id, attributes)
92   {
93   this.name = id;
94   
95   // create a new layer in the current document
96   this.create = function(arg)
97     {
98     var l = (arg.x) ? arg.x : 0;
99     var t = (arg.y) ? arg.y : 0;
100     var w = arg.width;
101     var h = arg.height;
102     var z = arg.zindex;
103     var vis = arg.vis;
104     var parent = arg.parent;
105     var obj;
106
107     obj = document.createElement('DIV');
108     with(obj)
109       {
110       id = this.name;
111       with(style)
112         {
113         position = 'absolute';
114         visibility = (vis) ? (vis==2) ? 'inherit' : 'visible' : 'hidden';
115         left = l+'px';
116         top = t+'px';
117         if(w) width = w+'px';
118         if(h) height = h+'px';
119         if(z) zIndex = z;
120         }
121       }
122       
123     if(parent) parent.appendChild(obj);
124     else document.body.appendChild(obj);
125
126     this.elm = obj;
127     };
128
129
130   // create new layer
131   if(attributes!=null)
132     {
133     this.create(attributes);
134     this.name = this.elm.id;
135     }
136   else  // just refer to the object
137     this.elm = document.getElementById(id);
138
139
140   if(!this.elm)
141     return false;
142
143
144   // ********* layer object properties *********
145
146   this.css = this.elm.style;
147   this.event = this.elm;
148   this.width = this.elm.offsetWidth;
149   this.height = this.elm.offsetHeight;
150   this.x = parseInt(this.elm.offsetLeft);
151   this.y = parseInt(this.elm.offsetTop);
152   this.visible = (this.css.visibility=='visible' || this.css.visibility=='show' || this.css.visibility=='inherit') ? true : false;
153
154   this.id = rcube_layer_objects.length;
155   this.obj = 'rcube_layer_objects['+this.id+']';
156   rcube_layer_objects[this.id] = this;
157
158
159   // ********* layer object methods *********
160
161
162   // move the layer to a specific position
163   this.move = function(x, y)
164     {
165     this.x = x;
166     this.y = y;
167     this.css.left = Math.round(this.x)+'px';
168     this.css.top = Math.round(this.y)+'px';
169     }
170
171
172   // move the layer for a specific step
173   this.shift = function(x,y)
174     {
175     x = Math.round(x*100)/100;
176     y = Math.round(y*100)/100;
177     this.move(this.x+x, this.y+y);
178     }
179
180
181   // change the layers width and height
182   this.resize = function(w,h)
183     {
184     this.css.width  = w+'px';
185     this.css.height = h+'px';
186     this.width = w;
187     this.height = h;
188     }
189
190
191   // cut the layer (top,width,height,left)
192   this.clip = function(t,w,h,l)
193     {
194     this.css.clip='rect('+t+' '+w+' '+h+' '+l+')';
195     this.clip_height = h;
196     this.clip_width = w;
197     }
198
199
200   // show or hide the layer
201   this.show = function(a)
202     {
203     if(a==1)
204       {
205       this.css.visibility = 'visible';
206       this.visible = true;
207       }
208     else if(a==2)
209       {
210       this.css.visibility = 'inherit';
211       this.visible = true;
212       }
213     else
214       {
215       this.css.visibility = 'hidden';
216       this.visible = false;
217       }
218     }
219
220
221   // write new content into a Layer
222   this.write = function(cont)
223     {
224     this.elm.innerHTML = cont;
225     }
226
227
228   // set the given color to the layer background
229   this.set_bgcolor = function(c)
230     {
231     if(!c || c=='#')
232       c = 'transparent';
233
234     this.css.backgroundColor = c;
235     }
236
237
238   // set the opacity of a layer to the given ammount (in %)
239   this.set_opacity = function(v)
240     {
241     if(!bw.opacity)
242       return;
243
244     var op = v<=1 ? Math.round(v*100) : parseInt(v);
245
246     if(bw.ie)
247       this.css.filter = 'alpha(opacity:'+op+')';
248     else if(bw.safari)
249       {
250       this.css.opacity = op/100;
251       this.css.KhtmlOpacity = op/100;
252       }
253     else if(bw.mz)
254       this.css.MozOpacity = op/100;
255     }
256   }
257
258
259
260 // find a value in a specific array and returns the index
261 function find_in_array()
262   {
263   var args = find_in_array.arguments;
264   if(!args.length) return -1;
265
266   var haystack = typeof(args[0])=='object' ? args[0] : args.length>1 && typeof(args[1])=='object' ? args[1] : new Array();
267   var needle = typeof(args[0])!='object' ? args[0] : args.length>1 && typeof(args[1])!='object' ? args[1] : '';
268   var nocase = args.length==3 ? args[2] : false;
269
270   if(!haystack.length) return -1;
271
272   for(var i=0; i<haystack.length; i++)
273     if(nocase && haystack[i].toLowerCase()==needle.toLowerCase())
274       return i;
275     else if(haystack[i]==needle)
276       return i;
277
278   return -1;
279   }
280
281
282 // get any type of html objects by id/name
283 function rcube_find_object(id, d)
284   {
285   var n, f, obj, e;
286   if(!d) d = document;
287
288   if(d.getElementsByName && (e = d.getElementsByName(id)))
289     obj = e[0];
290   if(!obj && d.getElementById)
291     obj = d.getElementById(id);
292   if(!obj && d.all)
293     obj = d.all[id];
294
295   if(!obj && d.images.length)
296     obj = d.images[id];
297
298   if(!obj && d.forms.length)
299     for(f=0; f<d.forms.length; f++)
300       {
301       if(d.forms[f].name == id)
302         obj = d.forms[f];
303       else if(d.forms[f].elements[id])
304         obj = d.forms[f].elements[id];
305       }
306
307   if(!obj && d.layers)
308     {
309     if(d.layers[id]) obj = d.layers[id];
310     for(n=0; !obj && n<d.layers.length; n++)
311       obj = nex_get_object(id, d.layers[n].document);
312     }
313
314   return obj;
315   }
316
317
318 // return the absolute position of an object within the document
319 function rcube_get_object_pos(obj)
320   {
321   if(typeof(obj)=='string')
322     obj = nex_get_object(obj);
323
324   if(!obj) return {x:0, y:0};
325
326   var iX = (bw.layers) ? obj.x : obj.offsetLeft;
327   var iY = (bw.layers) ? obj.y : obj.offsetTop;
328
329   if(bw.ie || bw.mz)
330     {
331     var elm = obj.offsetParent;
332     while(elm && elm!=null)
333       {
334       iX += elm.offsetLeft;
335       iY += elm.offsetTop;
336       elm = elm.offsetParent;
337       }
338     }
339
340   if(bw.mac && bw.ie5) iX += document.body.leftMargin;
341   if(bw.mac && bw.ie5) iY += document.body.topMargin;
342
343   return {x:iX, y:iY};
344   }
345
346 var bw = new roundcube_browser();