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.

102 lines (99 loc) 3.72 kB
/*! * NENT 2022 */ import { r as registerInstance, h, a as getElement, H as Host } from './index-916ca544.js'; import { b as resolveRemoteContent, r as replaceHtmlInElement } from './remote-0f753d9a.js'; import { r as resolveChildElementXAttributes } from './elements-1b845a48.js'; import { D as DATA_EVENTS } from './interfaces-8c5cd1b8.js'; import { f as filterData } from './jsonata.worker-8bcb5e74.js'; import { R as ROUTE_EVENTS } from './interfaces-3b78db83.js'; import { s as state$1 } from './state-adf07580.js'; import { C as CommonStateSubscriber } from './state-subscriber-eb44164a.js'; import { a as state } from './state-27a8a5bc.js'; import { f as debugIf, w as warn } from './logging-5a93c8af.js'; import './tokens-78f8cdbe.js'; import './factory-acbf0d3d.js'; import './index-f7016b94.js'; import './index-4bfabbbd.js'; import './values-ddfac998.js'; import './promises-584c4ece.js'; import './expressions-2c27c47c.js'; import './strings-47d55561.js'; const ContentInclude = class { constructor(hostRef) { registerInstance(this, hostRef); this.contentClass = 'remote-content'; this.contentElement = null; /** * Cross Origin Mode */ this.mode = 'cors'; /** * Before rendering HTML, replace any data-tokens with their * resolved values. This also commands this element to * re-render it's HTML for data-changes. This can affect * performance. */ this.resolveTokens = false; /** * If set, disables auto-rendering of this instance. * To fetch the contents change to false or remove * attribute. */ this.deferLoad = false; } componentWillLoad() { if (this.resolveTokens || this.when != undefined) { this.dataSubscription = new CommonStateSubscriber(this, 'dataEnabled', DATA_EVENTS.DataChanged); this.routeSubscription = new CommonStateSubscriber(this, 'routingEnabled', ROUTE_EVENTS.RouteChanged); } } async componentWillRender() { let shouldRender = !this.deferLoad; if (state.dataEnabled && this.when) { const { evaluatePredicate } = await import('./expressions-2c27c47c.js').then(function (n) { return n.f; }); shouldRender = await evaluatePredicate(this.when); } if (shouldRender) this.contentElement = this.src ? await this.resolveContentElement() : null; else if (this.resolveTokens) this.contentElement = null; } async resolveContentElement() { var _a; try { let content = await resolveRemoteContent(window, this.src, this.mode, this.resolveTokens); if (content && this.json) { debugIf(state.debug, `n-content-include: filtering: ${this.json}`); const data = JSON.parse(content); content = await filterData(this.json, data); } if (content == null) return null; const div = document.createElement('div'); div.innerHTML = content; div.className = this.contentClass; if (state.elementsEnabled) resolveChildElementXAttributes(div); (_a = state$1.router) === null || _a === void 0 ? void 0 : _a.captureInnerLinks(div); return div; } catch (_b) { warn(`n-content: unable to retrieve from ${this.src}`); return null; } } render() { replaceHtmlInElement(this.el, `.${this.contentClass}`, this.contentElement); return h(Host, { hidden: this.contentElement == null }); } disconnectedCallback() { var _a, _b; (_a = this.dataSubscription) === null || _a === void 0 ? void 0 : _a.destroy(); (_b = this.routeSubscription) === null || _b === void 0 ? void 0 : _b.destroy(); } get el() { return getElement(this); } }; export { ContentInclude as n_content_include };