Skip to content

Instantly share code, notes, and snippets.

@sneakers-the-rat
Created March 3, 2022 22:19
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 sneakers-the-rat/4a127ecc6af67a80a0a6779d5e8de4c0 to your computer and use it in GitHub Desktop.
Save sneakers-the-rat/4a127ecc6af67a80a0a6779d5e8de4c0 to your computer and use it in GitHub Desktop.
Override Academic Theme Filter
function multiFilter(e){
// get isotope instance and filter buttons
let iso = Isotope.data('.projects-container');
let filterButtons = document.querySelectorAll('.project-filters a');
// iterate through buttons...
filterButtons.forEach((button) => {
// remove existing event listeners
let buttonClone = button.cloneNode(true);
button.parentNode.replaceChild(buttonClone, button);
// add new event listener to do the filtering
buttonClone.addEventListener('click', (e) => {
// prevent the page from jumping back up to the top on click
e.preventDefault();
// toggle active state of this button
e.target.classList.toggle('active');
// get all active buttons
let activeButtons = document.querySelectorAll('.project-filters a.active');
// if a button besides 'all' is active, make sure 'all' isnt
if (activeButtons.length > 1){
activeButtons.forEach((b) => {
if (b.getAttribute('data-filter') === '*'){
b.classList.remove('active')
}
})
// refresh activeButton list
activeButtons = document.querySelectorAll('.project-filters a.active');
}
// get data filters of active buttons
let filters = Array.from(activeButtons).map(a => a.getAttribute('data-filter'));
// join filters to single string
let filterstring = filters.join('');
console.log('filtering with filter string', filterstring)
// filter isotope elements
iso.arrange({filter:filterstring})
})
})
}
window.addEventListener('load', multiFilter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment