diff --git a/webapp/components/MasonryGrid/MasonryGrid.vue b/webapp/components/MasonryGrid/MasonryGrid.vue index b852173b1..d6ab6c140 100644 --- a/webapp/components/MasonryGrid/MasonryGrid.vue +++ b/webapp/components/MasonryGrid/MasonryGrid.vue @@ -1,10 +1,11 @@ @@ -12,24 +13,31 @@ export default { data() { return { - rowSpan: 10, - rowHeight: null, + itemsCalculating: 0, } }, - methods: { - calculateItemHeight(gridItem) { - const grid = this.$el - const rowHeight = this.rowHeight - const rowGap = parseInt(grid.style.gridRowGap) - - const itemHeight = gridItem.$el.clientHeight - const rowSpan = Math.ceil((itemHeight + rowGap) / (rowHeight + rowGap)) - gridItem.rowSpan = rowSpan + computed: { + rowHeight() { + if (this.$el) { + return parseInt(this.$el.style.gridAutoRows) + } + return 0 + }, + rowGap() { + if (this.$el) { + return parseInt(this.$el.style.gridRowGap) + } + return 0 }, }, - mounted() { - this.rowHeight = parseInt(this.$el.style.gridAutoRows) - this.$el.classList.remove('reset-grid-height') + methods: { + startCalculation() { + this.itemsCalculating += 1 + return { rowHeight: this.rowHeight, rowGap: this.rowGap } + }, + endCalculation() { + this.itemsCalculating -= 1 + }, }, } diff --git a/webapp/components/MasonryGrid/MasonryGridItem.vue b/webapp/components/MasonryGrid/MasonryGridItem.vue index 8a923fb22..07ffc094a 100644 --- a/webapp/components/MasonryGrid/MasonryGridItem.vue +++ b/webapp/components/MasonryGrid/MasonryGridItem.vue @@ -8,21 +8,30 @@ export default { data() { return { - span: 10, + rowSpan: 10, } }, - computed: { - rowSpan: { - get() { - return this.span - }, - set(rowSpan) { - this.span = rowSpan - }, + methods: { + calculateItemHeight() { + const grid = this.$parent.$emit('calculating-item-height') + this.$nextTick(() => { + const rowHeight = parseInt(grid.$el.style.gridAutoRows) + const rowGap = parseInt(grid.$el.style.gridRowGap) + + const itemHeight = this.$el.clientHeight + const rowSpan = Math.ceil((itemHeight + rowGap) / (rowHeight + rowGap)) + this.rowSpan = rowSpan + this.$parent.$emit('finished-calculating-item-height') + }) }, }, mounted() { - this.$parent.$emit('grid-item-mounted', this) + const image = this.$el.querySelector('img') + if (image) { + image.onload = () => this.calculateItemHeight() + } else { + setTimeout(() => this.calculateItemHeight(), 0) + } }, } diff --git a/webapp/pages/index.vue b/webapp/pages/index.vue index 9d4304199..11e2a453e 100644 --- a/webapp/pages/index.vue +++ b/webapp/pages/index.vue @@ -1,6 +1,6 @@