Browse Source

masonry horizontal

pull/563/head
David DeSandro 11 years ago
parent
commit
fb81a14bd1
  1. 224
      examples/masonry-horizontal.html
  2. 145
      layout-modes/masonry-horizontal.js
  3. 8
      notes.md

224
examples/masonry-horizontal.html

@ -0,0 +1,224 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>horizontal layout modes</title>
<link rel="stylesheet" href="examples.css" />
<style>
body, html {
height: 100%;
}
#container {
position: relative;
height: 70%;
margin-bottom: 20px;
}
.element.alkali { width: 170px; height: 190px; }
.element.alkaline-earth {}
.element.lanthanoid { height: 190px; }
.element.actinoid { width: 170px; }
.element.transition { width: 220px; }
</style>
</head>
<body>
<h1>horizontal layout modes</h1>
<div id="options">
<h2>Filter</h2>
<div class="option-set" data-isotope-key="filter">
<button data-isotope-value="*">show all</button>
<button data-isotope-value=".metal">metal</button>
<button data-isotope-value=".post-transition">post-transition</button>
</div>
<h2>Sort</h2>
<div class="option-set" data-isotope-key="sortBy">
<button data-isotope-value="original-order">original-order</button>
<button data-isotope-value="number">number</button>
<button data-isotope-value="name">name</button>
<button data-isotope-value="symbol">symbol</button>
<button data-isotope-value="weight">weight</button>
<button data-isotope-value="category">category</button>
</div>
</div>
<div id="container">
<div class="element transition metal " data-symbol="Hg" data-category="transition">
<p class="number">80</p>
<h3 class="symbol">Hg</h3>
<h2 class="name">Mercury</h2>
<p class="weight">200.59</p>
</div>
<div class="element metalloid " data-symbol="Te" data-category="metalloid">
<p class="number">52</p>
<h3 class="symbol">Te</h3>
<h2 class="name">Tellurium</h2>
<p class="weight">127.6</p>
</div>
<div class="element post-transition metal " data-symbol="Bi" data-category="post-transition">
<p class="number">83</p>
<h3 class="symbol">Bi</h3>
<h2 class="name">Bismuth</h2>
<p class="weight">208.9804</p>
</div>
<div class="element transition metal " data-symbol="Cd" data-category="transition">
<p class="number">48</p>
<h3 class="symbol">Cd</h3>
<h2 class="name">Cadmium</h2>
<p class="weight">112.411</p>
</div>
<div class="element alkaline-earth metal " data-symbol="Ca" data-category="alkaline-earth">
<p class="number">20</p>
<h3 class="symbol">Ca</h3>
<h2 class="name">Calcium</h2>
<p class="weight">40.078</p>
</div>
<div class="element transition metal " data-symbol="Re" data-category="transition">
<p class="number">75</p>
<h3 class="symbol">Re</h3>
<h2 class="name">Rhenium</h2>
<p class="weight">186.207</p>
</div>
<div class="element post-transition metal " data-symbol="Tl" data-category="post-transition">
<p class="number">81</p>
<h3 class="symbol">Tl</h3>
<h2 class="name">Thallium</h2>
<p class="weight">204.3833</p>
</div>
<div class="element metalloid " data-symbol="Sb" data-category="metalloid">
<p class="number">51</p>
<h3 class="symbol">Sb</h3>
<h2 class="name">Antimony</h2>
<p class="weight">121.76</p>
</div>
<div class="element transition metal " data-symbol="Co" data-category="transition">
<p class="number">27</p>
<h3 class="symbol">Co</h3>
<h2 class="name">Cobalt</h2>
<p class="weight">58.933195</p>
</div>
<div class="element lanthanoid metal inner-transition " data-symbol="Lu" data-category="lanthanoid">
<p class="number">71</p>
<h3 class="symbol">Lu</h3>
<h2 class="name">Lutetium</h2>
<p class="weight">174.9668</p>
</div>
<div class="element noble-gas nonmetal " data-symbol="Ar" data-category="noble-gas">
<p class="number">18</p>
<h3 class="symbol">Ar</h3>
<h2 class="name">Argon</h2>
<p class="weight">39.948</p>
</div>
<div class="element alkali metal " data-symbol="Rb" data-category="alkali">
<p class="number">37</p>
<h3 class="symbol">Rb</h3>
<h2 class="name">Rubidium</h2>
<p class="weight">85.4678</p>
</div>
<div class="element other nonmetal " data-symbol="N" data-category="other">
<p class="number">7</p>
<h3 class="symbol">N</h3>
<h2 class="name">Nitrogen</h2>
<p class="weight">14.0067</p>
</div>
<div class="element actinoid metal inner-transition " data-symbol="Np" data-category="actinoid">
<p class="number">93</p>
<h3 class="symbol">Np</h3>
<h2 class="name">Neptunium</h2>
<p class="weight">(237)</p>
</div>
<div class="element actinoid metal inner-transition " data-symbol="Ac" data-category="actinoid">
<p class="number">89</p>
<h3 class="symbol">Ac</h3>
<h2 class="name">Actinium</h2>
<p class="weight">(227)</p>
</div>
</div>
<script src="../bower_components/eventEmitter/EventEmitter.js"></script>
<script src="../bower_components/eventie/eventie.js"></script>
<script src="../bower_components/doc-ready/doc-ready.js"></script>
<script src="../bower_components/get-style-property/get-style-property.js"></script>
<script src="../bower_components/get-size/get-size.js"></script>
<script src="../bower_components/jquery-bridget/jquery.bridget.js"></script>
<script src="../bower_components/matches-selector/matches-selector.js"></script>
<script src="../bower_components/outlayer/item.js"></script>
<script src="../bower_components/outlayer/outlayer.js"></script>
<script src="../item.js"></script>
<script src="../layout-mode.js"></script>
<script src="../isotope.js"></script>
<script src="../layout-modes/fit-columns.js"></script>
<script src="../layout-modes/horizontal.js"></script>
<script src="../layout-modes/cells-by-column.js"></script>
<script src="../layout-modes/masonry-horizontal.js"></script>
<script>
docReady( function() {
var container = document.querySelector('#container');
var iso = window.iso = new Isotope( container, {
itemSelector: '.element',
// stamp: '.stamp',
layoutMode: 'masonryHorizontal',
transitionDuration: '0.8s',
masonryHorizontal: {
rowHeight: 100
},
cellsByColumn: {
columnWidth: 130,
rowHeight: 140,
},
getSortData: {
number: '.number parseInt',
symbol: '.symbol',
name: '.name',
category: '[data-category]',
weight: function( itemElem ) {
// remove parenthesis
return parseFloat( getText( itemElem.querySelector('.weight') ).replace( /[\(\)]/g, '') );
}
}
});
var options = document.querySelector('#options');
eventie.bind( options, 'click', function( event ) {
if ( !matchesSelector( event.target, 'button' ) ) {
return;
}
// var opt = {};
var key = event.target.parentNode.getAttribute('data-isotope-key');
var value = event.target.getAttribute('data-isotope-value');
console.log( key, value );
iso.options[ key ] = value;
iso.layout();
});
});
function getText( elem ) {
return elem.textContent || elem.innerText;
}
</script>
</body>
</html>

145
layout-modes/masonry-horizontal.js

@ -0,0 +1,145 @@
( function( window ) {
'use strict';
// -------------------------- helpers -------------------------- //
var indexOf = Array.prototype.indexOf ?
function( items, value ) {
return items.indexOf( value );
} :
function ( items, value ) {
for ( var i=0, len = items.length; i < len; i++ ) {
var item = items[i];
if ( item === value ) {
return i;
}
}
return -1;
};
// -------------------------- definition -------------------------- //
function masonryHorizontalDefinition( getSize, layoutMode ) {
// create an Outlayer layout class
var MasonryHorizontal = layoutMode.create('masonryHorizontal');
MasonryHorizontal.prototype._resetLayout = function() {
this.getRowHeight();
this._getMeasurement( 'gutter', 'outerHeight' );
this.rowHeight += this.gutter;
// measure rows
this.rows = Math.floor( ( this.isotope.size.innerHeight + this.gutter ) / this.rowHeight );
this.rows = Math.max( this.rows, 1 );
// reset row Xs
var i = this.rows;
this.rowXs = [];
while (i--) {
this.rowXs.push( 0 );
}
this.maxX = 0;
};
MasonryHorizontal.prototype._getItemLayoutPosition = function( item ) {
item.getSize();
// how many rows does this brick span
var rowSpan = Math.ceil( item.size.outerHeight / this.rowHeight );
rowSpan = Math.min( rowSpan, this.rows );
var rowGroup = this._getRowGroup( rowSpan );
// get the minimum Y value from the rows
var minimumX = Math.min.apply( Math, rowGroup );
var shortRowIndex = indexOf( rowGroup, minimumX );
// position the brick
var position = {
x: minimumX,
y: this.rowHeight * shortRowIndex
};
// apply setHeight to necessary rows
var setWidth = minimumX + item.size.outerWidth;
var setSpan = this.rows + 1 - rowGroup.length;
for ( var i = 0; i < setSpan; i++ ) {
this.rowXs[ shortRowIndex + i ] = setWidth;
}
return position;
};
/**
* @param {Number} rowSpan - number of rows the element spans
* @returns {Array} rowGroup
*/
MasonryHorizontal.prototype._getRowGroup = function( rowSpan ) {
if ( rowSpan < 2 ) {
// if brick spans only one row, use all the row Xs
return this.rowXs;
}
var rowGroup = [];
// how many different places could this brick fit horizontally
var groupCount = this.rows + 1 - rowSpan;
// for each group potential horizontal position
for ( var i = 0; i < groupCount; i++ ) {
// make an array of rowX values for that one group
var groupRowXs = this.rowXs.slice( i, i + rowSpan );
// and get the max value of the array
rowGroup[i] = Math.max.apply( Math, groupRowXs );
}
return rowGroup;
};
MasonryHorizontal.prototype._manageStamp = function( stamp ) {
var stampSize = getSize( stamp );
var offset = this._getElementOffset( stamp );
// get the rows that this stamp affects
var firstY = this.options.isOriginTop ? offset.top : offset.bottom;
var lastY = firstY + stampSize.outerHeight;
var firstRow = Math.floor( firstY / this.rowHeight );
firstRow = Math.max( 0, firstRow );
var lastRow = Math.floor( lastY / this.rowHeight );
lastRow = Math.min( this.rows - 1, lastRow );
// set rowXs to bottom of the stamp
var stampMaxX = ( this.options.isOriginLeft ? offset.left : offset.right ) +
stampSize.outerWidth;
for ( var i = firstRow; i <= lastRow; i++ ) {
this.rowXs[i] = Math.max( stampMaxX, this.rowXs[i] );
}
};
MasonryHorizontal.prototype._getContainerSize = function() {
this.maxX = Math.max.apply( Math, this.rowXs );
return {
width: this.maxX
};
};
MasonryHorizontal.prototype.resize = function() {
this.resizeVertical();
};
return MasonryHorizontal;
}
if ( typeof define === 'function' && define.amd ) {
// AMD
define( [
'get-size/get-size',
'../layout-mode'
],
masonryHorizontalDefinition );
} else {
// browser global
masonryHorizontalDefinition(
window.getSize,
window.Isotope.layoutMode
);
}
})( window );

8
notes.md

@ -14,3 +14,11 @@ filter
## bugs
transitionDuration = '2s'
hide most
reveal,
then while transitioning, sort
items jump into place :(
- could be problem with EventEmitter

Loading…
Cancel
Save