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.

99 lines
3.5 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. _downloadFile: function() {
  47. return $.ajax({
  48. url: this.url,
  49. dataType: "binary",
  50. }).fail(function(jqXHR, textStatus) {
  51. console.error(textStatus);
  52. });
  53. },
  54. _createAttachment: function(file) {
  55. var form = new FormData();
  56. form.append('ufile', file, this.filename);
  57. form.append('csrf_token', core.csrf_token);
  58. return $.ajax({
  59. data: form,
  60. type: 'POST',
  61. dataType: 'json',
  62. url: '/utils/attachment/add',
  63. enctype: 'multipart/form-data',
  64. processData: false,
  65. contentType: false
  66. }).fail(function(jqXHR, textStatus) {
  67. console.error(textStatus);
  68. });
  69. },
  70. downloadable: false,
  71. printable: false,
  72. });
  73. _.each([
  74. 'doc', 'docx', 'docm', 'ppt', 'pptx', 'pptm', 'xls', 'xlsx', 'xlsm', 'xlsb'
  75. ], function(extension) {
  76. registry.add(extension, PreviewContentMSOffice);
  77. registry.add("." + extension, PreviewContentMSOffice);
  78. });
  79. _.each([
  80. 'application/msword', 'application/ms-word', 'application/vnd.ms-word.document.macroEnabled.12',
  81. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.mspowerpoint',
  82. 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  83. 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'application/vnd.ms-excel',
  84. 'application/vnd.msexcel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  85. 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'application/vnd.ms-excel.sheet.macroEnabled.12'
  86. ], function(mimetype) {
  87. registry.add(mimetype, PreviewContentMSOffice);
  88. });
  89. return PreviewContentMSOffice;
  90. });