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.

131 lines
4.8 KiB

7 years ago
7 years ago
7 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_preview_mail.PreviewHandler', function (require) {
  20. "use strict";
  21. var ajax = require('web.ajax');
  22. var core = require('web.core');
  23. var PreviewHandler = require('muk_preview.PreviewHandler');
  24. var PreviewGenerator = require('muk_preview.PreviewGenerator');
  25. var QWeb = core.qweb;
  26. var _t = core._t;
  27. var MailHandler = PreviewHandler.BaseHandler.extend({
  28. checkExtension: function(extension) {
  29. return ['.eml', 'eml'].includes(extension);
  30. },
  31. checkType: function(mimetype) {
  32. return ['message/rfc822'].includes(mimetype);
  33. },
  34. createHtml: function(url, mimetype, extension, title) {
  35. var self = this;
  36. var result = $.Deferred();
  37. var $content = $(QWeb.render('MailHTMLContent'));
  38. $.ajax({
  39. url: '/web/preview/converter/mail',
  40. dataType: "json",
  41. data: {
  42. url: url,
  43. },
  44. success: function(mail) {
  45. $content.find('.mail-loader').hide();
  46. $content.find('.mail-container').show();
  47. $content.find('.reply').click(function() {
  48. $('.modal').modal('hide');
  49. self.widget.do_action({
  50. type: 'ir.actions.act_window',
  51. res_model: 'mail.mail',
  52. views: [[false, 'form']],
  53. context: {
  54. default_subject: _t("Re: " + mail.subject),
  55. default_email_to: mail.from
  56. },
  57. });
  58. });
  59. $content.find('#subject').text(mail.subject);
  60. $content.find('#meta-to').text(mail.to);
  61. $content.find('#meta-cc').text(mail.cc);
  62. $content.find('#meta-from').text(mail.from);
  63. $content.find('#meta-date').text(mail.date);
  64. mail.body = mail.body.replace(/cid:(\w|\d)+.\w+/g, function(cid) {
  65. var attachments = $.grep(mail.attachments, function(attachment) {
  66. return attachment[4].cid && attachment[4].cid.includes(cid.substring(4, cid.length));
  67. });
  68. return attachments[0][3];
  69. });
  70. $content.find('#body').html(mail.body);
  71. _.each(mail.attachments, function(attachment, index, attachments) {
  72. if(!attachment[4].cid) {
  73. var $download = $('<i/>');
  74. $download.addClass('fa fa-download');
  75. $download.css('cursor', 'pointer');
  76. $download.data('toggle', 'tooltip');
  77. $download.data('placement', 'top');
  78. $download.data('url', attachment[3]);
  79. $download.attr('title', _t("Download!"));
  80. $download.tooltip();
  81. $download.click(function(e) {
  82. window.location = $(e.target).data('url');
  83. });
  84. var $tab = $('<a/>');
  85. $tab.attr('href', '#attachment-' + index);
  86. $tab.attr('aria-controls', 'attachment-' + index);
  87. $tab.attr('role', 'tab');
  88. $tab.attr('data-toggle', 'tab');
  89. $tab.append($download);
  90. $tab.append($('<span/>').text(attachment[0]));
  91. $content.find('.nav-tabs').append($('<li/>').append($tab));
  92. $tab.click(function(e) {
  93. var $target = $content.find($(e.currentTarget).attr('href'));
  94. if(!$target.data('loaded')) {
  95. $target.data('loaded', true);
  96. PreviewGenerator.createPreview(self, $target.data('url'), $target.data('mimetype'),
  97. $target.data('extension'), $target.data('title')).then(function($preview) {
  98. $target.append($preview);
  99. });
  100. }
  101. });
  102. var $pane = $('<div/>');
  103. $pane.addClass('tab-pane attachment-container');
  104. $pane.attr('id', 'attachment-' + index);
  105. $pane.data('loaded', false);
  106. $pane.data('url', attachment[3]);
  107. $pane.data('mimetype', attachment[1]);
  108. $pane.data('extension', attachment[2]);
  109. $pane.data('title', attachment[0]);
  110. $content.find('.tab-content').append($pane);
  111. }
  112. });
  113. },
  114. error: function(request, status, error) {
  115. console.error(request.responseText);
  116. }
  117. });
  118. result.resolve($content);
  119. return result;
  120. },
  121. });
  122. return {
  123. MailHandler: MailHandler,
  124. }
  125. });