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.

151 lines
5.6 KiB

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