hugo-academic.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. });
  93. // Filter items when filter link is clicked.
  94. $('#filters a').click(function () {
  95. let selector = $(this).attr('data-filter');
  96. $grid_projects.isotope({filter: selector});
  97. $(this).removeClass('active').addClass('active').siblings().removeClass('active all');
  98. return false;
  99. });
  100. });
  101. /* ---------------------------------------------------------------------------
  102. * Filter publications.
  103. * --------------------------------------------------------------------------- */
  104. let $grid_pubs = $('#container-publications');
  105. $grid_pubs.isotope({
  106. itemSelector: '.isotope-item',
  107. percentPosition: true,
  108. masonry: {
  109. // Use Bootstrap compatible grid layout.
  110. columnWidth: '.grid-sizer'
  111. }
  112. });
  113. // Bind publication filter on dropdown change.
  114. $('.pub-filters-select').on('change', function() {
  115. // Get filter value from option value.
  116. let filterValue = this.value;
  117. // Apply filter to Isotope.
  118. $grid_pubs.isotope({ filter: filterValue });
  119. // Set hash URL to current filter.
  120. let url = $(this).val();
  121. if (url.substr(0, 9) == '.pubtype-') {
  122. window.location.hash = url.substr(9);
  123. } else {
  124. window.location.hash = '';
  125. }
  126. });
  127. // Filter publications according to hash in URL.
  128. function filter_publications() {
  129. let urlHash = window.location.hash.replace('#','');
  130. let filterValue = '*';
  131. // Check if hash is numeric.
  132. if (urlHash != '' && !isNaN(urlHash)) {
  133. filterValue = '.pubtype-' + urlHash;
  134. }
  135. $('.pub-filters-select').val(filterValue);
  136. $grid_pubs.isotope({ filter: filterValue });
  137. }
  138. /* ---------------------------------------------------------------------------
  139. * On window load.
  140. * --------------------------------------------------------------------------- */
  141. $(window).on('load', function() {
  142. if (window.location.hash) {
  143. // When accessing homepage from another page and `#top` hash is set, show top of page (no hash).
  144. if (window.location.hash == "#top") {
  145. window.location.hash = ""
  146. } else {
  147. // If URL contains a hash, scroll to target ID taking into account responsive offset.
  148. scrollToAnchor();
  149. }
  150. }
  151. // Initialize Scrollspy.
  152. let $body = $('body');
  153. $body.scrollspy({offset: navbar_offset });
  154. // Call `fixScrollspy` when window is resized.
  155. let resizeTimer;
  156. $(window).resize(function() {
  157. clearTimeout(resizeTimer);
  158. resizeTimer = setTimeout(fixScrollspy, 200);
  159. });
  160. // Enable publication filter for publication index page.
  161. if ($('.pub-filters-select')) {
  162. filter_publications();
  163. // Useful for changing hash manually (e.g. in development):
  164. // window.addEventListener('hashchange', filter_publications, false);
  165. }
  166. });
  167. })(jQuery);