aboutsummaryrefslogtreecommitdiff
path: root/src/components/attachment/attachment.js
blob: de551611832b0e209dd12329c1d27897079557ad (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
import nsfwImage from '../../assets/nsfw.png'
import fileTypeService from '../../services/file_type/file_type.service.js'

const Attachment = {
  props: [
    'attachment',
    'nsfw',
    'statusId'
  ],
  data () {
    return {
      nsfwImage,
      hideNsfwLocal: this.$store.state.config.hideNsfw,
      showHidden: false,
      loading: false,
      img: document.createElement('img')
    }
  },
  computed: {
    type () {
      return fileTypeService.fileType(this.attachment.mimetype)
    },
    hidden () {
      return this.nsfw && this.hideNsfwLocal && !this.showHidden
    },
    isEmpty () {
      return this.type === 'html' && !this.attachment.oembed
    }
  },
  methods: {
    linkClicked ({target}) {
      if (target.tagName === 'A') {
        window.open(target.href, '_blank')
      }
    },
    toggleHidden () {
      if (this.img.onload) {
        this.img.onload()
      } else {
        this.loading = true
        this.img.src = this.attachment.url
        this.img.onload = () => {
          this.loading = false
          this.showHidden = !this.showHidden
        }
      }
    }
  }
}

export default Attachment