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.

130 lines (125 loc) 4.08 kB
/*! * NENT 2022 */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const index = require('./index-1829aebc.js'); const index$1 = require('./index-637e8c28.js'); const interfaces = require('./interfaces-1da33056.js'); const state = require('./state-246c735d.js'); require('./index-96f3ab3f.js'); const ViewLinkList = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.route = null; this.match = null; this.routes = []; /** * The display mode for which routes to display. */ this.mode = 'parents'; /** * The active-class to use with the n-view-link elements. */ this.activeClass = 'active'; /** * The class to add to the anchor tag. */ this.linkClass = ''; /** * The list-class to use with the UL tag */ this.listClass = ''; /** * The list-item-class to use with the li tag */ this.itemClass = ''; /** * Specify if the '/' route should * be skipped in the list. */ this.excludeRoot = false; } get parentView() { return this.el.closest('n-view'); } get parentViewPrompt() { return this.el.closest('n-view-prompt'); } componentWillLoad() { if (state.state.router) { this.setupRoute(); } else { const dispose = state.onChange('router', () => { this.setupRoute(); dispose(); }); } } get inView() { return this.parentView != null || this.parentViewPrompt != null; } setupRoute() { var _a, _b; if (this.parentViewPrompt) { this.route = ((_a = this.parentViewPrompt) === null || _a === void 0 ? void 0 : _a.route) || null; } else if (this.parentView) { this.route = ((_b = this.parentView) === null || _b === void 0 ? void 0 : _b.route) || null; } if (this.route == null) this.subscribe(); } subscribe() { var _a, _b, _c; this.matchSubscription = index$1.eventBus.on(interfaces.ROUTE_EVENTS.RouteMatchedExact, ({ route, match }) => { this.route = route; this.match = match; if (this.inView) { this.matchSubscription.call(this); } }); this.route = ((_a = state.state.router) === null || _a === void 0 ? void 0 : _a.exactRoute) || null; this.match = ((_c = (_b = state.state.router) === null || _b === void 0 ? void 0 : _b.exactRoute) === null || _c === void 0 ? void 0 : _c.match) || null; } async componentWillRender() { let routes = await this.getRoutes(); if (this.mode == 'parents' && (routes === null || routes === void 0 ? void 0 : routes.length) && this.excludeRoot) routes.shift(); this.routes = routes || []; } async getRoutes() { var _a, _b, _c; switch (this.mode) { case 'parents': return await ((_a = this.route) === null || _a === void 0 ? void 0 : _a.getParentRoutes()); case 'siblings': return await ((_b = this.route) === null || _b === void 0 ? void 0 : _b.getSiblingRoutes()); case 'children': return await ((_c = this.route) === null || _c === void 0 ? void 0 : _c.getChildRoutes()); } } getUrl(route) { var _a, _b; let url = ((_a = route.match) === null || _a === void 0 ? void 0 : _a.url) || route.path; if (this.match) { Object.keys((_b = this.match) === null || _b === void 0 ? void 0 : _b.params).forEach(param => { url = url.replace(`:${param}`, this.match.params[param]); }); } return url; } render() { var _a; return (index.h("ul", { class: this.listClass }, (_a = this.routes) === null || _a === void 0 ? void 0 : _a.map((r) => [ index.h("li", { class: this.itemClass }, index.h("n-view-link", { path: this.getUrl(r.route), exact: true, "link-class": this.linkClass, "active-class": this.activeClass }, r.title)), ]))); } disconnectedCallback() { var _a; (_a = this.matchSubscription) === null || _a === void 0 ? void 0 : _a.call(this); } get el() { return index.getElement(this); } }; exports.n_view_link_list = ViewLinkList;