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.

142 lines
5.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
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_msoffice.PreviewHandler', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var PreviewHandler = require('muk_preview.PreviewHandler');
  23. var QWeb = core.qweb;
  24. var _t = core._t;
  25. var WordHandler = PreviewHandler.PDFHandler.extend({
  26. checkExtension: function(extension) {
  27. return ['.doc', '.docx', '.docm', 'doc', 'docx', 'docm'].includes(extension);
  28. },
  29. checkType: function(mimetype) {
  30. return ['application/msword', 'application/ms-word', 'application/vnd.ms-word.document.macroEnabled.12',
  31. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'].includes(mimetype);
  32. },
  33. createHtml: function(url, mimetype, extension, title) {
  34. return this._super('/web/preview/converter/msoffice?' + $.param({
  35. 'url': url,
  36. 'title': title,
  37. }));
  38. },
  39. });
  40. var PowerPointHandler = PreviewHandler.PDFHandler.extend({
  41. checkExtension: function(extension) {
  42. return ['.ppt', '.pptx', '.pptm', 'ppt', 'pptx', 'pptm'].includes(extension);
  43. },
  44. checkType: function(mimetype) {
  45. return ['application/vnd.mspowerpoint', 'application/vnd.ms-powerpoint',
  46. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  47. 'application/vnd.ms-powerpoint.presentation.macroEnabled.12'].includes(mimetype);
  48. },
  49. createHtml: function(url, mimetype, extension, title) {
  50. return this._super('/web/preview/converter/msoffice?' + $.param({
  51. 'url': url,
  52. 'title': title,
  53. }));
  54. },
  55. });
  56. var ExcelHandler = PreviewHandler.BaseHandler.extend({
  57. checkExtension: function(extension) {
  58. return ['.xls', '.xlsx', '.xlsm', '.xlsb', 'xls', 'xlsx', 'xlsm', 'xlsb'].includes(extension);
  59. },
  60. checkType: function(mimetype) {
  61. return ['application/vnd.ms-excel', 'application/vnd.msexcel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  62. 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'application/vnd.ms-excel.sheet.macroEnabled.12'].includes(mimetype);
  63. },
  64. createHtml: function(url, mimetype, extension, title) {
  65. var result = $.Deferred();
  66. var $content = $(QWeb.render('ExcelHTMLContent'));
  67. $.ajax(url, {
  68. type: "GET",
  69. dataType: "binary",
  70. responseType:'arraybuffer',
  71. processData: false,
  72. success: function(arraybuffer) {
  73. var data = new Uint8Array(arraybuffer);
  74. var arr = new Array();
  75. for(var i = 0; i != data.length; ++i) {
  76. arr[i] = String.fromCharCode(data[i]);
  77. }
  78. var workbook = XLSX.read(arr.join(""), {
  79. type:"binary",
  80. cellDates:true,
  81. cellStyles:true,
  82. cellNF:true
  83. });
  84. var jsonWorkbook = {};
  85. _.each(workbook.SheetNames, function(sheet, index, list) {
  86. var jsonData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet], {header:1});
  87. if(jsonData.length > 0) {
  88. jsonWorkbook[sheet] = jsonData;
  89. }
  90. var worksheet = workbook.Sheets[sheet];
  91. });
  92. $content.find('.excel-loader').hide();
  93. $content.find('.excel-container').show();
  94. var index = 0;
  95. _.each(jsonWorkbook, function(sheet, sheetname, list) {
  96. var $tab = $('<a/>');
  97. $tab.attr('href', '#sheet-' + index);
  98. $tab.attr('aria-controls', 'sheet-' + index);
  99. $tab.attr('role', 'tab');
  100. $tab.attr('data-toggle', 'tab');
  101. $tab.append('<i class="fa fa-table" aria-hidden="true"></i>');
  102. $tab.append($('<span/>').text(sheetname));
  103. $content.find('.nav-tabs').append($('<li/>').append($tab));
  104. var $pane = $('<div/>');
  105. $pane.addClass('tab-pane table-container');
  106. $pane.attr('id', 'sheet-' + index);
  107. $pane.handsontable({
  108. data: sheet,
  109. rowHeaders: true,
  110. colHeaders: true,
  111. stretchH: 'all',
  112. readOnly: true,
  113. columnSorting: true,
  114. autoColumnSize: true,
  115. });
  116. $content.find('.tab-content').append($pane);
  117. index++;
  118. });
  119. $content.find('.tab-line a:first').tab('show')
  120. },
  121. error: function(request, status, error) {
  122. console.error(request.responseText);
  123. },
  124. });
  125. result.resolve($content);
  126. return $.when(result);
  127. },
  128. });
  129. return {
  130. ExcelHandler: ExcelHandler,
  131. WordHandler: WordHandler,
  132. PowerPointHandler: PowerPointHandler,
  133. }
  134. });