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.

121 lines
5.6 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_text.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 TextHandler = PreviewHandler.BaseHandler.extend({
  27. cssLibs: [
  28. '/muk_web_preview_text/static/lib/highlight/styles/default.css',
  29. ],
  30. jsLibs: [
  31. '/muk_web_preview_text/static/lib/highlight/highlight.pack.js',
  32. '/muk_web_preview_text/static/lib/highlight_line_numbers/highlight_line_numbers.js',
  33. ],
  34. checkExtension: function(extension) {
  35. return ['.abc', '.acgi', '.aip', '.asm', '.asp', '.c', '.c', '.c++', '.cc', '.cc', '.com', '.conf',
  36. '.cpp', '.csh', '.css', '.cxx', '.def', '.el', '.etx', '.f', '.f', '.f77', '.f90', '.f90',
  37. '.flx', '.for', '.for', '.g', '.h', '.h', '.hh', '.hh', '.hlb', '.htc', '.htm', '.html',
  38. '.htmls', '.htt', '.htx', '.idc', '.jav', '.jav', '.java', '.java', '.js', '.js', '.ksh',
  39. '.list', '.log', '.lsp', '.lst', '.lsx', '.m', '.m', '.mar', '.mcf', '.p', '.pas', '.pl',
  40. '.pl', '.pm', '.py', '.rexx', '.rt', '.rt', '.rtf', '.rtx', '.s', '.scm', '.scm', '.sdml',
  41. '.sgm', '.sgm', '.sgml', '.sgml', '.sh', '.shtml', '.shtml', '.spc', '.ssi', '.talk', '.tcl',
  42. '.tcsh', '.text', '.tsv', '.txt', '.uil', '.uni', '.unis', '.uri', '.uris', '.uu', '.uue',
  43. '.vcs', '.wml', '.wmls', '.wsc', '.xml', '.zsh', '.less', '.aj', '.cbl', '.cs', '.coffee',
  44. '.go', '.groovy', '.sc', 'abc', 'acgi', 'aip', 'asm', 'asp', 'c', 'c', 'c++', 'cc', 'cc',
  45. 'com', 'conf', 'cpp', 'csh', 'css', 'cxx', 'def', 'el', 'etx', 'f', 'f', 'f77', 'f90',
  46. 'f90', 'flx', 'for', 'for', 'g', 'h', 'h', 'hh', 'hh', 'hlb', 'htc', 'htm', 'html', 'htmls',
  47. 'htt', 'htx', 'idc', 'jav', 'jav', 'java', 'java', 'js', 'js', 'ksh', 'list', 'log', 'lsp',
  48. 'lst', 'lsx', 'm', 'm', 'mar', 'mcf', 'p', 'pas', 'pl', 'pl', 'pm', 'py', 'rexx', 'rt',
  49. 'rt', 'rtf', 'rtx', 's', 'scm', 'scm', 'sdml', 'sgm', 'sgm', 'sgml', 'sgml', 'sh', 'shtml',
  50. 'shtml', 'spc', 'ssi', 'talk', 'tcl', 'tcsh', 'text', 'tsv', 'txt', 'uil', 'uni', 'unis',
  51. 'uri', 'uris', 'uu', 'uue', 'vcs', 'wml', 'wmls', 'wsc', 'xml', 'zsh', 'less', 'aj', 'cbl',
  52. 'cs', 'coffee', 'go', 'groovy', 'sc'].includes(extension);
  53. },
  54. checkType: function(mimetype) {
  55. return ['text/vnd.abc', 'text/html', 'text/x-audiosoft-intra', 'text/x-asm', 'text/asp', 'text/plain',
  56. 'text/x-c', 'text/x-script.csh', 'text/css', 'text/x-script.elisp', 'text/x-setext', 'text/x-fortran',
  57. 'text/vnd.fmi.flexstor', 'text/x-h', 'text/x-script', 'text/x-component', 'text/webviewhtml',
  58. 'text/x-java-source', 'text/javascript', 'text/ecmascript', 'text/x-script.ksh', 'text/x-script.lisp',
  59. 'text/x-la-asf', 'text/x-m', 'text/mcf', 'text/x-pascal', 'text/pascal', 'text/x-script.perl',
  60. 'text/x-script.perl-module', 'text/x-script.phyton', 'text/x-script.rexx', 'text/richtext',
  61. 'text/vnd.rn-realtext', 'text/x-script.guile', 'text/x-script.scheme', 'text/sgml', 'text/x-sgml',
  62. 'text/x-script.sh', 'text/x-server-parsed-html', 'text/x-speech', 'text/x-script.tcl',
  63. 'text/x-script.tcsh', 'text/tab-separated-values', 'text/x-uil', 'text/uri-list', 'text/x-uuencode',
  64. 'text/x-vcalendar', 'text/vnd.wap.wml', 'text/vnd.wap.wmlscript', 'text/scriplet', 'text/xml',
  65. 'text/x-script.zsh'].includes(mimetype);
  66. },
  67. createHtml: function(url, mimetype, extension, title) {
  68. var result = $.Deferred();
  69. var $content = $(QWeb.render('TextHTMLContent'));
  70. ajax.loadLibs(this).then(function() {
  71. $.ajax(url, {
  72. dataType: "text",
  73. success: function(text) {
  74. $content.find('.code-loader').hide();
  75. $content.find('.code-container').show();
  76. var $codeBlock = $content.find('.code-view');
  77. var $codeLang = $content.find(".code-lang");
  78. function setText(language) {
  79. $codeBlock.removeClass();
  80. $codeBlock.addClass('code-view');
  81. if(language) {
  82. $codeBlock.addClass(language);
  83. }
  84. $codeBlock.text(text);
  85. hljs.highlightBlock($codeBlock[0]);
  86. hljs.lineNumbersBlock($codeBlock[0]);
  87. }
  88. setText();
  89. $codeLang.select2();
  90. $codeLang.on("change", function(e) {
  91. if(e.val === 'default') {
  92. setText();
  93. } else {
  94. setText(e.val);
  95. }
  96. });
  97. $.each($codeBlock.attr('class').split(" "), function (i, cls) {
  98. if($content.find(".code-lang option[value='" + cls + "']").val()) {
  99. $codeLang.val(cls).trigger("change");
  100. }
  101. });
  102. },
  103. error: function(request, status, error) {
  104. console.error(request.responseText);
  105. }
  106. });
  107. });
  108. result.resolve($content);
  109. return result;
  110. },
  111. });
  112. return {
  113. TextHandler: TextHandler,
  114. }
  115. });