|
| 1 | +jQuery(document).ready(function ($) { |
| 2 | + // FAQ Page |
| 3 | + if ($(".page-template-template-faq").length > 0) { |
| 4 | + var lock = false; |
| 5 | + var $faqLinks = $('[href^="#"]'); |
| 6 | + |
| 7 | + // Set the active link from the url hash |
| 8 | + function setActiveFromHash() { |
| 9 | + var $activeFAQLink = $('.faq-sidebar a[href="' + window.location.hash + '"]'); |
| 10 | + |
| 11 | + if ($activeFAQLink.length > 0) { |
| 12 | + $faqLinks.removeClass("active"); |
| 13 | + $activeFAQLink.addClass("active"); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + setActiveFromHash(); |
| 18 | + |
| 19 | + // When the hash is updated, add the active class to the sidebar items |
| 20 | + $(window).on("hashchange", function (event) { |
| 21 | + event.preventDefault(); |
| 22 | + setActiveFromHash(); |
| 23 | + }); |
| 24 | + |
| 25 | + TableOfContents.init(); |
| 26 | + } |
| 27 | +}); |
| 28 | + |
| 29 | +/** |
| 30 | + * Object to set the links active when headings are scrolled by. |
| 31 | + * @todo: Move to own file. |
| 32 | + */ |
| 33 | +var TableOfContents = { |
| 34 | + container: document.body, |
| 35 | + links: null, |
| 36 | + headings: null, |
| 37 | + intersectionOptions: { |
| 38 | + rootMargin: "0px", |
| 39 | + threshold: 1, |
| 40 | + }, |
| 41 | + previousSection: null, |
| 42 | + observer: null, |
| 43 | + activeClass: "active", |
| 44 | + |
| 45 | + init: function () { |
| 46 | + this.handleObserver = this.handleObserver.bind(this); |
| 47 | + |
| 48 | + this.setUpObserver(); |
| 49 | + this.findLinksAndHeadings(); |
| 50 | + this.observeSections(); |
| 51 | + }, |
| 52 | + |
| 53 | + handleLinkClick: function (evt) { |
| 54 | + evt.preventDefault(); |
| 55 | + let id = evt.target.getAttribute("href").replace("#", ""); |
| 56 | + |
| 57 | + let section = this.headings.find((heading) => { |
| 58 | + return heading.getAttribute("id") === id; |
| 59 | + }); |
| 60 | + |
| 61 | + section.setAttribute("tabindex", -1); |
| 62 | + section.focus(); |
| 63 | + |
| 64 | + window.scroll({ |
| 65 | + behavior: "smooth", |
| 66 | + top: section.offsetTop - 15, |
| 67 | + block: "start", |
| 68 | + }); |
| 69 | + |
| 70 | + if (this.container.classList.contains(this.activeClass)) { |
| 71 | + this.container.classList.remove(this.activeClass); |
| 72 | + } |
| 73 | + }, |
| 74 | + |
| 75 | + handleObserver: function (entries, observer) { |
| 76 | + entries.forEach((entry) => { |
| 77 | + let href = `#${entry.target.getAttribute("id")}`, |
| 78 | + link = this.links.find((l) => l.getAttribute("href") === href); |
| 79 | + |
| 80 | + if (entry.isIntersecting && entry.intersectionRatio >= 1) { |
| 81 | + link.classList.add("is-visible"); |
| 82 | + this.previousSection = entry.target.getAttribute("id"); |
| 83 | + } else { |
| 84 | + link.classList.remove("is-visible"); |
| 85 | + } |
| 86 | + |
| 87 | + this.highlightFirstActive(); |
| 88 | + }); |
| 89 | + }, |
| 90 | + |
| 91 | + highlightFirstActive: function () { |
| 92 | + let firstVisibleLink = this.container.querySelector(".is-visible"); |
| 93 | + |
| 94 | + this.links.forEach((link) => { |
| 95 | + link.classList.remove(this.activeClass); |
| 96 | + }); |
| 97 | + |
| 98 | + if (firstVisibleLink) { |
| 99 | + firstVisibleLink.classList.add(this.activeClass); |
| 100 | + } |
| 101 | + |
| 102 | + if (!firstVisibleLink && this.previousSection) { |
| 103 | + this.container |
| 104 | + .querySelector(`a[href="#${this.previousSection}"]`) |
| 105 | + .classList.add(this.activeClass); |
| 106 | + } |
| 107 | + }, |
| 108 | + |
| 109 | + observeSections: function () { |
| 110 | + this.headings.forEach((heading) => { |
| 111 | + this.observer.observe(heading); |
| 112 | + }); |
| 113 | + }, |
| 114 | + |
| 115 | + setUpObserver: function () { |
| 116 | + this.observer = new IntersectionObserver(this.handleObserver, this.intersectionOptions); |
| 117 | + }, |
| 118 | + |
| 119 | + findLinksAndHeadings: function () { |
| 120 | + this.links = [...this.container.querySelectorAll(".faq-sidebar a")]; |
| 121 | + this.headings = this.links.map((link) => { |
| 122 | + let id = link.getAttribute("href"); |
| 123 | + return document.querySelector(id); |
| 124 | + }); |
| 125 | + }, |
| 126 | +}; |
0 commit comments