You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2018 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. **********************************************************************************/
  19. odoo.define('muk_web_utils.common', function (require) {
  20. "use strict";
  21. var ajax = require('web.ajax');
  22. var core = require('web.core');
  23. var utils = require('web.utils');
  24. var QWeb = core.qweb;
  25. var _t = core._t;
  26. var delay = (function(){
  27. var timer = 0;
  28. return function(callback, ms){
  29. clearTimeout (timer);
  30. timer = setTimeout(callback, ms);
  31. };
  32. })();
  33. function format_number(value) {
  34. if (value === false || typeof value !== "number") {
  35. return "";
  36. }
  37. var l10n = core._t.database.parameters;
  38. var formatted = _.str.sprintf('%.' + 2 + 'f', value || 0).split('.');
  39. formatted[0] = utils.insert_thousand_seps(formatted[0]);
  40. return formatted.join(l10n.decimal_point);
  41. }
  42. function format_size(bytes, options) {
  43. var options = options || {};
  44. var thresh = options.si ? 1000 : 1024;
  45. if(Math.abs(bytes) < thresh) {
  46. return format_number(bytes) + ' B';
  47. }
  48. var units = options.si
  49. ? ['KB','MB','GB','TB','PB','EB','ZB','YB']
  50. : ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
  51. var u = -1;
  52. do {
  53. bytes /= thresh;
  54. ++u;
  55. } while(Math.abs(bytes) >= thresh && u < units.length - 1);
  56. return format_number(bytes) + ' ' + units[u];
  57. }
  58. function unique_string() {
  59. function chr4() {
  60. return Math.random().toString(16).slice(-4);
  61. }
  62. return chr4() + chr4() + '-' + chr4() + '-' + chr4() + '-' + chr4() + '-' + chr4() + chr4() + chr4();
  63. }
  64. function unique_id(prefix) {
  65. var random = unique_string();
  66. var prefix = prefix || "";
  67. var id = prefix + random;
  68. while ($('#' + id).length >= 1) {
  69. id = prefix + unique_string();
  70. }
  71. return id;
  72. }
  73. return {
  74. delay: delay,
  75. format_number: format_number,
  76. format_size: format_size,
  77. unique_string: unique_string,
  78. unique_id: unique_id,
  79. };
  80. });