aboutsummaryrefslogtreecommitdiff
path: root/src/components/gallery/gallery.js
blob: 96ac1b93ca2a583dc42ab7cd81b3e2da7a2dd631 (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
import Attachment from '../attachment/attachment.vue'
import { chunk, last, dropRight } from 'lodash'

const Gallery = {
  data: () => ({
    width: 500
  }),
  props: [
    'attachments',
    'nsfw',
    'setMedia'
  ],
  components: { Attachment },
  mounted () {
    this.resize()
    window.addEventListener('resize', this.resize)
  },
  destroyed () {
    window.removeEventListener('resize', this.resize)
  },
  computed: {
    rows () {
      if (!this.attachments) {
        return []
      }
      const rows = chunk(this.attachments, 3)
      if (last(rows).length === 1 && rows.length > 1) {
        // if 1 attachment on last row -> add it to the previous row instead
        const lastAttachment = last(rows)[0]
        const allButLastRow = dropRight(rows)
        last(allButLastRow).push(lastAttachment)
        return allButLastRow
      }
      return rows
    },
    rowHeight () {
      return itemsPerRow => ({ 'height': `${(this.width / (itemsPerRow + 0.6))}px` })
    },
    useContainFit () {
      return this.$store.getters.mergedConfig.useContainFit
    }
  },
  methods: {
    resize () {
      // Quick optimization to make resizing not always trigger state change,
      // only update attachment size in 10px steps
      const width = Math.floor(this.$el.getBoundingClientRect().width / 10) * 10
      if (this.width !== width) {
        this.width = width
      }
    }
  }
}

export default Gallery