collapse.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. function hideAllButCurrent(){
  2. //by default all submenut items are hidden
  3. //but we need to rehide them for search
  4. document.querySelectorAll("nav > ul").forEach(function(parent) {
  5. if (parent.className.indexOf("collapse_top") !== -1) {
  6. parent.style.display = "none";
  7. }
  8. });
  9. document.querySelectorAll("nav > ul > li > ul li").forEach(function(parent) {
  10. parent.style.display = "none";
  11. });
  12. document.querySelectorAll("nav > h3").forEach(function(section) {
  13. if (section.className.indexOf("collapsed_header") !== -1) {
  14. section.addEventListener("click", function(){
  15. if (section.nextSibling.style.display === "none") {
  16. section.nextSibling.style.display = "block";
  17. } else {
  18. section.nextSibling.style.display = "none";
  19. }
  20. });
  21. }
  22. });
  23. //only current page (if it exists) should be opened
  24. var file = window.location.pathname.split("/").pop().replace(/\.html/, '');
  25. document.querySelectorAll("nav > ul > li > a").forEach(function(parent) {
  26. var href = parent.attributes.href.value.replace(/\.html/, '');
  27. if (file === href) {
  28. if (parent.parentNode.parentNode.className.indexOf("collapse_top") !== -1) {
  29. parent.parentNode.parentNode.style.display = "block";
  30. }
  31. parent.parentNode.querySelectorAll("ul li").forEach(function(elem) {
  32. elem.style.display = "block";
  33. });
  34. }
  35. });
  36. }
  37. hideAllButCurrent();