jazzy.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. window.jazzy = {'docset': false}
  2. if (typeof window.dash != 'undefined') {
  3. document.documentElement.className += ' dash'
  4. window.jazzy.docset = true
  5. }
  6. if (navigator.userAgent.match(/xcode/i)) {
  7. document.documentElement.className += ' xcode'
  8. window.jazzy.docset = true
  9. }
  10. function toggleItem($link, $content) {
  11. var animationDuration = 300;
  12. $link.toggleClass('token-open');
  13. $content.slideToggle(animationDuration);
  14. }
  15. function itemLinkToContent($link) {
  16. return $link.parent().parent().next();
  17. }
  18. // On doc load + hash-change, open any targetted item
  19. function openCurrentItemIfClosed() {
  20. if (window.jazzy.docset) {
  21. return;
  22. }
  23. var $link = $(`.token[href="${location.hash}"]`);
  24. $content = itemLinkToContent($link);
  25. if ($content.is(':hidden')) {
  26. toggleItem($link, $content);
  27. }
  28. }
  29. $(openCurrentItemIfClosed);
  30. $(window).on('hashchange', openCurrentItemIfClosed);
  31. // On item link ('token') click, toggle its discussion
  32. $('.token').on('click', function(event) {
  33. if (window.jazzy.docset) {
  34. return;
  35. }
  36. var $link = $(this);
  37. toggleItem($link, itemLinkToContent($link));
  38. // Keeps the document from jumping to the hash.
  39. var href = $link.attr('href');
  40. if (history.pushState) {
  41. history.pushState({}, '', href);
  42. } else {
  43. location.hash = href;
  44. }
  45. event.preventDefault();
  46. });
  47. // Clicks on links to the current, closed, item need to open the item
  48. $("a:not('.token')").on('click', function() {
  49. if (location == this.href) {
  50. openCurrentItemIfClosed();
  51. }
  52. });