From 4ddb4803af0e757ff4da49c91b8bc2f5e7310b75 Mon Sep 17 00:00:00 2001 From: Matthew Grove Date: Sun, 23 Jun 2024 18:37:51 +0100 Subject: [PATCH] Enable masonry layout for photos --- assets/js/photos.js | 79 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 assets/js/photos.js diff --git a/assets/js/photos.js b/assets/js/photos.js new file mode 100644 index 0000000..6f9e8e5 --- /dev/null +++ b/assets/js/photos.js @@ -0,0 +1,79 @@ +/** + * Set appropriate spanning to any masonry item + * + * Get different properties we already set for the masonry, calculate + * height or spanning for any cell of the masonry grid based on its + * content-wrapper's height, the (row) gap of the grid, and the size + * of the implicit row tracks. + * + * @param item Object A brick/tile/cell inside the masonry + */ +function resizeMasonryItem(item){ + /* Get the grid object, its row-gap, and the size of its implicit rows */ + var grid = document.getElementsByClassName('photo-list')[0], + rowGap = parseInt(window.getComputedStyle(grid).getPropertyValue('grid-row-gap')), + rowHeight = parseInt(window.getComputedStyle(grid).getPropertyValue('grid-auto-rows')); + + /* + * Spanning for any brick = S + * Grid's row-gap = G + * Size of grid's implicitly create row-track = R + * Height of item content = H + * Net height of the item = H1 = H + G + * Net height of the implicit row-track = T = G + R + * S = H1 / T + */ + var rowSpan = Math.ceil((item.querySelector('a').getBoundingClientRect().height+rowGap)/(rowHeight+rowGap)); + + /* Set the spanning as calculated above (S) */ + item.style.gridRowEnd = 'span '+rowSpan; +} + +/** + * Apply spanning to all the masonry items + * + * Loop through all the items and apply the spanning to them using + * `resizeMasonryItem()` function. + * + * @uses resizeMasonryItem + */ +function resizeAllMasonryItems(){ + // Get all item class objects in one list + var allItems = document.getElementsByClassName('photo-item'); + + /* + * Loop through the above list and execute the spanning function to + * each list-item (i.e. each masonry item) + */ + for(var i=0;i