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.

100 lines
3.6 KiB

6 years ago
  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_export.ExportWidgets', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var utils = require('web.utils');
  23. var fields = require('web.basic_fields');
  24. var QWeb = core.qweb;
  25. var _t = core._t;
  26. fields.FieldBinaryFile.include({
  27. willStart: function() {
  28. var self = this;
  29. var filename_fieldname = this.attrs.filename;
  30. var filename = this.recordData[filename_fieldname] || null;
  31. if (this.mode === 'readonly' && filename && !this.nodeOptions.no_export) {
  32. var export_action = this._rpc({
  33. route: '/web/export_action',
  34. }).then(function (result) {
  35. self.export_action = result;
  36. });
  37. var check_export = this._rpc({
  38. route: '/web/check_export',
  39. params: {
  40. filename: filename,
  41. },
  42. }).then(function (result) {
  43. self.export_enabled = !!result;
  44. });
  45. return $.when(this._super.apply(this, arguments), export_action, check_export);
  46. } else {
  47. return this._super.apply(this, arguments);
  48. }
  49. },
  50. _renderReadonly: function () {
  51. this._super();
  52. var self = this;
  53. var $el = this.$el;
  54. if(this.export_enabled) {
  55. var $wrapper = $('<div/>');
  56. var $button = $('<button type="button" class="o_binary_export" aria-hidden="true"/>');
  57. $button.append($('<i class="fa fa-sign-out"></i>'));
  58. $button.click(function(e) {
  59. e.preventDefault();
  60. e.stopPropagation();
  61. var value = self.get('value');
  62. var filename_fieldname = self.attrs.filename;
  63. var filename = self.recordData[filename_fieldname] || null;
  64. var url = '/web/content?' + $.param({
  65. 'model': self.model,
  66. 'id': self.res_id,
  67. 'field': self.name,
  68. 'filename_field': filename_fieldname,
  69. 'filename': filename,
  70. 'download': true,
  71. 'data': utils.is_bin_size(value) ? null : value,
  72. });
  73. self.do_action({
  74. 'type': 'ir.actions.act_window',
  75. 'res_model': "muk_converter.convert",
  76. 'name': _t('Convert File'),
  77. 'views': [[self.export_action, 'form']],
  78. 'view_type': 'form',
  79. 'view_mode': 'form',
  80. 'target': 'new',
  81. 'context': {
  82. 'default_type': "url",
  83. 'default_input_url': url,
  84. 'default_input_name': filename,
  85. },
  86. });
  87. });
  88. $wrapper.addClass($el.attr('class'));
  89. $el.removeClass("o_field_widget o_hidden");
  90. self.replaceElement($wrapper);
  91. $wrapper.append($button);
  92. $wrapper.append($el);
  93. }
  94. },
  95. });
  96. });