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.

116 lines
4.0 KiB

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