Skip to content

Commit

Permalink
Merge 9639bc9 into 47e8d62
Browse files Browse the repository at this point in the history
  • Loading branch information
PatelUtkarsh committed Aug 10, 2022
2 parents 47e8d62 + 9639bc9 commit 96cc559
Showing 1 changed file with 75 additions and 69 deletions.
144 changes: 75 additions & 69 deletions plugin/assets/src/front-end/masonry.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,92 +13,98 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let gridElement = null;
let rowHeight = 24;
let rowGap = 24;

export const masonryInit = () => {
gridElement = document.querySelector( '.is-flex-container' );
const gridElements = document.querySelectorAll( '.is-flex-container' );
gridElements.forEach( gridElement => {
init( gridElement );
} );
};

const init = gridElement => {
let rowHeight = 24;
let rowGap = 24;
if ( ! gridElement ) {
return;
}

const mediaQuery = window.matchMedia( '(min-width: 840px)' );
const handleResize = mediaQuery => {
if ( mediaQuery.matches ) {
resizeAllGridItems();
}
};

mediaQuery.addEventListener( 'change', handleResize );
handleResize( mediaQuery );
};
const resizeAllGridItems = () => {
const cells = gridElement.querySelectorAll(
'.is-style-material-masonry .wp-block-post'
);

const handleResize = mediaQuery => {
if ( mediaQuery.matches ) {
resizeAllGridItems();
}
};
if ( cells.length <= 0 ) {
return;
}

const resizeAllGridItems = () => {
const cells = gridElement.querySelectorAll(
'.is-style-material-masonry .wp-block-post'
);
rowHeight = parseInt(
window
.getComputedStyle( gridElement )
.getPropertyValue( 'grid-auto-rows' ),
10
);

if ( cells.length <= 0 ) {
return;
}
rowGap = parseInt(
window
.getComputedStyle( gridElement )
.getPropertyValue( 'grid-row-gap' ),
10
);

rowHeight = parseInt(
window
.getComputedStyle( gridElement )
.getPropertyValue( 'grid-auto-rows' ),
10
);

rowGap = parseInt(
window
.getComputedStyle( gridElement )
.getPropertyValue( 'grid-row-gap' ),
10
);

const hasPostCard = cells[ 0 ].querySelectorAll( '.post-card' ).length > 0;

if ( ! hasPostCard ) {
gridElement.style.gridAutoRows = 'minmax(min-content,max-content)';

// Let it re-render to compute height.
setTimeout( () => {
cells.forEach( resizeGridItem );
gridElement.style.removeProperty( 'grid-auto-rows' );
}, 0 );
return;
}
const hasPostCard =
cells[ 0 ].querySelectorAll( '.post-card' ).length > 0;

cells.forEach( resizeGridItem );
};
if ( ! hasPostCard ) {
gridElement.style.gridAutoRows = 'minmax(min-content,max-content)';

const resizeGridItem = cell => {
if ( ! cell ) {
return;
}
// Let it re-render to compute height.
setTimeout( () => {
cells.forEach( resizeGridItem );
gridElement.style.removeProperty( 'grid-auto-rows' );
}, 0 );
return;
}

let cellCard = cell.querySelector( '.post-card' );
cells.forEach( resizeGridItem );
};

if ( ! cellCard ) {
const imageCardBlock = cell.querySelector(
'.wp-block-material-image-card-query'
);
if ( imageCardBlock ) {
// For material image card.
cellCard = imageCardBlock;
} else {
// If we have a cell without card wrapper inside let's use that, Used for default WP template.
cellCard = cell;
const resizeGridItem = cell => {
if ( ! cell ) {
return;
}
}

const contentHeight = cellCard.getBoundingClientRect().height;
let cellCard = cell.querySelector( '.post-card' );

if ( ! cellCard ) {
const imageCardBlock = cell.querySelector(
'.wp-block-material-image-card-query'
);
if ( imageCardBlock ) {
// For material image card.
cellCard = imageCardBlock;
} else {
// If we have a cell without card wrapper inside let's use that, Used for default WP template.
cellCard = cell;
}
}

const rowSpan = Math.ceil(
( contentHeight + rowGap ) / ( rowHeight + rowGap )
);
const contentHeight = cellCard.getBoundingClientRect().height;

cell.style.gridRowEnd = 'span ' + rowSpan;
const rowSpan = Math.ceil(
( contentHeight + rowGap ) / ( rowHeight + rowGap )
);

cell.style.gridRowEnd = 'span ' + rowSpan;
};

const mediaQuery = window.matchMedia( '(min-width: 840px)' );

mediaQuery.addEventListener( 'change', handleResize );
handleResize( mediaQuery );
};

0 comments on commit 96cc559

Please sign in to comment.