UNPKG

typedoc-theme-hierarchy

Version:
1 lines 2.81 kB
(()=>{"use strict";class t{openedPathLsKey="opened-path-state";openedPaths=[];constructor(){const t=localStorage.getItem("opened-path-state");this.openedPaths=t?JSON.parse(t):[]}addOpenedPath(t){this.openedPaths.push(t),this.updateState()}removeOpenedPath(t){this.openedPaths=this.openedPaths.filter((e=>e!==t)),this.updateState()}getOpenedPaths(){return this.openedPaths}updateState(){localStorage.setItem(this.openedPathLsKey,JSON.stringify(this.openedPaths))}}(new class{stateManager=new t;titleSelector=".js-category-title";listSelector=".js-category-list";init(){this.addListeners(),this.initSaved(),this.openCurrentPath()}openPathAndSave(t){this.openPath(t),this.stateManager.addOpenedPath(t)}openPath(t){const e=document.querySelector(`${this.listSelector}[data-id="${t}"]`);e&&(e.classList.add("_open"),e.parentNode?.querySelector(this.titleSelector)?.classList.add("_open"))}closePath(t){const e=document.querySelector(`${this.listSelector}[data-id="${t}"]`);e&&(e.classList.remove("_open"),e.parentNode?.querySelector(this.titleSelector)?.classList.remove("_open"),this.stateManager.removeOpenedPath(t))}closePathWithChildren(t){this.closePath(t);const e=document.querySelector(`${this.listSelector}[data-id="${t}"]`);if(!e)return;const s=e.querySelectorAll(this.listSelector);for(const t of s)this.closePath(t.dataset.id||"")}togglePath(t){const e=document.querySelector(`${this.listSelector}[data-id="${t}"]`);e&&(e.classList.contains("_open")?this.closePathWithChildren(t):this.openPathAndSave(t))}addListeners(){const t=document.querySelectorAll('.js-category-title:not([data-id="root"])');for(const e of t)e.addEventListener("click",(()=>{const t=e.dataset.id||"";this.togglePath(t)}));this.addExpandListener(),this.addCollapseListener(),this.addTargetListener()}addExpandListener(){document.querySelector(".js-tree-expand")?.addEventListener("click",(()=>{const t=document.querySelectorAll(this.listSelector);for(const e of t){const t=e.dataset.id||"";this.openPathAndSave(t)}}))}addCollapseListener(){document.querySelector(".js-tree-collapse")?.addEventListener("click",(()=>{const t=document.querySelectorAll(this.listSelector);for(const e of t){const t=e.dataset.id||"";this.closePath(t)}}))}addTargetListener(){document.querySelector(".js-tree-target")?.addEventListener("click",(()=>{this.openCurrentPath()?.scrollIntoView()}))}initSaved(){const t=this.stateManager.getOpenedPaths();for(const e of t)this.openPath(e)}openCurrentPath(){const t=window.location.pathname.split("/"),e=`/${t[t.length-2]||""}/${t[t.length-1]||""}`,s=document.querySelector(`.js-category-link[data-id="${e}"]`);if(!s)return null;s.classList.add("_active");let o=s.closest(this.listSelector);for(;o;){const t=o.dataset.id||"";this.openPath(t),o=o.parentNode.closest(this.listSelector)}return s}}).init()})();