aboutsummaryrefslogtreecommitdiff
path: root/src/components/exporter/exporter.js
blob: 8f507416752b90c3faf0fbd0895df4274ac4e14b (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
const Exporter = {
  props: {
    getContent: {
      type: Function,
      required: true
    },
    filename: {
      type: String,
      default: 'export.csv'
    },
    exportButtonLabel: {
      type: String,
      default () {
        return this.$t('exporter.export')
      }
    },
    processingMessage: {
      type: String,
      default () {
        return this.$t('exporter.processing')
      }
    }
  },
  data () {
    return {
      processing: false
    }
  },
  methods: {
    process () {
      this.processing = true
      this.getContent()
        .then((content) => {
          const fileToDownload = document.createElement('a')
          fileToDownload.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(content))
          fileToDownload.setAttribute('download', this.filename)
          fileToDownload.style.display = 'none'
          document.body.appendChild(fileToDownload)
          fileToDownload.click()
          document.body.removeChild(fileToDownload)
          // Add delay before hiding processing state since browser takes some time to handle file download
          setTimeout(() => { this.processing = false }, 2000)
        })
    }
  }
}

export default Exporter