hugo-academic.js 6.7 KB

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