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.

81 lines
2.4 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. function format_number(value) {
  27. if (value === false || typeof value !== "number") {
  28. return "";
  29. }
  30. var l10n = core._t.database.parameters;
  31. var formatted = _.str.sprintf('%.' + 2 + 'f', value || 0).split('.');
  32. formatted[0] = utils.insert_thousand_seps(formatted[0]);
  33. return formatted.join(l10n.decimal_point);
  34. }
  35. function format_size(bytes, options) {
  36. var options = options || {};
  37. var thresh = options.si ? 1000 : 1024;
  38. if(Math.abs(bytes) < thresh) {
  39. return format_number(bytes) + ' B';
  40. }
  41. var units = options.si
  42. ? ['KB','MB','GB','TB','PB','EB','ZB','YB']
  43. : ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
  44. var u = -1;
  45. do {
  46. bytes /= thresh;
  47. ++u;
  48. } while(Math.abs(bytes) >= thresh && u < units.length - 1);
  49. return format_number(bytes) + ' ' + units[u];
  50. }
  51. function unique_string() {
  52. function chr4() {
  53. return Math.random().toString(16).slice(-4);
  54. }
  55. return chr4() + chr4() + '-' + chr4() + '-' + chr4() + '-' + chr4() + '-' + chr4() + chr4() + chr4();
  56. }
  57. function unique_id(prefix) {
  58. var random = unique_string();
  59. var prefix = prefix || "";
  60. var id = prefix + random;
  61. while ($('#' + id).length >= 1) {
  62. id = prefix + unique_string();
  63. }
  64. return id;
  65. }
  66. return {
  67. format_number: format_number,
  68. format_size: format_size,
  69. unique_string: unique_string,
  70. unique_id: unique_id,
  71. };
  72. });