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.

113 lines (112 loc) 3.13 kB
/*! * NENT 2022 */ import { Component, Element, h, Prop, State } from '@stencil/core'; import { eventBus } from '../../services/actions'; import { ROUTE_EVENTS } from '../n-views/services/interfaces'; import { onRoutingChange, routingState, } from '../n-views/services/state'; /** * * @system routing */ export class ViewLinkBack { constructor() { this.route = null; } get parentView() { return this.el.closest('n-view'); } get parentViewPrompt() { return this.el.closest('n-view-prompt'); } async componentWillLoad() { if (routingState.router) { await this.setupRoute(); } else { const dispose = onRoutingChange('router', async () => { await this.setupRoute(); dispose(); }); } } async setupRoute() { if (this.parentViewPrompt) { this.setPage(this.parentViewPrompt.route); } else if (this.parentView) { this.setPage(this.parentView.route); } else { this.subscribe(); } } async setPage(route) { var _a; this.route = await route.getPreviousRoute(); this.title = await ((_a = this.route) === null || _a === void 0 ? void 0 : _a.resolvePageTitle()); } async subscribe() { var _a, _b; this.matchSubscription = eventBus.on(ROUTE_EVENTS.RouteMatchedExact, async ({ route }) => { await this.setPage(route); }); if ((_a = routingState.router) === null || _a === void 0 ? void 0 : _a.exactRoute) await this.setPage((_b = routingState.router) === null || _b === void 0 ? void 0 : _b.exactRoute); } render() { var _a; const text = this.text || this.title; return (h("n-view-link", { "link-class": this.linkClass, path: ((_a = this.route) === null || _a === void 0 ? void 0 : _a.path) || '', title: this.title, "active-class": "none" }, h("slot", { name: "start" }), text ? text : h("slot", null), h("slot", { name: "end" }))); } disconnectedCallback() { var _a; (_a = this.matchSubscription) === null || _a === void 0 ? void 0 : _a.call(this); } static get is() { return "n-view-link-back"; } static get styles() { return "n-view-link-back { display: inline-block; }"; } static get properties() { return { "text": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The link text" }, "attribute": "text", "reflect": false }, "linkClass": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The class to add to the anchor tag." }, "attribute": "link-class", "reflect": false } }; } static get states() { return { "route": {}, "title": {} }; } static get elementRef() { return "el"; } }