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>
{{ end }}
{{/* Array of distinct years. */}}
{{ range .Pages.ByDate.Reverse }}
{{ $year := print (.Date.Format "2006") }}
{{ $.Scratch.SetInMap "years" $year $year }}
{{ end }}
<p>
{{ 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>
{{ range $index, $taxonomy := .Site.Taxonomies.publication_types }}
<option value=".pubtype-{{ (int $index) }}">
@ -22,6 +29,16 @@
</option>
{{ end }}
</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>
<div id="container-publications">
@ -33,7 +50,7 @@
{{ $.Scratch.Set "pubtype" 0 }}
{{ 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 }}
{{ partial "publication_li_classic" . }}
{{ else if eq $.Params.list_format 2 }}

View file

@ -133,19 +133,42 @@
}
});
// Bind publication filter on dropdown change.
$('.pub-filters-select').on('change', function() {
// Get filter value from option value.
let filterValue = this.value;
// Apply filter to Isotope.
$grid_pubs.isotope({ filter: filterValue });
// Active publication filters.
let pubFilters = {};
// 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 = '';
// Flatten object by concatenating values.
function concatValues( obj ) {
let value = '';
for ( let prop in obj ) {
value += obj[ prop ];
}
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;
}
$('.pub-filters-select').val(filterValue);
$grid_pubs.isotope({ filter: filterValue });
// Set filter.
let filterGroup = 'pubtype';
pubFilters[ filterGroup ] = filterValue;
let filterValues = concatValues( pubFilters );
// Activate filters.
$grid_pubs.isotope({ filter: filterValues });
// Set selected option.
$('.pubtype-select').val(filterValue);
}
/* ---------------------------------------------------------------------------