diff --git a/assets/js/academic.js b/assets/js/academic.js index e49f75d2..a562ef1f 100644 --- a/assets/js/academic.js +++ b/assets/js/academic.js @@ -103,18 +103,54 @@ * Filter publications. * --------------------------------------------------------------------------- */ + // Active publication filters. + let pubFilters = {}; + + // Search term. + let searchRegex; + + // Filter values (concatenated). + let filterValues; + + // Publication container. let $grid_pubs = $('#container-publications'); + + // Initialise Isotope. $grid_pubs.isotope({ itemSelector: '.isotope-item', percentPosition: true, masonry: { // Use Bootstrap compatible grid layout. columnWidth: '.grid-sizer' + }, + filter: function() { + let $this = $(this); + let searchResults = searchRegex ? $this.text().match( searchRegex ) : true; + let filterResults = filterValues ? $this.is( filterValues ) : true; + return searchResults && filterResults; } }); - // Active publication filters. - let pubFilters = {}; + // Filter by search term. + let $quickSearch = $('.filter-search').keyup( debounce( function() { + searchRegex = new RegExp( $quickSearch.val(), 'gi' ); + $grid_pubs.isotope(); + }) ); + + // Debounce input to prevent spamming filter requests. + function debounce( fn, threshold ) { + let timeout; + threshold = threshold || 100; + return function debounced() { + clearTimeout( timeout ); + let args = arguments; + let _this = this; + function delayed() { + fn.apply( _this, args ); + } + timeout = setTimeout( delayed, threshold ); + }; + } // Flatten object by concatenating values. function concatValues( obj ) { @@ -135,10 +171,10 @@ pubFilters[ filterGroup ] = this.value; // Combine filters. - let filterValues = concatValues( pubFilters ); + filterValues = concatValues( pubFilters ); // Activate filters. - $grid_pubs.isotope({ filter: filterValues }); + $grid_pubs.isotope(); // If filtering by publication type, update the URL hash to enable direct linking to results. if (filterGroup == "pubtype") { @@ -165,10 +201,10 @@ // Set filter. let filterGroup = 'pubtype'; pubFilters[ filterGroup ] = filterValue; - let filterValues = concatValues( pubFilters ); + filterValues = concatValues( pubFilters ); // Activate filters. - $grid_pubs.isotope({ filter: filterValues }); + $grid_pubs.isotope(); // Set selected option. $('.pubtype-select').val(filterValue); diff --git a/exampleSite/content/home/publications.md b/exampleSite/content/home/publications.md index 27b91c3c..ccfc5588 100644 --- a/exampleSite/content/home/publications.md +++ b/exampleSite/content/home/publications.md @@ -35,3 +35,6 @@ publication_type = "-1" exclude_featured = false +++ +{{% alert note %}} +Quickly discover relevant content by [filtering publications]({{< ref "/publication/_index.md" >}}). +{{% /alert %}} diff --git a/i18n/en.yaml b/i18n/en.yaml index 9d58f952..f430cfd1 100644 --- a/i18n/en.yaml +++ b/i18n/en.yaml @@ -167,14 +167,6 @@ - id: pub_book_section translation: Book section -# Filtering - -- id: filter_by_type - translation: Filter by type - -- id: filter_all - translation: All - # Project details - id: open_project_site diff --git a/layouts/section/publication.html b/layouts/section/publication.html index 8ed53423..13bed7c4 100644 --- a/layouts/section/publication.html +++ b/layouts/section/publication.html @@ -17,13 +17,14 @@ {{ $.Scratch.SetInMap "years" $year $year }} {{ end }} -
+
- {{ i18n "filter_by_type" }}: +
- + {{ $years_sorted := $.Scratch.GetSortedMapValues "years" }} {{ if $years_sorted }} {{ range $year := sort $years_sorted "" "desc" }}