navigation-loader.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. navigationPageText = fetch(pathToRoot + "navigation.html").then(response => response.text())
  2. displayNavigationFromPage = () => {
  3. navigationPageText.then(data => {
  4. document.getElementById("sideMenu").innerHTML = data;
  5. }).then(() => {
  6. document.querySelectorAll(".overview > a").forEach(link => {
  7. link.setAttribute("href", pathToRoot + link.getAttribute("href"));
  8. })
  9. }).then(() => {
  10. document.querySelectorAll(".sideMenuPart").forEach(nav => {
  11. if (!nav.classList.contains("hidden"))
  12. nav.classList.add("hidden")
  13. })
  14. }).then(() => {
  15. revealNavigationForCurrentPage()
  16. })
  17. document.querySelectorAll('.footer a[href^="#"]').forEach(anchor => {
  18. anchor.addEventListener('click', function (e) {
  19. e.preventDefault();
  20. document.querySelector(this.getAttribute('href')).scrollIntoView({
  21. behavior: 'smooth'
  22. });
  23. });
  24. });
  25. }
  26. revealNavigationForCurrentPage = () => {
  27. let pageId = document.getElementById("content").attributes["pageIds"].value.toString();
  28. let parts = document.querySelectorAll(".sideMenuPart");
  29. let found = 0;
  30. do {
  31. parts.forEach(part => {
  32. if (part.attributes['pageId'].value.indexOf(pageId) !== -1 && found === 0) {
  33. found = 1;
  34. if (part.classList.contains("hidden")) {
  35. part.classList.remove("hidden");
  36. part.setAttribute('data-active', "");
  37. }
  38. revealParents(part)
  39. }
  40. });
  41. pageId = pageId.substring(0, pageId.lastIndexOf("/"))
  42. } while (pageId.indexOf("/") !== -1 && found === 0)
  43. };
  44. revealParents = (part) => {
  45. if (part.classList.contains("sideMenuPart")) {
  46. if (part.classList.contains("hidden"))
  47. part.classList.remove("hidden");
  48. revealParents(part.parentNode)
  49. }
  50. };
  51. /*
  52. This is a work-around for safari being IE of our times.
  53. It doesn't fire a DOMContentLoaded, presumabely because eventListener is added after it wants to do it
  54. */
  55. if (document.readyState == 'loading') {
  56. window.addEventListener('DOMContentLoaded', () => {
  57. displayNavigationFromPage()
  58. })
  59. } else {
  60. displayNavigationFromPage()
  61. }