Skip to content

Instantly share code, notes, and snippets.

@turtlepod
Created August 9, 2017 05:59
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 turtlepod/035c16ebd8ef57655a30925e19c638fc to your computer and use it in GitHub Desktop.
Save turtlepod/035c16ebd8ef57655a30925e19c638fc to your computer and use it in GitHub Desktop.
Listing Labels JS without History JS
/**
* Filter by labels.
*
* @since 2.0.0
*/
(function( window, undefined ) {
window.wp = window.wp || {};
var document = window.document;
var $ = window.jQuery;
/**
* @since 2.0.0
*/
var $document = $(document);
/**
* Wait for DOM ready.
*
* @since 2.0.0
*/
$document.ready(function() {
$( '.job_listings' )
// Monitor clicking of a label.
.on( 'click', '.astoundify-listing-labels a', function(e) {
e.preventDefault;
var $link = $(this);
var label = $(this).text();
var $selectedLabels = $( '.astoundify-listing-labels' ).find('input[value="' + label + '"]');
console.log($selectedLabels);
if ( $selectedLabels.length > 0 ) {
$selectedLabels.remove();
$link.removeClass( 'active' );
} else {
$( '.astoundify-listing-labels' ).append( '<input type="hidden" name="listing_label[]" value="' + label + '" />' );
$link.addClass( 'active' );
}
var target = $(this).closest( 'div.job_listings' );
target.trigger( 'update_results', [ 1, false ] );
return false;
})
// Monitor reset link.
.on( 'reset', function() {
$( '.astoundify-listing-labels a.active', this ).removeClass( 'active' );
$( '.astoundify-listing-labels input', this ).remove();
})
// Once results are sent back update tag cloud.
.on( 'updated_results', function( event, results ) {
var labels = results.listing_labels_filter;
if ( labels ) {
var $target = $(this);
$target.find( '.astoundify-listing-labels-cloud' ).html( labels );
$target.find( '.astoundify-listing-labels' ).show();
$target.find( '.astoundify-listing-labels input' ).each(function(){
var val = $(this).val();
$target.find( '.astoundify-listing-labels a:contains(' + val + ')' ).addClass( 'active' );
});
} else {
$(this).find( '.astoundify-listing-labels' ).hide();
}
})
// Update labels when categories change.
.on( 'change', '#search_categories', function() {
var target = $( this ).closest( 'div.job_listings' );
target.find( '.astoundify-listing-labels input' ).remove();
target.trigger( 'update_results', [ 1, false ] );
});
});
}( window ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment