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.

113 lines
3.9 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_preview_markdown.PreviewContentMSOffice', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var ajax = require('web.ajax');
  23. var utils = require('web.utils');
  24. var session = require('web.session');
  25. var registry = require('muk_preview.registry');
  26. var AbstractPreviewContent = require('muk_preview.AbstractPreviewContent');
  27. var QWeb = core.qweb;
  28. var _t = core._t;
  29. var PreviewContentMSOffice = AbstractPreviewContent.extend({
  30. template: "muk_preview.PreviewContentMSOffice",
  31. willStart: function() {
  32. var def = $.Deferred();
  33. this._downloadFile().done(function(file) {
  34. this._createAttachment(file).done(function(data) {
  35. this.attachment = data;
  36. def.resolve();
  37. }.bind(this));
  38. }.bind(this));
  39. return $.when(this._super.apply(this, arguments), def);
  40. },
  41. renderPreviewContent: function() {
  42. var viewer = 'https://view.officeapps.live.com/op/embed.aspx?src=';
  43. this.$('iframe').attr('src', viewer + encodeURIComponent(this.attachment.url));
  44. return this._super.apply(this, arguments);
  45. },
  46. destroy: function () {
  47. if (this.attachment) {
  48. this._rpc({
  49. model: 'ir.attachment',
  50. method: 'unlink',
  51. args: [this.attachment.id],
  52. context: session.user_context,
  53. }, {
  54. shadow: true,
  55. });
  56. }
  57. return this._super.apply(this, arguments);
  58. },
  59. _downloadFile: function() {
  60. return $.ajax({
  61. url: this.url,
  62. dataType: "binary",
  63. }).fail(function(jqXHR, textStatus) {
  64. console.error(textStatus);
  65. });
  66. },
  67. _createAttachment: function(file) {
  68. var form = new FormData();
  69. form.append('temporary', true);
  70. form.append('ufile', file, this.filename);
  71. form.append('csrf_token', core.csrf_token);
  72. return $.ajax({
  73. data: form,
  74. type: 'POST',
  75. dataType: 'json',
  76. url: '/utils/attachment/add',
  77. enctype: 'multipart/form-data',
  78. processData: false,
  79. contentType: false
  80. }).fail(function(jqXHR, textStatus) {
  81. console.error(textStatus);
  82. });
  83. },
  84. downloadable: false,
  85. printable: false,
  86. });
  87. _.each([
  88. 'doc', 'docx', 'docm', 'ppt', 'pptx', 'pptm', 'xls', 'xlsx', 'xlsm', 'xlsb'
  89. ], function(extension) {
  90. registry.add(extension, PreviewContentMSOffice);
  91. registry.add("." + extension, PreviewContentMSOffice);
  92. });
  93. _.each([
  94. 'application/msword', 'application/ms-word', 'application/vnd.ms-word.document.macroEnabled.12',
  95. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.mspowerpoint',
  96. 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  97. 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'application/vnd.ms-excel',
  98. 'application/vnd.msexcel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  99. 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'application/vnd.ms-excel.sheet.macroEnabled.12'
  100. ], function(mimetype) {
  101. registry.add(mimetype, PreviewContentMSOffice);
  102. });
  103. return PreviewContentMSOffice;
  104. });