hugo-academic.js 10.0 KB

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