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 3.87 kB
/*! * NENT 2022 */ import{r as t,h as s,H as e,a as i}from"./index-916ca544.js";import{r as n}from"./elements-1b845a48.js";import{D as r}from"./interfaces-8c5cd1b8.js";import{f as a}from"./jsonata.worker-8bcb5e74.js";import{resolveTokens as o,hasToken as h}from"./tokens-78f8cdbe.js";import{R as c}from"./interfaces-3b78db83.js";import{s as l}from"./state-adf07580.js";import{f as m,b as p}from"./logging-5a93c8af.js";import{C as f}from"./state-subscriber-eb44164a.js";import{a as d}from"./state-27a8a5bc.js";import{v as u}from"./values-ddfac998.js";import"./expressions-2c27c47c.js";import"./factory-acbf0d3d.js";import"./index-f7016b94.js";import"./index-4bfabbbd.js";import"./promises-584c4ece.js";import"./strings-47d55561.js";const b=class{constructor(s){t(this,s),this.dynamicContent=null,this.deferLoad=!1,this.debug=!1,this.noCache=!1}get childTemplate(){return this.el.querySelector("template")}get childScript(){return this.el.querySelector("script")}componentWillLoad(){m(this.debug,"n-content-repeat: loading"),this.dataSubscription=new f(this,"dataEnabled",r.DataChanged),this.routeSubscription=new f(this,"routingEnabled",c.RouteChanged),null===this.childTemplate?p(this.debug,"n-content-repeat: missing child <template> tag"):this.innerTemplate=this.childTemplate.innerHTML,this.contentKey="data-content"}async componentWillRender(){var t;if(!this.innerTemplate)return;if(this.dynamicContent&&!this.noCache)return void(d.elementsEnabled&&n(this.el));const s=this.el.querySelector(`.${this.contentKey}`);null==s||s.remove();const e=await this.resolveItems(),i=await this.resolveHtml(e);i&&(this.dynamicContent=this.el.ownerDocument.createElement("div"),this.dynamicContent.className=this.contentKey,this.dynamicContent.innerHTML=i,d.elementsEnabled&&n(this.dynamicContent),this.dynamicContent.innerHTML=i,(null==l?void 0:l.router)&&(null===(t=l.router)||void 0===t||t.captureInnerLinks(this.dynamicContent)),this.el.append(this.dynamicContent))}async resolveHtml(t){m(this.debug,"n-content-repeat: resolving html");let s=!this.deferLoad;if(s&&this.when&&d.dataEnabled){const{evaluatePredicate:t}=await import("./expressions-2c27c47c.js").then((function(t){return t.f}));s=await t(this.when)}if(!s)return null;if(this.innerTemplate){let s="";return await t.reduce(((t,e)=>t.then((async()=>o(this.innerTemplate.slice(),!1,e).then((t=>(s+=t,s)))))),Promise.resolve())}return null}async resolveItems(){var t;let s=[];if(this.items)s=await this.resolveItemsExpression();else if(this.childScript)try{let e=(null===(t=this.childScript.textContent)||void 0===t?void 0:t.replace("\n",""))||"";e=d.dataEnabled&&h(e)?await o(e,!0):e,s=u(JSON.parse(e||"[]"))}catch(t){p(this.debug,`n-content-repeat: unable to deserialize JSON: ${t}`)}else this.itemsSrc?s=await this.fetchJson():p(this.debug,"n-content-repeat: you must include at least one of the following: items, json-src or a <script> element with a JSON array.");if(this.filter){let t=this.filter.slice();h(t)&&(t=await o(t)),m(this.debug,`n-content-repeat: filtering: ${t}`),s=u(await a(t,s))}return s}async fetchJson(){try{m(this.debug,`n-content-repeat: fetching items from ${this.itemsSrc}`);const t=await window.fetch(this.itemsSrc);if(200===t.status){const s=await t.json();return u(s)}p(this.debug,`n-content-repeat: Unable to parse response from ${this.itemsSrc}`)}catch(t){p(this.debug,`n-content-repeat: Unable to parse response from ${this.itemsSrc}: ${t}`)}return[]}async resolveItemsExpression(){let t=[];try{let s=this.items;s&&h(s)&&(s=await o(s),m(this.debug,`n-content-repeat: items resolved to ${s}`)),t=s?JSON.parse(s):[]}catch(t){p(this.debug,`n-content-repeat: unable to deserialize JSON: ${t}`)}return t}render(){return s(e,null,s("slot",null))}disconnectedCallback(){this.dataSubscription.destroy(),this.routeSubscription.destroy()}get el(){return i(this)}};export{b as n_content_repeat}