-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.min.js
7 lines (7 loc) · 1.54 KB
/
index.min.js
1
2
3
4
5
6
7
function c(i,t={}){let e=Object.keys(t),r=Object.values(t);try{return new Function(...e,`return \`${i}\`;`)(...r)}catch(s){throw new n(i,t,s)}}var n=class extends Error{constructor(t,e,r){super();this.name="TemplateError";let s=`
------------------
`;s+=`Template: \`${t}\``,s+=`
------------------
`,s+=`Tags: ${JSON.stringify(e,null,2)}`,s+=`
------------------
`,s+=r,this.message=s}};var a=class extends HTMLElement{static get observedAttributes(){return["src","context"]}attributeChangedCallback(t,e,r){!this.__initialized||e!==r&&(this[t]=r)}get src(){return this.getAttribute("src")}set src(t){this.setAttribute("src",t),this.setSrc(),this.render()}get context(){return this.getAttribute("context")}set context(t){this.setAttribute("context",t),this.setContext(),this.render()}constructor(){super();this.__initialized=!1,this.__template="",this.__context={}}async connectedCallback(){this.hasAttribute("src")&&await this.setSrc(),this.hasAttribute("context")&&await this.setContext(),this.render(),this.__initialized=!0}async setSrc(){let t=this.getAttribute("src");this.__template=await this.fetchSrc(t)}async fetchSrc(t){let e=await fetch(t);if(e.status!==200)throw Error(`ERR ${e.status}: ${e.statusText}`);return e.text()}async setContext(){let t=this.getAttribute("context");this.__context=await this.fetchContext(t)}async fetchContext(t){let e=await fetch(t);if(e.status!==200)throw Error(`ERR ${e.status}: ${e.statusText}`);return e.json()}render(){this.innerHTML=c(this.__template,this.__context)}};customElements.define("wc-template",a);export{a as WCTemplate};