helpers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. }
  123. App.Helpers.beforeAjax = function(jedi_method)
  124. {
  125. switch(jedi_method) {
  126. case 'DNS.getList':
  127. App.Helpers.showLoading();
  128. break;
  129. default:
  130. App.Helpers.showLoading();
  131. break;
  132. }
  133. }
  134. App.Helpers.afterAjax = function()
  135. {
  136. App.Helpers.removeLoading();
  137. }
  138. App.Helpers.removeLoading = function()
  139. {
  140. var ref = $('#loading');
  141. if (ref.length > 0) {
  142. ref.remove();
  143. }
  144. }
  145. App.Helpers.showLoading = function()
  146. {
  147. App.Helpers.removeLoading();
  148. var tpl = App.Templates.get('loading', 'general');
  149. $(document.body).append(tpl.finalize());
  150. }
  151. // todo: no iteration here
  152. App.Helpers.getFirstValue = function(obj)
  153. {
  154. var first = '';
  155. $.each(obj, function(key, i) {
  156. return first = obj[key];
  157. });
  158. return first;
  159. }
  160. App.Helpers.evalJSON = function(str)
  161. {
  162. return $.parseJSON(str);
  163. }
  164. App.Helpers.toJSON = function(object)
  165. {
  166. return ($.toJSON(object).replace(/\\'/gi, ''));
  167. }
  168. //
  169. // Hints
  170. //
  171. App.Helpers.showConsoleHint = function()
  172. {
  173. // TODO:
  174. }
  175. App.Helpers.markBrowserDetails = function()
  176. {
  177. var b = App.Env.BROWSER;
  178. var classes = [
  179. b.type.toLowerCase(),
  180. b.type.toLowerCase() + b.version,
  181. b.os.toLowerCase()
  182. ];
  183. $(document.body).addClass(classes.join(' '));
  184. }
  185. App.Utils.detectBrowser = function()
  186. {
  187. App.Env.BROWSER = {
  188. type: $.browser.browser(),
  189. version: $.browser.version.number(),
  190. os: $.browser.OS()
  191. };
  192. App.Helpers.markBrowserDetails();
  193. }
  194. App.Helpers.getFormValues = function(form)
  195. {
  196. var values = {};
  197. $(form).find('input, select, textarea').each(function(i, o) {
  198. if ($.inArray($(o).attr('class'), ['source', 'target'])) {
  199. values[$(o).attr('name')] = $(o).val();
  200. }
  201. });
  202. return values;
  203. }
  204. App.Helpers.getFormValuesFromElement = function(ref)
  205. {
  206. var values = {};
  207. $(ref).find('input, select, textarea').each(function(i, o) {
  208. if ($.inArray($(o).attr('class'), ['source', 'target'])) {
  209. values[$(o).attr('name')] = $(o).val();
  210. }
  211. });
  212. return values;
  213. }
  214. App.Helpers.updateScreen = function()
  215. {
  216. /*App.Ajax.request('MAIN.getInitial', {}, function(reply){
  217. App.Env.initialParams = reply.data;
  218. App.Helpers.updateInitial();
  219. });*/
  220. $('.first-row').removeClass('first-row');
  221. $('.row:first').addClass('first-row');
  222. Custom.init();
  223. }
  224. App.Helpers.alert = function(msg)
  225. {
  226. alert(msg);
  227. }
  228. App.Helpers.isEmpty = function(o)
  229. {
  230. return 'undefined' == typeof o ? true : jQuery.isEmptyObject(o);
  231. }
  232. App.Helpers.liveValidate = function()
  233. {
  234. }
  235. App.Helpers.generatePassword = function()
  236. {
  237. var length = 8;
  238. var chars = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789";
  239. var pass = "";
  240. for (x=0;x<length;x++) {
  241. var i = Math.floor(Math.random() * chars.length);
  242. pass += chars.charAt(i);
  243. }
  244. return pass;
  245. }
  246. App.Helpers.Warn = function(msg)
  247. {
  248. alert(msg);
  249. }
  250. App.Helpers.openInnerPopup = function(elm, html, title)
  251. {
  252. var title = title || '';
  253. App.Helpers.closeInnerPopup();
  254. var offset = $(elm).offset();
  255. var tpl = App.Templates.get('inner_popup', 'general');
  256. tpl.set(':CONTENT', html);
  257. tpl.set(':LEFT', offset.left);
  258. tpl.set(':TOP', offset.top);
  259. tpl.set(':POPUP_TITLE', title);
  260. $(document.body).append(tpl.finalize());
  261. }
  262. App.Helpers.closeInnerPopup = function(evt)
  263. {
  264. $('#inner-popup').remove();
  265. }
  266. App.Helpers.getUploadUrl = function()
  267. {
  268. return App.Helpers.generateUrl('vesta/upload.php');
  269. }
  270. App.Helpers.getBackendUrl = function()
  271. {
  272. return App.Helpers.generateUrl('dispatch.php');
  273. }
  274. App.Helpers.generateUrl = function(to_file)
  275. {
  276. var url_parts = location.href.split('#');
  277. if (url_parts.length > 1) {
  278. var tab = url_parts[url_parts.length - 1];
  279. if ($.inArray(tab, App.Constants.TABS) != -1) {
  280. App.Tmp.loadTAB = tab;
  281. }
  282. }
  283. var url_parts = location.href.split('?', 1);
  284. var url = url_parts[0];
  285. url_parts = url.split('/');
  286. if (url_parts[url_parts.length -1] == 'index.html') {
  287. url_parts[url_parts.length -1] = to_file;
  288. }
  289. else {
  290. url_parts.push(to_file);
  291. }
  292. return url_parts.join('/').replace('#', '');
  293. }
  294. App.Helpers.disableNotEditable = function()
  295. {
  296. if ('undefined' == typeof App.Settings.Imutable[App.Env.world]) {
  297. return false;
  298. }
  299. $('.form').each(function(i, form)
  300. {
  301. if ($(form).attr('id') == '') {
  302. $('input, select, textarea', form).each(function(i, elm) {
  303. if ($.inArray($(elm).attr('name'), App.Settings.Imutable[App.Env.world]) != -1) {
  304. $(elm).attr('disabled', true);
  305. }
  306. });
  307. }
  308. });
  309. }
  310. App.Helpers.handleItemsRegisteredInBackground = function(evt)
  311. {
  312. // complex selects
  313. if (!$(evt.target).hasClass('c-s-opt')) { // complex select option
  314. $('.complex-select-content').addClass('hidden');
  315. }
  316. }
  317. //
  318. // HELPERS
  319. //
  320. App.Helpers.keyboard_ESC = function()
  321. {
  322. $('.complex-select-content').addClass('hidden');
  323. App.Tmp.focusedComplexSelect = null;
  324. }
  325. App.Helpers.keyboard_ENTER = function()
  326. {
  327. if (null != App.Tmp.focusedComplexSelectInput) {
  328. var val = App.Tmp.focusedComplexSelectInput.find('.c-s-value').val();
  329. App.Tmp.focusedComplexSelect.find('.c-s-title').text(val);
  330. App.Tmp.focusedComplexSelect.find('.c-s-value-ref').val(val);
  331. $('.complex-select-content').addClass('hidden');
  332. }
  333. }
  334. App.Helpers.keyboard_DOWN = function(evt)
  335. {
  336. if (null != App.Tmp.focusedComplexSelect) {
  337. App.Tmp.focusedComplexSelect.find('.complex-select-content').removeClass('hidden');
  338. $('.s-c-highlighted').removeClass('s-c-highlighted');
  339. if (null == App.Tmp.focusedComplexSelectInput) {
  340. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:first');
  341. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  342. }
  343. else {
  344. var ref = App.Tmp.focusedComplexSelectInput.next();
  345. App.Tmp.focusedComplexSelectInput = ref;
  346. if (ref.length == 1) {
  347. ref.addClass('s-c-highlighted');
  348. }
  349. else {
  350. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:first');
  351. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  352. }
  353. }
  354. }
  355. }
  356. App.Helpers.keyboard_UP = function(evt)
  357. {
  358. if (null != App.Tmp.focusedComplexSelect) {
  359. App.Tmp.focusedComplexSelect.find('.complex-select-content').removeClass('hidden');
  360. $('.s-c-highlighted').removeClass('s-c-highlighted');
  361. if (null == App.Tmp.focusedComplexSelectInput) {
  362. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:last');
  363. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  364. }
  365. else {
  366. var ref = App.Tmp.focusedComplexSelectInput.prev();
  367. App.Tmp.focusedComplexSelectInput = ref;
  368. if (ref.length == 1) {
  369. ref.addClass('s-c-highlighted');
  370. }
  371. else {
  372. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:last');
  373. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  374. }
  375. }
  376. }
  377. }