UNPKG

@luigi-project/client

Version:

Javascript library supporting consumers of the Luigi framework

1 lines 1.68 kB
export class LuigiElement extends HTMLElement{constructor(t){super();const e=!!t&&t.openShadow;this._shadowRoot=this.attachShadow({mode:e?"open":"closed",delegatesFocus:!1}),this.__initialized=!1,this.deferLuigiClientWCInit=!!t&&t.deferLuigiClientWCInit}__postProcess(t,e,i){this.LuigiClient=e,this.context=t;const o=document.createElement("template");o.innerHTML=this.render(t);const s=()=>{if(!this.__initialized){this._shadowRoot.appendChild(o.content.cloneNode(!0)),Reflect.ownKeys(Reflect.getPrototypeOf(this)).forEach((t=>{t.startsWith("$_")&&(this._shadowRoot[t]=this[t].bind(this))}));const e=this._shadowRoot.querySelectorAll("[id]");e&&e.forEach((t=>{this["$"+t.getAttribute("id")]=t})),this.afterInit(t),this.__initialized=!0}};if(this.luigiConfig&&this.luigiConfig.styleSources&&this.luigiConfig.styleSources.length>0){let t=this.luigiConfig.styleSources.length;const e=this.luigiConfig.loadStylesSync,o=()=>{t--,t<1&&s()};this.luigiConfig.styleSources.forEach(((t,s)=>{const n=document.createElement("link");n.setAttribute("rel","stylesheet"),n.setAttribute("href",i+t),e&&(n.addEventListener("load",o),n.addEventListener("error",o)),this._shadowRoot.appendChild(n)})),e||s()}else s()}afterInit(t){}render(t){return""}update(){}onContextUpdate(t){}querySelector(t){return this._shadowRoot.querySelector(t)}set context(t){this.__lui_ctx=t,this.__initialized&&(this.onContextUpdate(t),this.attributeChangedCallback())}get context(){return this.__lui_ctx}attributeChangedCallback(t,e,i){this.update()}}export function html(t,...e){let i="";return t.forEach(((t,o)=>{i+=t,o<e.length&&void 0!==e[o]&&null!==e[o]&&(i+=e[o])})),i.replace(/\$\_/gi,"this.getRootNode().$_")}