aboutsummaryrefslogtreecommitdiff
path: root/src/components/attachment/attachment.js
blob: cb31020dde21798bdeb70484ac71d855a925363e (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import StillImage from '../still-image/still-image.vue'
import VideoAttachment from '../video_attachment/video_attachment.vue'
import nsfwImage from '../../assets/nsfw.png'
import fileTypeService from '../../services/file_type/file_type.service.js'
import { mapGetters } from 'vuex'

const Attachment = {
  props: [
    'attachment',
    'nsfw',
    'size',
    'allowPlay',
    'setMedia',
    'naturalSizeLoad'
  ],
  data () {
    return {
      nsfwImage: this.$store.state.instance.nsfwCensorImage || nsfwImage,
      hideNsfwLocal: this.$store.getters.mergedConfig.hideNsfw,
      preloadImage: this.$store.getters.mergedConfig.preloadImage,
      loading: false,
      img: fileTypeService.fileType(this.attachment.mimetype) === 'image' && document.createElement('img'),
      modalOpen: false,
      showHidden: false
    }
  },
  components: {
    StillImage,
    VideoAttachment
  },
  computed: {
    usePlaceholder () {
      return this.size === 'hide' || this.type === 'unknown'
    },
    placeholderName () {
      if (this.attachment.description === '' || !this.attachment.description) {
        return this.type.toUpperCase()
      }
      return this.attachment.description
    },
    placeholderIconClass () {
      if (this.type === 'image') return 'icon-picture'
      if (this.type === 'video') return 'icon-video'
      if (this.type === 'audio') return 'icon-music'
      return 'icon-doc'
    },
    referrerpolicy () {
      return this.$store.state.instance.mediaProxyAvailable ? '' : 'no-referrer'
    },
    type () {
      return fileTypeService.fileType(this.attachment.mimetype)
    },
    hidden () {
      return this.nsfw && this.hideNsfwLocal && !this.showHidden
    },
    isEmpty () {
      return (this.type === 'html' && !this.attachment.oembed) || this.type === 'unknown'
    },
    isSmall () {
      return this.size === 'small'
    },
    fullwidth () {
      if (this.size === 'hide') return false
      return this.type === 'html' || this.type === 'audio' || this.type === 'unknown'
    },
    useModal () {
      const modalTypes = this.size === 'hide' ? ['image', 'video', 'audio']
        : this.mergedConfig.playVideosInModal
          ? ['image', 'video']
          : ['image']
      return modalTypes.includes(this.type)
    },
    ...mapGetters(['mergedConfig'])
  },
  methods: {
    linkClicked ({ target }) {
      if (target.tagName === 'A') {
        window.open(target.href, '_blank')
      }
    },
    openModal (event) {
      if (this.useModal) {
        event.stopPropagation()
        event.preventDefault()
        this.setMedia()
        this.$store.dispatch('setCurrent', this.attachment)
      }
    },
    toggleHidden (event) {
      if (
        (this.mergedConfig.useOneClickNsfw && !this.showHidden) &&
        (this.type !== 'video' || this.mergedConfig.playVideosInModal)
      ) {
        this.openModal(event)
        return
      }
      if (this.img && !this.preloadImage) {
        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
          }
        }
      } else {
        this.showHidden = !this.showHidden
      }
    },
    onImageLoad (image) {
      const width = image.naturalWidth
      const height = image.naturalHeight
      this.naturalSizeLoad && this.naturalSizeLoad({ width, height })
    }
  }
}

export default Attachment