123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*************************************************
- * Wowchemy
- * https://github.com/wowchemy/wowchemy-hugo-themes
- *
- * Wowchemy Publications
- **************************************************/
- // Active publication filters.
- let pubFilters = {};
- // Search term.
- let searchRegex;
- // Filter values (concatenated).
- let filterValues;
- // Publication container.
- let $grid_pubs = $('#container-publications');
- // Initialise Isotope publication layout if required.
- if ($grid_pubs.length) {
- $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;
- },
- });
- // Filter by search term.
- let $quickSearch = $('.filter-search').keyup(
- debounce(function () {
- searchRegex = new RegExp($quickSearch.val(), 'gi');
- $grid_pubs.isotope();
- }),
- );
- $('.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.
- filterValues = concatValues(pubFilters);
- // Activate filters.
- $grid_pubs.isotope();
- // 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 = '';
- }
- }
- });
- }
- // 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) {
- let value = '';
- for (let prop in obj) {
- value += obj[prop];
- }
- return value;
- }
- // Filter publications according to hash in URL.
- function filter_publications() {
- // Check for Isotope publication layout.
- if (!$grid_pubs.length) return;
- let urlHash = window.location.hash.replace('#', '');
- let filterValue = '*';
- // Check if hash is numeric.
- if (urlHash != '' && !isNaN(urlHash)) {
- filterValue = '.pubtype-' + urlHash;
- }
- // Set filter.
- let filterGroup = 'pubtype';
- pubFilters[filterGroup] = filterValue;
- filterValues = concatValues(pubFilters);
- // Activate filters.
- $grid_pubs.isotope();
- // Set selected option.
- $('.pubtype-select').val(filterValue);
- }
- document.addEventListener('DOMContentLoaded', function () {
- // Enable publication filter for publication index page.
- if ($('.pub-filters-select')) {
- filter_publications();
- // Useful for changing hash manually (e.g. in development):
- // window.addEventListener('hashchange', filter_publications, false);
- }
- // Load citation modal on 'Cite' click.
- $('.js-cite-modal').click(function (e) {
- e.preventDefault();
- let filename = $(this).attr('data-filename');
- let modal = $('#modal');
- modal.find('.modal-body code').load(filename, function (response, status, xhr) {
- if (status == 'error') {
- let msg = 'Error: ';
- $('#modal-error').html(msg + xhr.status + ' ' + xhr.statusText);
- } else {
- $('.js-download-cite').attr('href', filename);
- }
- });
- modal.modal('show');
- });
- // Copy citation text on 'Copy' click.
- $('.js-copy-cite').click(function (e) {
- e.preventDefault();
- // Get text to copy.
- let citation = document.querySelector('#modal .modal-body').innerHTML;
- navigator.clipboard
- .writeText(citation)
- .then(function () {
- console.debug('Citation copied!');
- })
- .catch(function () {
- console.error('Citation copy failed!');
- });
- });
- });
|