選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

common.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /**
  2. * Roundcube common js library
  3. *
  4. * This file is part of the Roundcube Webmail client
  5. *
  6. * @licstart The following is the entire license notice for the
  7. * JavaScript code in this file.
  8. *
  9. * Copyright (c) 2005-2014, The Roundcube Dev Team
  10. *
  11. * The JavaScript code in this page is free software: you can
  12. * redistribute it and/or modify it under the terms of the GNU
  13. * General Public License (GNU GPL) as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option)
  15. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  16. * without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  18. *
  19. * As additional permission under GNU GPL version 3 section 7, you
  20. * may distribute non-source (e.g., minimized or compacted) forms of
  21. * that code without the copy of the GNU GPL normally required by
  22. * section 4, provided you include this license notice and a URL
  23. * through which recipients can access the Corresponding Source.
  24. *
  25. * @licend The above is the entire license notice
  26. * for the JavaScript code in this file.
  27. */
  28. // Constants
  29. var CONTROL_KEY = 1;
  30. var SHIFT_KEY = 2;
  31. var CONTROL_SHIFT_KEY = 3;
  32. /**
  33. * Default browser check class
  34. * @constructor
  35. */
  36. function roundcube_browser()
  37. {
  38. var n = navigator;
  39. this.agent = n.userAgent;
  40. this.agent_lc = n.userAgent.toLowerCase();
  41. this.name = n.appName;
  42. this.vendor = n.vendor ? n.vendor : '';
  43. this.vendver = n.vendorSub ? parseFloat(n.vendorSub) : 0;
  44. this.product = n.product ? n.product : '';
  45. this.platform = String(n.platform).toLowerCase();
  46. this.lang = n.language ? n.language.substring(0,2) :
  47. n.browserLanguage ? n.browserLanguage.substring(0,2) :
  48. n.systemLanguage ? n.systemLanguage.substring(0,2) : 'en';
  49. this.win = this.platform.indexOf('win') >= 0;
  50. this.mac = this.platform.indexOf('mac') >= 0;
  51. this.linux = this.platform.indexOf('linux') >= 0;
  52. this.unix = this.platform.indexOf('unix') >= 0;
  53. this.dom = document.getElementById ? true : false;
  54. this.dom2 = document.addEventListener && document.removeEventListener;
  55. this.webkit = this.agent_lc.indexOf('applewebkit') > 0;
  56. this.ie = (document.all && !window.opera) || (this.win && this.agent_lc.indexOf('trident/') > 0);
  57. if (this.ie) {
  58. this.ie7 = n.appVersion.indexOf('MSIE 7') > 0;
  59. this.ie8 = n.appVersion.indexOf('MSIE 8') > 0;
  60. this.ie9 = n.appVersion.indexOf('MSIE 9') > 0;
  61. }
  62. else if (window.opera) {
  63. this.opera = true; // Opera < 15
  64. this.vendver = opera.version();
  65. }
  66. else {
  67. this.chrome = this.agent_lc.indexOf('chrome') > 0;
  68. this.opera = this.webkit && this.agent.indexOf(' OPR/') > 0; // Opera >= 15
  69. this.safari = !this.chrome && !this.opera && (this.webkit || this.agent_lc.indexOf('safari') > 0);
  70. this.konq = this.agent_lc.indexOf('konqueror') > 0;
  71. this.mz = this.dom && !this.chrome && !this.safari && !this.konq && !this.opera && this.agent.indexOf('Mozilla') >= 0;
  72. this.iphone = this.safari && (this.agent_lc.indexOf('iphone') > 0 || this.agent_lc.indexOf('ipod') > 0);
  73. this.ipad = this.safari && this.agent_lc.indexOf('ipad') > 0;
  74. }
  75. if (!this.vendver) {
  76. // common version strings
  77. this.vendver = /(opera|opr|khtml|chrome|safari|applewebkit|msie)(\s|\/)([0-9\.]+)/.test(this.agent_lc) ? parseFloat(RegExp.$3) : 0;
  78. // any other (Mozilla, Camino, IE>=11)
  79. if (!this.vendver)
  80. this.vendver = /rv:([0-9\.]+)/.test(this.agent) ? parseFloat(RegExp.$1) : 0;
  81. }
  82. // get real language out of safari's user agent
  83. if (this.safari && (/;\s+([a-z]{2})-[a-z]{2}\)/.test(this.agent_lc)))
  84. this.lang = RegExp.$1;
  85. this.tablet = /ipad|android|xoom|sch-i800|playbook|tablet|kindle/i.test(this.agent_lc);
  86. this.mobile = /iphone|ipod|blackberry|iemobile|opera mini|opera mobi|mobile/i.test(this.agent_lc);
  87. this.touch = this.mobile || this.tablet;
  88. this.cookies = n.cookieEnabled;
  89. // test for XMLHTTP support
  90. this.xmlhttp_test = function()
  91. {
  92. var activeX_test = new Function("try{var o=new ActiveXObject('Microsoft.XMLHTTP');return true;}catch(err){return false;}");
  93. this.xmlhttp = window.XMLHttpRequest || (('ActiveXObject' in window) && activeX_test());
  94. return this.xmlhttp;
  95. };
  96. // set class names to html tag according to the current user agent detection
  97. // this allows browser-specific css selectors like "html.chrome .someclass"
  98. this.set_html_class = function()
  99. {
  100. var classname = ' js';
  101. if (this.ie)
  102. classname += ' ie ie'+parseInt(this.vendver);
  103. else if (this.opera)
  104. classname += ' opera';
  105. else if (this.konq)
  106. classname += ' konqueror';
  107. else if (this.safari)
  108. classname += ' chrome';
  109. else if (this.chrome)
  110. classname += ' chrome';
  111. else if (this.mz)
  112. classname += ' mozilla';
  113. if (this.iphone)
  114. classname += ' iphone';
  115. else if (this.ipad)
  116. classname += ' ipad';
  117. else if (this.webkit)
  118. classname += ' webkit';
  119. if (this.mobile)
  120. classname += ' mobile';
  121. if (this.tablet)
  122. classname += ' tablet';
  123. if (document.documentElement)
  124. document.documentElement.className += classname;
  125. };
  126. };
  127. // static functions for DOM event handling
  128. var rcube_event = {
  129. /**
  130. * returns the event target element
  131. */
  132. get_target: function(e)
  133. {
  134. e = e || window.event;
  135. return e && e.target ? e.target : e.srcElement;
  136. },
  137. /**
  138. * returns the event key code
  139. */
  140. get_keycode: function(e)
  141. {
  142. e = e || window.event;
  143. return e && e.keyCode ? e.keyCode : (e && e.which ? e.which : 0);
  144. },
  145. /**
  146. * returns the event key code
  147. */
  148. get_button: function(e)
  149. {
  150. e = e || window.event;
  151. return e && e.button !== undefined ? e.button : (e && e.which ? e.which : 0);
  152. },
  153. /**
  154. * returns modifier key (constants defined at top of file)
  155. */
  156. get_modifier: function(e)
  157. {
  158. var opcode = 0;
  159. e = e || window.event;
  160. if (bw.mac && e)
  161. opcode += (e.metaKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
  162. else if (e)
  163. opcode += (e.ctrlKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
  164. return opcode;
  165. },
  166. /**
  167. * Return absolute mouse position of an event
  168. */
  169. get_mouse_pos: function(e)
  170. {
  171. if (!e) e = window.event;
  172. var mX = (e.pageX) ? e.pageX : e.clientX,
  173. mY = (e.pageY) ? e.pageY : e.clientY;
  174. if (document.body && document.all) {
  175. mX += document.body.scrollLeft;
  176. mY += document.body.scrollTop;
  177. }
  178. if (e._offset) {
  179. mX += e._offset.left;
  180. mY += e._offset.top;
  181. }
  182. return { x:mX, y:mY };
  183. },
  184. /**
  185. * Add an object method as event listener to a certain element
  186. */
  187. add_listener: function(p)
  188. {
  189. if (!p.object || !p.method) // not enough arguments
  190. return;
  191. if (!p.element)
  192. p.element = document;
  193. if (!p.object._rc_events)
  194. p.object._rc_events = {};
  195. var key = p.event + '*' + p.method;
  196. if (!p.object._rc_events[key])
  197. p.object._rc_events[key] = function(e){ return p.object[p.method](e); };
  198. if (p.element.addEventListener)
  199. p.element.addEventListener(p.event, p.object._rc_events[key], false);
  200. else if (p.element.attachEvent) {
  201. // IE allows multiple events with the same function to be applied to the same object
  202. // forcibly detach the event, then attach
  203. p.element.detachEvent('on'+p.event, p.object._rc_events[key]);
  204. p.element.attachEvent('on'+p.event, p.object._rc_events[key]);
  205. }
  206. else
  207. p.element['on'+p.event] = p.object._rc_events[key];
  208. },
  209. /**
  210. * Remove event listener
  211. */
  212. remove_listener: function(p)
  213. {
  214. if (!p.element)
  215. p.element = document;
  216. var key = p.event + '*' + p.method;
  217. if (p.object && p.object._rc_events && p.object._rc_events[key]) {
  218. if (p.element.removeEventListener)
  219. p.element.removeEventListener(p.event, p.object._rc_events[key], false);
  220. else if (p.element.detachEvent)
  221. p.element.detachEvent('on'+p.event, p.object._rc_events[key]);
  222. else
  223. p.element['on'+p.event] = null;
  224. }
  225. },
  226. /**
  227. * Prevent event propagation and bubbling
  228. */
  229. cancel: function(evt)
  230. {
  231. var e = evt ? evt : window.event;
  232. if (e.preventDefault)
  233. e.preventDefault();
  234. else
  235. e.returnValue = false;
  236. if (e.stopPropagation)
  237. e.stopPropagation();
  238. e.cancelBubble = true;
  239. return false;
  240. },
  241. /**
  242. * Determine whether the given event was trigered from keyboard
  243. */
  244. is_keyboard: function(e)
  245. {
  246. return e && (
  247. (e.pointerType !== undefined && e.pointerType !== 'mouse') || // IE 11+
  248. (e.mozInputSource && e.mozInputSource == e.MOZ_SOURCE_KEYBOARD) || // Firefox
  249. (e.offsetX === 0 && e.offsetY === 0) || // Opera
  250. (!e.pageX && (e.pageY || 0) <= 0 && !e.clientX && (e.clientY || 0) <= 0) || // others
  251. (bw.ie && rcube_event._last_keyboard_event && rcube_event._last_keyboard_event.target == e.target) // hack for IE <= 10
  252. );
  253. },
  254. /**
  255. * Accept event if triggered from keyboard action (e.g. <Enter>)
  256. */
  257. keyboard_only: function(e)
  258. {
  259. return rcube_event.is_keyboard(e) ? true : rcube_event.cancel(e);
  260. },
  261. touchevent: function(e)
  262. {
  263. return { pageX:e.pageX, pageY:e.pageY, offsetX:e.pageX - e.target.offsetLeft, offsetY:e.pageY - e.target.offsetTop, target:e.target, istouch:true };
  264. }
  265. };
  266. /**
  267. * rcmail objects event interface
  268. */
  269. function rcube_event_engine()
  270. {
  271. this._events = {};
  272. };
  273. rcube_event_engine.prototype = {
  274. /**
  275. * Setter for object event handlers
  276. *
  277. * @param {String} Event name
  278. * @param {Function} Handler function
  279. */
  280. addEventListener: function(evt, func, obj)
  281. {
  282. if (!this._events)
  283. this._events = {};
  284. if (!this._events[evt])
  285. this._events[evt] = [];
  286. this._events[evt].push({func:func, obj:obj ? obj : window});
  287. return this; // chainable
  288. },
  289. /**
  290. * Removes a specific event listener
  291. *
  292. * @param {String} Event name
  293. * @param {Int} Listener ID to remove
  294. */
  295. removeEventListener: function(evt, func, obj)
  296. {
  297. if (obj === undefined)
  298. obj = window;
  299. for (var h,i=0; this._events && this._events[evt] && i < this._events[evt].length; i++)
  300. if ((h = this._events[evt][i]) && h.func == func && h.obj == obj)
  301. this._events[evt][i] = null;
  302. },
  303. /**
  304. * This will execute all registered event handlers
  305. *
  306. * @param {String} Event to trigger
  307. * @param {Object} Event object/arguments
  308. */
  309. triggerEvent: function(evt, e)
  310. {
  311. var ret, h;
  312. if (e === undefined)
  313. e = this;
  314. else if (typeof e === 'object')
  315. e.event = evt;
  316. if (!this._event_exec)
  317. this._event_exec = {};
  318. if (this._events && this._events[evt] && !this._event_exec[evt]) {
  319. this._event_exec[evt] = true;
  320. for (var i=0; i < this._events[evt].length; i++) {
  321. if ((h = this._events[evt][i])) {
  322. if (typeof h.func === 'function')
  323. ret = h.func.call ? h.func.call(h.obj, e) : h.func(e);
  324. else if (typeof h.obj[h.func] === 'function')
  325. ret = h.obj[h.func](e);
  326. // cancel event execution
  327. if (ret !== undefined && !ret)
  328. break;
  329. }
  330. }
  331. if (ret && ret.event) {
  332. try {
  333. delete ret.event;
  334. } catch (err) {
  335. // IE6-7 doesn't support deleting HTMLFormElement attributes (#1488017)
  336. $(ret).removeAttr('event');
  337. }
  338. }
  339. }
  340. delete this._event_exec[evt];
  341. if (e.event) {
  342. try {
  343. delete e.event;
  344. } catch (err) {
  345. // IE6-7 doesn't support deleting HTMLFormElement attributes (#1488017)
  346. $(e).removeAttr('event');
  347. }
  348. }
  349. return ret;
  350. }
  351. }; // end rcube_event_engine.prototype
  352. // check if input is a valid email address
  353. // By Cal Henderson <cal@iamcal.com>
  354. // http://code.iamcal.com/php/rfc822/
  355. function rcube_check_email(input, inline)
  356. {
  357. if (input && window.RegExp) {
  358. var qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]',
  359. dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]',
  360. atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+',
  361. quoted_pair = '\\x5c[\\x00-\\x7f]',
  362. quoted_string = '\\x22('+qtext+'|'+quoted_pair+')*\\x22',
  363. ipv4 = '\\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\\]',
  364. ipv6 = '\\[IPv6:[0-9a-f:.]+\\]',
  365. ip_addr = '(' + ipv4 + ')|(' + ipv6 + ')',
  366. // Use simplified domain matching, because we need to allow Unicode characters here
  367. // So, e-mail address should be validated also on server side after idn_to_ascii() use
  368. //domain_literal = '\\x5b('+dtext+'|'+quoted_pair+')*\\x5d',
  369. //sub_domain = '('+atom+'|'+domain_literal+')',
  370. // allow punycode/unicode top-level domain
  371. domain = '(('+ip_addr+')|(([^@\\x2e]+\\x2e)+([^\\x00-\\x40\\x5b-\\x60\\x7b-\\x7f]{2,}|xn--[a-z0-9]{2,})))',
  372. // ICANN e-mail test (http://idn.icann.org/E-mail_test)
  373. icann_domains = [
  374. '\\u0645\\u062b\\u0627\\u0644\\x2e\\u0625\\u062e\\u062a\\u0628\\u0627\\u0631',
  375. '\\u4f8b\\u5b50\\x2e\\u6d4b\\u8bd5',
  376. '\\u4f8b\\u5b50\\x2e\\u6e2c\\u8a66',
  377. '\\u03c0\\u03b1\\u03c1\\u03ac\\u03b4\\u03b5\\u03b9\\u03b3\\u03bc\\u03b1\\x2e\\u03b4\\u03bf\\u03ba\\u03b9\\u03bc\\u03ae',
  378. '\\u0909\\u0926\\u093e\\u0939\\u0930\\u0923\\x2e\\u092a\\u0930\\u0940\\u0915\\u094d\\u0937\\u093e',
  379. '\\u4f8b\\u3048\\x2e\\u30c6\\u30b9\\u30c8',
  380. '\\uc2e4\\ub840\\x2e\\ud14c\\uc2a4\\ud2b8',
  381. '\\u0645\\u062b\\u0627\\u0644\\x2e\\u0622\\u0632\\u0645\\u0627\\u06cc\\u0634\u06cc',
  382. '\\u043f\\u0440\\u0438\\u043c\\u0435\\u0440\\x2e\\u0438\\u0441\\u043f\\u044b\\u0442\\u0430\\u043d\\u0438\\u0435',
  383. '\\u0b89\\u0ba4\\u0bbe\\u0bb0\\u0ba3\\u0bae\\u0bcd\\x2e\\u0baa\\u0bb0\\u0bbf\\u0b9f\\u0bcd\\u0b9a\\u0bc8',
  384. '\\u05d1\\u05f2\\u05b7\\u05e9\\u05e4\\u05bc\\u05d9\\u05dc\\x2e\\u05d8\\u05e2\\u05e1\\u05d8'
  385. ],
  386. icann_addr = 'mailtest\\x40('+icann_domains.join('|')+')',
  387. word = '('+atom+'|'+quoted_string+')',
  388. delim = '[,;\\s\\n]',
  389. local_part = word+'(\\x2e'+word+')*',
  390. addr_spec = '(('+local_part+'\\x40'+domain+')|('+icann_addr+'))',
  391. reg1 = inline ? new RegExp('(^|<|'+delim+')'+addr_spec+'($|>|'+delim+')', 'i') : new RegExp('^'+addr_spec+'$', 'i');
  392. return reg1.test(input) ? true : false;
  393. }
  394. return false;
  395. };
  396. // recursively copy an object
  397. function rcube_clone_object(obj)
  398. {
  399. var out = {};
  400. for (var key in obj) {
  401. if (obj[key] && typeof obj[key] === 'object')
  402. out[key] = rcube_clone_object(obj[key]);
  403. else
  404. out[key] = obj[key];
  405. }
  406. return out;
  407. };
  408. // make a string URL safe (and compatible with PHP's rawurlencode())
  409. function urlencode(str)
  410. {
  411. if (window.encodeURIComponent)
  412. return encodeURIComponent(str).replace('*', '%2A');
  413. return escape(str)
  414. .replace('+', '%2B')
  415. .replace('*', '%2A')
  416. .replace('/', '%2F')
  417. .replace('@', '%40');
  418. };
  419. // get any type of html objects by id/name
  420. function rcube_find_object(id, d)
  421. {
  422. var n, f, obj, e;
  423. if (!d) d = document;
  424. if (d.getElementById)
  425. if (obj = d.getElementById(id))
  426. return obj;
  427. if (!obj && d.getElementsByName && (e = d.getElementsByName(id)))
  428. obj = e[0];
  429. if (!obj && d.all)
  430. obj = d.all[id];
  431. if (!obj && d.images.length)
  432. obj = d.images[id];
  433. if (!obj && d.forms.length) {
  434. for (f=0; f<d.forms.length; f++) {
  435. if (d.forms[f].name == id)
  436. obj = d.forms[f];
  437. else if(d.forms[f].elements[id])
  438. obj = d.forms[f].elements[id];
  439. }
  440. }
  441. if (!obj && d.layers) {
  442. if (d.layers[id])
  443. obj = d.layers[id];
  444. for (n=0; !obj && n<d.layers.length; n++)
  445. obj = rcube_find_object(id, d.layers[n].document);
  446. }
  447. return obj;
  448. };
  449. // determine whether the mouse is over the given object or not
  450. function rcube_mouse_is_over(ev, obj)
  451. {
  452. var mouse = rcube_event.get_mouse_pos(ev),
  453. pos = $(obj).offset();
  454. return (mouse.x >= pos.left) && (mouse.x < (pos.left + obj.offsetWidth)) &&
  455. (mouse.y >= pos.top) && (mouse.y < (pos.top + obj.offsetHeight));
  456. };
  457. // cookie functions by GoogieSpell
  458. function setCookie(name, value, expires, path, domain, secure)
  459. {
  460. var curCookie = name + "=" + escape(value) +
  461. (expires ? "; expires=" + expires.toGMTString() : "") +
  462. (path ? "; path=" + path : "") +
  463. (domain ? "; domain=" + domain : "") +
  464. (secure ? "; secure" : "");
  465. document.cookie = curCookie;
  466. };
  467. function getCookie(name)
  468. {
  469. var dc = document.cookie,
  470. prefix = name + "=",
  471. begin = dc.indexOf("; " + prefix);
  472. if (begin == -1) {
  473. begin = dc.indexOf(prefix);
  474. if (begin != 0)
  475. return null;
  476. }
  477. else {
  478. begin += 2;
  479. }
  480. var end = dc.indexOf(";", begin);
  481. if (end == -1)
  482. end = dc.length;
  483. return unescape(dc.substring(begin + prefix.length, end));
  484. };
  485. // deprecated aliases, to be removed, use rcmail.set_cookie/rcmail.get_cookie
  486. roundcube_browser.prototype.set_cookie = setCookie;
  487. roundcube_browser.prototype.get_cookie = getCookie;
  488. var bw = new roundcube_browser();
  489. bw.set_html_class();
  490. // Add escape() method to RegExp object
  491. // http://dev.rubyonrails.org/changeset/7271
  492. RegExp.escape = function(str)
  493. {
  494. return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
  495. };
  496. // Extend Date prototype to detect Standard timezone without DST
  497. // from http://www.michaelapproved.com/articles/timezone-detect-and-ignore-daylight-saving-time-dst/
  498. Date.prototype.getStdTimezoneOffset = function()
  499. {
  500. var m = 12,
  501. d = new Date(null, m, 1),
  502. tzo = d.getTimezoneOffset();
  503. while (--m) {
  504. d.setUTCMonth(m);
  505. if (tzo != d.getTimezoneOffset()) {
  506. return Math.max(tzo, d.getTimezoneOffset());
  507. }
  508. }
  509. return tzo;
  510. }
  511. // define String's startsWith() method for old browsers
  512. if (!String.prototype.startsWith) {
  513. String.prototype.startsWith = function(search, position) {
  514. position = position || 0;
  515. return this.slice(position, search.length) === search;
  516. };
  517. }
  518. // array utility function
  519. jQuery.last = function(arr) {
  520. return arr && arr.length ? arr[arr.length-1] : undefined;
  521. }
  522. // jQuery plugin to emulate HTML5 placeholder attributes on input elements
  523. jQuery.fn.placeholder = function(text) {
  524. return this.each(function() {
  525. var active = false, elem = $(this);
  526. this.title = text;
  527. // Try HTML5 placeholder attribute first
  528. if ('placeholder' in this) {
  529. elem.attr('placeholder', text);
  530. }
  531. // Fallback to Javascript emulation of placeholder
  532. else {
  533. this._placeholder = text;
  534. elem.blur(function(e) {
  535. if ($.trim(elem.val()) == "")
  536. elem.val(text);
  537. elem.triggerHandler('change');
  538. })
  539. .focus(function(e) {
  540. if ($.trim(elem.val()) == text)
  541. elem.val("");
  542. elem.triggerHandler('change');
  543. })
  544. .change(function(e) {
  545. var active = elem.val() == text;
  546. elem[(active ? 'addClass' : 'removeClass')]('placeholder').attr('spellcheck', active);
  547. });
  548. // Do not blur currently focused element (catch exception: #1489008)
  549. try { active = this == document.activeElement; } catch(e) {}
  550. if (!active)
  551. elem.blur();
  552. }
  553. });
  554. };
  555. // function to parse query string into an object
  556. rcube_parse_query = function(query)
  557. {
  558. if (!query)
  559. return {};
  560. var params = {}, e, k, v,
  561. re = /([^&=]+)=?([^&]*)/g,
  562. decodeRE = /\+/g, // Regex for replacing addition symbol with a space
  563. decode = function (str) { return decodeURIComponent(str.replace(decodeRE, ' ')); };
  564. query = query.replace(/\?/, '');
  565. while (e = re.exec(query)) {
  566. k = decode(e[1]);
  567. v = decode(e[2]);
  568. if (k.substring(k.length - 2) === '[]') {
  569. k = k.substring(0, k.length - 2);
  570. (params[k] || (params[k] = [])).push(v);
  571. }
  572. else
  573. params[k] = v;
  574. }
  575. return params;
  576. };
  577. // Base64 code from Tyler Akins -- http://rumkin.com
  578. var Base64 = (function () {
  579. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  580. // private method for UTF-8 encoding
  581. var utf8_encode = function(string) {
  582. string = string.replace(/\r\n/g, "\n");
  583. var utftext = '';
  584. for (var n = 0; n < string.length; n++) {
  585. var c = string.charCodeAt(n);
  586. if (c < 128) {
  587. utftext += String.fromCharCode(c);
  588. }
  589. else if(c > 127 && c < 2048) {
  590. utftext += String.fromCharCode((c >> 6) | 192);
  591. utftext += String.fromCharCode((c & 63) | 128);
  592. }
  593. else {
  594. utftext += String.fromCharCode((c >> 12) | 224);
  595. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  596. utftext += String.fromCharCode((c & 63) | 128);
  597. }
  598. }
  599. return utftext;
  600. };
  601. // private method for UTF-8 decoding
  602. var utf8_decode = function (utftext) {
  603. var i = 0, string = '', c = c2 = c3 = 0;
  604. while (i < utftext.length) {
  605. c = utftext.charCodeAt(i);
  606. if (c < 128) {
  607. string += String.fromCharCode(c);
  608. i++;
  609. }
  610. else if (c > 191 && c < 224) {
  611. c2 = utftext.charCodeAt(i + 1);
  612. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  613. i += 2;
  614. }
  615. else {
  616. c2 = utftext.charCodeAt(i + 1);
  617. c3 = utftext.charCodeAt(i + 2);
  618. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  619. i += 3;
  620. }
  621. }
  622. return string;
  623. };
  624. var obj = {
  625. /**
  626. * Encodes a string in base64
  627. * @param {String} input The string to encode in base64.
  628. */
  629. encode: function (input) {
  630. // encode UTF8 as btoa() may fail on some characters
  631. input = utf8_encode(input);
  632. if (typeof(window.btoa) === 'function') {
  633. try {
  634. return btoa(input);
  635. }
  636. catch (e) {};
  637. }
  638. var chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0, output = '', len = input.length;
  639. while (i < len) {
  640. chr1 = input.charCodeAt(i++);
  641. chr2 = input.charCodeAt(i++);
  642. chr3 = input.charCodeAt(i++);
  643. enc1 = chr1 >> 2;
  644. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  645. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  646. enc4 = chr3 & 63;
  647. if (isNaN(chr2))
  648. enc3 = enc4 = 64;
  649. else if (isNaN(chr3))
  650. enc4 = 64;
  651. output = output
  652. + keyStr.charAt(enc1) + keyStr.charAt(enc2)
  653. + keyStr.charAt(enc3) + keyStr.charAt(enc4);
  654. }
  655. return output;
  656. },
  657. /**
  658. * Decodes a base64 string.
  659. * @param {String} input The string to decode.
  660. */
  661. decode: function (input) {
  662. if (typeof(window.atob) === 'function') {
  663. try {
  664. return utf8_decode(atob(input));
  665. }
  666. catch (e) {};
  667. }
  668. var chr1, chr2, chr3, enc1, enc2, enc3, enc4, len, i = 0, output = '';
  669. // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  670. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  671. len = input.length;
  672. while (i < len) {
  673. enc1 = keyStr.indexOf(input.charAt(i++));
  674. enc2 = keyStr.indexOf(input.charAt(i++));
  675. enc3 = keyStr.indexOf(input.charAt(i++));
  676. enc4 = keyStr.indexOf(input.charAt(i++));
  677. chr1 = (enc1 << 2) | (enc2 >> 4);
  678. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  679. chr3 = ((enc3 & 3) << 6) | enc4;
  680. output = output + String.fromCharCode(chr1);
  681. if (enc3 != 64)
  682. output = output + String.fromCharCode(chr2);
  683. if (enc4 != 64)
  684. output = output + String.fromCharCode(chr3);
  685. }
  686. return utf8_decode(output);
  687. }
  688. };
  689. return obj;
  690. })();