|
| 1 | +/** |
| 2 | + * TOC button, topbar and popup for mobile devices |
| 3 | + */ |
| 4 | + |
| 5 | +const $tocBar = document.getElementById('toc-bar'); |
| 6 | +const $soloTrigger = document.getElementById('toc-solo-trigger'); |
| 7 | +const $triggers = document.getElementsByClassName('toc-trigger'); |
| 8 | +const $popup = document.getElementById('toc-popup'); |
| 9 | +const $btnClose = document.getElementById('toc-popup-close'); |
| 10 | + |
| 11 | +const SCROLL_LOCK = 'overflow-hidden'; |
| 12 | +const CLOSING = 'closing'; |
| 13 | + |
| 14 | +export class TocMobile { |
| 15 | + static invisible = true; |
| 16 | + static barHeight = 16 * 3; // 3rem |
| 17 | + |
| 18 | + static options = { |
| 19 | + tocSelector: '#toc-popup-content', |
| 20 | + contentSelector: '.content', |
| 21 | + ignoreSelector: '[data-toc-skip]', |
| 22 | + headingSelector: 'h2, h3, h4', |
| 23 | + orderedList: false, |
| 24 | + scrollSmooth: false, |
| 25 | + collapseDepth: 4, |
| 26 | + headingsOffset: this.barHeight |
| 27 | + }; |
| 28 | + |
| 29 | + static initBar() { |
| 30 | + const observer = new IntersectionObserver( |
| 31 | + (entries) => { |
| 32 | + entries.forEach((entry) => { |
| 33 | + $tocBar.classList.toggle('invisible', entry.isIntersecting); |
| 34 | + }); |
| 35 | + }, |
| 36 | + { rootMargin: `-${this.barHeight}px 0px 0px 0px` } |
| 37 | + ); |
| 38 | + |
| 39 | + observer.observe($soloTrigger); |
| 40 | + this.invisible = false; |
| 41 | + } |
| 42 | + |
| 43 | + static listenAnchors() { |
| 44 | + const $anchors = document.getElementsByClassName('toc-link'); |
| 45 | + [...$anchors].forEach((anchor) => { |
| 46 | + anchor.onclick = this.hidePopup; |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + static refresh() { |
| 51 | + if (this.invisible) { |
| 52 | + this.initComponents(); |
| 53 | + } |
| 54 | + tocbot.refresh(this.options); |
| 55 | + this.listenAnchors(); |
| 56 | + } |
| 57 | + |
| 58 | + static showPopup() { |
| 59 | + TocMobile.lockScroll(true); |
| 60 | + $popup.showModal(); |
| 61 | + const activeItem = $popup.querySelector('li.is-active-li'); |
| 62 | + activeItem.scrollIntoView({ block: 'center' }); |
| 63 | + } |
| 64 | + |
| 65 | + static hidePopup() { |
| 66 | + if (!$popup.open) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + $popup.toggleAttribute(CLOSING); |
| 71 | + |
| 72 | + $popup.addEventListener( |
| 73 | + 'animationend', |
| 74 | + () => { |
| 75 | + $popup.toggleAttribute(CLOSING); |
| 76 | + $popup.close(); |
| 77 | + }, |
| 78 | + { once: true } |
| 79 | + ); |
| 80 | + |
| 81 | + TocMobile.lockScroll(false); |
| 82 | + } |
| 83 | + |
| 84 | + static lockScroll(enable) { |
| 85 | + document.documentElement.classList.toggle(SCROLL_LOCK, enable); |
| 86 | + document.body.classList.toggle(SCROLL_LOCK, enable); |
| 87 | + } |
| 88 | + |
| 89 | + static clickBackdrop(event) { |
| 90 | + const rect = event.target.getBoundingClientRect(); |
| 91 | + if ( |
| 92 | + event.clientX < rect.left || |
| 93 | + event.clientX > rect.right || |
| 94 | + event.clientY < rect.top || |
| 95 | + event.clientY > rect.bottom |
| 96 | + ) { |
| 97 | + TocMobile.hidePopup(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + static initComponents() { |
| 102 | + this.initBar(); |
| 103 | + |
| 104 | + [...$triggers].forEach((trigger) => { |
| 105 | + trigger.onclick = this.showPopup; |
| 106 | + }); |
| 107 | + |
| 108 | + $popup.onclick = this.clickBackdrop; |
| 109 | + $btnClose.onclick = $popup.oncancel = this.hidePopup; |
| 110 | + } |
| 111 | + |
| 112 | + static init() { |
| 113 | + tocbot.init(this.options); |
| 114 | + this.listenAnchors(); |
| 115 | + this.initComponents(); |
| 116 | + } |
| 117 | +} |
0 commit comments