UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

4 lines 2.91 kB
/*! * NENT 2022 */ import{r as t,a as s}from"./index-916ca544.js";import{w as i,f as e}from"./logging-5a93c8af.js";import{a}from"./state-27a8a5bc.js";import{a as r,r as n}from"./remote-0f753d9a.js";import{r as o}from"./elements-1b845a48.js";import{D as c}from"./interfaces-8c5cd1b8.js";import{f as h}from"./jsonata.worker-8bcb5e74.js";import{resolveTokens as l}from"./tokens-78f8cdbe.js";import{d as m}from"./utils-337f03b7.js";import{R as d}from"./interfaces-3b78db83.js";import{s as p}from"./state-adf07580.js";import{C as f}from"./state-subscriber-eb44164a.js";import"./index-4bfabbbd.js";import"./expressions-2c27c47c.js";import"./factory-acbf0d3d.js";import"./index-f7016b94.js";import"./values-ddfac998.js";import"./promises-584c4ece.js";import"./strings-47d55561.js";const u=class{constructor(s){t(this,s),this.contentClass="dynamic",this.contentElement=null,this.deferLoad=!1,this.debug=!1,this.noCache=!1,this.mode="cors",this.graphql=!1}get childTemplate(){return this.el.querySelector("template")}get childScript(){return this.el.querySelector("script")}componentWillLoad(){this.dataSubscription=new f(this,"dataEnabled",c.DataChanged),this.routeSubscription=new f(this,"routingEnabled",d.RouteChanged),null!==this.childTemplate&&(this.innerTemplate=this.childTemplate.innerHTML)}async componentWillRender(){let t=!this.deferLoad;if(t&&this.when&&a.dataEnabled){const{evaluatePredicate:s}=await import("./expressions-2c27c47c.js").then((function(t){return t.f}));t=await s(this.when)}this.contentElement=t?await this.resolveContentElement():null}async resolveContentElement(){var t;const s=await this.getContent();if(null==s)return null;const i=document.createElement(this.innerTemplate?"div":"span");return i.innerHTML=s,i.className=this.contentClass,a.elementsEnabled&&o(i),p.router&&(null===(t=p.router)||void 0===t||t.captureInnerLinks(i)),i}async getContent(){let t=null;const s=await this.resolveData();return this.innerTemplate?t=await l(this.innerTemplate,!1,s):this.text&&(t=await l(this.text,!1,s)),t}async resolveData(){var t;let s={};if(this.el.dataset&&Object.assign(s,this.el.dataset),null!==this.childScript&&!this.graphql)try{const t=JSON.parse(this.childScript.textContent||"");s=Object.assign(s,t)}catch(t){i(`n-content-template: unable to deserialize JSON: ${t}`)}if(this.src)try{s=this.graphql?await r(window,this.src,this.mode,"POST",JSON.stringify({query:m((null===(t=this.childScript)||void 0===t?void 0:t.textContent)||"")})):await r(window,this.src,this.mode),this.filter&&(e(this.debug,`n-content-template: filtering: ${this.filter}`),s=await h(this.filter,s))}catch(t){i(`n-content-template: unable to fetch and filter data data ${t}`)}return s}render(){return n(this.el,`.${this.contentClass}`,this.contentElement),null}disconnectedCallback(){this.dataSubscription.destroy(),this.routeSubscription.destroy()}get el(){return s(this)}};export{u as n_content_template}