Welcome to mirror list, hosted at ThFree Co, Russian Federation.

csv_viewer.vue « csv « blob « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7231d02302401ff8dff976b0a615484fe0daaab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<script>
import { GlLoadingIcon, GlTable, GlButton } from '@gitlab/ui';
import Papa from 'papaparse';
import { setUrlParams } from '~/lib/utils/url_utility';
import PapaParseAlert from '~/vue_shared/components/papa_parse_alert.vue';
import { MAX_ROWS_TO_RENDER } from './constants';

export default {
  components: {
    PapaParseAlert,
    GlTable,
    GlButton,
    GlLoadingIcon,
  },
  props: {
    csv: {
      type: String,
      required: true,
    },
    remoteFile: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      items: [],
      papaParseErrors: [],
      loading: true,
      isTooLarge: false,
    };
  },
  computed: {
    pathToRawFile() {
      return setUrlParams({ plain: 1 });
    },
  },
  mounted() {
    if (!this.remoteFile) {
      const parsed = Papa.parse(this.csv, { skipEmptyLines: true });
      this.handleParsedData(parsed);
    } else {
      Papa.parse(this.csv, {
        download: true,
        skipEmptyLines: true,
        complete: (parsed) => {
          this.handleParsedData(parsed);
        },
      });
    }
  },
  methods: {
    handleParsedData(parsed) {
      if (parsed.data.length > MAX_ROWS_TO_RENDER) {
        this.isTooLarge = true;
      }

      this.items = parsed.data.slice(0, MAX_ROWS_TO_RENDER);

      if (parsed.errors.length) {
        this.papaParseErrors = parsed.errors;
      }

      this.loading = false;
    },
  },
};
</script>

<template>
  <div class="container-fluid md gl-mt-3 gl-mb-3">
    <div v-if="loading" class="gl-text-center loading">
      <gl-loading-icon class="gl-mt-5" size="lg" />
    </div>
    <div v-else>
      <papa-parse-alert v-if="papaParseErrors.length" :papa-parse-errors="papaParseErrors" />
      <gl-table
        :empty-text="s__('CsvViewer|No CSV data to display.')"
        :items="items"
        :fields="$options.fields"
        show-empty
        thead-class="gl-display-none"
      />
      <div
        v-if="isTooLarge"
        class="gl-display-flex gl-flex-direction-column gl-align-items-center gl-p-5"
      >
        <p data-testid="large-csv-text">
          {{
            s__(
              'CsvViewer|The file is too large to render all the rows. To see the entire file, switch to the raw view.',
            )
          }}
        </p>

        <gl-button category="secondary" variant="confirm" :href="pathToRawFile">{{
          s__('CsvViewer|View raw data')
        }}</gl-button>
      </div>
    </div>
  </div>
</template>