Skip to content

Instantly share code, notes, and snippets.

@JohnLauber
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnLauber/f0c41ecada6b0998c454 to your computer and use it in GitHub Desktop.
Save JohnLauber/f0c41ecada6b0998c454 to your computer and use it in GitHub Desktop.
Enqueue and Load Isotope
/* Setup Isotope in functions.php*/
function add_isotope() {
wp_register_script('isotope', get_stylesheet_directory_uri() . '/assets/js/isotope.pkgd.min.js', array('jquery'), true);
wp_register_script('isotope-init', get_stylesheet_directory_uri() . '/assets/js/the-isotope.js', array('jquery', 'isotope'), true);
wp_enqueue_script('isotope-init');
}
add_action('wp_enqueue_scripts', 'add_isotope');
/* JS code that uses isotope (file name 'the-isotope.js' above) */
jQuery(function ($) {
$(window).load(function() {
// quick search regex
var qsRegex;
var buttonFilter;
// init Isotope
var $container = $('#isotope-list').isotope({
itemSelector: '.item',
layoutMode: 'fitRows',
filter: function() {
var $this = $(this);
var searchResult = qsRegex ? $this.text().match( qsRegex ) : true;
var buttonResult = buttonFilter ? $this.is( buttonFilter ) : true;
return searchResult && buttonResult;
}
});
// reveal all items after init
var iso = $container.data('isotope');
$container.isotope( 'reveal', iso.items );
// use value of search field to filter
var $quicksearch = $('#quicksearch').keyup( debounce( function() {
qsRegex = new RegExp( $quicksearch.val(), 'gi' );
$container.isotope();
}) );
$('#filters').on( 'click', 'button', function() {
buttonFilter = $( this ).attr('data-filter');
$container.isotope();
});
// change is-checked class on buttons
$('.button-group').each( function( i, buttonGroup ) {
var $buttonGroup = $( buttonGroup );
$buttonGroup.on( 'click', 'button', function() {
$buttonGroup.find('.active').removeClass('active');
$( this ).addClass('active');
});
});
$('#resetme').click ( function(){
qsRegex = new RegExp( '', 'gi');
buttonFilter = '*';
$container.isotope()
$('#quicksearch').val("");
});
});
});
// debounce so filtering doesn't happen every millisecond
function debounce( fn, threshold ) {
var timeout;
return function debounced() {
if ( timeout ) {
clearTimeout( timeout );
}
function delayed() {
fn();
timeout = null;
}
setTimeout( delayed, threshold || 100 );
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment