hugo-academic.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. $('.projects-container').each(function(index, container) {
  87. let $container = $(container);
  88. let $section = $container.closest('section');
  89. $container.imagesLoaded(function() {
  90. // Initialize Isotope after all images have loaded.
  91. $container.isotope({
  92. itemSelector: '.isotope-item',
  93. layoutMode: 'masonry',
  94. filter: $section.find('.default-project-filter').text()
  95. });
  96. // Filter items when filter link is clicked.
  97. $section.find('.project-filters a').click(function() {
  98. let selector = $(this).attr('data-filter');
  99. $container.isotope({filter: selector});
  100. $(this).removeClass('active').addClass('active').siblings().removeClass('active all');
  101. return false;
  102. });
  103. });
  104. });
  105. /* ---------------------------------------------------------------------------
  106. * Filter publications.
  107. * --------------------------------------------------------------------------- */
  108. let $grid_pubs = $('#container-publications');
  109. $grid_pubs.isotope({
  110. itemSelector: '.isotope-item',
  111. percentPosition: true,
  112. masonry: {
  113. // Use Bootstrap compatible grid layout.
  114. columnWidth: '.grid-sizer'
  115. }
  116. });
  117. // Active publication filters.
  118. let pubFilters = {};
  119. // Flatten object by concatenating values.
  120. function concatValues( obj ) {
  121. let value = '';
  122. for ( let prop in obj ) {
  123. value += obj[ prop ];
  124. }
  125. return value;
  126. }
  127. $('.pub-filters').on( 'change', function() {
  128. let $this = $(this);
  129. // Get group key.
  130. let filterGroup = $this[0].getAttribute('data-filter-group');
  131. // Set filter for group.
  132. pubFilters[ filterGroup ] = this.value;
  133. // Combine filters.
  134. let filterValues = concatValues( pubFilters );
  135. // Activate filters.
  136. $grid_pubs.isotope({ filter: filterValues });
  137. // If filtering by publication type, update the URL hash to enable direct linking to results.
  138. if (filterGroup == "pubtype") {
  139. // Set hash URL to current filter.
  140. let url = $(this).val();
  141. if (url.substr(0, 9) == '.pubtype-') {
  142. window.location.hash = url.substr(9);
  143. } else {
  144. window.location.hash = '';
  145. }
  146. }
  147. });
  148. // Filter publications according to hash in URL.
  149. function filter_publications() {
  150. let urlHash = window.location.hash.replace('#','');
  151. let filterValue = '*';
  152. // Check if hash is numeric.
  153. if (urlHash != '' && !isNaN(urlHash)) {
  154. filterValue = '.pubtype-' + urlHash;
  155. }
  156. // Set filter.
  157. let filterGroup = 'pubtype';
  158. pubFilters[ filterGroup ] = filterValue;
  159. let filterValues = concatValues( pubFilters );
  160. // Activate filters.
  161. $grid_pubs.isotope({ filter: filterValues });
  162. // Set selected option.
  163. $('.pubtype-select').val(filterValue);
  164. }
  165. /* ---------------------------------------------------------------------------
  166. * Google maps.
  167. * --------------------------------------------------------------------------- */
  168. function initMap () {
  169. if ($('#map').length) {
  170. let lat = $('#gmap-lat').val();
  171. let lng = $('#gmap-lng').val();
  172. let address = $('#gmap-dir').val();
  173. let map = new GMaps({
  174. div: '#map',
  175. lat: lat,
  176. lng: lng,
  177. zoomControl: true,
  178. zoomControlOpt: {
  179. style: 'SMALL',
  180. position: 'TOP_LEFT'
  181. },
  182. panControl: false,
  183. streetViewControl: false,
  184. mapTypeControl: false,
  185. overviewMapControl: false,
  186. scrollwheel: true,
  187. draggable: true
  188. });
  189. map.addMarker({
  190. lat: lat,
  191. lng: lng,
  192. click: function (e) {
  193. let url = 'https://www.google.com/maps/place/' + encodeURIComponent(address) + '/@' + lat + ',' + lng +'/';
  194. window.open(url, '_blank')
  195. },
  196. title: address
  197. })
  198. }
  199. }
  200. /* ---------------------------------------------------------------------------
  201. * On window load.
  202. * --------------------------------------------------------------------------- */
  203. $(window).on('load', function() {
  204. if (window.location.hash) {
  205. // When accessing homepage from another page and `#top` hash is set, show top of page (no hash).
  206. if (window.location.hash == "#top") {
  207. window.location.hash = ""
  208. } else {
  209. // If URL contains a hash, scroll to target ID taking into account responsive offset.
  210. scrollToAnchor();
  211. }
  212. }
  213. // Initialize Scrollspy.
  214. let $body = $('body');
  215. $body.scrollspy({offset: navbar_offset });
  216. // Call `fixScrollspy` when window is resized.
  217. let resizeTimer;
  218. $(window).resize(function() {
  219. clearTimeout(resizeTimer);
  220. resizeTimer = setTimeout(fixScrollspy, 200);
  221. });
  222. // Enable publication filter for publication index page.
  223. if ($('.pub-filters-select')) {
  224. filter_publications();
  225. // Useful for changing hash manually (e.g. in development):
  226. // window.addEventListener('hashchange', filter_publications, false);
  227. }
  228. // Load citation modal on 'Cite' click.
  229. $('.js-cite-modal').click(function(e) {
  230. e.preventDefault();
  231. let filename = $(this).attr('data-filename');
  232. let modal = $('#modal');
  233. modal.find('.modal-body').load( filename , function( response, status, xhr ) {
  234. if ( status == 'error' ) {
  235. let msg = "Error: ";
  236. $('#modal-error').html( msg + xhr.status + " " + xhr.statusText );
  237. } else {
  238. $('.js-download-cite').attr('href', filename);
  239. }
  240. });
  241. modal.modal('show');
  242. });
  243. // Copy citation text on 'Copy' click.
  244. $('.js-copy-cite').click(function(e) {
  245. e.preventDefault();
  246. // Get selection.
  247. let range = document.createRange();
  248. let code_node = document.querySelector('#modal .modal-body');
  249. range.selectNode(code_node);
  250. window.getSelection().addRange(range);
  251. try {
  252. // Execute the copy command.
  253. document.execCommand('copy');
  254. } catch(e) {
  255. console.log('Error: citation copy failed.');
  256. }
  257. // Remove selection.
  258. window.getSelection().removeRange(range);
  259. });
  260. // Initialise Google Maps if necessary.
  261. initMap();
  262. });
  263. })(jQuery);