2019-01-26 07:45:03 -08:00
|
|
|
import Attachment from '../attachment/attachment.vue'
|
2019-10-21 12:13:11 -07:00
|
|
|
import { chunk, last, dropRight, sumBy } from 'lodash'
|
2019-01-26 07:45:03 -08:00
|
|
|
|
|
|
|
const Gallery = {
|
|
|
|
props: [
|
|
|
|
'attachments',
|
|
|
|
'nsfw',
|
|
|
|
'setMedia'
|
|
|
|
],
|
2019-10-18 13:04:17 -07:00
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
sizes: {}
|
|
|
|
}
|
|
|
|
},
|
2019-01-26 07:45:03 -08:00
|
|
|
components: { Attachment },
|
|
|
|
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
|
|
|
|
},
|
|
|
|
useContainFit () {
|
|
|
|
return this.$store.state.config.useContainFit
|
|
|
|
}
|
2019-10-18 13:04:17 -07:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onNaturalSizeLoad (id, size) {
|
|
|
|
this.$set(this.sizes, id, size)
|
|
|
|
},
|
|
|
|
rowStyle (itemsPerRow) {
|
|
|
|
return { 'padding-bottom': `${(100 / (itemsPerRow + 0.6))}%` }
|
|
|
|
},
|
2019-10-21 12:13:11 -07:00
|
|
|
itemStyle (id, row) {
|
|
|
|
const total = sumBy(row, item => this.getAspectRatio(item.id))
|
2019-10-22 04:10:52 -07:00
|
|
|
return { flex: this.getAspectRatio(id) / total }
|
2019-10-21 12:13:11 -07:00
|
|
|
},
|
|
|
|
getAspectRatio (id) {
|
2019-10-18 13:04:17 -07:00
|
|
|
const size = this.sizes[id]
|
2019-10-21 12:13:11 -07:00
|
|
|
return size ? size.width / size.height : 1
|
2019-10-18 13:04:17 -07:00
|
|
|
}
|
2019-01-26 07:45:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Gallery
|