mirror of https://github.com/metafizzy/isotope
Filter & sort magical layouts
http://isotope.metafizzy.co
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
392 lines
11 KiB
392 lines
11 KiB
11 years ago
|
/*!
|
||
|
* Isotope v2.0.0
|
||
|
* Magical sorting and filtering layouts
|
||
|
* http://isotope.metafizzy.co
|
||
|
*/
|
||
|
|
||
|
( function( window ) {
|
||
|
|
||
|
'use strict';
|
||
|
|
||
|
// -------------------------- helpers -------------------------- //
|
||
|
|
||
11 years ago
|
// extend objects
|
||
|
function extend( a, b ) {
|
||
|
for ( var prop in b ) {
|
||
|
a[ prop ] = b[ prop ];
|
||
|
}
|
||
|
return a;
|
||
|
}
|
||
|
|
||
11 years ago
|
var trim = String.prototype.trim ?
|
||
|
function( str ) {
|
||
|
return str.trim();
|
||
|
} :
|
||
|
function( str ) {
|
||
|
return str.replace( /^\s+|\s+$/g, '' );
|
||
|
};
|
||
|
|
||
|
var docElem = document.documentElement;
|
||
|
|
||
|
var getText = docElem.textContent ?
|
||
|
function( elem ) {
|
||
|
return elem.textContent;
|
||
|
} :
|
||
|
function( elem ) {
|
||
|
return elem.innerText;
|
||
|
};
|
||
|
|
||
11 years ago
|
// -------------------------- isotopeDefinition -------------------------- //
|
||
11 years ago
|
|
||
|
// used for AMD definition and requires
|
||
11 years ago
|
function isotopeDefinition( Outlayer, getSize, matchesSelector, Item, LayoutMode ) {
|
||
11 years ago
|
// create an Outlayer layout class
|
||
11 years ago
|
var Isotope = Outlayer.create( 'isotope', {
|
||
|
sortAscending: true
|
||
|
});
|
||
11 years ago
|
|
||
11 years ago
|
Isotope.Item = Isotope.prototype.settings.item = Item;
|
||
11 years ago
|
Isotope.LayoutMode = LayoutMode;
|
||
11 years ago
|
|
||
11 years ago
|
Isotope.prototype._create = function() {
|
||
11 years ago
|
this.itemGUID = 0;
|
||
11 years ago
|
// call super
|
||
|
Outlayer.prototype._create.call( this );
|
||
|
|
||
|
// create layout modes
|
||
|
this.modes = {};
|
||
|
// create from registered layout modes
|
||
11 years ago
|
for ( var name in LayoutMode.modes ) {
|
||
11 years ago
|
this._initLayoutMode( name );
|
||
11 years ago
|
}
|
||
11 years ago
|
// functions that sort items
|
||
|
this._sorters = {};
|
||
11 years ago
|
// keep of track of sortBys
|
||
|
this.sortHistory = [ 'original-order' ];
|
||
|
this.updateSortData();
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
Isotope.prototype.reloadItems = function() {
|
||
|
// reset item ID counter
|
||
|
this.itemGUID = 0;
|
||
|
// call super
|
||
|
Outlayer.prototype.reloadItems.call( this );
|
||
|
};
|
||
|
|
||
11 years ago
|
Isotope.prototype._itemize = function() {
|
||
|
var items = Outlayer.prototype._itemize.apply( this, arguments );
|
||
11 years ago
|
// assign ID for original-order
|
||
|
for ( var i=0, len = items.length; i < len; i++ ) {
|
||
|
var item = items[i];
|
||
|
item.id = this.itemGUID++;
|
||
|
}
|
||
|
return items;
|
||
|
};
|
||
|
|
||
|
// -------------------------- layout -------------------------- //
|
||
|
|
||
11 years ago
|
Isotope.prototype._initLayoutMode = function( name ) {
|
||
11 years ago
|
var Mode = LayoutMode.modes[ name ];
|
||
11 years ago
|
// set mode options
|
||
|
// HACK extend initial options, back-fill in default options
|
||
11 years ago
|
var initialOpts = this.options[ name ] || {};
|
||
11 years ago
|
this.options[ name ] = Mode.options ?
|
||
|
extend( Mode.options, initialOpts ) : initialOpts;
|
||
11 years ago
|
// init layout mode instance
|
||
11 years ago
|
this.modes[ name ] = new Mode( this );
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
Isotope.prototype.layout = function( opts ) {
|
||
11 years ago
|
// set any options pass
|
||
11 years ago
|
this.option( opts );
|
||
11 years ago
|
// don't animate first layout
|
||
|
var isInstant = this._isInitInstant = this.options.isLayoutInstant !== undefined ?
|
||
|
this.options.isLayoutInstant : !this._isLayoutInited;
|
||
11 years ago
|
//
|
||
11 years ago
|
this.filteredItems = this._filter( this.items );
|
||
11 years ago
|
this._sort();
|
||
11 years ago
|
// layout flow
|
||
11 years ago
|
this._resetLayout();
|
||
|
this._manageStamps();
|
||
|
this.layoutItems( this.filteredItems, isInstant );
|
||
|
|
||
|
// flag for initalized
|
||
|
this._isLayoutInited = true;
|
||
11 years ago
|
};
|
||
|
|
||
|
|
||
|
// -------------------------- filter -------------------------- //
|
||
|
|
||
|
Isotope.prototype._filter = function( items ) {
|
||
|
var filter = this.options.filter;
|
||
11 years ago
|
filter = filter || '*';
|
||
11 years ago
|
var matches = [];
|
||
|
var hiddenMatched = [];
|
||
|
var visibleUnmatched = [];
|
||
|
|
||
11 years ago
|
var test = getFilterTest( filter );
|
||
11 years ago
|
|
||
|
// test each item
|
||
|
for ( var i=0, len = items.length; i < len; i++ ) {
|
||
|
var item = items[i];
|
||
|
if ( item.isIgnored ) {
|
||
|
continue;
|
||
|
}
|
||
|
// add item to either matched or unmatched group
|
||
|
var isMatched = test( item );
|
||
11 years ago
|
// item.isFilterMatched = isMatched;
|
||
11 years ago
|
// add to matches if its a match
|
||
|
if ( isMatched ) {
|
||
|
matches.push( item );
|
||
|
}
|
||
11 years ago
|
// add to additional group if item needs to be hidden or revealed
|
||
|
if ( isMatched && item.isHidden ) {
|
||
|
hiddenMatched.push( item );
|
||
|
} else if ( !isMatched && !item.isHidden ) {
|
||
|
visibleUnmatched.push( item );
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
// HACK
|
||
|
// disable transition on init
|
||
|
var _transitionDuration = this.options.transitionDuration;
|
||
|
if ( this._isInitInstant ) {
|
||
|
this.options.transitionDuration = 0;
|
||
|
}
|
||
11 years ago
|
this.reveal( hiddenMatched );
|
||
|
this.hide( visibleUnmatched );
|
||
11 years ago
|
// set back
|
||
|
if ( this._isInitInstant ) {
|
||
|
this.options.transitionDuration = _transitionDuration;
|
||
|
}
|
||
11 years ago
|
|
||
|
return matches;
|
||
|
};
|
||
|
|
||
11 years ago
|
// get a function or a matchesSelector test given the filter
|
||
|
function getFilterTest( filter ) {
|
||
|
var test;
|
||
|
if ( typeof filter === 'function' ) {
|
||
|
test = function( item ) {
|
||
|
return filter( item.element );
|
||
|
};
|
||
|
} else {
|
||
|
test = function( item ) {
|
||
|
return matchesSelector( item.element, filter );
|
||
|
};
|
||
|
}
|
||
|
return test;
|
||
|
}
|
||
|
|
||
11 years ago
|
// -------------------------- sorting -------------------------- //
|
||
11 years ago
|
|
||
11 years ago
|
Isotope.prototype.updateSortData = function( items ) {
|
||
11 years ago
|
// update sorters
|
||
|
var getSortData = this.options.getSortData;
|
||
|
for ( var key in getSortData ) {
|
||
|
var sorter = getSortData[ key ];
|
||
|
this._sorters[ key ] = mungeSorter( sorter );
|
||
|
}
|
||
|
// update item sort data
|
||
11 years ago
|
// default to all items if none are passed in
|
||
|
items = items || this.items;
|
||
|
for ( var i=0, len = items.length; i < len; i++ ) {
|
||
|
var item = items[i];
|
||
11 years ago
|
if ( item.isIgnored ) {
|
||
|
continue;
|
||
|
}
|
||
11 years ago
|
item.updateSortData();
|
||
|
}
|
||
|
};
|
||
11 years ago
|
|
||
11 years ago
|
// ----- munge sorter ----- //
|
||
|
|
||
|
// encapsulate this, as we just need mungeSorter
|
||
|
// other functions in here are just for munging
|
||
|
var mungeSorter = ( function() {
|
||
|
// add a magic layer to sorters for convienent shorthands
|
||
|
// `.foo-bar` will use the text of .foo-bar querySelector
|
||
|
// `[foo-bar]` will use attribute
|
||
|
// you can also add parser
|
||
|
// `.foo-bar parseInt` will parse that as a number
|
||
|
function mungeSorter( sorter ) {
|
||
|
// if not a string, return function or whatever it is
|
||
|
if ( typeof sorter !== 'string' ) {
|
||
|
return sorter;
|
||
|
}
|
||
|
// parse the sorter string
|
||
|
var args = trim( sorter ).split(' ');
|
||
|
var query = args[0];
|
||
|
// check if query looks like [an-attribute]
|
||
|
var attrMatch = query.match( /^\[(.+)\]$/ );
|
||
|
var attr = attrMatch && attrMatch[1];
|
||
|
var getValue = getValueGetter( attr, query );
|
||
|
// use second argument as a parser
|
||
|
var parser = getParser( args[1] );
|
||
|
// parse the value, if there was a parser
|
||
|
sorter = parser ? function( elem ) {
|
||
|
return elem && parser( getValue( elem ) );
|
||
|
} :
|
||
|
// otherwise just return value
|
||
|
function( elem ) {
|
||
|
return elem && getValue( elem );
|
||
|
};
|
||
|
|
||
|
return sorter;
|
||
|
}
|
||
|
|
||
|
// get an attribute getter, or get text of the querySelector
|
||
|
function getValueGetter( attr, query ) {
|
||
|
var getValue;
|
||
|
// if query looks like [foo-bar], get attribute
|
||
|
if ( attr ) {
|
||
|
getValue = function( elem ) {
|
||
|
return elem.getAttribute( attr );
|
||
|
};
|
||
|
} else {
|
||
|
// otherwise, assume its a querySelector, and get its text
|
||
|
getValue = function( elem ) {
|
||
|
var child = elem.querySelector( query );
|
||
|
return child && getText( child );
|
||
|
};
|
||
|
}
|
||
|
return getValue;
|
||
|
}
|
||
|
|
||
|
// return a parser function if arg matches
|
||
|
function getParser( arg ) {
|
||
|
var parser;
|
||
|
switch ( arg ) {
|
||
|
case 'parseInt' :
|
||
|
parser = function( val ) {
|
||
|
return parseInt( val, 10 );
|
||
|
};
|
||
|
break;
|
||
|
case 'parseFloat' :
|
||
|
parser = function( val ) {
|
||
|
return parseFloat( val );
|
||
|
};
|
||
|
break;
|
||
|
default :
|
||
|
// just return val if parser isn't one of these
|
||
|
// TODO - console log that that parser doesn't exist
|
||
|
parser = function( val ) {
|
||
|
return val;
|
||
|
};
|
||
|
}
|
||
|
return parser;
|
||
|
}
|
||
|
|
||
|
return mungeSorter;
|
||
|
})();
|
||
|
|
||
|
|
||
|
// ----- sort method ----- //
|
||
|
|
||
11 years ago
|
// sort filteredItem order
|
||
|
Isotope.prototype._sort = function() {
|
||
|
var sortByOpt = this.options.sortBy;
|
||
|
if ( !sortByOpt ) {
|
||
|
return;
|
||
|
}
|
||
|
// concat all sortBy and sortHistory
|
||
|
var sortBys = [].concat.apply( sortByOpt, this.sortHistory );
|
||
|
// sort magic
|
||
11 years ago
|
var itemSorter = getItemSorter( sortBys, this.options.sortAscending );
|
||
|
this.filteredItems.sort( itemSorter );
|
||
11 years ago
|
// keep track of sortBy History
|
||
11 years ago
|
if ( sortByOpt !== this.sortHistory[0] ) {
|
||
11 years ago
|
// add to front, oldest goes in last
|
||
|
this.sortHistory.unshift( sortByOpt );
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// returns a function used for sorting
|
||
11 years ago
|
function getItemSorter( sortBys, sortAsc ) {
|
||
11 years ago
|
return function sorter( itemA, itemB ) {
|
||
11 years ago
|
// cycle through all sortKeys
|
||
|
for ( var i = 0, len = sortBys.length; i < len; i++ ) {
|
||
|
var sortBy = sortBys[i];
|
||
|
var a = itemA.sortData[ sortBy ];
|
||
|
var b = itemB.sortData[ sortBy ];
|
||
|
if ( a > b || a < b ) {
|
||
|
// if sortAsc is an object, use the value given the sortBy key
|
||
|
var isAscending = sortAsc[ sortBy ] !== undefined ? sortAsc[ sortBy ] : sortAsc;
|
||
|
var direction = isAscending ? 1 : -1;
|
||
|
return ( a > b ? 1 : -1 ) * direction;
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
11 years ago
|
};
|
||
|
}
|
||
11 years ago
|
|
||
|
// -------------------------- methods -------------------------- //
|
||
|
|
||
11 years ago
|
// get layout mode
|
||
|
Isotope.prototype._mode = function() {
|
||
|
var layoutMode = this.options.layoutMode;
|
||
|
var mode = this.modes[ layoutMode ];
|
||
|
if ( !mode ) {
|
||
11 years ago
|
// TODO console.error
|
||
11 years ago
|
throw new Error( 'No layout mode: ' + layoutMode );
|
||
|
}
|
||
|
// HACK sync mode's options
|
||
|
// any options set after init for layout mode need to be synced
|
||
|
mode.options = this.options[ layoutMode ];
|
||
|
return mode;
|
||
|
};
|
||
|
|
||
11 years ago
|
Isotope.prototype._resetLayout = function() {
|
||
11 years ago
|
// trigger original reset layout
|
||
|
Outlayer.prototype._resetLayout.call( this );
|
||
11 years ago
|
this._mode()._resetLayout();
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
Isotope.prototype._getItemLayoutPosition = function( item ) {
|
||
|
return this._mode()._getItemLayoutPosition( item );
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
Isotope.prototype._manageStamp = function( stamp ) {
|
||
11 years ago
|
var mode = this._mode();
|
||
|
// HACK copy over some options
|
||
|
mode.options.isOriginLeft = this.options.isOriginLeft;
|
||
|
mode.options.isOriginTop = this.options.isOriginTop;
|
||
|
mode._manageStamp( stamp );
|
||
11 years ago
|
};
|
||
|
|
||
|
Isotope.prototype._getContainerSize = function() {
|
||
11 years ago
|
return this._mode()._getContainerSize();
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
Isotope.prototype.resize = function() {
|
||
|
this._mode().resize();
|
||
11 years ago
|
};
|
||
|
|
||
|
return Isotope;
|
||
|
}
|
||
|
|
||
|
// -------------------------- transport -------------------------- //
|
||
|
|
||
|
if ( typeof define === 'function' && define.amd ) {
|
||
|
// AMD
|
||
|
define( [
|
||
11 years ago
|
'outlayer/outlayer',
|
||
|
'get-size/get-size',
|
||
|
'matches-selector/matches-selector',
|
||
|
'./item.js',
|
||
|
'./layout-modes.js'
|
||
11 years ago
|
],
|
||
|
isotopeDefinition );
|
||
|
} else {
|
||
|
// browser global
|
||
|
window.Isotope = isotopeDefinition(
|
||
|
window.Outlayer,
|
||
|
window.getSize,
|
||
11 years ago
|
window.matchesSelector,
|
||
11 years ago
|
window.Isotope.Item,
|
||
11 years ago
|
window.Isotope.LayoutMode
|
||
11 years ago
|
);
|
||
|
}
|
||
|
|
||
|
})( window );
|