浏览代码

publications: Add filter for year (Close #171)

George Cushen 7 年之前
父节点
当前提交
fcdf9b887f
共有 2 个文件被更改,包括 65 次插入17 次删除
  1. 19 2
      layouts/section/publication.html
  2. 46 15
      static/js/hugo-academic.js

+ 19 - 2
layouts/section/publication.html

@@ -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 }}

+ 46 - 15
static/js/hugo-academic.js

@@ -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 });
-
-    // 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 = '';
+  // Active publication filters.
+  let pubFilters = {};
+
+  // 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);
   }
 
   /* ---------------------------------------------------------------------------