Reload masonry layout after filtering – with imagesLoaded

// Init and reload masonry layout after facet filtering
// Works for https://masonry.desandro.com
// This needs the imagesLoaded() script, to be downloaded separately: 
// https://masonry.desandro.com/layout.html#imagesloaded

// Init Masonry
var $grid = jQuery('.facetwp-template').masonry({
  itemSelector: '.facetwp-template',
  columnWidth: 220,
  gutter:20,
});

// Reload and update on facetwp-loaded
jQuery(document).on('facetwp-loaded', function() {
  $grid.masonry('reloadItems')

  // Update Masonry layout after each image has loaded
  $grid.imagesLoaded().progress( function() {
    $grid.masonry('layout');
  });
});