index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { getDeviceState } from '../../core'
  2. // Toggle Table of Contents on click. Here, class "hide" open the toc
  3. function toggleTOC () {
  4. const toc = document.getElementById('toc-section')
  5. if (toc == null) {
  6. return
  7. }
  8. if (toc.classList.contains('hide')) {
  9. toc.classList.remove('hide')
  10. } else {
  11. // if sidebar-section is open, then close it first
  12. const sidebar = document.getElementById('sidebar-section')
  13. if (sidebar != null && sidebar.classList.contains('hide')) {
  14. sidebar.classList.remove('hide')
  15. }
  16. // add "hide" class
  17. toc.classList.add('hide')
  18. // if it is mobile device. then scroll to top.
  19. const { isMobile } = getDeviceState()
  20. if (isMobile && toc.classList.contains('hide')) {
  21. document.body.scrollTop = 0
  22. document.documentElement.scrollTop = 0
  23. }
  24. }
  25. if (document.getElementById('hero-area') != null) {
  26. document.getElementById('hero-area').classList.toggle('hide')
  27. }
  28. }
  29. window.addEventListener('DOMContentLoaded', () => {
  30. // bind click event to #toc-toggle in navbar-2.html
  31. const toggle = document.getElementById('toc-toggler')
  32. if (toggle) toggle.addEventListener('click', toggleTOC)
  33. // hide TOC when user clicks on a TOC link.
  34. // Only applies if it's mobile.
  35. const toc = document.getElementById('TableOfContents')
  36. if (toc) {
  37. toc.addEventListener('click', (event) => {
  38. const { isMobile } = getDeviceState()
  39. if (isMobile && event.target.nodeName === 'A') {
  40. toggleTOC()
  41. }
  42. })
  43. }
  44. })