Isotope - masonry and centeredMasonry

re #280

by Reusablecode

HTML

<script src="http://isotope.metafizzy.co/jquery.isotope.min.js"></script>
<div id="filters">
  <input type="checkbox" name="red" value=".red" id="red"><label for="red">red</label>
  <input type="checkbox" name="blue" value=".blue" id="blue"><label for="blue">blue</label>
  <input type="checkbox" name="green" value=".green" id="green"><label for="green">green</label>
  <input type="checkbox" name="yellow" value=".yellow" id="yellow"><label for="yellow">yellow</label>
</div>

<p><button id="shuffle">Shuffle</button></p>

<div id="alpha" class="container">
  <div class="item red"></div>
  <div class="item blue h2"></div>
  <div class="item green"></div>
  <div class="item yellow"></div>
  <div class="item red w2"></div>
  <div class="item blue"></div>
  <div class="item green"></div>
  <div class="item yellow h2"></div>
  <div class="item red"></div>
  <div class="item blue h2"></div>
  <div class="item green w2"></div>
  <div class="item yellow"></div>
</div>

<div id="beta" class="container">
  <div class="item red"></div>
  <div class="item blue h2"></div>
  <div class="item green"></div>
  <div class="item yellow"></div>
  <div class="item red w2"></div>
  <div class="item blue"></div>
  <div class="item green"></div>
  <div class="item yellow h2"></div>
  <div class="item red"></div>
  <div class="item blue h2"></div>
  <div class="item green w2"></div>
  <div class="item yellow"></div>
</div>

CSS

body {
  font-family: 'Helvetica Neue', Arial, sans-serif;
}

.container {
  border: 1px solid;
  padding: 3px;
  margin-bottom: 10px;
}

#beta {
  margin: 0 auto;
}

.item {
  width: 70px;
  height: 70px;
  margin: 3px;
  float: left;
}

.item.w2 { width: 146px; }
.item.h2 { height: 146px; }

.red { background: red; }
.blue { background: blue; }
.green { background: green; }
.yellow { background: yellow; }

/* Start: Recommended Isotope styles */

/**** Isotope Filtering ****/

.isotope-item {
  z-index: 2;
}

.isotope-hidden.isotope-item {
  pointer-events: none;
  z-index: 1;
}

/**** Isotope CSS3 transitions ****/

.isotope,
.isotope .isotope-item {
  -webkit-transition-duration: 0.8s;
     -moz-transition-duration: 0.8s;
          transition-duration: 0.8s;
}

.isotope {
  -webkit-transition-property: height, width;
     -moz-transition-property: height, width;
          transition-property: height, width;
}

.isotope .isotope-item {
  -webkit-transition-property: -webkit-transform, opacity;
     -moz-transition-property:    -moz-transform, opacity;
          transition-property:         transform, opacity;
}

/**** disabling Isotope CSS3 transitions ****/

.isotope.no-transition,
.isotope.no-transition .isotope-item,
.isotope .isotope-item.no-transition {
  -webkit-transition-duration: 0s;
     -moz-transition-duration: 0s;
          transition-duration: 0s;
}

/* End: Recommended Isotope styles */

JavaScript

// centered masonry layout mode

$.Isotope.prototype._getCenteredMasonryColumns = function() {
  this.width = this.element.width();
    
  var parentWidth = this.element.parent().width();
    
                // i.e. options.masonry && options.masonry.columnWidth
  var colW = this.options.masonry && this.options.masonry.columnWidth ||
                // or use the size of the first item
                this.$filteredAtoms.outerWidth(true) ||
                // if there's no items, use size of container
                parentWidth;
    
  var cols = Math.floor( parentWidth / colW );
  cols = Math.max( cols, 1 );

  // i.e. this.masonry.cols = ....
  this.masonry.cols = cols;
  // i.e. this.masonry.columnWidth = ...
  this.masonry.columnWidth = colW;
};
  
$.Isotope.prototype._centeredMasonryLayout = $.Isotope.prototype._masonryLayout;
  
$.Isotope.prototype._centeredMasonryReset = function() {
  // layout-specific props
  this.masonry = {};
  // FIXME shouldn't have to call this again
  this._getCenteredMasonryColumns();
  var i = this.masonry.cols;
  this.masonry.colYs = [];
  while (i--) {
    this.masonry.colYs.push( 0 );
  }
};

$.Isotope.prototype._centeredMasonryResizeChanged = function() {
  var prevColCount = this.masonry.cols;
  // get updated colCount
  this._getCenteredMasonryColumns();
  return ( this.masonry.cols !== prevColCount );
};
  
$.Isotope.prototype._centeredMasonryGetContainerSize = function() {
  var unusedCols = 0,
      i = this.masonry.cols;
  // count unused columns
  while ( --i ) {
    if ( this.masonry.colYs[i] !== 0 ) {
      break;
    }
    unusedCols++;
  }

  return {
        height : Math.max.apply( Math, this.masonry.colYs ),
        // fit container to columns that have been used;
        width : (this.masonry.cols - unusedCols) * this.masonry.columnWidth
      };
};


$(function(){
  
  var $containers = $('.container'),
      $checkboxes = $('#filters input');
  
  // masonry
  $('#alpha').isotope({
    itemSelector:...