helpers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. $('.row:first').addClass('first-row');
  221. Custom.init();
  222. }
  223. App.Helpers.alert = function(msg)
  224. {
  225. alert(msg);
  226. }
  227. App.Helpers.isEmpty = function(o)
  228. {
  229. return 'undefined' == typeof o ? true : jQuery.isEmptyObject(o);
  230. }
  231. App.Helpers.liveValidate = function()
  232. {
  233. }
  234. App.Helpers.generatePassword = function()
  235. {
  236. var length = 8;
  237. var chars = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789";
  238. var pass = "";
  239. for (x=0;x<length;x++) {
  240. var i = Math.floor(Math.random() * chars.length);
  241. pass += chars.charAt(i);
  242. }
  243. return pass;
  244. }
  245. App.Helpers.Warn = function(msg)
  246. {
  247. alert(msg);
  248. }
  249. App.Helpers.openInnerPopup = function(elm, html, title)
  250. {
  251. var title = title || '';
  252. App.Helpers.closeInnerPopup();
  253. var offset = $(elm).offset();
  254. var tpl = App.Templates.get('inner_popup', 'general');
  255. tpl.set(':CONTENT', html);
  256. tpl.set(':LEFT', offset.left);
  257. tpl.set(':TOP', offset.top);
  258. tpl.set(':POPUP_TITLE', title);
  259. $(document.body).append(tpl.finalize());
  260. }
  261. App.Helpers.closeInnerPopup = function(evt)
  262. {
  263. $('#inner-popup').remove();
  264. }
  265. App.Helpers.getUploadUrl = function()
  266. {
  267. return App.Helpers.generateUrl('vesta/upload.php');
  268. }
  269. App.Helpers.getBackendUrl = function()
  270. {
  271. return App.Helpers.generateUrl('dispatch.php');
  272. }
  273. App.Helpers.generateUrl = function(to_file)
  274. {
  275. var url_parts = location.href.split('#');
  276. if (url_parts.length > 1) {
  277. var tab = url_parts[url_parts.length - 1];
  278. if ($.inArray(tab, App.Constants.TABS) != -1) {
  279. App.Tmp.loadTAB = tab;
  280. }
  281. }
  282. var url_parts = location.href.split('?', 1);
  283. var url = url_parts[0];
  284. url_parts = url.split('/');
  285. if (url_parts[url_parts.length -1] == 'index.html') {
  286. url_parts[url_parts.length -1] = to_file;
  287. }
  288. else {
  289. url_parts.push(to_file);
  290. }
  291. return url_parts.join('/').replace('#', '');
  292. }
  293. App.Helpers.disableNotEditable = function()
  294. {
  295. if ('undefined' == typeof App.Settings.Imutable[App.Env.world]) {
  296. return false;
  297. }
  298. $('.form').each(function(i, form)
  299. {
  300. if ($(form).attr('id') == '') {
  301. $('input, select, textarea', form).each(function(i, elm) {
  302. if ($.inArray($(elm).attr('name'), App.Settings.Imutable[App.Env.world]) != -1) {
  303. $(elm).attr('disabled', true);
  304. }
  305. });
  306. }
  307. });
  308. }
  309. App.Helpers.handleItemsRegisteredInBackground = function(evt)
  310. {
  311. // complex selects
  312. if (!$(evt.target).hasClass('c-s-opt')) { // complex select option
  313. $('.complex-select-content').addClass('hidden');
  314. }
  315. }
  316. //
  317. // HELPERS
  318. //
  319. App.Helpers.keyboard_ESC = function()
  320. {
  321. $('.complex-select-content').addClass('hidden');
  322. App.Tmp.focusedComplexSelect = null;
  323. }
  324. App.Helpers.keyboard_ENTER = function()
  325. {
  326. if (null != App.Tmp.focusedComplexSelectInput) {
  327. var val = App.Tmp.focusedComplexSelectInput.find('.c-s-value').val();
  328. App.Tmp.focusedComplexSelect.find('.c-s-title').text(val);
  329. App.Tmp.focusedComplexSelect.find('.c-s-value-ref').val(val);
  330. $('.complex-select-content').addClass('hidden');
  331. }
  332. }
  333. App.Helpers.keyboard_DOWN = function(evt)
  334. {
  335. if (null != App.Tmp.focusedComplexSelect) {
  336. App.Tmp.focusedComplexSelect.find('.complex-select-content').removeClass('hidden');
  337. $('.s-c-highlighted').removeClass('s-c-highlighted');
  338. if (null == App.Tmp.focusedComplexSelectInput) {
  339. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:first');
  340. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  341. }
  342. else {
  343. var ref = App.Tmp.focusedComplexSelectInput.next();
  344. App.Tmp.focusedComplexSelectInput = ref;
  345. if (ref.length == 1) {
  346. ref.addClass('s-c-highlighted');
  347. }
  348. else {
  349. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:first');
  350. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  351. }
  352. }
  353. }
  354. }
  355. App.Helpers.keyboard_UP = function(evt)
  356. {
  357. if (null != App.Tmp.focusedComplexSelect) {
  358. App.Tmp.focusedComplexSelect.find('.complex-select-content').removeClass('hidden');
  359. $('.s-c-highlighted').removeClass('s-c-highlighted');
  360. if (null == App.Tmp.focusedComplexSelectInput) {
  361. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:last');
  362. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  363. }
  364. else {
  365. var ref = App.Tmp.focusedComplexSelectInput.prev();
  366. App.Tmp.focusedComplexSelectInput = ref;
  367. if (ref.length == 1) {
  368. ref.addClass('s-c-highlighted');
  369. }
  370. else {
  371. App.Tmp.focusedComplexSelectInput = App.Tmp.focusedComplexSelect.find('.cust-sel-option:last');
  372. App.Tmp.focusedComplexSelectInput.addClass('s-c-highlighted');
  373. }
  374. }
  375. }
  376. }