helpers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. App.Helpers.formatNumber = function(number, no_commas){
  2. no_commas = no_commas || false;
  3. number = number.toString().replace(/,/g, '');
  4. var nStr = parseFloat(number).toFixed(2);
  5. fb.info(nStr);
  6. nStr = nStr.toString();
  7. nStr += '';
  8. x = nStr.split('.');
  9. x1 = x[0];
  10. x2 = x.length > 1 ? '.' + x[1] : '';
  11. if(!no_commas){
  12. var rgx = /(\d+)(\d{3})/;
  13. while (rgx.test(x1)) {
  14. x1 = x1.replace(rgx, '$1' + ',' + '$2');
  15. }
  16. }
  17. return x1 + x2;
  18. }
  19. App.Helpers.getHumanTabName = function()
  20. {
  21. if (App.Env.world == 'WEB_DOMAIN') {
  22. return 'WEB DOMAIN';
  23. }
  24. if (App.Env.world == 'MAIL') {
  25. return 'MAIL DOMAIN';
  26. }
  27. if (App.Env.world == 'DNS') {
  28. return 'DNS DOMAIN';
  29. }
  30. if (App.Env.world == 'IP') {
  31. return 'IP ADDRESS';
  32. }
  33. if (App.Env.world == 'CRON') {
  34. return 'CRON JOB';
  35. }
  36. if (App.Env.world == 'DB') {
  37. return 'DATABASE';
  38. }
  39. if (App.Env.world == 'BACKUPS') {
  40. return 'BACKUP';
  41. }
  42. if (App.Env.world == 'STATS') {
  43. return 'STATS';
  44. }
  45. return App.Env.world;
  46. }
  47. App.Helpers.scrollTo = function(elm)
  48. {
  49. var scroll_to = $(elm).offset().top;
  50. if (scroll_to > 1000) {
  51. var scroll_time = 300;
  52. }
  53. else {
  54. var scroll_time = 550;
  55. }
  56. $('html, body').animate({ 'scrollTop': scroll_to }, scroll_time);
  57. }
  58. App.Helpers.getMbHumanMeasure = function(val)
  59. {
  60. return App.Helpers.getMbHuman(val, true);
  61. }
  62. /**
  63. * Currently no bytes are used, minimal value is in MB
  64. * uncomment in case we will use bytes instead
  65. */
  66. App.Helpers.getMbHuman = function(val, only_measure)
  67. {
  68. var bytes = val * 1024 * 1024;
  69. var kilobyte = 1024;
  70. var megabyte = kilobyte * 1024;
  71. var gigabyte = megabyte * 1024;
  72. var terabyte = gigabyte * 1024;
  73. var precision = 0;
  74. return only_measure ? 'MB' : (bytes / megabyte).toFixed(precision);
  75. /*if ((bytes >= 0) && (bytes < kilobyte)) {
  76. return bytes + ' B';
  77. } else if ((bytes >= kilobyte) && (bytes < megabyte)) {
  78. return (bytes / kilobyte).toFixed(precision) + ' KB';
  79. } else */
  80. if ((bytes >= megabyte) && (bytes < gigabyte)) {
  81. return only_measure ? 'MB' : (bytes / megabyte).toFixed(precision);
  82. } else if ((bytes >= gigabyte) && (bytes < terabyte)) {
  83. return only_measure ? 'GB' : (bytes / gigabyte).toFixed(precision);
  84. } else if (bytes >= terabyte) {
  85. return only_measure ? 'TB' : (bytes / terabyte).toFixed(precision);
  86. } else {
  87. return only_measure ? 'MB' : bytes;
  88. }
  89. }
  90. App.Helpers.getFirst = function(obj)
  91. {
  92. var first = {};
  93. var key = App.Helpers.getFirstKey(obj);
  94. first[key] = obj[key];
  95. return first;
  96. }
  97. App.Helpers.getFirstKey = function(obj)
  98. {
  99. for (key in obj) break;
  100. return key;
  101. }
  102. App.Helpers.updateInitial = function()
  103. {
  104. $.each(App.Env.initialParams.totals, function(key) {
  105. var item = App.Env.initialParams.totals[key];
  106. var expr_id = '#'+key;
  107. if ('undefined' != typeof item.total) {
  108. var ref = $(expr_id).find('.num-total');
  109. if (ref.length > 0) {
  110. $(ref).html(item.total);
  111. }
  112. }
  113. if ('undefined' != typeof item.blocked) {
  114. var ref = $(expr_id).find('.num-blocked');
  115. if (ref.length > 0) {
  116. $(ref).html(item.blocked);
  117. }
  118. }
  119. });
  120. $('#user-name').html(App.Env.initialParams.PROFILE.uid);
  121. $('#page').removeClass('hidden');
  122. if (App.Env.initialParams.real_user) {
  123. var tpl = App.Templates.get('logged_as', 'general');
  124. tpl.set(':YOU_ARE', App.Env.initialParams.real_user);
  125. tpl.set(':USER', App.Env.initialParams.auth_user.uid.uid);
  126. $('body').prepend(tpl.finalize());
  127. }
  128. }
  129. App.Helpers.beforeAjax = function(jedi_method)
  130. {
  131. switch(jedi_method) {
  132. case 'DNS.getList':
  133. App.Helpers.showLoading();
  134. break;
  135. default:
  136. App.Helpers.showLoading();
  137. break;
  138. }
  139. }
  140. App.Helpers.afterAjax = function()
  141. {
  142. App.Helpers.removeLoading();
  143. }
  144. App.Helpers.removeLoading = function()
  145. {
  146. var ref = $('#loading');
  147. if (ref.length > 0) {
  148. ref.remove();
  149. }
  150. }
  151. App.Helpers.showLoading = function()
  152. {
  153. App.Helpers.removeLoading();
  154. var tpl = App.Templates.get('loading', 'general');
  155. $(document.body).append(tpl.finalize());
  156. }
  157. // todo: no iteration here
  158. App.Helpers.getFirstValue = function(obj)
  159. {
  160. var first = '';
  161. $.each(obj, function(key, i) {
  162. return first = obj[key];
  163. });
  164. return first;
  165. }
  166. App.Helpers.evalJSON = function(str)
  167. {
  168. return $.parseJSON(str);
  169. }
  170. App.Helpers.toJSON = function(object)
  171. {
  172. return ($.toJSON(object).replace(/\\'/gi, ''));
  173. }
  174. //
  175. // Hints
  176. //
  177. App.Helpers.showConsoleHint = function()
  178. {
  179. // TODO:
  180. }
  181. App.Helpers.markBrowserDetails = function()
  182. {
  183. var b = App.Env.BROWSER;
  184. var classes = [
  185. b.type.toLowerCase(),
  186. b.type.toLowerCase() + b.version,
  187. b.os.toLowerCase()
  188. ];
  189. $(document.body).addClass(classes.join(' '));
  190. }
  191. App.Utils.detectBrowser = function()
  192. {
  193. App.Env.BROWSER = {
  194. type: $.browser.browser(),
  195. version: $.browser.version.number(),
  196. os: $.browser.OS()
  197. };
  198. App.Helpers.markBrowserDetails();
  199. }
  200. App.Helpers.getFormValues = function(form)
  201. {
  202. var values = {};
  203. $(form).find('input, select, textarea').each(function(i, o) {
  204. if ($.inArray($(o).attr('class'), ['source', 'target'])) {
  205. values[$(o).attr('name')] = $(o).val();
  206. }
  207. });
  208. return values;
  209. }
  210. App.Helpers.getFormValuesFromElement = function(ref)
  211. {
  212. var values = {};
  213. $(ref).find('input, select, textarea').each(function(i, o) {
  214. if ($.inArray($(o).attr('class'), ['source', 'target'])) {
  215. values[$(o).attr('name')] = $(o).val();
  216. }
  217. });
  218. return values;
  219. }
  220. App.Helpers.updateScreen = function()
  221. {
  222. /*App.Ajax.request('MAIN.getInitial', {}, function(reply){
  223. App.Env.initialParams = reply.data;
  224. App.Helpers.updateInitial();
  225. });*/
  226. $('.first-row').removeClass('first-row');
  227. $('.row:first').addClass('first-row');
  228. Custom.init();
  229. }
  230. App.Helpers.alert = function(msg)
  231. {
  232. alert(msg);
  233. }
  234. App.Helpers.isEmpty = function(o)
  235. {
  236. return 'undefined' == typeof o ? true : jQuery.isEmptyObject(o);
  237. }
  238. App.Helpers.liveValidate = function()
  239. {
  240. }
  241. App.Helpers.generatePassword = function()
  242. {
  243. var length = 8;
  244. var chars = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789";
  245. var pass = "";
  246. for (x=0;x<length;x++) {
  247. var i = Math.floor(Math.random() * chars.length);
  248. pass += chars.charAt(i);
  249. }
  250. return pass;
  251. }
  252. App.Helpers.Warn = function(msg)
  253. {
  254. alert(msg);
  255. }
  256. App.Helpers.openInnerPopup = function(elm, html, title)
  257. {
  258. var title = title || '';
  259. App.Helpers.closeInnerPopup();
  260. var offset = $(elm).offset();
  261. var tpl = App.Templates.get('inner_popup', 'general');
  262. tpl.set(':CONTENT', html);
  263. tpl.set(':LEFT', offset.left);
  264. tpl.set(':TOP', offset.top);
  265. tpl.set(':POPUP_TITLE', title);
  266. $(document.body).append(tpl.finalize());
  267. }
  268. App.Helpers.closeInnerPopup = function(evt)
  269. {
  270. $('#inner-popup').remove();
  271. }
  272. App.Helpers.getUploadUrl = function()
  273. {
  274. return App.Helpers.generateUrl('vesta/upload.php');
  275. }
  276. App.Helpers.getBackendUrl = function()
  277. {
  278. return App.Helpers.generateUrl('dispatch.php');
  279. }
  280. App.Helpers.generateUrl = function(to_file)
  281. {
  282. var url_parts = location.href.split('#');
  283. if (url_parts.length > 1) {
  284. var tab = url_parts[url_parts.length - 1];
  285. if ($.inArray(tab, App.Constants.TABS) != -1) {
  286. App.Tmp.loadTAB = tab;
  287. }
  288. }
  289. var url_parts = location.href.split('?', 1);
  290. var url = url_parts[0];
  291. url_parts = url.split('/');
  292. if (url_parts[url_parts.length -1] == 'index.html') {
  293. url_parts[url_parts.length -1] = to_file;
  294. }
  295. else {
  296. url_parts.push(to_file);
  297. }
  298. return url_parts.join('/').replace('#', '');
  299. }
  300. App.Helpers.disableNotEditable = function()
  301. {
  302. if ('undefined' == typeof App.Settings.Imutable[App.Env.world]) {
  303. return false;
  304. }
  305. $('.form').each(function(i, form)
  306. {
  307. if ($(form).attr('id') == '') {
  308. $('input, select, textarea', form).each(function(i, elm) {
  309. if ($.inArray($(elm).attr('name'), App.Settings.Imutable[App.Env.world]) != -1) {
  310. $(elm).attr('disabled', true);
  311. }
  312. });
  313. }
  314. });
  315. }
  316. App.Helpers.handleItemsRegisteredInBackground = function(evt)
  317. {
  318. // complex selects
  319. if (!$(evt.target).hasClass('c-s-opt')) { // complex select option
  320. $('.complex-select-content').addClass('hidden');
  321. }
  322. }
  323. //
  324. // HELPERS
  325. //
  326. App.Helpers.keyboard_ESC = function()
  327. {
  328. $('.complex-select-content').addClass('hidden');
  329. App.Tmp.focusedComplexSelect = null;
  330. }
  331. App.Helpers.keyboard_ENTER = function()
  332. {
  333. if (null != App.Tmp.focusedComplexSelectInput) {
  334. var val = App.Tmp.focusedComplexSelectInput.find('.c-s-value').val();
  335. App.Tmp.focusedComplexSelect.find('.c-s-title').text(val);
  336. App.Tmp.focusedComplexSelect.find('.c-s-value-ref').val(val);
  337. $('.complex-select-content').addClass('hidden');
  338. }
  339. }
  340. App.Helpers.keyboard_DOWN = function(evt)
  341. {
  342. if (null != App.Tmp.focusedComplexSelect) {
  343. App.Tmp.focusedComplexSelect.find('.complex-select-content').removeClass('hidden');
  344. $('.s-c-highlighted').removeClass('s-c-highlighted');
  345. if (null == App.Tmp.focusedComplexSelectInput) {
  346. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:first');
  347. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  348. }
  349. else {
  350. var ref = App.Tmp.focusedComplexSelectInput.next();
  351. App.Tmp.focusedComplexSelectInput = ref;
  352. if (ref.length == 1) {
  353. ref.addClass('s-c-highlighted');
  354. }
  355. else {
  356. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:first');
  357. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  358. }
  359. }
  360. }
  361. }
  362. App.Helpers.keyboard_UP = function(evt)
  363. {
  364. if (null != App.Tmp.focusedComplexSelect) {
  365. App.Tmp.focusedComplexSelect.find('.complex-select-content').removeClass('hidden');
  366. $('.s-c-highlighted').removeClass('s-c-highlighted');
  367. if (null == App.Tmp.focusedComplexSelectInput) {
  368. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:last');
  369. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  370. }
  371. else {
  372. var ref = App.Tmp.focusedComplexSelectInput.prev();
  373. App.Tmp.focusedComplexSelectInput = ref;
  374. if (ref.length == 1) {
  375. ref.addClass('s-c-highlighted');
  376. }
  377. else {
  378. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:last');
  379. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  380. }
  381. }
  382. }
  383. }