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.6 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2017 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.binary', function(require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var session = require('web.session');
  23. var utils = require('web.field_utils');
  24. var fields = require('web.basic_fields');
  25. var registry = require('web.field_registry');
  26. var _t = core._t;
  27. var QWeb = core.qweb;
  28. fields.FieldBinaryFile.include({
  29. willStart: function () {
  30. var def = this._rpc({
  31. route: '/params/muk_web_utils.binary_max_size',
  32. }).done(function(result) {
  33. this.max_upload_size = result.max_upload_size * 1024 * 1024;
  34. }.bind(this));
  35. return this._super.apply(this, arguments);
  36. },
  37. });
  38. fields.FieldBinaryImage.include({
  39. willStart: function () {
  40. var def = this._rpc({
  41. route: '/params/muk_web_utils.binary_max_size',
  42. }).done(function(result) {
  43. this.max_upload_size = result.max_upload_size * 1024 * 1024;
  44. }.bind(this));
  45. return this._super.apply(this, arguments);
  46. },
  47. });
  48. var FieldBinarySize = fields.FieldFloat.extend({
  49. init: function(parent, name, record) {
  50. this._super.apply(this, arguments);
  51. this.units = this.nodeOptions.si ?
  52. ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] :
  53. ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
  54. this.thresh = this.nodeOptions.si ? 1000 : 1024;
  55. },
  56. _formatValue: function (value) {
  57. if(Math.abs(value) < this.thresh) {
  58. return this._super.call(this, value) + ' B';
  59. }
  60. var unit = -1;
  61. do {
  62. value /= this.thresh;
  63. ++unit;
  64. } while(Math.abs(value) >= this.thresh && unit < this.units.length - 1);
  65. return this._super.call(this, value) + ' ' + this.units[unit];
  66. },
  67. });
  68. registry.add('binary_size', FieldBinarySize);
  69. return {
  70. FieldBinarySize: FieldBinarySize,
  71. };
  72. });