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.42 kB
/*! * NENT 2022 */ import{r as i,h as t,a as s}from"./index-916ca544.js";import{e as l}from"./index-f7016b94.js";import{R as n}from"./interfaces-3b78db83.js";import{s as e,o as h}from"./state-adf07580.js";import"./index-4bfabbbd.js";const r=class{constructor(t){i(this,t),this.route=null,this.match=null,this.routes=[],this.mode="parents",this.activeClass="active",this.linkClass="",this.listClass="",this.itemClass="",this.excludeRoot=!1}get parentView(){return this.el.closest("n-view")}get parentViewPrompt(){return this.el.closest("n-view-prompt")}componentWillLoad(){if(e.router)this.setupRoute();else{const i=h("router",(()=>{this.setupRoute(),i()}))}}get inView(){return null!=this.parentView||null!=this.parentViewPrompt}setupRoute(){var i,t;this.parentViewPrompt?this.route=(null===(i=this.parentViewPrompt)||void 0===i?void 0:i.route)||null:this.parentView&&(this.route=(null===(t=this.parentView)||void 0===t?void 0:t.route)||null),null==this.route&&this.subscribe()}subscribe(){var i,t,s;this.matchSubscription=l.on(n.RouteMatchedExact,(({route:i,match:t})=>{this.route=i,this.match=t,this.inView&&this.matchSubscription.call(this)})),this.route=(null===(i=e.router)||void 0===i?void 0:i.exactRoute)||null,this.match=(null===(s=null===(t=e.router)||void 0===t?void 0:t.exactRoute)||void 0===s?void 0:s.match)||null}async componentWillRender(){let i=await this.getRoutes();"parents"==this.mode&&(null==i?void 0:i.length)&&this.excludeRoot&&i.shift(),this.routes=i||[]}async getRoutes(){var i,t,s;switch(this.mode){case"parents":return await(null===(i=this.route)||void 0===i?void 0:i.getParentRoutes());case"siblings":return await(null===(t=this.route)||void 0===t?void 0:t.getSiblingRoutes());case"children":return await(null===(s=this.route)||void 0===s?void 0:s.getChildRoutes())}}getUrl(i){var t,s;let l=(null===(t=i.match)||void 0===t?void 0:t.url)||i.path;return this.match&&Object.keys(null===(s=this.match)||void 0===s?void 0:s.params).forEach((i=>{l=l.replace(`:${i}`,this.match.params[i])})),l}render(){var i;return t("ul",{class:this.listClass},null===(i=this.routes)||void 0===i?void 0:i.map((i=>[t("li",{class:this.itemClass},t("n-view-link",{path:this.getUrl(i.route),exact:!0,"link-class":this.linkClass,"active-class":this.activeClass},i.title))])))}disconnectedCallback(){var i;null===(i=this.matchSubscription)||void 0===i||i.call(this)}get el(){return s(this)}};export{r as n_view_link_list}