hugo-academic.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*************************************************
  2. * Academic: the personal website framework for Hugo.
  3. * https://github.com/gcushen/hugo-academic
  4. **************************************************/
  5. (function($){
  6. /* ---------------------------------------------------------------------------
  7. * Responsive scrolling for URL hashes.
  8. * --------------------------------------------------------------------------- */
  9. // Dynamically get responsive navigation bar offset.
  10. let $navbar = $('.navbar-header');
  11. let navbar_offset = $navbar.innerHeight();
  12. /**
  13. * Responsive hash scrolling.
  14. * Check for a URL hash as an anchor.
  15. * If it exists on current page, scroll to it responsively.
  16. * If `target` argument omitted (e.g. after event), assume it's the window's hash.
  17. */
  18. function scrollToAnchor(target) {
  19. // If `target` is undefined or HashChangeEvent object, set it to window's hash.
  20. target = (typeof target === 'undefined' || typeof target === 'object') ? window.location.hash : target;
  21. // Escape colons from IDs, such as those found in Markdown footnote links.
  22. target = target.replace(/:/g, '\\:');
  23. // If target element exists, scroll to it taking into account fixed navigation bar offset.
  24. if($(target).length) {
  25. $('body').addClass('scrolling');
  26. $('html, body').animate({
  27. scrollTop: $(target).offset().top - navbar_offset
  28. }, 600, function () {
  29. $('body').removeClass('scrolling');
  30. });
  31. }
  32. }
  33. // Make Scrollspy responsive.
  34. function fixScrollspy() {
  35. let $body = $('body');
  36. let data = $body.data('bs.scrollspy');
  37. if (data) {
  38. data.options.offset = navbar_offset;
  39. $body.data('bs.scrollspy', data);
  40. $body.scrollspy('refresh');
  41. }
  42. }
  43. // Check for hash change event and fix responsive offset for hash links (e.g. Markdown footnotes).
  44. window.addEventListener("hashchange", scrollToAnchor);
  45. /* ---------------------------------------------------------------------------
  46. * Add smooth scrolling to all links inside the main navbar.
  47. * --------------------------------------------------------------------------- */
  48. $('#navbar-main li.nav-item a').on('click', function(event) {
  49. // Store requested URL hash.
  50. let hash = this.hash;
  51. // If we are on the homepage and the navigation bar link is to a homepage section.
  52. if ( hash && $(hash).length && ($("#homepage").length > 0)) {
  53. // Prevent default click behavior.
  54. event.preventDefault();
  55. // Use jQuery's animate() method for smooth page scrolling.
  56. // The numerical parameter specifies the time (ms) taken to scroll to the specified hash.
  57. $('html, body').animate({
  58. scrollTop: $(hash).offset().top - navbar_offset
  59. }, 800);
  60. }
  61. });
  62. /* ---------------------------------------------------------------------------
  63. * Smooth scrolling for Back To Top link.
  64. * --------------------------------------------------------------------------- */
  65. $('#back_to_top').on('click', function(event) {
  66. event.preventDefault();
  67. $('html, body').animate({
  68. 'scrollTop': 0
  69. }, 800, function() {
  70. window.location.hash = "";
  71. });
  72. });
  73. /* ---------------------------------------------------------------------------
  74. * Hide mobile collapsable menu on clicking a link.
  75. * --------------------------------------------------------------------------- */
  76. $(document).on('click', '.navbar-collapse.in', function(e) {
  77. //get the <a> element that was clicked, even if the <span> element that is inside the <a> element is e.target
  78. let targetElement = $(e.target).is('a') ? $(e.target) : $(e.target).parent();
  79. if (targetElement.is('a') && targetElement.attr('class') != 'dropdown-toggle') {
  80. $(this).collapse('hide');
  81. }
  82. });
  83. /* ---------------------------------------------------------------------------
  84. * Filter projects.
  85. * --------------------------------------------------------------------------- */
  86. let $grid_projects = $('#container-projects');
  87. $grid_projects.imagesLoaded(function () {
  88. // Initialize Isotope after all images have loaded.
  89. $grid_projects.isotope({
  90. itemSelector: '.isotope-item',
  91. layoutMode: 'masonry',
  92. filter: $('#default-project-filter').text()
  93. });
  94. // Filter items when filter link is clicked.
  95. $('#filters a').click(function () {
  96. let selector = $(this).attr('data-filter');
  97. $grid_projects.isotope({filter: selector});
  98. $(this).removeClass('active').addClass('active').siblings().removeClass('active all');
  99. return false;
  100. });
  101. });
  102. /* ---------------------------------------------------------------------------
  103. * Filter publications.
  104. * --------------------------------------------------------------------------- */
  105. let $grid_pubs = $('#container-publications');
  106. $grid_pubs.isotope({
  107. itemSelector: '.isotope-item',
  108. percentPosition: true,
  109. masonry: {
  110. // Use Bootstrap compatible grid layout.
  111. columnWidth: '.grid-sizer'
  112. }
  113. });
  114. // Active publication filters.
  115. let pubFilters = {};
  116. // Flatten object by concatenating values.
  117. function concatValues( obj ) {
  118. let value = '';
  119. for ( let prop in obj ) {
  120. value += obj[ prop ];
  121. }
  122. return value;
  123. }
  124. $('.pub-filters').on( 'change', function() {
  125. let $this = $(this);
  126. // Get group key.
  127. let filterGroup = $this[0].getAttribute('data-filter-group');
  128. // Set filter for group.
  129. pubFilters[ filterGroup ] = this.value;
  130. // Combine filters.
  131. let filterValues = concatValues( pubFilters );
  132. // Activate filters.
  133. $grid_pubs.isotope({ filter: filterValues });
  134. // If filtering by publication type, update the URL hash to enable direct linking to results.
  135. if (filterGroup == "pubtype") {
  136. // Set hash URL to current filter.
  137. let url = $(this).val();
  138. if (url.substr(0, 9) == '.pubtype-') {
  139. window.location.hash = url.substr(9);
  140. } else {
  141. window.location.hash = '';
  142. }
  143. }
  144. });
  145. // Filter publications according to hash in URL.
  146. function filter_publications() {
  147. let urlHash = window.location.hash.replace('#','');
  148. let filterValue = '*';
  149. // Check if hash is numeric.
  150. if (urlHash != '' && !isNaN(urlHash)) {
  151. filterValue = '.pubtype-' + urlHash;
  152. }
  153. // Set filter.
  154. let filterGroup = 'pubtype';
  155. pubFilters[ filterGroup ] = filterValue;
  156. let filterValues = concatValues( pubFilters );
  157. // Activate filters.
  158. $grid_pubs.isotope({ filter: filterValues });
  159. // Set selected option.
  160. $('.pubtype-select').val(filterValue);
  161. }
  162. /* ---------------------------------------------------------------------------
  163. * On window load.
  164. * --------------------------------------------------------------------------- */
  165. $(window).on('load', function() {
  166. if (window.location.hash) {
  167. // When accessing homepage from another page and `#top` hash is set, show top of page (no hash).
  168. if (window.location.hash == "#top") {
  169. window.location.hash = ""
  170. } else {
  171. // If URL contains a hash, scroll to target ID taking into account responsive offset.
  172. scrollToAnchor();
  173. }
  174. }
  175. // Initialize Scrollspy.
  176. let $body = $('body');
  177. $body.scrollspy({offset: navbar_offset });
  178. // Call `fixScrollspy` when window is resized.
  179. let resizeTimer;
  180. $(window).resize(function() {
  181. clearTimeout(resizeTimer);
  182. resizeTimer = setTimeout(fixScrollspy, 200);
  183. });
  184. // Enable publication filter for publication index page.
  185. if ($('.pub-filters-select')) {
  186. filter_publications();
  187. // Useful for changing hash manually (e.g. in development):
  188. // window.addEventListener('hashchange', filter_publications, false);
  189. }
  190. });
  191. })(jQuery);