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AASA,EAAOC,cAGhBD,GAAUA,IAAW/E,EAAKgC,UAC1BhC,EAAKgC,SAASM,oBAAoB,WAAYtC,EAAKuC,KACnDwC,EAAOG,iBAAiB,WAAYlF,EAAKuC,MAG7CvC,EAAKgC,QAAU+C,EACf/E,EAAKiC,MAAU8C,GAAS9C,OAAUjC,CACtC,CAKAuC,MACItC,KAAKoB,cAAc,gBACvB,CA3QA8D,cACIC,MAAM1F,EAAe,CAAEmB,eAAe,IAEtC,IAAIb,EAAOC,KAEXD,EAAKuC,IAAMvC,EAAKuC,IAAI8C,KAAKrF,GACzBA,EAAKW,IAAWtB,EAAYiG,OAC5BtF,EAAK2C,IAAM3C,EAAKD,KACpB,EAuQCgD,OAAOwC,eAAeC,IAAI/F,IAC3BsD,OAAOwC,eAAeE,OAAOhG,EAAKC,E","sources":["dist/lithtml-parser/lithtml-parser.js"],"sourcesContent":["let { nothing, CustomHTMLElement, createTemplateFn, translation, utils } = lithtml;\r\nlet tag = 'lithtml-parser';\r\nlet version = '0.9.0';\r\n\r\n/**\r\n * Lithtml parser that takes a template and dynamically 'htmlifies' it.\r\n */\r\nclass LitHtmlParser extends CustomHTMLElement {\r\n static get tag() { return tag }\r\n static get version() { return version }\r\n\r\n /** Make sure changing the template, context, ... trigger a rerender */\r\n static get observedAttributes() { return ['template-ref', 'use-shadow-root', 'default-context', 'wait-for-context', 'default-template', 'context-ref', 'translation-ref'] }\r\n static get observedProperties() { return ['context', 'template', 'removeOnRendered', 'translation']; }\r\n\r\n /**\r\n * Constructor\r\n */\r\n constructor() {\r\n super(LitHtmlParser, { useShadowRoot: false });\r\n\r\n let self = this;\r\n\r\n self._ai = self._ai.bind(self);\r\n self._aj = translation.init();\r\n self._ak = self._al();\r\n }\r\n\r\n /**\r\n *\r\n * @returns {object} the object containing obeserved property BL\r\n */\r\n _al() {\r\n let self = this;\r\n\r\n return {\r\n defaultTemplate: (newValue) => !self.template && (self.template = newValue),\r\n defaultContext : (newValue) => !self.context && (self.context = newValue),\r\n template : (newValue) => newValue && (self._am = createTemplateFn(newValue.trim(), { thisContext: self, contextParams: ['context', '$', '$root', '$parent', '$this', 't']})),\r\n translation : (newValue) => newValue && (self._aj.setTranslation(newValue)),\r\n useShadowRoot : (newValue) => self.options.useShadowRoot = newValue,\r\n context : (newValue) => self.getAttribute('observed-properties')?.split(',').forEach (propName => utils.defineProperty(newValue, propName, newValue[propName], _ => self.requestRender('observedprop'))),\r\n templateRef : (newValue) => {\r\n if (!newValue) {\r\n return;\r\n }\r\n\r\n // If the template tag is defined before the lithtml-parser tag, then the element is found here...\r\n self._an(newValue);\r\n\r\n // .. but if defined later in the html DOM we need to do an async callback to retrieve the element\r\n !self.template && setZeroTimeout(() => self._an(newValue));\r\n },\r\n contextRef: (newValue) => {\r\n if (!newValue || self.context) {\r\n return;\r\n }\r\n\r\n // Check if the data is located in a script tag...\r\n if (newValue[0] === '#') {\r\n self._ao(newValue.substring(1), data => self.context = data);\r\n !self.context && setZeroTimeout(() => self._ao(newValue.substring(1), data => self.context = data));\r\n\r\n return;\r\n }\r\n // .. or inside a global object\r\n self._ap(newValue, data => self.context = data);\r\n !self.context && setZeroTimeout(() => !self.context && self._ap(newValue, data => self.context = data));\r\n },\r\n translationRef: (newValue) => {\r\n if (!newValue || self.translation) {\r\n return;\r\n }\r\n\r\n // Check if the translation is located in a script tag...\r\n if (newValue[0] === '#') {\r\n self._ao(newValue.substring(1), data => self.translation = data);\r\n !self.context && setZeroTimeout(() => self._ao(newValue.substring(1), data => self.translation = data));\r\n\r\n return;\r\n }\r\n // .. or inside a global object\r\n self._ap(newValue, data => self.translation = data);\r\n !self.translation && setZeroTimeout(() => self._ap(newValue, data => self.translation = data));\r\n }\r\n }\r\n }\r\n\r\n /** Called when destroy has been called on the element, so we do some cleanup */\r\n onDestroy () {\r\n let self = this;\r\n\r\n self._aj =\r\n self._am =\r\n self.$parent =\r\n self.$root = null;\r\n }\r\n\r\n /** Called each time the custom element is (re-)connected to the DOM */\r\n onConnected() {\r\n this._aq();\r\n// TODO: if nodeType===8 and .data ==='end', if next is 'start', then remove the nodes before (from initial 'start')\r\n// for(let e of this.childNodes)\r\n// console.log(this.id, e.nodeType, e.data)\r\n }\r\n\r\n /** Called each time the custom element is moved around in the DOM */\r\n onMoved() {\r\n this._aq();\r\n }\r\n\r\n /** Called when the custom element is disconnected from the DOM */\r\n onDisconnected() {\r\n let self = this;\r\n\r\n self.$parent?.removeEventListener('rendered', self._ai);\r\n self.$parent = null;\r\n self.$root = null;\r\n }\r\n\r\n /**\r\n * Called when an observed property (or attribute) changes value\r\n * @param {string} name the name of the property\r\n * @param {string} oldValue the old value of the property\r\n * @param {string} newValue the new value of the property\r\n */\r\n onPropertyChanged(name, oldValue, newValue) {\r\n this._ak[name]?.(newValue, oldValue);\r\n }\r\n\r\n//onFirstRender() {\r\n// let self = this;\r\n// console.log('onFirstRender', self.id)\r\n// if (!self.template) {\r\n// console.log('onFirstRender', self.id, !!self.template, self.childNodes.length)\r\n// self._ar();\r\n// console.log('onFirstRender--', self.id, !!self.template, self.childNodes.length)\r\n// }\r\n//}\r\n\r\n /** Render the template */\r\n render() {\r\n let self = this;\r\n\r\n if (!(self.template || self.templateRef)) {\r\n queueMicrotask(_ => {\r\n self._ar();\r\n\r\n if (self.template || self.templateRef) {\r\n return;\r\n }\r\n\r\n window.setZeroTimeout(() => self._ar(true));\r\n });\r\n\r\n return nothing;\r\n }\r\n\r\n if (!self._am) {\r\n return nothing;\r\n }\r\n\r\n if (self.waitForContext && !self.context) {\r\n return nothing;\r\n }\r\n\r\n let context = self.context || {};\r\n\r\n return self._am({context, $: context, $root: self.$root, $parent: self.$parent, $this: self, t: self._aj});\r\n }\r\n\r\n /**\r\n *\r\n * @param {string} templateRef the template id\r\n */\r\n _an(templateRef) {\r\n let el = document.getElementById(templateRef);\r\n\r\n if (el) {\r\n this.template = el.innerText;\r\n }\r\n }\r\n\r\n /**\r\n *\r\n * @param {string} elementId the element id\r\n * @param {function} cb the callback\r\n */\r\n _ao(elementId, cb) {\r\n let el = document.getElementById(elementId);\r\n\r\n if (!el) {\r\n return;\r\n }\r\n\r\n // Make sure that data attached to the script tag is singleton\r\n let data = el.jsonData || (el.jsonData = JSON.parse(el.innerText));\r\n\r\n data && cb(data);\r\n }\r\n\r\n /**\r\n *\r\n * @param {string} propertyName the name of the property\r\n * @param {function} cb the callback\r\n * @param {object} [obj=window] the object in which propertyName is to be searched for\r\n */\r\n _ap(propertyName, cb, obj = window) {\r\n let data = propertyName.split('.').reduce((obj, prop) => obj ? obj[prop] || null : null, obj);\r\n\r\n data && cb(data);\r\n }\r\n\r\n /**\r\n *\r\n * @param {boolean} async\r\n * @returns\r\n */\r\n _ar (async) {\r\n var self = this;\r\n\r\n if (self.template || self.templateRef) {\r\n return;\r\n }\r\n\r\n for (let node of self.childNodes) {\r\n if (node.localName === 'script') {\r\n node.remove();\r\n self.template = node.innerText;\r\n break;\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Called when a render has happend, we use it to trigger a rendered event that\r\n * any child lithtml-parser's can listen to and rerender themself.\r\n */\r\n onRendered() {\r\n let self = this;\r\n\r\n self.dispatchEvent(new CustomEvent('rendered', { bubbles: false }));\r\n\r\n // Support removing the element itself after beeing rendered, making sure the\r\n // elements content is still shown. May be used to make styling the resulting\r\n // html easier or to use a little lesss resources (since the custom element\r\n // is destoyed and frees context etc.)\r\n // TODO: Take defaultContext into consideration? That is, wait to remove until self.context !== self.defaultContext? The same with default template?\r\n if ((self.$attribs.removeOnRendered || self.removeOnRendered) && self.context && self._am) {\r\n queueMicrotask(() => {\r\n self.replaceWith(...(self.shadowRoot || self).childNodes);\r\n self.destroy();\r\n });\r\n }\r\n }\r\n\r\n /**\r\n * Finds the parent and root parsers\r\n */\r\n _aq() {\r\n let self = this;\r\n let parent = self.parentElement;\r\n\r\n while (parent != null) {\r\n if (parent.localName.startsWith('lithtml-parser')) {\r\n break;\r\n }\r\n\r\n parent = parent.parentElement;\r\n }\r\n\r\n if (parent && parent !== self.$parent) {\r\n self.$parent?.removeEventListener('rendered', self._ai);\r\n parent.addEventListener('rendered', self._ai);\r\n }\r\n\r\n self.$parent = parent;\r\n self.$root = (parent?.$root) || self;\r\n }\r\n\r\n /**\r\n * Event called each time the parent lithtml-parser has been rendered\r\n */\r\n _ai() {\r\n this.requestRender('parentUpdated');\r\n }\r\n}\r\n\r\n// Define the new web component\r\nif (!window.customElements.get(tag)) {\r\n window.customElements.define(tag, LitHtmlParser);\r\n}\r\n\r\n// TODO: -on onDisconnect set _as to null and reattach on onConnect"],"names":["nothing","$87255d4a463c5829$var$nothing","CustomHTMLElement","$87255d4a463c5829$var$CustomHTMLElement","createTemplateFn","$87255d4a463c5829$var$createTemplateFn","translation","$87255d4a463c5829$var$translation","utils","$87255d4a463c5829$var$utils","lithtml","$87255d4a463c5829$var$tag","$87255d4a463c5829$var$LitHtmlParser","tag","version","observedAttributes","observedProperties","_al","self","this","defaultTemplate","newValue","template","defaultContext","context","_am","trim","thisContext","contextParams","_aj","setTranslation","useShadowRoot","options","getAttribute","split","forEach","propName","defineProperty","_","requestRender","templateRef","_an","setZeroTimeout","contextRef","_ao","substring","data","_ap","translationRef","onDestroy","$parent","$root","onConnected","_aq","onMoved","onDisconnected","removeEventListener","_ai","onPropertyChanged","name","oldValue","_ak","render","queueMicrotask","_ar","window","waitForContext","$","$this","t","el","document","getElementById","innerText","elementId","cb","jsonData","JSON","parse","propertyName","obj","reduce","prop","async","node","childNodes","localName","remove","onRendered","dispatchEvent","CustomEvent","bubbles","$attribs","removeOnRendered","replaceWith","shadowRoot","destroy","parent","parentElement","startsWith","addEventListener","constructor","super","bind","init","customElements","get","define"],"version":3,"file":"lithtml-parser.js.map"}