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.

126 lines
4.4 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 filename = this.recordData[filename_fieldname] || null;
  48. var unique = last_update && field_utils.format.datetime(last_update);
  49. var binary_url = session.url('/web/content', {
  50. model: this.model,
  51. id: JSON.stringify(this.res_id),
  52. data: utils.is_bin_size(this.value) ? null : this.value,
  53. unique: unique ? unique.replace(/[^0-9]/g, '') : null,
  54. filename_field: filename_fieldname,
  55. filename: filename,
  56. field: this.name,
  57. download: true,
  58. });
  59. var preview = new PreviewDialog(
  60. this, [{
  61. url: binary_url,
  62. filename: filename,
  63. mimetype: undefined,
  64. }], 0
  65. );
  66. preview.appendTo($('body'));
  67. event.stopPropagation();
  68. event.preventDefault();
  69. },
  70. });
  71. var FieldBinaryPreview = fields.FieldBinaryFile.extend({
  72. template: 'muk_preview.FieldBinaryPreview',
  73. _renderReadonly: function () {
  74. this._renderPreview();
  75. },
  76. _renderEdit: function () {
  77. if (this.value) {
  78. this.$('.mk_field_preview_container').removeClass("o_hidden");
  79. this.$('.o_select_file_button').first().addClass("o_hidden");
  80. this._renderPreview();
  81. } else {
  82. this.$('.mk_field_preview_container').addClass("o_hidden");
  83. this.$('.o_select_file_button').first().removeClass("o_hidden");
  84. }
  85. },
  86. _renderPreview: function() {
  87. this.$('.mk_field_preview_container').empty();
  88. var filename_fieldname = this.attrs.filename;
  89. var last_update = this.recordData.__last_update;
  90. var filename = this.recordData[filename_fieldname] || null;
  91. var unique = last_update && field_utils.format.datetime(last_update);
  92. var binary_url = session.url('/web/content', {
  93. model: this.model,
  94. id: JSON.stringify(this.res_id),
  95. data: utils.is_bin_size(this.value) ? null : this.value,
  96. unique: unique ? unique.replace(/[^0-9]/g, '') : null,
  97. filename_field: filename_fieldname,
  98. filename: filename,
  99. field: this.name,
  100. download: true,
  101. });
  102. var manager = new PreviewManager(
  103. this, [{
  104. url: binary_url,
  105. filename: filename,
  106. mimetype: undefined,
  107. }], 0
  108. );
  109. manager.appendTo(this.$('.mk_field_preview_container'));
  110. },
  111. on_save_as: function (event) {
  112. event.stopPropagation();
  113. },
  114. });
  115. registry.add('binary_preview', FieldBinaryPreview);
  116. return FieldBinaryPreview;
  117. });