PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/files/w2ui/1.4.3/w2ui.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 1093 lines | 907 code | 92 blank | 94 comment | 314 complexity | 52bd9c26a2c64db6f0eb83305fd7c4f1 MD5 | raw file
  1. /* w2ui 1.4.3 (c) http://w2ui.com, vitmalina@gmail.com */
  2. var w2ui = w2ui || {};
  3. var w2obj = w2obj || {}; // expose object to be able to overwrite default functions
  4. /************************************************
  5. * Library: Web 2.0 UI for jQuery
  6. * - Following objects are defines
  7. * - w2ui - object that will contain all widgets
  8. * - w2obj - object with widget prototypes
  9. * - w2utils - basic utilities
  10. * - $().w2render - common render
  11. * - $().w2destroy - common destroy
  12. * - $().w2marker - marker plugin
  13. * - $().w2tag - tag plugin
  14. * - $().w2overlay - overlay plugin
  15. * - $().w2menu - menu plugin
  16. * - w2utils.event - generic event object
  17. * - w2utils.keyboard - object for keyboard navigation
  18. * - Dependencies: jQuery
  19. *
  20. * == NICE TO HAVE ==
  21. * - overlay should be displayed where more space (on top or on bottom)
  22. * - write and article how to replace certain framework functions
  23. * - onComplete should pass widget as context (this)
  24. * - add maxHeight for the w2menu
  25. * - user localization from another lib (make it generic), https://github.com/jquery/globalize#readme
  26. * - hidden and disabled in menus
  27. * - isTime should support seconds
  28. * - TEST On IOS
  29. *
  30. ************************************************/
  31. var w2utils = (function () {
  32. var tmp = {}; // for some temp variables
  33. var obj = {
  34. version : '1.4.3',
  35. settings : {
  36. "locale" : "en-us",
  37. "date_format" : "m/d/yyyy",
  38. "date_display" : "Mon d, yyyy",
  39. "time_format" : "hh:mi pm",
  40. "currencyPrefix" : "$",
  41. "currencySuffix" : "",
  42. "currencyPrecision" : 2,
  43. "groupSymbol" : ",",
  44. "decimalSymbol" : ".",
  45. "shortmonths" : ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  46. "fullmonths" : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  47. "shortdays" : ["M", "T", "W", "T", "F", "S", "S"],
  48. "fulldays" : ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
  49. "dataType" : 'HTTP', // can be HTTP, RESTFULL, JSON (case sensative)
  50. "phrases" : {} // empty object for english phrases
  51. },
  52. isInt : isInt,
  53. isFloat : isFloat,
  54. isMoney : isMoney,
  55. isHex : isHex,
  56. isAlphaNumeric : isAlphaNumeric,
  57. isEmail : isEmail,
  58. isDate : isDate,
  59. isTime : isTime,
  60. age : age,
  61. date : date,
  62. size : size,
  63. formatNumber : formatNumber,
  64. formatDate : formatDate,
  65. formatTime : formatTime,
  66. formatDateTime : formatDateTime,
  67. stripTags : stripTags,
  68. encodeTags : encodeTags,
  69. escapeId : escapeId,
  70. base64encode : base64encode,
  71. base64decode : base64decode,
  72. transition : transition,
  73. lock : lock,
  74. unlock : unlock,
  75. lang : lang,
  76. locale : locale,
  77. getSize : getSize,
  78. scrollBarSize : scrollBarSize,
  79. checkName : checkName,
  80. checkUniqueId : checkUniqueId,
  81. parseRoute : parseRoute,
  82. // some internal variables
  83. isIOS : ((navigator.userAgent.toLowerCase().indexOf('iphone') != -1 ||
  84. navigator.userAgent.toLowerCase().indexOf('ipod') != -1 ||
  85. navigator.userAgent.toLowerCase().indexOf('ipad') != -1)
  86. ? true : false),
  87. isIE : ((navigator.userAgent.toLowerCase().indexOf('msie') != -1 ||
  88. navigator.userAgent.toLowerCase().indexOf('trident') != -1 )
  89. ? true : false)
  90. };
  91. return obj;
  92. function isInt (val) {
  93. var re = /^[-+]?[0-9]+$/;
  94. return re.test(val);
  95. }
  96. function isFloat (val) {
  97. if (typeof val == 'string') val = val.replace(w2utils.settings.decimalSymbol, '.');
  98. return (typeof val === 'number' || (typeof val === 'string' && val !== '')) && !isNaN(Number(val));
  99. }
  100. function isMoney (val) {
  101. var se = w2utils.settings;
  102. var re = new RegExp('^'+ (se.currencyPrefix ? '\\' + se.currencyPrefix + '?' : '') +'[-+]?[0-9]*[\\'+ w2utils.settings.decimalSymbol +']?[0-9]+'+ (se.currencySuffix ? '\\' + se.currencySuffix + '?' : '') +'$', 'i');
  103. if (typeof val === 'string') {
  104. val = val.replace(new RegExp(se.groupSymbol, 'g'), '');
  105. }
  106. if (typeof val === 'object' || val === '') return false;
  107. return re.test(val);
  108. }
  109. function isHex (val) {
  110. var re = /^[a-fA-F0-9]+$/;
  111. return re.test(val);
  112. }
  113. function isAlphaNumeric (val) {
  114. var re = /^[a-zA-Z0-9_-]+$/;
  115. return re.test(val);
  116. }
  117. function isEmail (val) {
  118. var email = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
  119. return email.test(val);
  120. }
  121. function isDate (val, format, retDate) {
  122. if (!val) return false;
  123. var dt = 'Invalid Date';
  124. var month, day, year;
  125. if (format == null) format = w2utils.settings.date_format;
  126. if (typeof val.getUTCFullYear === 'function' && typeof val.getUTCMonth === 'function' && typeof val.getUTCDate === 'function') {
  127. year = val.getUTCFullYear();
  128. month = val.getUTCMonth();
  129. day = val.getUTCDate();
  130. } else if (typeof val.getFullYear === 'function' && typeof val.getMonth === 'function' && typeof val.getDate === 'function') {
  131. year = val.getFullYear();
  132. month = val.getMonth();
  133. day = val.getDate();
  134. } else {
  135. val = String(val);
  136. // convert month formats
  137. if (RegExp('mon', 'ig').test(format)) {
  138. format = format.replace(/month/ig, 'm').replace(/mon/ig, 'm').replace(/dd/ig, 'd').replace(/[, ]/ig, '/').replace(/\/\//g, '/').toLowerCase();
  139. val = val.replace(/[, ]/ig, '/').replace(/\/\//g, '/').toLowerCase();
  140. for (var m = 0, len = w2utils.settings.fullmonths.length; m < len; m++) {
  141. var t = w2utils.settings.fullmonths[m];
  142. val = val.replace(RegExp(t, 'ig'), (parseInt(m) + 1)).replace(RegExp(t.substr(0, 3), 'ig'), (parseInt(m) + 1));
  143. }
  144. }
  145. // format date
  146. var tmp = val.replace(/-/g, '/').replace(/\./g, '/').toLowerCase().split('/');
  147. var tmp2 = format.replace(/-/g, '/').replace(/\./g, '/').toLowerCase();
  148. if (tmp2 === 'mm/dd/yyyy') { month = tmp[0]; day = tmp[1]; year = tmp[2]; }
  149. if (tmp2 === 'm/d/yyyy') { month = tmp[0]; day = tmp[1]; year = tmp[2]; }
  150. if (tmp2 === 'dd/mm/yyyy') { month = tmp[1]; day = tmp[0]; year = tmp[2]; }
  151. if (tmp2 === 'd/m/yyyy') { month = tmp[1]; day = tmp[0]; year = tmp[2]; }
  152. if (tmp2 === 'yyyy/dd/mm') { month = tmp[2]; day = tmp[1]; year = tmp[0]; }
  153. if (tmp2 === 'yyyy/d/m') { month = tmp[2]; day = tmp[1]; year = tmp[0]; }
  154. if (tmp2 === 'yyyy/mm/dd') { month = tmp[1]; day = tmp[2]; year = tmp[0]; }
  155. if (tmp2 === 'yyyy/m/d') { month = tmp[1]; day = tmp[2]; year = tmp[0]; }
  156. if (tmp2 === 'mm/dd/yy') { month = tmp[0]; day = tmp[1]; year = tmp[2]; }
  157. if (tmp2 === 'm/d/yy') { month = tmp[0]; day = tmp[1]; year = parseInt(tmp[2]) + 1900; }
  158. if (tmp2 === 'dd/mm/yy') { month = tmp[1]; day = tmp[0]; year = parseInt(tmp[2]) + 1900; }
  159. if (tmp2 === 'd/m/yy') { month = tmp[1]; day = tmp[0]; year = parseInt(tmp[2]) + 1900; }
  160. if (tmp2 === 'yy/dd/mm') { month = tmp[2]; day = tmp[1]; year = parseInt(tmp[0]) + 1900; }
  161. if (tmp2 === 'yy/d/m') { month = tmp[2]; day = tmp[1]; year = parseInt(tmp[0]) + 1900; }
  162. if (tmp2 === 'yy/mm/dd') { month = tmp[1]; day = tmp[2]; year = parseInt(tmp[0]) + 1900; }
  163. if (tmp2 === 'yy/m/d') { month = tmp[1]; day = tmp[2]; year = parseInt(tmp[0]) + 1900; }
  164. }
  165. if (!isInt(year)) return false;
  166. if (!isInt(month)) return false;
  167. if (!isInt(day)) return false;
  168. year = +year;
  169. month = +month;
  170. day = +day;
  171. dt = new Date(year, month - 1, day);
  172. // do checks
  173. if (month == null) return false;
  174. if (String(dt) == 'Invalid Date') return false;
  175. if ((dt.getMonth() + 1 !== month) || (dt.getDate() !== day) || (dt.getFullYear() !== year)) return false;
  176. if (retDate === true) return dt; else return true;
  177. }
  178. function isTime (val, retTime) {
  179. // Both formats 10:20pm and 22:20
  180. if (val == null) return false;
  181. var max, pm;
  182. // -- process american format
  183. val = String(val);
  184. val = val.toUpperCase();
  185. pm = val.indexOf('PM') >= 0;
  186. var ampm = (pm || val.indexOf('AM') >= 0);
  187. if (ampm) max = 12; else max = 24;
  188. val = val.replace('AM', '').replace('PM', '');
  189. val = $.trim(val);
  190. // ---
  191. var tmp = val.split(':');
  192. var h = parseInt(tmp[0] || 0), m = parseInt(tmp[1] || 0);
  193. // accept edge case: 3PM is a good timestamp, but 3 (without AM or PM) is NOT:
  194. if ((!ampm || tmp.length !== 1) && tmp.length !== 2) { return false; }
  195. if (tmp[0] === '' || h < 0 || h > max || !this.isInt(tmp[0]) || tmp[0].length > 2) { return false; }
  196. if (tmp.length === 2 && (tmp[1] === '' || m < 0 || m > 59 || !this.isInt(tmp[1]) || tmp[1].length !== 2)) { return false; }
  197. // check the edge cases: 12:01AM is ok, as is 12:01PM, but 24:01 is NOT ok while 24:00 is (midnight; equivalent to 00:00).
  198. // meanwhile, there is 00:00 which is ok, but 0AM nor 0PM are okay, while 0:01AM and 0:00AM are.
  199. if (!ampm && max === h && m !== 0) { return false; }
  200. if (ampm && tmp.length === 1 && h === 0) { return false; }
  201. if (retTime === true) {
  202. if (pm) h += 12;
  203. return {
  204. hours: h,
  205. minutes: m
  206. };
  207. }
  208. return true;
  209. }
  210. function age (dateStr) {
  211. if (dateStr === '' || dateStr == null) return '';
  212. var d1 = new Date(dateStr);
  213. if (w2utils.isInt(dateStr)) d1 = new Date(Number(dateStr)); // for unix timestamps
  214. if (String(d1) == 'Invalid Date') return '';
  215. var d2 = new Date();
  216. var sec = (d2.getTime() - d1.getTime()) / 1000;
  217. var amount = '';
  218. var type = '';
  219. if (sec < 0) {
  220. amount = '<span style="color: #aaa">future</span>';
  221. type = '';
  222. } else if (sec < 60) {
  223. amount = Math.floor(sec);
  224. type = 'sec';
  225. if (sec < 0) { amount = 0; type = 'sec'; }
  226. } else if (sec < 60*60) {
  227. amount = Math.floor(sec/60);
  228. type = 'min';
  229. } else if (sec < 24*60*60) {
  230. amount = Math.floor(sec/60/60);
  231. type = 'hour';
  232. } else if (sec < 30*24*60*60) {
  233. amount = Math.floor(sec/24/60/60);
  234. type = 'day';
  235. } else if (sec < 365.25*24*60*60) {
  236. amount = Math.floor(sec/365.25/24/60/60*10)/10;
  237. type = 'month';
  238. } else if (sec >= 365.25*24*60*60) {
  239. amount = Math.floor(sec/365.25/24/60/60*10)/10;
  240. type = 'year';
  241. }
  242. return amount + ' ' + type + (amount > 1 ? 's' : '');
  243. }
  244. function date (dateStr) {
  245. if (dateStr === '' || dateStr == null) return '';
  246. var d1 = new Date(dateStr);
  247. if (w2utils.isInt(dateStr)) d1 = new Date(Number(dateStr)); // for unix timestamps
  248. if (String(d1) == 'Invalid Date') return '';
  249. var months = w2utils.settings.shortmonths;
  250. var d2 = new Date(); // today
  251. var d3 = new Date();
  252. d3.setTime(d3.getTime() - 86400000); // yesterday
  253. var dd1 = months[d1.getMonth()] + ' ' + d1.getDate() + ', ' + d1.getFullYear();
  254. var dd2 = months[d2.getMonth()] + ' ' + d2.getDate() + ', ' + d2.getFullYear();
  255. var dd3 = months[d3.getMonth()] + ' ' + d3.getDate() + ', ' + d3.getFullYear();
  256. var time = (d1.getHours() - (d1.getHours() > 12 ? 12 :0)) + ':' + (d1.getMinutes() < 10 ? '0' : '') + d1.getMinutes() + ' ' + (d1.getHours() >= 12 ? 'pm' : 'am');
  257. var time2= (d1.getHours() - (d1.getHours() > 12 ? 12 :0)) + ':' + (d1.getMinutes() < 10 ? '0' : '') + d1.getMinutes() + ':' + (d1.getSeconds() < 10 ? '0' : '') + d1.getSeconds() + ' ' + (d1.getHours() >= 12 ? 'pm' : 'am');
  258. var dsp = dd1;
  259. if (dd1 === dd2) dsp = time;
  260. if (dd1 === dd3) dsp = w2utils.lang('Yesterday');
  261. return '<span title="'+ dd1 +' ' + time2 +'">'+ dsp +'</span>';
  262. }
  263. function size (sizeStr) {
  264. if (!w2utils.isFloat(sizeStr) || sizeStr === '') return '';
  265. sizeStr = parseFloat(sizeStr);
  266. if (sizeStr === 0) return 0;
  267. var sizes = ['Bt', 'KB', 'MB', 'GB', 'TB'];
  268. var i = parseInt( Math.floor( Math.log(sizeStr) / Math.log(1024) ) );
  269. return (Math.floor(sizeStr / Math.pow(1024, i) * 10) / 10).toFixed(i === 0 ? 0 : 1) + ' ' + sizes[i];
  270. }
  271. function formatNumber (val, groupSymbol, decimalSymbol) {
  272. var ret = '';
  273. if (groupSymbol == null) groupSymbol = w2utils.settings.groupSymbol || ',';
  274. if (decimalSymbol == null) decimalSymbol = w2utils.settings.decimalSymbol || '.';
  275. // check if this is a number
  276. if (w2utils.isFloat(val) || w2utils.isInt(val) || w2utils.isMoney(val)) {
  277. tmp = String(val).split('.');
  278. ret = String(tmp[0]).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + groupSymbol);
  279. if (tmp[1] != null) ret += w2utils.settings.decimalSymbol + tmp[1];
  280. }
  281. return ret;
  282. }
  283. function formatDate (dateStr, format) { // IMPORTANT dateStr HAS TO BE valid JavaScript Date String
  284. var months = w2utils.settings.shortmonths;
  285. var fullMonths = w2utils.settings.fullmonths;
  286. if (!format) format = this.settings.date_format;
  287. if (dateStr === '' || dateStr == null || (typeof dateStr == 'object' && !dateStr.getMonth)) return '';
  288. var dt = new Date(dateStr);
  289. if (w2utils.isInt(dateStr)) dt = new Date(Number(dateStr)); // for unix timestamps
  290. if (String(dt) == 'Invalid Date') return '';
  291. var year = dt.getFullYear();
  292. var month = dt.getMonth();
  293. var date = dt.getDate();
  294. return format.toLowerCase()
  295. .replace('month', w2utils.settings.fullmonths[month])
  296. .replace('mon', w2utils.settings.shortmonths[month])
  297. .replace(/yyyy/g, year)
  298. .replace(/yyy/g, year)
  299. .replace(/yy/g, year > 2000 ? 100 + parseInt(String(year).substr(2)) : String(year).substr(2))
  300. .replace(/(^|[^a-z$])y/g, '$1' + year) // only y's that are not preceeded by a letter
  301. .replace(/mm/g, (month + 1 < 10 ? '0' : '') + (month + 1))
  302. .replace(/dd/g, (date < 10 ? '0' : '') + date)
  303. .replace(/th/g, (date == 1 ? 'st' : 'th'))
  304. .replace(/th/g, (date == 2 ? 'nd' : 'th'))
  305. .replace(/th/g, (date == 3 ? 'rd' : 'th'))
  306. .replace(/(^|[^a-z$])m/g, '$1' + (month + 1)) // only y's that are not preceeded by a letter
  307. .replace(/(^|[^a-z$])d/g, '$1' + date); // only y's that are not preceeded by a letter
  308. }
  309. function formatTime (dateStr, format) { // IMPORTANT dateStr HAS TO BE valid JavaScript Date String
  310. var months = w2utils.settings.shortmonths;
  311. var fullMonths = w2utils.settings.fullmonths;
  312. if (!format) format = this.settings.time_format;
  313. if (dateStr === '' || dateStr == null || (typeof dateStr == 'object' && !dateStr.getMonth)) return '';
  314. var dt = new Date(dateStr);
  315. if (w2utils.isInt(dateStr)) dt = new Date(Number(dateStr)); // for unix timestamps
  316. if (w2utils.isTime(dateStr)) {
  317. var tmp = w2utils.isTime(dateStr, true);
  318. dt = new Date();
  319. dt.setHours(tmp.hours);
  320. dt.setMinutes(tmp.minutes);
  321. }
  322. if (String(dt) == 'Invalid Date') return '';
  323. var type = 'am';
  324. var hour = dt.getHours();
  325. var h24 = dt.getHours();
  326. var min = dt.getMinutes();
  327. var sec = dt.getSeconds();
  328. if (min < 10) min = '0' + min;
  329. if (sec < 10) sec = '0' + sec;
  330. if (format.indexOf('am') !== -1 || format.indexOf('pm') !== -1) {
  331. if (hour >= 12) type = 'pm';
  332. if (hour > 12) hour = hour - 12;
  333. }
  334. return format.toLowerCase()
  335. .replace('am', type)
  336. .replace('pm', type)
  337. .replace('hhh', (hour < 10 ? '0' + hour : hour))
  338. .replace('hh24', (h24 < 10 ? '0' + h24 : h24))
  339. .replace('h24', h24)
  340. .replace('hh', hour)
  341. .replace('mm', min)
  342. .replace('mi', min)
  343. .replace('ss', sec)
  344. .replace(/(^|[^a-z$])h/g, '$1' + hour) // only y's that are not preceeded by a letter
  345. .replace(/(^|[^a-z$])m/g, '$1' + min) // only y's that are not preceeded by a letter
  346. .replace(/(^|[^a-z$])s/g, '$1' + sec); // only y's that are not preceeded by a letter
  347. }
  348. function formatDateTime(dateStr, format) {
  349. var fmt;
  350. if (dateStr === '' || dateStr == null || (typeof dateStr == 'object' && !dateStr.getMonth)) return '';
  351. if (typeof format !== 'string') {
  352. fmt = [this.settings.date_format, this.settings.time_format];
  353. } else {
  354. fmt = format.split('|');
  355. }
  356. return this.formatDate(dateStr, fmt[0]) + ' ' + this.formatTime(dateStr, fmt[1]);
  357. }
  358. function stripTags (html) {
  359. if (html === null) return html;
  360. switch (typeof html) {
  361. case 'number':
  362. break;
  363. case 'string':
  364. html = $.trim(String(html).replace(/(<([^>]+)>)/ig, ""));
  365. break;
  366. case 'object':
  367. for (var a in html) html[a] = this.stripTags(html[a]);
  368. break;
  369. }
  370. return html;
  371. }
  372. function encodeTags (html) {
  373. if (html === null) return html;
  374. switch (typeof html) {
  375. case 'number':
  376. break;
  377. case 'string':
  378. html = String(html).replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
  379. break;
  380. case 'object':
  381. for (var a in html) html[a] = this.encodeTags(html[a]);
  382. break;
  383. }
  384. return html;
  385. }
  386. function escapeId (id) {
  387. if (id === '' || id == null) return '';
  388. return String(id).replace(/([;&,\.\+\*\~'`:"\!\^#$%@\[\]\(\)=<>\|\/? {}\\])/g, '\\$1');
  389. }
  390. function base64encode (input) {
  391. var output = "";
  392. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  393. var i = 0;
  394. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  395. input = utf8_encode(input);
  396. while (i < input.length) {
  397. chr1 = input.charCodeAt(i++);
  398. chr2 = input.charCodeAt(i++);
  399. chr3 = input.charCodeAt(i++);
  400. enc1 = chr1 >> 2;
  401. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  402. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  403. enc4 = chr3 & 63;
  404. if (isNaN(chr2)) {
  405. enc3 = enc4 = 64;
  406. } else if (isNaN(chr3)) {
  407. enc4 = 64;
  408. }
  409. output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
  410. }
  411. function utf8_encode (string) {
  412. var string = String(string).replace(/\r\n/g,"\n");
  413. var utftext = "";
  414. for (var n = 0; n < string.length; n++) {
  415. var c = string.charCodeAt(n);
  416. if (c < 128) {
  417. utftext += String.fromCharCode(c);
  418. }
  419. else if((c > 127) && (c < 2048)) {
  420. utftext += String.fromCharCode((c >> 6) | 192);
  421. utftext += String.fromCharCode((c & 63) | 128);
  422. }
  423. else {
  424. utftext += String.fromCharCode((c >> 12) | 224);
  425. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  426. utftext += String.fromCharCode((c & 63) | 128);
  427. }
  428. }
  429. return utftext;
  430. }
  431. return output;
  432. }
  433. function base64decode (input) {
  434. var output = "";
  435. var chr1, chr2, chr3;
  436. var enc1, enc2, enc3, enc4;
  437. var i = 0;
  438. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  439. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  440. while (i < input.length) {
  441. enc1 = keyStr.indexOf(input.charAt(i++));
  442. enc2 = keyStr.indexOf(input.charAt(i++));
  443. enc3 = keyStr.indexOf(input.charAt(i++));
  444. enc4 = keyStr.indexOf(input.charAt(i++));
  445. chr1 = (enc1 << 2) | (enc2 >> 4);
  446. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  447. chr3 = ((enc3 & 3) << 6) | enc4;
  448. output = output + String.fromCharCode(chr1);
  449. if (enc3 !== 64) {
  450. output = output + String.fromCharCode(chr2);
  451. }
  452. if (enc4 !== 64) {
  453. output = output + String.fromCharCode(chr3);
  454. }
  455. }
  456. output = utf8_decode(output);
  457. function utf8_decode (utftext) {
  458. var string = "";
  459. var i = 0;
  460. var c = 0, c2, c3;
  461. while ( i < utftext.length ) {
  462. c = utftext.charCodeAt(i);
  463. if (c < 128) {
  464. string += String.fromCharCode(c);
  465. i++;
  466. }
  467. else if((c > 191) && (c < 224)) {
  468. c2 = utftext.charCodeAt(i+1);
  469. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  470. i += 2;
  471. }
  472. else {
  473. c2 = utftext.charCodeAt(i+1);
  474. c3 = utftext.charCodeAt(i+2);
  475. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  476. i += 3;
  477. }
  478. }
  479. return string;
  480. }
  481. return output;
  482. }
  483. function transition (div_old, div_new, type, callBack) {
  484. var width = $(div_old).width();
  485. var height = $(div_old).height();
  486. var time = 0.5;
  487. if (!div_old || !div_new) {
  488. console.log('ERROR: Cannot do transition when one of the divs is null');
  489. return;
  490. }
  491. div_old.parentNode.style.cssText += cross('perspective', '700px') +'; overflow: hidden;';
  492. div_old.style.cssText += '; position: absolute; z-index: 1019; '+ cross('backface-visibility', 'hidden');
  493. div_new.style.cssText += '; position: absolute; z-index: 1020; '+ cross('backface-visibility', 'hidden');
  494. switch (type) {
  495. case 'slide-left':
  496. // init divs
  497. div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  498. div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d('+ width + 'px, 0, 0)', 'translate('+ width +'px, 0)');
  499. $(div_new).show();
  500. // -- need a timing function because otherwise not working
  501. window.setTimeout(function() {
  502. div_new.style.cssText += cross('transition', time+'s') +';'+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  503. div_old.style.cssText += cross('transition', time+'s') +';'+ cross('transform', 'translate3d(-'+ width +'px, 0, 0)', 'translate(-'+ width +'px, 0)');
  504. }, 1);
  505. break;
  506. case 'slide-right':
  507. // init divs
  508. div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  509. div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(-'+ width +'px, 0, 0)', 'translate(-'+ width +'px, 0)');
  510. $(div_new).show();
  511. // -- need a timing function because otherwise not working
  512. window.setTimeout(function() {
  513. div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0px, 0, 0)', 'translate(0px, 0)');
  514. div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d('+ width +'px, 0, 0)', 'translate('+ width +'px, 0)');
  515. }, 1);
  516. break;
  517. case 'slide-down':
  518. // init divs
  519. div_old.style.cssText += 'overflow: hidden; z-index: 1; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  520. div_new.style.cssText += 'overflow: hidden; z-index: 0; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  521. $(div_new).show();
  522. // -- need a timing function because otherwise not working
  523. window.setTimeout(function() {
  524. div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  525. div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, '+ height +'px, 0)', 'translate(0, '+ height +'px)');
  526. }, 1);
  527. break;
  528. case 'slide-up':
  529. // init divs
  530. div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  531. div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, '+ height +'px, 0)', 'translate(0, '+ height +'px)');
  532. $(div_new).show();
  533. // -- need a timing function because otherwise not working
  534. window.setTimeout(function() {
  535. div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  536. div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  537. }, 1);
  538. break;
  539. case 'flip-left':
  540. // init divs
  541. div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateY(0deg)');
  542. div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateY(-180deg)');
  543. $(div_new).show();
  544. // -- need a timing function because otherwise not working
  545. window.setTimeout(function() {
  546. div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(0deg)');
  547. div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(180deg)');
  548. }, 1);
  549. break;
  550. case 'flip-right':
  551. // init divs
  552. div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateY(0deg)');
  553. div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateY(180deg)');
  554. $(div_new).show();
  555. // -- need a timing function because otherwise not working
  556. window.setTimeout(function() {
  557. div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(0deg)');
  558. div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(-180deg)');
  559. }, 1);
  560. break;
  561. case 'flip-down':
  562. // init divs
  563. div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(0deg)');
  564. div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(180deg)');
  565. $(div_new).show();
  566. // -- need a timing function because otherwise not working
  567. window.setTimeout(function() {
  568. div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(0deg)');
  569. div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(-180deg)');
  570. }, 1);
  571. break;
  572. case 'flip-up':
  573. // init divs
  574. div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(0deg)');
  575. div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(-180deg)');
  576. $(div_new).show();
  577. // -- need a timing function because otherwise not working
  578. window.setTimeout(function() {
  579. div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(0deg)');
  580. div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(180deg)');
  581. }, 1);
  582. break;
  583. case 'pop-in':
  584. // init divs
  585. div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  586. div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') + '; '+ cross('transform', 'scale(.8)') + '; opacity: 0;';
  587. $(div_new).show();
  588. // -- need a timing function because otherwise not working
  589. window.setTimeout(function() {
  590. div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'scale(1)') +'; opacity: 1;';
  591. div_old.style.cssText += cross('transition', time+'s') +';';
  592. }, 1);
  593. break;
  594. case 'pop-out':
  595. // init divs
  596. div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') +'; '+ cross('transform', 'scale(1)') +'; opacity: 1;';
  597. div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') +'; opacity: 0;';
  598. $(div_new).show();
  599. // -- need a timing function because otherwise not working
  600. window.setTimeout(function() {
  601. div_new.style.cssText += cross('transition', time+'s') +'; opacity: 1;';
  602. div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'scale(1.7)') +'; opacity: 0;';
  603. }, 1);
  604. break;
  605. default:
  606. // init divs
  607. div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
  608. div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') +'; opacity: 0;';
  609. $(div_new).show();
  610. // -- need a timing function because otherwise not working
  611. window.setTimeout(function() {
  612. div_new.style.cssText += cross('transition', time +'s') +'; opacity: 1;';
  613. div_old.style.cssText += cross('transition', time +'s');
  614. }, 1);
  615. break;
  616. }
  617. setTimeout(function () {
  618. if (type === 'slide-down') {
  619. $(div_old).css('z-index', '1019');
  620. $(div_new).css('z-index', '1020');
  621. }
  622. if (div_new) {
  623. $(div_new).css({
  624. 'opacity': '1',
  625. '-webkit-transition': '',
  626. '-moz-transition': '',
  627. '-ms-transition': '',
  628. '-o-transition': '',
  629. '-webkit-transform': '',
  630. '-moz-transform': '',
  631. '-ms-transform': '',
  632. '-o-transform': '',
  633. '-webkit-backface-visibility': '',
  634. '-moz-backface-visibility': '',
  635. '-ms-backface-visibility': '',
  636. '-o-backface-visibility': ''
  637. });
  638. }
  639. if (div_old) {
  640. $(div_old).css({
  641. 'opacity': '1',
  642. '-webkit-transition': '',
  643. '-moz-transition': '',
  644. '-ms-transition': '',
  645. '-o-transition': '',
  646. '-webkit-transform': '',
  647. '-moz-transform': '',
  648. '-ms-transform': '',
  649. '-o-transform': '',
  650. '-webkit-backface-visibility': '',
  651. '-moz-backface-visibility': '',
  652. '-ms-backface-visibility': '',
  653. '-o-backface-visibility': ''
  654. });
  655. if (div_old.parentNode) $(div_old.parentNode).css({
  656. '-webkit-perspective': '',
  657. '-moz-perspective': '',
  658. '-ms-perspective': '',
  659. '-o-perspective': ''
  660. });
  661. }
  662. if (typeof callBack === 'function') callBack();
  663. }, time * 1000);
  664. function cross(property, value, none_webkit_value) {
  665. var isWebkit=!!window.webkitURL; // jQuery no longer supports $.browser - RR
  666. if (!isWebkit && typeof none_webkit_value !== 'undefined') value = none_webkit_value;
  667. return ';'+ property +': '+ value +'; -webkit-'+ property +': '+ value +'; -moz-'+ property +': '+ value +'; '+
  668. '-ms-'+ property +': '+ value +'; -o-'+ property +': '+ value +';';
  669. }
  670. }
  671. function lock (box, msg, spinner) {
  672. var options = {};
  673. if (typeof msg === 'object') {
  674. options = msg;
  675. } else {
  676. options.msg = msg;
  677. options.spinner = spinner;
  678. }
  679. if (!options.msg && options.msg !== 0) options.msg = '';
  680. w2utils.unlock(box);
  681. $(box).prepend(
  682. '<div class="w2ui-lock"></div>'+
  683. '<div class="w2ui-lock-msg"></div>'
  684. );
  685. var $lock = $(box).find('.w2ui-lock');
  686. var mess = $(box).find('.w2ui-lock-msg');
  687. if (!options.msg) mess.css({ 'background-color': 'transparent', 'border': '0px' });
  688. if (options.spinner === true) options.msg = '<div class="w2ui-spinner" '+ (!options.msg ? 'style="width: 35px; height: 35px"' : '') +'></div>' + options.msg;
  689. if (options.opacity != null) $lock.css('opacity', options.opacity);
  690. if (typeof $lock.fadeIn == 'function') {
  691. $lock.fadeIn(200);
  692. mess.html(options.msg).fadeIn(200);
  693. } else {
  694. $lock.show();
  695. mess.html(options.msg).show(0);
  696. }
  697. // hide all tags (do not hide overlays as the form can be in overlay)
  698. $().w2tag();
  699. }
  700. function unlock (box) {
  701. $(box).find('.w2ui-lock').remove();
  702. $(box).find('.w2ui-lock-msg').remove();
  703. }
  704. function getSize (el, type) {
  705. var $el = $(el);
  706. var bwidth = {
  707. left : parseInt($el.css('border-left-width')) || 0,
  708. right : parseInt($el.css('border-right-width')) || 0,
  709. top : parseInt($el.css('border-top-width')) || 0,
  710. bottom : parseInt($el.css('border-bottom-width')) || 0
  711. };
  712. var mwidth = {
  713. left : parseInt($el.css('margin-left')) || 0,
  714. right : parseInt($el.css('margin-right')) || 0,
  715. top : parseInt($el.css('margin-top')) || 0,
  716. bottom : parseInt($el.css('margin-bottom')) || 0
  717. };
  718. var pwidth = {
  719. left : parseInt($el.css('padding-left')) || 0,
  720. right : parseInt($el.css('padding-right')) || 0,
  721. top : parseInt($el.css('padding-top')) || 0,
  722. bottom : parseInt($el.css('padding-bottom')) || 0
  723. };
  724. switch (type) {
  725. case 'top' : return bwidth.top + mwidth.top + pwidth.top;
  726. case 'bottom' : return bwidth.bottom + mwidth.bottom + pwidth.bottom;
  727. case 'left' : return bwidth.left + mwidth.left + pwidth.left;
  728. case 'right' : return bwidth.right + mwidth.right + pwidth.right;
  729. case 'width' : return bwidth.left + bwidth.right + mwidth.left + mwidth.right + pwidth.left + pwidth.right + parseInt($el.width());
  730. case 'height' : return bwidth.top + bwidth.bottom + mwidth.top + mwidth.bottom + pwidth.top + pwidth.bottom + parseInt($el.height());
  731. case '+width' : return bwidth.left + bwidth.right + mwidth.left + mwidth.right + pwidth.left + pwidth.right;
  732. case '+height' : return bwidth.top + bwidth.bottom + mwidth.top + mwidth.bottom + pwidth.top + pwidth.bottom;
  733. }
  734. return 0;
  735. }
  736. function lang (phrase) {
  737. var translation = this.settings.phrases[phrase];
  738. if (translation == null) return phrase; else return translation;
  739. }
  740. function locale (locale) {
  741. if (!locale) locale = 'en-us';
  742. if (locale.length === 5) locale = 'locale/'+ locale +'.json';
  743. // load from the file
  744. $.ajax({
  745. url : locale,
  746. type : "GET",
  747. dataType : "JSON",
  748. async : false,
  749. cache : false,
  750. success : function (data, status, xhr) {
  751. w2utils.settings = $.extend(true, w2utils.settings, data);
  752. // apply translation to some prototype functions
  753. var p = w2obj.grid.prototype;
  754. for (var b in p.buttons) {
  755. p.buttons[b].caption = w2utils.lang(p.buttons[b].caption);
  756. p.buttons[b].hint = w2utils.lang(p.buttons[b].hint);
  757. }
  758. p.msgDelete = w2utils.lang(p.msgDelete);
  759. p.msgNotJSON = w2utils.lang(p.msgNotJSON);
  760. p.msgRefresh = w2utils.lang(p.msgRefresh);
  761. },
  762. error : function (xhr, status, msg) {
  763. console.log('ERROR: Cannot load locale '+ locale);
  764. }
  765. });
  766. }
  767. function scrollBarSize () {
  768. if (tmp.scrollBarSize) return tmp.scrollBarSize;
  769. var html =
  770. '<div id="_scrollbar_width" style="position: absolute; top: -300px; width: 100px; height: 100px; overflow-y: scroll;">'+
  771. ' <div style="height: 120px">1</div>'+
  772. '</div>';
  773. $('body').append(html);
  774. tmp.scrollBarSize = 100 - $('#_scrollbar_width > div').width();
  775. $('#_scrollbar_width').remove();
  776. if (String(navigator.userAgent).indexOf('MSIE') >= 0) tmp.scrollBarSize = tmp.scrollBarSize / 2; // need this for IE9+
  777. return tmp.scrollBarSize;
  778. }
  779. function checkName (params, component) { // was w2checkNameParam
  780. if (!params || typeof params.name === 'undefined') {
  781. console.log('ERROR: The parameter "name" is required but not supplied in $().'+ component +'().');
  782. return false;
  783. }
  784. if (typeof w2ui[params.name] !== 'undefined') {
  785. console.log('ERROR: The parameter "name" is not unique. There are other objects already created with the same name (obj: '+ params.name +').');
  786. return false;
  787. }
  788. if (!w2utils.isAlphaNumeric(params.name)) {
  789. console.log('ERROR: The parameter "name" has to be alpha-numeric (a-z, 0-9, dash and underscore). ');
  790. return false;
  791. }
  792. return true;
  793. }
  794. function checkUniqueId (id, items, itemsDecription, objName) { // was w2checkUniqueId
  795. if (!$.isArray(items)) items = [items];
  796. for (var i = 0; i < items.length; i++) {
  797. if (items[i].id === id) {
  798. console.log('ERROR: The parameter "id='+ id +'" is not unique within the current '+ itemsDecription +'. (obj: '+ objName +')');
  799. return false;
  800. }
  801. }
  802. return true;
  803. }
  804. function parseRoute(route) {
  805. var keys = [];
  806. var path = route
  807. .replace(/\/\(/g, '(?:/')
  808. .replace(/\+/g, '__plus__')
  809. .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional) {
  810. keys.push({ name: key, optional: !! optional });
  811. slash = slash || '';
  812. return '' + (optional ? '' : slash) + '(?:' + (optional ? slash : '') + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')' + (optional || '');
  813. })
  814. .replace(/([\/.])/g, '\\$1')
  815. .replace(/__plus__/g, '(.+)')
  816. .replace(/\*/g, '(.*)');
  817. return {
  818. path : new RegExp('^' + path + '$', 'i'),
  819. keys : keys
  820. };
  821. }
  822. })();
  823. /***********************************************************
  824. * Generic Event Object
  825. * --- This object is reused across all other
  826. * --- widgets in w2ui.
  827. *
  828. *********************************************************/
  829. w2utils.event = {
  830. on: function (eventData, handler) {
  831. if (!$.isPlainObject(eventData)) eventData = { type: eventData };
  832. eventData = $.extend({ type: null, execute: 'before', target: null, onComplete: null }, eventData);
  833. if (!eventData.type) { console.log('ERROR: You must specify event type when calling .on() method of '+ this.name); return; }
  834. if (!handler) { console.log('ERROR: You must specify event handler function when calling .on() method of '+ this.name); return; }
  835. if (!$.isArray(this.handlers)) this.handlers = [];
  836. this.handlers.push({ event: eventData, handler: handler });
  837. },
  838. off: function (eventData, handler) {
  839. if (!$.isPlainObject(eventData)) eventData = { type: eventData };
  840. eventData = $.extend({}, { type: null, execute: 'before', target: null, onComplete: null }, eventData);
  841. if (!eventData.type) { console.log('ERROR: You must specify event type when calling .off() method of '+ this.name); return; }
  842. if (!handler) { handler = null; }
  843. // remove handlers
  844. var newHandlers = [];
  845. for (var h = 0, len = this.handlers.length; h < len; h++) {
  846. var t = this.handlers[h];
  847. if ((t.event.type === eventData.type || eventData.type === '*') &&
  848. (t.event.target === eventData.target || eventData.target === null) &&
  849. (t.handler === handler || handler === null))
  850. {
  851. // match
  852. } else {
  853. newHandlers.push(t);
  854. }
  855. }
  856. this.handlers = newHandlers;
  857. },
  858. trigger: function (eventData) {
  859. var eventData = $.extend({ type: null, phase: 'before', target: null }, eventData, {
  860. isStopped: false, isCancelled: false,
  861. preventDefault : function () { this.isCancelled = true; },
  862. stopPropagation : function () { this.isStopped = true; }
  863. });
  864. if (eventData.phase === 'before') eventData.onComplete = null;
  865. var args, fun, tmp;
  866. if (eventData.target == null) eventData.target = null;
  867. if (!$.isArray(this.handlers)) this.handlers = [];
  868. // process events in REVERSE order
  869. for (var h = this.handlers.length-1; h >= 0; h--) {
  870. var item = this.handlers[h];
  871. if ((item.event.type === eventData.type || item.event.type === '*') &&
  872. (item.event.target === eventData.target || item.event.target === null) &&
  873. (item.event.execute === eventData.phase || item.event.execute === '*' || item.event.phase === '*'))
  874. {
  875. eventData = $.extend({}, item.event, eventData);
  876. // check handler arguments
  877. args = [];
  878. tmp = RegExp(/\((.*?)\)/).exec(item.handler);
  879. if (tmp) args = tmp[1].split(/\s*,\s*/);
  880. if (args.length === 2) {
  881. item.handler.call(this, eventData.target, eventData); // old way for back compatibility
  882. } else {
  883. item.handler.call(this, eventData); // new way
  884. }
  885. if (eventData.isStopped === true || eventData.stop === true) return eventData; // back compatibility eventData.stop === true
  886. }
  887. }
  888. // main object events
  889. var funName = 'on' + eventData.type.substr(0,1).toUpperCase() + eventData.type.substr(1);
  890. if (eventData.phase === 'before' && typeof this[funName] === 'function') {
  891. fun = this[funName];
  892. // check handler arguments
  893. args = [];
  894. tmp = RegExp(/\((.*?)\)/).exec(fun);
  895. if (tmp) args = tmp[1].split(/\s*,\s*/);
  896. if (args.length === 2) {
  897. fun.call(this, eventData.target, eventData); // old way for back compatibility
  898. } else {
  899. fun.call(this, eventData); // new way
  900. }
  901. if (eventData.isStopped === true || eventData.stop === true) return eventData; // back compatibility eventData.stop === true
  902. }
  903. // item object events
  904. if (eventData.object != null && eventData.phase === 'before' &&
  905. typeof eventData.object[funName] === 'function')
  906. {
  907. fun = eventData.object[funName];
  908. // check handler arguments
  909. args = [];
  910. tmp = RegExp(/\((.*?)\)/).exec(fun);
  911. if (tmp) args = tmp[1].split(/\s*,\s*/);
  912. if (args.length === 2) {
  913. fun.call(this, eventData.target, eventData); // old way for back compatibility
  914. } else {
  915. fun.call(this, eventData); // new way
  916. }
  917. if (eventData.isStopped === true || eventData.stop === true) return eventData;
  918. }
  919. // execute onComplete
  920. if (eventData.phase === 'after' && typeof eventData.onComplete === 'function') eventData.onComplete.call(this, eventData);
  921. return eventData;
  922. }
  923. };
  924. /***********************************************************
  925. * Common Keyboard Handler. Supported in
  926. * - grid
  927. * - sidebar
  928. * - popup
  929. *
  930. *********************************************************/
  931. w2utils.keyboard = (function (obj) {
  932. // private scope
  933. var w2ui_name = null;
  934. obj.active = active;
  935. obj.clear = clear;
  936. init();
  937. return obj;
  938. function init() {
  939. $(document).on('keydown', keydown);
  940. $(document).on('mousedown', mousedown);
  941. }
  942. function keydown (event) {
  943. var tag = event.target.tagName;
  944. if ($.inArray(tag, ['INPUT', 'SELECT', 'TEXTAREA']) !== -1) return;
  945. if ($(event.target).prop('contenteditable') === 'true') return;
  946. if (!w2ui_name) return;
  947. // pass to appropriate widget
  948. if (w2ui[w2ui_name] && typeof w2ui[w2ui_name].keydown === 'function') {
  949. w2ui[w2ui_name].keydown.call(w2ui[w2ui_name], event);
  950. }
  951. }
  952. function mousedown (event) {
  953. var tag = event.target.tagName;
  954. var obj = $(event.target).parents('.w2ui-reset');
  955. if (obj.length > 0) {
  956. var name = obj.attr('name');
  957. if (w2ui[name] && w2ui[name].keyboard) w2ui_name = name;
  958. }
  959. }
  960. function active (new_w2ui_name) {
  961. if (typeof new_w2ui_name !== 'undefined') w2ui_name = new_w2ui_name;
  962. return w2ui_name;
  963. }
  964. function clear () {
  965. w2ui_name = null;
  966. }
  967. })({});
  968. /***********************************************************
  969. * Commonly used plugins
  970. * --- used primarily in grid and form
  971. *
  972. *********************************************************/
  973. (function () {
  974. $.fn.w2render = function (name) {
  975. if ($(this).length > 0) {
  976. if (typeof name === 'string' && w2ui[name]) w2ui[name].render($(this)[0]);
  977. if (typeof name === 'object') name.render($(this)[0]);
  978. }
  979. };
  980. $.fn.w2destroy = function (name) {
  981. if (!name && this.length > 0) name = this.attr('name');
  982. if (typeof name === 'string' && w2ui[name]) w2ui[name].destroy();
  983. if (typeof name === 'object') name.destroy();
  984. };
  985. $.fn.w2marker = function (str) {
  986. if (str === '' || str == null) { // remove marker
  987. return $(this).each(function (index, el) {
  988. el.innerHTML = el.innerHTML.replace(/\<span class=\"w2ui\-marker\"\>(.*)\<\/span\>/ig, '$1'); // unmark
  989. });
  990. } else { // add marker
  991. return $(this).each(function (index, el) {
  992. if (typeof str === 'string') str = [str];
  993. el.innerHTML = el.innerHTML.replace(/\<span class=\"w2ui\-marker\"\>(.*)\<\/span\>/ig, '$1'); // unmark
  994. for (var s in str) {
  995. var tmp = str[s];
  996. if (typeof tmp !== 'string') tmp = String(tmp);
  997. // escape regex special chars
  998. tmp = tmp.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&").replace(/&/g, '&amp;').replace(/</g, '&gt;').replace(/>/g, '&lt;');
  999. var regex = new RegExp(tmp + '(?!([^<]+)?>)', "gi"); // only outside tags
  1000. el.innerHTML = el.innerHT