blob: e3a45fa5fe81d220305c8fdec3f983147d6adf09 [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.
*/class r{constructor(t,e){this.table=t;this.toggleAll=e;this.expandAllItems=()=>{this.toggles.map(t=>t.setAttribute("aria-expanded","true")),this.update()};this.collapseAllItems=()=>{this.toggles.map(t=>t.setAttribute("aria-expanded","false")),this.update()};this.update=()=>{this.updateVisibleItems(),setTimeout(()=>this.updateGlobalToggle())};this.rows=Array.from(t.querySelectorAll("[data-aria-controls]")),this.toggles=Array.from(this.table.querySelectorAll("[aria-expanded]")),this.setAttributes(),this.attachEventListeners(),this.update()}setAttributes(){for(const t of["data-aria-controls","data-aria-labelledby","data-id"])this.table.querySelectorAll(`[${t}]`).forEach(e=>{var a;e.setAttribute(t.replace("data-",""),(a=e.getAttribute(t))!=null?a:""),e.removeAttribute(t)})}attachEventListeners(){var t;this.rows.forEach(e=>{e.addEventListener("click",a=>{this.handleToggleClick(a)})}),(t=this.toggleAll)==null||t.addEventListener("click",()=>{this.expandAllItems()}),document.addEventListener("keydown",e=>{(e.ctrlKey||e.metaKey)&&e.key==="f"&&this.expandAllItems()})}handleToggleClick(t){let e=t.currentTarget;(e==null?void 0:e.hasAttribute("aria-expanded"))||(e=this.table.querySelector(`button[aria-controls="${e==null?void 0:e.getAttribute("aria-controls")}"]`));const a=(e==null?void 0:e.getAttribute("aria-expanded"))==="true";e==null||e.setAttribute("aria-expanded",a?"false":"true"),t.stopPropagation(),this.update()}updateVisibleItems(){this.rows.map(t=>{var s;const e=(t==null?void 0:t.getAttribute("aria-expanded"))==="true",a=(s=t==null?void 0:t.getAttribute("aria-controls"))==null?void 0:s.trimEnd().split(" ");a==null||a.map(i=>{const l=document.getElementById(`${i}`);e?(l==null||l.classList.add("visible"),l==null||l.classList.remove("hidden")):(l==null||l.classList.add("hidden"),l==null||l.classList.remove("visible"))})})}updateGlobalToggle(){if(!this.toggleAll)return;this.rows.some(e=>e.hasAttribute("aria-expanded"))&&(this.toggleAll.style.display="block"),this.toggles.some(e=>e.getAttribute("aria-expanded")==="false")?(this.toggleAll.innerText="Expand all",this.toggleAll.onclick=this.expandAllItems):(this.toggleAll.innerText="Collapse all",this.toggleAll.onclick=this.collapseAllItems)}}export{r as ExpandableRowsTableController};
//# sourceMappingURL=table.js.map