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 |
+-----------------------------------------------------------------------+ |
|
10 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
11 |
+-----------------------------------------------------------------------+ |
5e3512
|
12 |
|
T |
13 |
$Id$ |
4e17e6
|
14 |
*/ |
T |
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 |
|
10a699
|
266 |
// check if input is a valid email address |
2c5762
|
267 |
// By Cal Henderson <cal@iamcal.com> |
S |
268 |
// http://code.iamcal.com/php/rfc822/ |
10a699
|
269 |
function rcube_check_email(input, inline) |
T |
270 |
{ |
|
271 |
if (input && window.RegExp) |
|
272 |
{ |
2c5762
|
273 |
var no_ws_ctl = "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]"; |
S |
274 |
var alpha = "[\\x41-\\x5a\\x61-\\x7a]"; |
|
275 |
var digit = "[\\x30-\\x39]"; |
|
276 |
var cr = "\\x0d"; |
|
277 |
var lf = "\\x0a"; |
|
278 |
var crlf = "(" + cr + lf + ")"; |
|
279 |
|
|
280 |
var obs_char = "[\\x00-\\x09\\x0b\\x0c\\x0e-\\x7f]"; |
|
281 |
var obs_text = "("+lf+"*"+cr+"*("+obs_char+lf+"*"+cr+"*)*)"; |
|
282 |
var text = "([\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f]|"+obs_text+")"; |
|
283 |
var obs_qp = "(\\x5c[\\x00-\\x7f])"; |
|
284 |
var quoted_pair = "(\\x5c"+text+"|"+obs_qp+")"; |
|
285 |
|
|
286 |
var wsp = "[\\x20\\x09]"; |
|
287 |
var obs_fws = "("+wsp+"+("+crlf+wsp+"+)*)"; |
|
288 |
var fws = "((("+wsp+"*"+crlf+")?"+wsp+"+)|"+obs_fws+")"; |
|
289 |
var ctext = "("+no_ws_ctl+"|[\\x21-\\x27\\x2A-\\x5b\\x5d-\\x7e])"; |
|
290 |
var ccontent = "("+ctext+"|"+quoted_pair+")"; |
|
291 |
var comment = "(\\x28("+fws+"?"+ccontent+")*"+fws+"?\\x29)"; |
|
292 |
var cfws = "(("+fws+"?"+comment+")*("+fws+"?"+comment+"|"+fws+"))"; |
|
293 |
var cfws = fws+"*"; |
|
294 |
|
|
295 |
var atext = "("+alpha+"|"+digit+"|[\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2e\\x3d\\x3f\\x5e\\x5f\\x60\\x7b-\\x7e])"; |
|
296 |
var atom = "("+cfws+"?"+atext+"+"+cfws+"?)"; |
|
297 |
|
|
298 |
var qtext = "("+no_ws_ctl+"|[\\x21\\x23-\\x5b\\x5d-\\x7e])"; |
|
299 |
var qcontent = "("+qtext+"|"+quoted_pair+")"; |
|
300 |
var quoted_string = "("+cfws+"?\\x22("+fws+"?"+qcontent+")*"+fws+"?\\x22"+cfws+"?)"; |
|
301 |
var word = "("+atom+"|"+quoted_string+")"; |
|
302 |
|
|
303 |
var obs_local_part = "("+word+"(\\x2e"+word+")*)"; |
|
304 |
var obs_domain = "("+atom+"(\\x2e"+atom+")*)"; |
|
305 |
|
|
306 |
var dot_atom_text = "("+atext+"+(\\x2e"+atext+"+)*)"; |
|
307 |
var dot_atom = "("+cfws+"?"+dot_atom_text+cfws+"?)"; |
|
308 |
|
|
309 |
var dtext = "("+no_ws_ctl+"|[\\x21-\\x5a\\x5e-\\x7e])"; |
|
310 |
var dcontent = "("+dtext+"|"+quoted_pair+")"; |
|
311 |
var domain_literal = "("+cfws+"?\\x5b("+fws+"?"+dcontent+")*"+fws+"?\\x5d"+cfws+"?)"; |
|
312 |
|
|
313 |
var local_part = "("+dot_atom+"|"+quoted_string+"|"+obs_local_part+")"; |
|
314 |
var domain = "("+dot_atom+"|"+domain_literal+"|"+obs_domain+")"; |
|
315 |
var addr_spec = "("+local_part+"\\x40"+domain+")"; |
|
316 |
|
|
317 |
var reg1 = inline ? new RegExp(addr_spec, 'i') : new RegExp('^'+addr_spec+'$', 'i'); |
|
318 |
return reg1.test(input) ? true : false; |
10a699
|
319 |
} |
T |
320 |
return false; |
|
321 |
} |
|
322 |
|
4e17e6
|
323 |
|
T |
324 |
// find a value in a specific array and returns the index |
|
325 |
function find_in_array() |
|
326 |
{ |
|
327 |
var args = find_in_array.arguments; |
|
328 |
if(!args.length) return -1; |
|
329 |
|
|
330 |
var haystack = typeof(args[0])=='object' ? args[0] : args.length>1 && typeof(args[1])=='object' ? args[1] : new Array(); |
|
331 |
var needle = typeof(args[0])!='object' ? args[0] : args.length>1 && typeof(args[1])!='object' ? args[1] : ''; |
|
332 |
var nocase = args.length==3 ? args[2] : false; |
|
333 |
|
|
334 |
if(!haystack.length) return -1; |
|
335 |
|
|
336 |
for(var i=0; i<haystack.length; i++) |
|
337 |
if(nocase && haystack[i].toLowerCase()==needle.toLowerCase()) |
|
338 |
return i; |
|
339 |
else if(haystack[i]==needle) |
|
340 |
return i; |
|
341 |
|
|
342 |
return -1; |
|
343 |
} |
|
344 |
|
|
345 |
|
4d4264
|
346 |
// make a string URL safe |
T |
347 |
function urlencode(str) |
|
348 |
{ |
|
349 |
return window.encodeURI ? encodeURI(str).replace(/&/g, '%26') : escape(str); |
|
350 |
} |
|
351 |
|
|
352 |
|
4e17e6
|
353 |
// get any type of html objects by id/name |
T |
354 |
function rcube_find_object(id, d) |
|
355 |
{ |
|
356 |
var n, f, obj, e; |
|
357 |
if(!d) d = document; |
|
358 |
|
|
359 |
if(d.getElementsByName && (e = d.getElementsByName(id))) |
|
360 |
obj = e[0]; |
|
361 |
if(!obj && d.getElementById) |
|
362 |
obj = d.getElementById(id); |
|
363 |
if(!obj && d.all) |
|
364 |
obj = d.all[id]; |
|
365 |
|
|
366 |
if(!obj && d.images.length) |
|
367 |
obj = d.images[id]; |
|
368 |
|
|
369 |
if(!obj && d.forms.length) |
|
370 |
for(f=0; f<d.forms.length; f++) |
|
371 |
{ |
|
372 |
if(d.forms[f].name == id) |
|
373 |
obj = d.forms[f]; |
|
374 |
else if(d.forms[f].elements[id]) |
|
375 |
obj = d.forms[f].elements[id]; |
|
376 |
} |
|
377 |
|
|
378 |
if(!obj && d.layers) |
|
379 |
{ |
|
380 |
if(d.layers[id]) obj = d.layers[id]; |
|
381 |
for(n=0; !obj && n<d.layers.length; n++) |
|
382 |
obj = nex_get_object(id, d.layers[n].document); |
|
383 |
} |
|
384 |
|
|
385 |
return obj; |
|
386 |
} |
|
387 |
|
|
388 |
|
|
389 |
// return the absolute position of an object within the document |
|
390 |
function rcube_get_object_pos(obj) |
|
391 |
{ |
|
392 |
if(typeof(obj)=='string') |
|
393 |
obj = nex_get_object(obj); |
|
394 |
|
|
395 |
if(!obj) return {x:0, y:0}; |
|
396 |
|
|
397 |
var iX = (bw.layers) ? obj.x : obj.offsetLeft; |
|
398 |
var iY = (bw.layers) ? obj.y : obj.offsetTop; |
|
399 |
|
|
400 |
if(bw.ie || bw.mz) |
|
401 |
{ |
|
402 |
var elm = obj.offsetParent; |
|
403 |
while(elm && elm!=null) |
|
404 |
{ |
|
405 |
iX += elm.offsetLeft; |
|
406 |
iY += elm.offsetTop; |
|
407 |
elm = elm.offsetParent; |
|
408 |
} |
|
409 |
} |
|
410 |
|
|
411 |
if(bw.mac && bw.ie5) iX += document.body.leftMargin; |
|
412 |
if(bw.mac && bw.ie5) iY += document.body.topMargin; |
|
413 |
|
|
414 |
return {x:iX, y:iY}; |
|
415 |
} |
dd53e2
|
416 |
|
T |
417 |
|
|
418 |
// cookie functions by GoogieSpell |
|
419 |
function setCookie(name, value, expires, path, domain, secure) |
|
420 |
{ |
|
421 |
var curCookie = name + "=" + escape(value) + |
|
422 |
(expires ? "; expires=" + expires.toGMTString() : "") + |
|
423 |
(path ? "; path=" + path : "") + |
|
424 |
(domain ? "; domain=" + domain : "") + |
|
425 |
(secure ? "; secure" : ""); |
|
426 |
document.cookie = curCookie; |
|
427 |
} |
|
428 |
|
|
429 |
function getCookie(name) |
|
430 |
{ |
|
431 |
var dc = document.cookie; |
|
432 |
var prefix = name + "="; |
|
433 |
var begin = dc.indexOf("; " + prefix); |
|
434 |
if (begin == -1) |
|
435 |
{ |
|
436 |
begin = dc.indexOf(prefix); |
|
437 |
if (begin != 0) return null; |
|
438 |
} |
|
439 |
else |
|
440 |
begin += 2; |
|
441 |
var end = document.cookie.indexOf(";", begin); |
|
442 |
if (end == -1) |
|
443 |
end = dc.length; |
|
444 |
return unescape(dc.substring(begin + prefix.length, end)); |
|
445 |
} |
|
446 |
|
4e17e6
|
447 |
|
b4b081
|
448 |
var bw = new roundcube_browser(); |