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AAE;YAClB,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC;SACnB;QAED,OAAO,GAAG,CAAC;IACf,CAAC;CACJ,CAAA","sourcesContent":["import { TemplateResult, TemplateFactory } from '../lit-html/lit-html.js';\n\nexport interface Properties {\n [propName: string]: PropConfig | Type;\n}\n\nexport type Type = typeof String | typeof Number | typeof Boolean | typeof Array | typeof Object | typeof Date;\n\nexport interface PropConfig {\n type: Type;\n reflectToAttribute?: boolean;\n value?: any;\n observer?: string;\n notify?: boolean;\n}\n\nexport interface Data {\n [propName: string]: any;\n}\n\nexport interface MethodsToCall {\n [propName: string]: (newValue: any, oldValue: any) => any;\n}\n\nexport interface HTMLCollectionByID {\n [id: string]: HTMLElement | Element;\n}\n\nexport interface LitEventInit extends EventInit {\n composed: boolean;\n}\n\n/**\n * Coverts a camelCase string to kebab-case.\n *\n * @export\n * @param {string} str The camelCaseString\n * @returns {string} The kebab-version of the string\n */\nexport function camelCaseToKebab(str: string): string {\n return str.replace(/([A-Z])/g, '-$1').toLowerCase();\n}\n\n/**\n * \n * @param {string} prop The name of the property to create\n * @param {string} attr The name of the attribute\n * @param {any} context The context of the element\n * @param {PropConfig} info The configuration of the property\n */\nexport function createProperty(prop: string, attr: string, context: any, info: PropConfig) {\n // get value that was already set on the property (if any)\n const setVal = context[prop];\n Object.defineProperty(context, prop, {\n get() {\n return context.__data[prop];\n },\n async set(val: any) {\n const resolved: any = (val != null && val instanceof Promise\n ? await val\n : val);\n context.setProperty(prop, resolved);\n }\n });\n\n if (info.reflectToAttribute &&\n (info.type === Object || info.type === Array)) {\n console.warn('Rich Data shouldn\\'t be set as attribte!')\n }\n if (info.observer) {\n if (context[info.observer]) {\n // Establish the property-change observer\n context.__methodsToCall[prop] = context[info.observer].bind(context);\n } else {\n console.warn(`Method ${info.observer} not defined!`);\n }\n }\n // Check, if the property was already set, set it accordingly\n if (setVal) {\n context[prop] = setVal;\n return;\n }\n if (info.value !== undefined) {\n // Initialize using the included value and the new setter()\n context[prop] = (typeof (info.value) === 'function'\n ? info.value.call(context)\n : info.value);\n\n }\n}\n\n/**\n * Returns a class with the Lit-Element features, that extends `superclass`.\n * @param superclass\n */\nexport const LitLite =\n (superclass = HTMLElement,\n html: (strings: TemplateStringsArray, ...values: any[]) => TemplateResult,\n renderFunction: (result: TemplateResult, container: Element | DocumentFragment, templateFactory?: TemplateFactory) => void) =>\n class extends superclass {\n static properties: Properties;\n __renderCallbacks: Set<any> = new Set();\n __pendingRender: boolean = false;\n __data: Data = {};\n __methodsToCall: MethodsToCall = {};\n __wait: any;\n __firstRender: boolean = false;\n afterRender?: (isFirst: boolean) => void;\n shadowRoot!: ShadowRoot;\n __propAttr: Map<string, string> = new Map(); // propertyName -> attribute-name\n __attrProp: Map<string, string> = new Map(); // attribute-name -> propertyName\n [key: string]: any\n\n static get observedAttributes(): Array<string> {\n let attrs: Array<string> = [];\n for (const prop in this.properties) {\n if ((<PropConfig>this.properties[prop]).reflectToAttribute) {\n attrs.push(camelCaseToKebab(prop));\n }\n }\n return attrs;\n }\n\n constructor() {\n super();\n this.attachShadow({ mode: 'open' });\n\n for (let prop in (this.constructor as any).properties) {\n const attr = camelCaseToKebab(prop);\n this.__propAttr.set(prop, attr);\n this.__attrProp.set(attr, prop);\n }\n }\n\n connectedCallback() {\n const props = (this.constructor as any).properties;\n this.__wait = true;\n for (let prop in props) {\n if (typeof props[prop] === 'function')\n props[prop] = { type: props[prop] };\n this.__makeGetterSetter(prop, props[prop])\n }\n delete this.__wait;\n\n this.__firstRender = true;\n\n if(this.connected)\n this.connected.call(this);\n\n /* Perform the first render after connection immediately\n * without the delay of refresh()\n */\n this.postponedRender();\n }\n\n disconnectedCallback() {\n if(this.disconnected)\n this.disconnected.call(this);\n }\n\n /**\n * Creates the Propertyaccessors for the defined properties of the Element.\n * @param {string} prop\n * @param {PropConfig} info\n */\n __makeGetterSetter(prop: string, info: PropConfig) {\n const attr = <string>this.__propAttr.get(prop);\n createProperty(prop, attr, this, info);\n }\n\n /**\n * Gets called when the properties change and the Element should rerender.\n *\n * @param {string} prop\n * @param {any} newVal\n */\n __propertiesChanged(prop: string, newVal: any) {\n if (this.__data[prop] !== newVal) {\n const oldVal = this.__data[prop];\n let doRefresh = true;\n this.__data[prop] = newVal;\n\n if (this.__methodsToCall[prop]) {\n if (this.__methodsToCall[prop](newVal, oldVal) === false) {\n doRefresh = false;\n }\n }\n\n if (doRefresh) {\n this.refresh();\n }\n }\n }\n\n setProperty(prop: string, newVal: any) {\n const info = (this.constructor as any).properties[prop];\n const attr = this.__propAttr.get(prop);\n if (info.reflectToAttribute) {\n /* Set the new value by setting the observed attribute.\n * This will trigger attributeChangedCallback() which will\n * convert the attribute data to a property,\n * (this.__data[prop]) and trigger __propertiesChanged().\n */\n this.setAttribute(attr!, newVal);\n \n } else {\n /* Set the property directly and trigger\n * __propertiesChanged()\n */\n this.__propertiesChanged(prop, newVal);\n }\n if (info.notify) {\n this.dispatchEvent(new CustomEvent(`${attr}-changed`, <LitEventInit>{\n bubbles: true,\n composed: true,\n detail: newVal\n }));\n }\n }\n\n /**\n * Gets called when an observed attribute changes. Calls `__propertiesChanged`\n *\n * @param {string} prop\n * @param {any} old\n * @param {any} val\n */\n attributeChangedCallback(attr: string, old: any, val: any) {\n if (old === val) return;\n const prop = <string>this.__attrProp.get(attr);\n if (this.__data[prop] !== val) {\n const { type } = (this.constructor as any).properties[prop];\n let newVal = val;\n\n switch (type.name) {\n case 'Boolean':\n /* Ensure attribute values the indicate that absense of the\n * attribute actually cause the attribute to be absent.\n */\n if (val === 'false' || val === 'null' ||\n val === 'undefined' ||\n val === false || val === null) {\n this.removeAttribute(attr);\n newVal = false;\n } else {\n newVal = this.hasAttribute(attr);\n if (newVal)\n this.setAttribute(attr, '');\n }\n break;\n\n case 'String':\n /* If a String value is falsey or the explicit 'null'\n * or 'undefined' string, ensure that the attribute is\n * removed.\n */\n if (!val || val === 'null' || val === 'undefined') {\n this.removeAttribute(attr);\n newVal = '';\n\n } else {\n newVal = type(val);\n }\n break;\n\n default:\n newVal = type(val);\n break;\n }\n\n /* Pass along the new, more concrete *property* value instead of\n * the fuzzy attribute value.\n */\n this.__propertiesChanged(prop, newVal);\n }\n }\n\n /**\n * Handle a postponed render.\n * @method postponedRender\n *\n * @return void\n */\n postponedRender() {\n renderFunction(this.render({...this.__data}), this.shadowRoot)\n\n for (let callback of this.__renderCallbacks) {\n callback();\n }\n this.__renderCallbacks.clear();\n\n if (this.afterRender) {\n this.afterRender(this._firstRender);\n this._firstRender = false;\n }\n }\n\n /**\n * Refresh this element, re-rendering.\n * @method refresh\n * @param {function} callback\n *\n * @return void\n */\n async refresh(callback?: () => any) {\n if (this._wait === true) { return }\n\n if (callback != null) {\n // Queue this render/refresh callback\n this.__renderCallbacks.add(callback);\n }\n\n if (!this.__pendingRender) {\n this.__pendingRender = true;\n /* Schedule the following as a microtask, which runs before\n * requestAnimationFrame. Any additional refresh() calls\n * will have any callback queued but otherwise will be\n * ignored.\n *\n * https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/\n */\n this.__pendingRender = await false;\n this.postponedRender();\n }\n }\n\n /**\n * Returns what lit-html should render.\n *\n * @returns\n */\n render(data?: object): TemplateResult {\n return html``;\n }\n\n /**\n * Gets all children with ids.\n *\n * @readonly\n */\n get $(): HTMLCollectionByID {\n const arr = this.shadowRoot.querySelectorAll('[id]');\n const obj: HTMLCollectionByID = {};\n for (const el of arr) {\n obj[el.id] = el;\n }\n\n return obj;\n }\n }\n"]}
\No newline at end of file