publications: Add filter for year (Close #171)

This commit is contained in:
George Cushen 2017-09-19 23:07:56 +01:00
commit fcdf9b887f
2 changed files with 64 additions and 16 deletions

View file

@ -12,9 +12,16 @@
<div class="article-style" itemprop="articleBody">{{ . }}</div> <div class="article-style" itemprop="articleBody">{{ . }}</div>
{{ end }} {{ end }}
{{/* Array of distinct years. */}}
{{ range .Pages.ByDate.Reverse }}
{{ $year := print (.Date.Format "2006") }}
{{ $.Scratch.SetInMap "years" $year $year }}
{{ end }}
<p> <p>
{{ i18n "filter_by_type" }}: {{ i18n "filter_by_type" }}:
<select class="pub-filters-select">
<select class="pub-filters pubtype-select" data-filter-group="pubtype">
<option value="*">{{ i18n "filter_all" }}</option> <option value="*">{{ i18n "filter_all" }}</option>
{{ range $index, $taxonomy := .Site.Taxonomies.publication_types }} {{ range $index, $taxonomy := .Site.Taxonomies.publication_types }}
<option value=".pubtype-{{ (int $index) }}"> <option value=".pubtype-{{ (int $index) }}">
@ -22,6 +29,16 @@
</option> </option>
{{ end }} {{ end }}
</select> </select>
<select class="pub-filters" data-filter-group="year">
<option value="*">{{ i18n "filter_all" }}</option>
{{ $years_sorted := $.Scratch.GetSortedMapValues "years" }}
{{ range $year := sort $years_sorted "" "desc" }}
<option value=".year-{{ $year }}">
{{ $year }}
</option>
{{ end }}
</select>
</p> </p>
<div id="container-publications"> <div id="container-publications">
@ -33,7 +50,7 @@
{{ $.Scratch.Set "pubtype" 0 }} {{ $.Scratch.Set "pubtype" 0 }}
{{ end }} {{ end }}
<div class="grid-sizer col-md-12 isotope-item pubtype-{{ $.Scratch.Get "pubtype" }}"> <div class="grid-sizer col-md-12 isotope-item pubtype-{{ $.Scratch.Get "pubtype" }} year-{{ .Date.Format "2006" }}">
{{ if eq $.Params.list_format 1 }} {{ if eq $.Params.list_format 1 }}
{{ partial "publication_li_classic" . }} {{ partial "publication_li_classic" . }}
{{ else if eq $.Params.list_format 2 }} {{ else if eq $.Params.list_format 2 }}

View file

@ -133,19 +133,42 @@
} }
}); });
// Bind publication filter on dropdown change. // Active publication filters.
$('.pub-filters-select').on('change', function() { let pubFilters = {};
// Get filter value from option value.
let filterValue = this.value;
// Apply filter to Isotope.
$grid_pubs.isotope({ filter: filterValue });
// Set hash URL to current filter. // Flatten object by concatenating values.
let url = $(this).val(); function concatValues( obj ) {
if (url.substr(0, 9) == '.pubtype-') { let value = '';
window.location.hash = url.substr(9); for ( let prop in obj ) {
} else { value += obj[ prop ];
window.location.hash = ''; }
return value;
}
$('.pub-filters').on( 'change', function() {
let $this = $(this);
// Get group key.
let filterGroup = $this[0].getAttribute('data-filter-group');
// Set filter for group.
pubFilters[ filterGroup ] = this.value;
// Combine filters.
let filterValues = concatValues( pubFilters );
// Activate filters.
$grid_pubs.isotope({ filter: filterValues });
// If filtering by publication type, update the URL hash to enable direct linking to results.
if (filterGroup == "pubtype") {
// Set hash URL to current filter.
let url = $(this).val();
if (url.substr(0, 9) == '.pubtype-') {
window.location.hash = url.substr(9);
} else {
window.location.hash = '';
}
} }
}); });
@ -159,8 +182,16 @@
filterValue = '.pubtype-' + urlHash; filterValue = '.pubtype-' + urlHash;
} }
$('.pub-filters-select').val(filterValue); // Set filter.
$grid_pubs.isotope({ filter: filterValue }); let filterGroup = 'pubtype';
pubFilters[ filterGroup ] = filterValue;
let filterValues = concatValues( pubFilters );
// Activate filters.
$grid_pubs.isotope({ filter: filterValues });
// Set selected option.
$('.pubtype-select').val(filterValue);
} }
/* --------------------------------------------------------------------------- /* ---------------------------------------------------------------------------