blob: c5de51bdeca51f7f610d73703cb9a92ca2e2fb7a [file] [log] [blame]
/*!
* @license
* Copyright 2020 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/export class ExpandableRowsTableController{constructor(e,t){this.table=e;this.expandAll=t;this.toggles=e.querySelectorAll("[data-aria-controls]"),this.setAttributes(),this.attachEventListeners(),this.updateVisibleItems()}setAttributes(){for(const e of["data-aria-controls","data-aria-labelledby","data-id"])this.table.querySelectorAll(`[${e}]`).forEach(t=>{t.setAttribute(e.replace("data-",""),t.getAttribute(e)??""),t.removeAttribute(e)})}attachEventListeners(){this.toggles.forEach(e=>{e.addEventListener("click",t=>{this.handleToggleClick(t)})}),this.expandAll?.addEventListener("click",()=>{this.expandAllItems()}),document.addEventListener("keydown",e=>{(e.ctrlKey||e.metaKey)&&e.key==="f"&&this.expandAllItems()})}handleToggleClick(e){let t=e.currentTarget;t?.hasAttribute("aria-expanded")||(t=this.table.querySelector(`button[aria-controls="${t?.getAttribute("aria-controls")}"]`));const s=t?.getAttribute("aria-expanded")==="true";t?.setAttribute("aria-expanded",s?"false":"true"),e.stopPropagation(),this.updateVisibleItems()}expandAllItems(){this.table.querySelectorAll("[aria-expanded=false]").forEach(e=>e.setAttribute("aria-expanded","true")),this.updateVisibleItems()}updateVisibleItems(){this.toggles.forEach(e=>{const t=e?.getAttribute("aria-expanded")==="true";e?.getAttribute("aria-controls")?.trimEnd().split(" ")?.forEach(i=>{const a=document.getElementById(`${i}`);t?(a?.classList.add("visible"),a?.classList.remove("hidden")):(a?.classList.add("hidden"),a?.classList.remove("visible"))})})}}
//# sourceMappingURL=table.js.map