helpers.js 11 KB

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