| 1 |
export default ( elemId, args ) => { |
| 2 |
return { |
| 3 |
forcingHeight: false, |
| 4 |
forceHeight() { |
| 5 |
if ( ! this.forcingHeight ) { |
| 6 |
this.forcingHeight = true; |
| 7 |
|
| 8 |
const rows = this.getVisibleItemsPerRow(); |
| 9 |
let changesMade = false; |
| 10 |
|
| 11 |
for ( let row of rows ) { |
| 12 |
let heights = []; |
| 13 |
|
| 14 |
for ( let item of row ) { |
| 15 |
const currentHeight = item.style.height; |
| 16 |
|
| 17 |
// Get item heights, without height set. |
| 18 |
item.style.height = ''; |
| 19 |
heights.push( item.offsetHeight ); |
| 20 |
|
| 21 |
// Reset to current height. |
| 22 |
item.style.height = currentHeight; |
| 23 |
} |
| 24 |
|
| 25 |
const maxHeight = Math.max( ...heights ); |
| 26 |
|
| 27 |
// If an item is not the same size, resize. |
| 28 |
for ( let item of row ) { |
| 29 |
const itemHeight = item.offsetHeight; |
| 30 |
|
| 31 |
if ( itemHeight !== maxHeight ) { |
| 32 |
changesMade = true; |
| 33 |
|
| 34 |
item.style.height = `${ maxHeight }px`; |
| 35 |
item.animate( |
| 36 |
[{ height: `${ itemHeight }px` }, { height: `${ maxHeight }px` } ], |
| 37 |
300 |
| 38 |
); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// Wait until animations are done. |
| 44 |
setTimeout(() => { |
| 45 |
// Relayout if changes were made. |
| 46 |
if ( changesMade ) { |
| 47 |
this.layout(); |
| 48 |
} |
| 49 |
|
| 50 |
this.forcingHeight = false; |
| 51 |
}, 400); |
| 52 |
} |
| 53 |
}, |
| 54 |
getVisibleItemsPerRow() { |
| 55 |
const visibleItems = this.isotope.getFilteredItemElements(); |
| 56 |
|
| 57 |
let currentRowTop = 0; |
| 58 |
let currentRow = []; |
| 59 |
let rows = []; |
| 60 |
|
| 61 |
for ( let item of visibleItems ) { |
| 62 |
if ( currentRowTop < item.offsetTop ) { |
| 63 |
// Item is on a new row. |
| 64 |
if ( 0 < currentRow.length ) { |
| 65 |
rows.push( currentRow ); |
| 66 |
} |
| 67 |
|
| 68 |
currentRow = []; |
| 69 |
currentRowTop = item.offsetTop; |
| 70 |
} |
| 71 |
|
| 72 |
// Add item to current row. |
| 73 |
currentRow.push( item ); |
| 74 |
} |
| 75 |
|
| 76 |
// Make sure to add last row. |
| 77 |
if ( 0 < currentRow.length ) { |
| 78 |
rows.push( currentRow ); |
| 79 |
} |
| 80 |
|
| 81 |
return rows; |
| 82 |
}, |
| 83 |
initLayout() { |
| 84 |
if ( args.hasOwnProperty( 'force_height' ) && args.force_height ) { |
| 85 |
this.isotope.on( 'layoutComplete', () => { |
| 86 |
this.forceHeight(); |
| 87 |
} ); |
| 88 |
} |
| 89 |
}, |
| 90 |
} |
| 91 |
}; |