UNPKG

1.85 kBJavaScriptView Raw
1import{html,render as litRender}from"../lit-html/lit-html.js";export const LitElement=t=>class extends t{static get observedAttributes(){let t=[];for(const e in this.properties)this.properties[e].reflectToAttribute&&t.push(e);return t}constructor(){super(),this.__data={},this._methodsToCall={},this.attachShadow({mode:"open"})}connectedCallback(){const t=this.constructor.properties;this._wait=!0;for(let e in t)"object"==typeof t[e]?this._makeComplexGetterSetter(e,t[e]):this._makeGetterSetter(e,t[e]);delete this._wait,litRender(this.render(),this.shadowRoot),this.afterFirstRender&&this.afterFirstRender()}_makeGetterSetter(t,e){Object.defineProperty(this,t,{get(){return this.__data[t]},set(e){this.__data[t]=e,this._propertiesChanged(t,e)}}),this[t]=this.getAttribute(t)}_makeComplexGetterSetter(t,e){Object.defineProperty(this,t,{get(){return e.reflectToAttribute&&(e.type!==Object&&e.type!==Array||console.warn("Rich Data shouldn't be set as attribte!")),this.__data[t]},set(r){e.reflectToAttribute?(e.type!==Object&&e.type!==Array||console.warn("Rich Data shouldn't be set as attribte!"),this.setAttribute(t,r)):this.__data[t]=r,this._propertiesChanged(t,r)}}),e.observer&&(this[e.observer]?this._methodsToCall[t]=this[e.observer]:console.warn(`Method ${e.observer} not defined!`)),e.value&&("function"==typeof e.value?this[t]=e.value():this[t]=e.value)}_propertiesChanged(t,e){this._methodsToCall[t]&&this._methodsToCall[t](e),this._wait||litRender(this.render(),this.shadowRoot)}attributeChangedCallback(t,e,r){if(this[t]!==r){const{type:e}=this.constructor.properties[t];"Boolean"===e.name?this.__data[t]="false"!==r&&this.hasAttribute(t):this.__data[t]=e(r),this._propertiesChanged(t,r)}}render(){return html`Render Function not defined`}get $(){const t=this.shadowRoot.querySelectorAll("[id]"),e={};for(const r of t)e[r.id]=r;return e}};
\No newline at end of file