jquerymergeshuffleimagesloaded

Combining jQuery code - ImagesLoaded + Shuffle.js


My goal is to put the setupSorting function inside of the ImageLoad function. They work when separated (here), but sorting fails when combined (here).

This:

      $(document).ready (function(){
            /* reshuffle when user clicks a filter item */
            $('.filter-tags a').click(function (e) {
                e.preventDefault();

                // set active class
                $('.filter-tags a').removeClass('active');
                $(this).addClass('active');

                // get group name from clicked item
                var groupName = $(this).attr('data-group');

                // reshuffle grid
                $grid.shuffle('shuffle', groupName );
            });  
     

Needs to go inside of this:

var ImageLoad = (function( $, imagesLoaded ) {

  var $shuffle = $('.masonry-gallery'),
      $imgs = $shuffle.find('img'),
      $loader = $('#loader'),
      sizer = document.getElementById('#shuffle-sizer'),
      imgLoad,

  init = function() {

    // Create a new imagesLoaded instance
    imgLoad = new imagesLoaded( $imgs.get() );

    // Listen for when all images are done
    // will be executed even if some images fail
    imgLoad.on( 'always', onAllImagesFinished );
  },

  onAllImagesFinished = function( instance ) {

    if ( window.console && window.console.log ) {
      console.log( instance );
    }

    // Hide loader
    $loader.addClass('hidden');

    // Adds visibility: visible;
    $shuffle.addClass('images-loaded');

    // Initialize shuffle
    $shuffle.shuffle({
      sizer: sizer,
      itemSelector: '.gallery-photo'
    });
  };

  return {
    init: init
  };
}( jQuery, window.imagesLoaded ));

$(document).ready(function() {
  ImageLoad.init();
});  

With the ultimate goal of being like this page, only with sorting: http://vestride.github.io/Shuffle/images/


Solution

  • After a few hours of experimenting, I finally got it. You're right that I wasn't calling it. I also forgot to change $grid.shuffle to $shuffle.shuffle upon merging.

    That still didn't do the trick, so I changed the setupSorting function (and subsequently, some html) to more closely match the author's (here)

    The resulting code works!

    ///////////////////////  IMAGESLOADED & SHUFFLE  //////////////////////
    
    var ImageLoad = (function( $, imagesLoaded ) {
    
      var $shuffle = $('.masonry-gallery'),
          $filterOptions = $('.filter-list'),
          $imgs = $shuffle.find('.gallery-photo img'),
          $loader = $('#loader'),
          sizer = document.getElementById('#shuffle-sizer'),
          imgLoad,
    
      init = function() {
    
          // None of these need to be executed synchronously
        setTimeout(function() {
          setupSorting();
        }, 10);
    
        // Create a new imagesLoaded instance
        imgLoad = new imagesLoaded( $imgs.get() );
    
        // Listen for when all images are done
        // will be executed even if some images fail
        imgLoad.on( 'always', onAllImagesFinished );
      },
    
      onAllImagesFinished = function( instance ) {
    
        if ( window.console && window.console.log ) {
          console.log( instance );
        }
    
        // Hide loader
        $loader.addClass('hidden');
    
        // Adds visibility: visible;
        $shuffle.addClass('images-loaded');
    
        // Initialize shuffle
        $shuffle.shuffle({
          sizer: sizer,
          itemSelector: '.gallery-photo'
        });
      },
    
           // Set up button clicks
      setupSorting = function() {
        var $btns = $filterOptions.children();
        $btns.on('click', function() {
          var $this = $(this),
              isActive = $this.hasClass( 'active' ),
              group = isActive ? 'all' : $this.data('group');
    
          // Hide current label, show current label in title
          if ( !isActive ) {
            $('.filter-list .active').removeClass('active');
          }
    
          $this.toggleClass('active');
    
          // Filter elements
          $shuffle.shuffle( 'shuffle', group );
        });
    
        $btns = null;
      };
    
      return {
        init: init
      };
    }( jQuery, window.imagesLoaded ));
    
    $(document).ready(function() {
        ImageLoad.init();
    }); 
    

    You can see the full fiddle (here).