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.

130 lines
4.5 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (c) 2017-2019 MuK IT GmbH.
  4. *
  5. * This file is part of MuK Preview
  6. * (see https://mukit.at).
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. **********************************************************************************/
  22. odoo.define('muk_web_preview.binary', function(require) {
  23. "use strict";
  24. var core = require('web.core');
  25. var utils = require('web.utils');
  26. var session = require('web.session');
  27. var fields = require('web.basic_fields');
  28. var registry = require('web.field_registry');
  29. var field_utils = require('web.field_utils');
  30. var PreviewManager = require('muk_preview.PreviewManager');
  31. var PreviewDialog = require('muk_preview.PreviewDialog');
  32. var _t = core._t;
  33. var QWeb = core.qweb;
  34. fields.FieldBinaryFile.include({
  35. events: _.extend({}, fields.FieldBinaryFile.prototype.events, {
  36. "click .mk_field_preview_button": "_onPreviewButtonClick",
  37. }),
  38. _renderReadonly: function () {
  39. this._super.apply(this, arguments);
  40. var $button = $('<button/>', {
  41. class: 'mk_field_preview_button',
  42. type: 'button',
  43. html: '<i class="fa fa-file-text-o"></i>',
  44. });
  45. this.$el.prepend($button);
  46. },
  47. _onPreviewButtonClick: function(event) {
  48. var filename_fieldname = this.attrs.filename;
  49. var last_update = this.recordData.__last_update;
  50. var mimetype = this.recordData['mimetype'] || null;
  51. var filename = this.recordData[filename_fieldname] || null;
  52. var unique = last_update && field_utils.format.datetime(last_update);
  53. var binary_url = session.url('/web/content', {
  54. model: this.model,
  55. id: JSON.stringify(this.res_id),
  56. data: utils.is_bin_size(this.value) ? null : this.value,
  57. unique: unique ? unique.replace(/[^0-9]/g, '') : null,
  58. filename_field: filename_fieldname,
  59. filename: filename,
  60. field: this.name,
  61. download: true,
  62. });
  63. var preview = new PreviewDialog(
  64. this, [{
  65. url: binary_url,
  66. filename: filename,
  67. mimetype: mimetype,
  68. }], 0
  69. );
  70. preview.appendTo($('body'));
  71. event.stopPropagation();
  72. event.preventDefault();
  73. },
  74. });
  75. var FieldBinaryPreview = fields.FieldBinaryFile.extend({
  76. template: 'muk_preview.FieldBinaryPreview',
  77. _renderReadonly: function () {
  78. this._renderPreview();
  79. },
  80. _renderEdit: function () {
  81. if (this.value) {
  82. this.$('.mk_field_preview_container').removeClass("o_hidden");
  83. this.$('.o_select_file_button').first().addClass("o_hidden");
  84. this._renderPreview();
  85. } else {
  86. this.$('.mk_field_preview_container').addClass("o_hidden");
  87. this.$('.o_select_file_button').first().removeClass("o_hidden");
  88. }
  89. },
  90. _renderPreview: function() {
  91. this.$('.mk_field_preview_container').empty();
  92. var filename_fieldname = this.attrs.filename;
  93. var last_update = this.recordData.__last_update;
  94. var filename = this.recordData[filename_fieldname] || null;
  95. var unique = last_update && field_utils.format.datetime(last_update);
  96. var binary_url = session.url('/web/content', {
  97. model: this.model,
  98. id: JSON.stringify(this.res_id),
  99. data: utils.is_bin_size(this.value) ? null : this.value,
  100. unique: unique ? unique.replace(/[^0-9]/g, '') : null,
  101. filename_field: filename_fieldname,
  102. filename: filename,
  103. field: this.name,
  104. download: true,
  105. });
  106. var manager = new PreviewManager(
  107. this, [{
  108. url: binary_url,
  109. filename: filename,
  110. mimetype: undefined,
  111. }], 0
  112. );
  113. manager.appendTo(this.$('.mk_field_preview_container'));
  114. },
  115. on_save_as: function (event) {
  116. event.stopPropagation();
  117. },
  118. });
  119. registry.add('binary_preview', FieldBinaryPreview);
  120. return FieldBinaryPreview;
  121. });