-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.min.js
7 lines (7 loc) · 3.17 KB
/
index.min.js
1
2
3
4
5
6
7
function o(h,t={}){let e=Object.keys(t),s=Object.values(t);try{return new Function(...e,`return \`${h}\`;`)(...s)}catch(i){throw new l(h,t,i)}}var l=class extends Error{constructor(t,e,s){super();this.name="TemplateError";let i=`
------------------
`;i+=`Template: \`${t}\``,i+=`
------------------
`,i+=`Tags: ${JSON.stringify(e,null,2)}`,i+=`
------------------
`,i+=s,this.message=i}};var d=class extends HTMLElement{static get observedAttributes(){return["src"]}attributeChangedCallback(t,e,s){!this.__initialized||e!==s&&(this[t]=s)}get src(){return this.getAttribute("src")}set src(t){this.setAttribute("src",t),this.setSrc(t)}get value(){return this.__data}set value(t){this.setValue(t)}constructor(){super();this.__initialized=!1,this.__theme=null,this.__data=null,this.__table=null,this.__column=null,this.__headers=null,this.__selected=null}async connectedCallback(){this.__table=document.createElement("table"),this.hasAttribute("theme")?(this.__theme=document.createElement("template"),this.__theme.innerHTML=await this.getTheme(),this.attachShadow({mode:"open"}),this.shadowRoot.appendChild(this.__theme.content.cloneNode(!0)),this.shadowRoot.appendChild(this.__table)):this.appendChild(this.__table),this.hasAttribute("src")&&this.setSrc(),this.__initialized=!0}async getTheme(){let t=this.getAttribute("theme"),e=await this.fetchTheme(t);return o(e)}async fetchTheme(t){let e=await fetch(t);if(e.status!==200)throw Error(`ERR ${e.status}: ${e.statusText}`);return e.text()}async setSrc(){this.hasAttribute("src")&&(this.__data=await this.fetchSrc(this.src),this.render())}async fetchSrc(t){let e=await fetch(t);if(e.status!==200)throw Error(`ERR ${e.status}: ${e.statusText}`);return e.json()}setValue(t){this.__data=t,this.render()}sortData(t){if(this.__column===null)return t;let e=this.__column,i=this.__direction?(r,n)=>r<n:(r,n)=>r>n;return t.sort((r,n)=>i(r[e],n[e])?-1:1)}headerClicked(t){this.__selected=t.target.cellIndex;let e=t.target.cellIndex;this.__direction=e!==this.__column?!0:!this.__direction,this.__column=e,this.render()}styleHeaders(){let t=this.__direction?"asc":"desc",e=this.__direction?"asc":"desc";for(let s of this.__headers.children)s.cellIndex===this.__selected?s.classList.contains(e)?s.classList.remove(e):s.classList.add(t):(s.classList.contains(t)&&s.classList.remove(t),s.classList.contains(e)&&s.classList.remove(e))}render(){let t=[...this.__data],e=document.createElement("table");this.__headers=document.createElement("tr"),this.__headers.addEventListener("click",n=>this.headerClicked(n)),t.shift().forEach(n=>{let a=document.createElement("th");a.innerText=n,this.__headers.appendChild(a)}),this.styleHeaders();let i=document.createElement("thead");i.appendChild(this.__headers),e.appendChild(i),t=this.sortData(t);let r=document.createElement("tbody");t.forEach(n=>{let a=document.createElement("tr");n.forEach(_=>{let c=document.createElement("td");c.innerText=_,a.appendChild(c)}),r.appendChild(a)}),e.appendChild(r),this.__theme?(this.shadowRoot.removeChild(this.__table),this.__table=e,this.shadowRoot.appendChild(this.__table)):(this.removeChild(this.__table),this.__table=e,this.appendChild(this.__table))}};customElements.define("wc-sortable-table",d);export{d as WCSortableTable};