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.

127 lines
4.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_preview.binary', function(require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var utils = require('web.utils');
  23. var session = require('web.session');
  24. var fields = require('web.basic_fields');
  25. var registry = require('web.field_registry');
  26. var field_utils = require('web.field_utils');
  27. var PreviewManager = require('muk_preview.PreviewManager');
  28. var PreviewDialog = require('muk_preview.PreviewDialog');
  29. var _t = core._t;
  30. var QWeb = core.qweb;
  31. fields.FieldBinaryFile.include({
  32. events: _.extend({}, fields.FieldBinaryFile.prototype.events, {
  33. "click .mk_field_preview_button": "_onPreviewButtonClick",
  34. }),
  35. _renderReadonly: function () {
  36. this._super.apply(this, arguments);
  37. var $button = $('<button/>', {
  38. class: 'mk_field_preview_button',
  39. type: 'button',
  40. html: '<i class="fa fa-file-text-o"></i>',
  41. });
  42. this.$el.prepend($button);
  43. },
  44. _onPreviewButtonClick: function(event) {
  45. var filename_fieldname = this.attrs.filename;
  46. var last_update = this.recordData.__last_update;
  47. var mimetype = this.recordData['mimetype'] || null;
  48. var filename = this.recordData[filename_fieldname] || null;
  49. var unique = last_update && field_utils.format.datetime(last_update);
  50. var binary_url = session.url('/web/content', {
  51. model: this.model,
  52. id: JSON.stringify(this.res_id),
  53. data: utils.is_bin_size(this.value) ? null : this.value,
  54. unique: unique ? unique.replace(/[^0-9]/g, '') : null,
  55. filename_field: filename_fieldname,
  56. filename: filename,
  57. field: this.name,
  58. download: true,
  59. });
  60. var preview = new PreviewDialog(
  61. this, [{
  62. url: binary_url,
  63. filename: filename,
  64. mimetype: mimetype,
  65. }], 0
  66. );
  67. preview.appendTo($('body'));
  68. event.stopPropagation();
  69. event.preventDefault();
  70. },
  71. });
  72. var FieldBinaryPreview = fields.FieldBinaryFile.extend({
  73. template: 'muk_preview.FieldBinaryPreview',
  74. _renderReadonly: function () {
  75. this._renderPreview();
  76. },
  77. _renderEdit: function () {
  78. if (this.value) {
  79. this.$('.mk_field_preview_container').removeClass("o_hidden");
  80. this.$('.o_select_file_button').first().addClass("o_hidden");
  81. this._renderPreview();
  82. } else {
  83. this.$('.mk_field_preview_container').addClass("o_hidden");
  84. this.$('.o_select_file_button').first().removeClass("o_hidden");
  85. }
  86. },
  87. _renderPreview: function() {
  88. this.$('.mk_field_preview_container').empty();
  89. var filename_fieldname = this.attrs.filename;
  90. var last_update = this.recordData.__last_update;
  91. var filename = this.recordData[filename_fieldname] || null;
  92. var unique = last_update && field_utils.format.datetime(last_update);
  93. var binary_url = session.url('/web/content', {
  94. model: this.model,
  95. id: JSON.stringify(this.res_id),
  96. data: utils.is_bin_size(this.value) ? null : this.value,
  97. unique: unique ? unique.replace(/[^0-9]/g, '') : null,
  98. filename_field: filename_fieldname,
  99. filename: filename,
  100. field: this.name,
  101. download: true,
  102. });
  103. var manager = new PreviewManager(
  104. this, [{
  105. url: binary_url,
  106. filename: filename,
  107. mimetype: undefined,
  108. }], 0
  109. );
  110. manager.appendTo(this.$('.mk_field_preview_container'));
  111. },
  112. on_save_as: function (event) {
  113. event.stopPropagation();
  114. },
  115. });
  116. registry.add('binary_preview', FieldBinaryPreview);
  117. return FieldBinaryPreview;
  118. });