nav-additions.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Un-active everything when you click it
  2. Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) {
  3. el.addEventHandler("click", function() {
  4. Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) {
  5. el.classList.remove("active");
  6. });
  7. el.classList.add("active");
  8. });
  9. });
  10. var updateFunction = function() {
  11. var id;
  12. var elements = document.getElementsByClassName("header");
  13. Array.prototype.forEach.call(elements, function(el) {
  14. if (window.pageYOffset >= el.offsetTop) {
  15. id = el;
  16. }
  17. });
  18. Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) {
  19. el.classList.remove("active");
  20. });
  21. if (id == undefined)
  22. return;
  23. Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) {
  24. if (id.href.localeCompare(el.href) == 0) {
  25. el.classList.add("active");
  26. }
  27. });
  28. };
  29. window.addEventListener('load', function() {
  30. /* Scroll the page index on the left to the current active page */
  31. var active = document.querySelector(".chapter li a.active");
  32. active.scrollIntoView({ behavior: "instant", block: "center", inline: "nearest" });
  33. /* Populate subpage footer */
  34. /* Get next sibling of active list element
  35. then check if it has a nested ol
  36. if so, this implies our current active element has nested subpages */
  37. var nextListEl = active.parentElement.nextElementSibling;
  38. var footer = document.getElementById("subpage-footer");
  39. footer.style.display = "none";
  40. if (nextListEl != null)
  41. {
  42. console.log(nextListEl);
  43. var list = nextListEl.querySelector(".section")
  44. if (list != null)
  45. {
  46. footer.style.display = "block";
  47. footer.appendChild(list.cloneNode(true));
  48. }
  49. }
  50. /* Populate pagetoc sidebar */
  51. var pagetoc = document.getElementsByClassName("pagetoc")[0];
  52. var elements = document.getElementsByClassName("header");
  53. // don't show pagetoc sidebar with less than 2 headers
  54. if (elements.length < 2)
  55. return;
  56. Array.prototype.forEach.call(elements, function(el) {
  57. var link = document.createElement("a");
  58. // Indent shows hierarchy
  59. var indent = "";
  60. switch (el.parentElement.tagName) {
  61. case "H2":
  62. indent = "20px";
  63. break;
  64. case "H3":
  65. indent = "40px";
  66. break;
  67. case "H4":
  68. indent = "60px";
  69. break;
  70. case "H5":
  71. indent = "80px";
  72. default:
  73. break;
  74. }
  75. link.appendChild(document.createTextNode(el.text));
  76. link.style.paddingLeft = indent;
  77. link.href = el.href;
  78. pagetoc.appendChild(link);
  79. });
  80. updateFunction.call();
  81. });
  82. // Handle active elements on scroll
  83. window.addEventListener("scroll", updateFunction);