Created
March 19, 2019 16:49
-
-
Save carmoreira/7d2f362b952909be5b52c33ec7f543e5 to your computer and use it in GitHub Desktop.
Convert Team Showcase search to live search if live filter exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function(){ | |
ts_live_search(); | |
}); | |
function ts_live_search() { | |
if( jQuery('#tshowcasesearch').length > 0 ){ | |
// quick search regex | |
var qsRegex; | |
jQuery('#tshowcasesearch input[type="submit"]').on('click',function(e){ | |
e.preventDefault(); | |
ts_filter_key( jQuery('#tshowcasesearch .ts_text_search').val() ); | |
}); | |
// use value of search field to filter | |
var quicksearch = jQuery('#tshowcasesearch .ts_text_search').keyup( ts_debounce( function() { | |
ts_filter_key( jQuery(this).val() ); | |
}, 200 ) ); | |
} | |
} | |
function ts_filter_key( search ) { | |
jQuery("div[id*='tshowcase_id_']").each(function(index) { | |
var container = jQuery(this); | |
qsRegex = new RegExp( search, 'gi' ); | |
container.find('.tshowcase-filter-active').each( function(){ | |
if( jQuery(this).text().match( qsRegex ) ){ | |
jQuery(this).show(1600, 'easeInOutExpo'); | |
} else { | |
jQuery(this).hide(1600, 'easeInOutExpo'); | |
} | |
}); | |
}); | |
} | |
// debounce so filtering doesn't happen every millisecond | |
function ts_debounce( fn, threshold ) { | |
var timeout; | |
threshold = threshold || 100; | |
return function debounced() { | |
clearTimeout( timeout ); | |
var args = arguments; | |
var _this = this; | |
function delayed() { | |
fn.apply( _this, args ); | |
} | |
timeout = setTimeout( delayed, threshold ); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment