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.

133 lines
3.6 KiB

  1. import hljs from 'highlight.js/lib/highlight';
  2. import hljsLanguages from './hljsLanguages';
  3. import HTTP, { newHttpClient } from './http';
  4. import Messages from './messages.vue';
  5. hljsLanguages.forEach((languageName) => {
  6. /* eslint-disable import/no-dynamic-require, global-require */
  7. const languageModule = require(`highlight.js/lib/languages/${languageName}`);
  8. /* eslint-enable import/no-dynamic-require, global-require */
  9. hljs.registerLanguage(languageName, languageModule);
  10. });
  11. export default {
  12. components: { Messages },
  13. data: () => ({
  14. file: '',
  15. messages: [],
  16. format: 'json',
  17. isLoading: false,
  18. isCloudUploadActive: false,
  19. canUploadFromCloud: false,
  20. }),
  21. mounted() {
  22. hljs.initHighlighting();
  23. },
  24. created() {
  25. newHttpClient().get('/v1/features').then((response) => {
  26. this.canUploadFromCloud = response.data.cloud_upload;
  27. });
  28. },
  29. computed: {
  30. projectId() {
  31. return window.location.pathname.split('/')[2];
  32. },
  33. postUploadUrl() {
  34. return window.location.pathname.split('/').slice(0, -1).join('/');
  35. },
  36. cloudUploadUrl() {
  37. return '/cloud-storage'
  38. + `?project_id=${this.projectId}`
  39. + `&upload_format=${this.format}`
  40. + `&next=${encodeURIComponent('about:blank')}`;
  41. },
  42. },
  43. methods: {
  44. cloudUpload() {
  45. const iframeUrl = this.$refs.cloudUploadPane.contentWindow.location.href;
  46. if (iframeUrl.indexOf('/v1/cloud-upload') > -1) {
  47. this.isCloudUploadActive = false;
  48. this.$nextTick(() => {
  49. window.location.href = this.postUploadUrl;
  50. });
  51. }
  52. },
  53. upload() {
  54. this.isLoading = true;
  55. this.file = this.$refs.file.files[0];
  56. const formData = new FormData();
  57. formData.append('file', this.file);
  58. formData.append('format', this.format);
  59. HTTP.post('docs/upload',
  60. formData,
  61. {
  62. headers: {
  63. 'Content-Type': 'multipart/form-data',
  64. },
  65. })
  66. .then((response) => {
  67. console.log(response); // eslint-disable-line no-console
  68. this.messages = [];
  69. window.location = this.postUploadUrl;
  70. })
  71. .catch((error) => {
  72. this.isLoading = false;
  73. this.handleError(error);
  74. });
  75. },
  76. handleError(error) {
  77. const problems = Array.isArray(error.response.data)
  78. ? error.response.data
  79. : [error.response.data];
  80. problems.forEach((problem) => {
  81. if ('detail' in problem) {
  82. this.messages.push(problem.detail);
  83. } else if ('text' in problem) {
  84. this.messages = problem.text;
  85. }
  86. });
  87. },
  88. download() {
  89. this.isLoading = true;
  90. const headers = {};
  91. if (this.format === 'csv') {
  92. headers.Accept = 'text/csv; charset=utf-8';
  93. headers['Content-Type'] = 'text/csv; charset=utf-8';
  94. } else {
  95. headers.Accept = 'application/json';
  96. headers['Content-Type'] = 'application/json';
  97. }
  98. HTTP({
  99. url: 'docs/download',
  100. method: 'GET',
  101. responseType: 'blob',
  102. params: {
  103. q: this.format,
  104. },
  105. headers,
  106. }).then((response) => {
  107. const url = window.URL.createObjectURL(new Blob([response.data]));
  108. const link = document.createElement('a');
  109. link.href = url;
  110. link.setAttribute('download', 'file.' + this.format); // or any other extension
  111. document.body.appendChild(link);
  112. this.isLoading = false;
  113. link.click();
  114. }).catch((error) => {
  115. this.isLoading = false;
  116. this.handleError(error);
  117. });
  118. },
  119. },
  120. };