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 22.9 kB
/*! * NENT 2022 */ import{r as t,w as i,h as s,H as e,a as n}from"./index-916ca544.js";import{E as o,e as h,a as r}from"./index-f7016b94.js";import{c as a,f as l,b as c,w as u}from"./logging-5a93c8af.js";import{a as d,o as v}from"./state-27a8a5bc.js";import{g as p,a as f}from"./factory-acbf0d3d.js";import{D as m}from"./interfaces-8c5cd1b8.js";import{a as g}from"./elements-4818d39b.js";import{A as y}from"./interfaces-837cdb60.js";import{r as w}from"./elements-1b845a48.js";import{hasToken as $,resolveTokens as b}from"./tokens-78f8cdbe.js";import{g as R}from"./elements-397b851b.js";import{R as j,N as k,a as x}from"./interfaces-3b78db83.js";import{i as P}from"./values-ddfac998.js";import{g as E,a as O,b as C}from"./visits-b52975ad.js";import{s as N}from"./state-adf07580.js";import"./index-4bfabbbd.js";import"./promises-584c4ece.js";import"./expressions-2c27c47c.js";import"./strings-47d55561.js";import"./mutex-e5645c85.js";import"./memory-0d63dacd.js";function A(t,i){let s=T(t,i)?t:`${i}/${t}`;return s=s.replace(/\/{2,}/g,"/"),I(s)}const T=(t,i="/")=>t.startsWith(i)||new RegExp(`^${i}(\\/|\\?|#|$)`,"i").test(t),S=(t,i)=>{let s=T(t,i)?t.slice(i.length):t;return I(s)},I=t=>(null==t?void 0:t.startsWith("/"))?t:`/${t}`,M=t=>(null==t?void 0:t.startsWith("/"))||!1,U=t=>Math.random().toString(36).slice(2,t),z=(t,i)=>{for(let s=i,e=s+1,n=t.length;e<n;s+=1,e+=1)t[s]=t[e];t.pop()};function L(t,i=""){let s,e=(null==i?void 0:i.split("/"))||[],n=0;const o=(null==t?void 0:t.split("/"))||[],h=t&&M(t),r=i&&M(i),a=h||r;if(t&&M(t)?e=o:o.length>0&&(e.pop(),e=e.concat(o)),0===e.length)return"/";if(e.length>0){const t=e[e.length-1];s="."===t||".."===t||""===t}else s=!1;for(let t=e.length;t>=0;t--){const i=e[t];"."===i?z(e,t):".."===i?(z(e,t),n++):n&&(z(e,t),n--)}if(!a)for(;n--;n)e.unshift("..");!a||""===e[0]||e[0]&&M(e[0])||e.unshift("");let l=e.join("/");return s&&!l.endsWith("/")&&(l+="/"),l}const D=(t,i)=>{if(t===i)return!0;if(null===t||null===i)return!1;if(Array.isArray(t))return Array.isArray(i)&&t.length===i.length&&t.every(((t,s)=>D(t,i[s])));const s=typeof t;if(s!==typeof i)return!1;if("object"===s){const s=t.valueOf(),e=i.valueOf();if(s!==t||e!==i)return D(s,e);const n=Object.keys(t),o=Object.keys(i);return n.length===o.length&&n.every((s=>D(t[s],i[s])))}return!1},J=(t,i)=>t.pathname===i.pathname&&t.search===i.search&&t.hash===i.hash&&t.key===i.key&&D(t.state,i.state),H=(t,i,s,e)=>{var n,o,h;let r;"string"==typeof t?(r=function(t="/"){let i=t,s="",e="";const n=i.indexOf("#");-1!==n&&(e=i.slice(n),i=i.slice(0,Math.max(0,n)));const o=i.indexOf("?");return-1!==o&&(s=i.slice(o),i=i.slice(0,Math.max(0,o))),{pathname:i,search:"?"===s?"":s,hash:"#"===e?"":e,query:{},key:"",params:{}}}(t),void 0!==i&&(r.state=i)):(r=Object.assign({},t),r.search&&!r.search.startsWith("?")&&(r.search=`?${r.search}`),r.hash&&!r.hash.startsWith("#")&&(r.hash=`#${r.hash}`),void 0!==i&&void 0===r.state&&(r.state=i));try{r.pathname=decodeURI(r.pathname)}catch(t){throw t instanceof URIError?new URIError(`Pathname "${r.pathname}" could not be decoded. This is likely caused by an invalid percent-encoding.`):t}var a;return r.key=s,r.params={},e?r.pathname?(null===(n=r.pathname)||void 0===n?void 0:n.startsWith("/"))||(r.pathname=L(r.pathname,e.pathname)):r.pathname=e.pathname:r.pathname||(r.pathname="/"),r.query=((a=r.search||"")?(/^[?#]/.test(a)?a.slice(1):a).split("&").reduce(((t,i)=>{const[s,e]=i.split("=");return t[s]=e?decodeURIComponent(e.replace(/\+/g," ")):"",t}),{}):{})||{},r.pathParts=(null===(o=r.pathname)||void 0===o?void 0:o.split("/"))||[],r.hashParts=(null===(h=r.hash)||void 0===h?void 0:h.split("/"))||[],r},W=new RegExp(["(\\\\.)","(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?"].join("|"),"g"),F=t=>t.replace(/([.+*?=^!:${}()\[\]|/\\])/g,"\\$1"),B=t=>t.replace(/([=!:$/()])/g,"\\$1"),K=t=>(null==t?void 0:t.sensitive)?"":"i",V=(t,i,s)=>{var e;s=s||{};const{strict:n}=s,o=!1!==s.end,h=F(s.delimiter||"/"),r=s.delimiters||"./",a=((null===(e=s.endsWith)||void 0===e?void 0:e.length)?[...s.endsWith]:s.endsWith?[s.endsWith]:[]).map((t=>F(t))).concat("$").join("|");let l="",c=!1;for(let s=0;s<t.length;s++){const e=t[s];if("string"==typeof e)l+=F(e),c=s===t.length-1&&r.includes(e[e.length-1]);else{const t=F(e.prefix||""),s=e.repeat?`(?:${e.pattern})(?:${t}(?:${e.pattern}))*`:e.pattern;i&&i.push(e),l+=e.optional?e.partial?`${t}(${s})?`:`(?:${t}(${s}))?`:`${t}(${s})`}}return o?(n||(l+=`(?:${h})?`),l+="$"===a?"$":`(?=${a})`):(n||(l+=`(?:${h}(?=${a}))?`),c||(l+=`(?=${h}|${a})`)),new RegExp(`^${l}`,K(s))},q=(t,i,s)=>t instanceof RegExp?((t,i)=>{if(!i)return t;const s=t.source.match(/\((?!\?)/g);if(s)for(let t=0;t<s.length;t++)i.push({name:t,prefix:null,delimiter:null,optional:!1,repeat:!1,partial:!1,pattern:null});return t})(t,i):Array.isArray(t)?((t,i,s)=>{const e=[];for(const n of t)e.push(q(n,i,s).source);return new RegExp(`(?:${e.join("|")})`,K(s))})(t,i,s):((t,i,s)=>V(((t,i)=>{const s=[];let e=0,n=0,o="";const h=(null==i?void 0:i.delimiter)||"/",r=(null==i?void 0:i.delimiters)||"./";let a,l=!1;for(;null!==(a=W.exec(t));){const i=a[0],c=a[1],u=a.index;if(o+=t.slice(n,u),n=u+i.length,c){o+=c[1],l=!0;continue}let d="";const v=t[n],p=a[2],f=a[3],m=a[4],g=a[5];if(!l&&o.length>0){const t=o.length-1;r.includes(o[t])&&(d=o[t],o=o.slice(0,t))}o&&(s.push(o),o="",l=!1);const y=d||h,w=f||m;s.push({name:p||e++,prefix:d,delimiter:y,optional:"?"===g||"*"===g,repeat:"+"===g||"*"===g,partial:""!==d&&void 0!==v&&v!==d,pattern:w?B(w):`[^${F(y)}]+?`})}return(o||n<t.length)&&s.push(o+t.slice(n)),s})(t,s),i,s))(t,i,s);let G=0;const Y={};class _{constructor(t,i,s,e=null,n=!0,o={},h=null,r=0,l=(()=>{}),c){var u;this.router=t,this.routeElement=i,this.path=s,this.parentRoute=e,this.exact=n,this.pageData=o,this.transition=h,this.scrollTopOffset=r,this.routeDestroy=c,this.completed=!1,this.match=null,this.scrollOnNextRender=!1,this.previousMatch=null,this.childRoutes=[],this.router.routes.push(this),null===(u=this.parentRoute)||void 0===u||u.addChildRoute(this),this.onStartedSubscription=t.eventBus.on(j.RouteChangeStart,(async t=>{a(d.debug,`route: ${this.path} started -> ${t.pathname} `),this.previousMatch=this.match,J(this.router.location,t)||(await this.activateActions(y.OnExit),this.completed=!1)}));const v=(i=!0)=>{a(d.debug,`route: ${this.path} changed -> ${location.pathname}`),this.match=t.matchPath({path:this.path,exact:this.exact,strict:!0},this,i),l(this.match),this.adjustClasses()};this.onChangedSubscription=t.eventBus.on(j.RouteChanged,(()=>{v()})),v()}addChildRoute(t){this.childRoutes=[...this.childRoutes,t].sort(((t,i)=>t.routeElement.compareDocumentPosition(i.routeElement)&Node.DOCUMENT_POSITION_FOLLOWING?-1:1))}normalizeChildUrl(t){return M(t)?t:this.router.normalizeChildUrl(t,this.path)}didExit(){var t,i,s;return!((null===(t=this.match)||void 0===t?void 0:t.isExact)||(i=this.match,s=this.previousMatch,null===i&&null===s||null!==s&&i&&s&&i.path===s.path&&i.url===s.url&&D(i.params,s.params)))}get actionActivators(){return Array.from(this.routeElement.querySelectorAll("n-action-activator")).filter((t=>this.isChild(t)))}isChild(t){var i;const s=this.routeElement.tagName.toLocaleLowerCase();return t.closest(s)==this.routeElement||t.parentElement==this.routeElement||(null===(i=t.parentElement)||void 0===i?void 0:i.closest(s))===this.routeElement}async loadCompleted(){var t;this.match&&(this.captureInnerLinksAndResolveHtml(),(null===(t=this.match)||void 0===t?void 0:t.isExact)&&(this.routeElement.querySelectorAll("[defer-load]").forEach((t=>{t.removeAttribute("defer-load")})),await this.activateActions(y.OnEnter),await this.adjustPageTags())),this.completed=!0,this.router.routeCompleted()}toggleClass(t,i){const s=this.routeElement.classList.contains(t);s&&0==i&&this.routeElement.classList.remove(t),!s&&i&&this.routeElement.classList.add(t)}adjustClasses(){var t;const i=(null===(t=this.match)||void 0===t?void 0:t.isExact)||!1;this.toggleClass("active",null!=this.match),this.toggleClass("exact",i),this.transition&&this.toggleClass(this.transition,i)}captureInnerLinksAndResolveHtml(t){this.router.captureInnerLinks(t||this.routeElement,this.path),w(this.routeElement)}async resolvePageTitle(){let t=this.pageData.title;return d.dataEnabled&&this.pageData.title&&$(this.pageData.title)&&(t=await b(this.pageData.title)),t||this.pageData.title}async adjustPageTags(){const t=this.pageData;t.title=await this.resolvePageTitle(),d.dataEnabled&&(!this.pageData.description&&$(this.pageData.description)&&(t.description=await b(this.pageData.description)),!this.pageData.keywords&&$(this.pageData.keywords)&&(t.keywords=await b(this.pageData.keywords))),this.router.setPageTags(t)}async getPreviousRoute(){var t;const i=await this.getSiblingRoutes(),s=this.getSiblingIndex(i.map((t=>t.route)));return(null===(t=(s>0?i.slice(s-1):[])[0])||void 0===t?void 0:t.route)||this.parentRoute}isValidForNext(){return R(this.routeElement)}async getNextRoute(){if("N-VIEW-PROMPT"==this.routeElement.tagName)return this.parentRoute;const t=await this.getSiblingRoutes(),i=this.getSiblingIndex(t.map((t=>t.route)));let s=t.slice(i+1);return s.length&&s[0]?s[0].route:this.parentRoute}async getParentRoutes(){return await Promise.all(function(t){if(!P(t))return[];let i=t.endsWith("/")?t.slice(0,t.length-1):t.slice();const s=[t.slice()];let e=i.lastIndexOf("/");for(;e>0;)i=i.substr(0,e),s.push(i.slice()),e=i.lastIndexOf("/");return"/"!=t&&s.push("/"),s.reverse()}(this.path).map((t=>this.router.routes.find((i=>i.path==t)))).filter((t=>t)).map((async t=>{var i;const s=(null===(i=null==t?void 0:t.match)||void 0===i?void 0:i.path.toString())||(null==t?void 0:t.path),e=await t.resolvePageTitle();return{route:t,path:s,title:e}})))}getParentRoute(){return this.parentRoute}sortRoutes(t){return t.sort(((t,i)=>t.compareDocumentPosition(i)&Node.DOCUMENT_POSITION_FOLLOWING?-1:1))}async getSiblingRoutes(){var t;return await Promise.all(((null===(t=this.parentRoute)||void 0===t?void 0:t.childRoutes)||this.router.routes.filter((t=>null==t.parentRoute))).sort(((t,i)=>t.routeElement.compareDocumentPosition(i.routeElement)&Node.DOCUMENT_POSITION_FOLLOWING?-1:1)).map((async t=>{var i;const s=(null===(i=t.match)||void 0===i?void 0:i.path.toString())||t.path,e=await t.resolvePageTitle();return{route:t,path:s,title:e}})))}async getChildRoutes(){return await Promise.all(this.childRoutes.sort(((t,i)=>t.routeElement.compareDocumentPosition(i.routeElement)&Node.DOCUMENT_POSITION_FOLLOWING?-1:1)).map((async t=>{var i;const s=(null===(i=t.match)||void 0===i?void 0:i.path.toString())||t.path,e=await t.resolvePageTitle();return{route:t,path:s,title:e}})))}getSiblingIndex(t){return(null==t?void 0:t.findIndex((t=>t.path==this.path)))||0}replaceWithRoute(t){const i=M(t)?t:this.router.resolvePathname(t,this.path);this.router.replaceWithRoute(i)}async activateActions(t,i=(()=>!0)){await g(this.actionActivators,t,i)}destroy(){var t;this.onStartedSubscription(),this.onChangedSubscription(),null===(t=this.routeDestroy)||void 0===t||t.call(this,this)}}class Q{constructor(t,i,s){this.router=t,this.events=i,this.actions=s,this.removeSubscription=this.actions.on(k,(t=>{this.handleEventAction(t)}))}notifyRouterInitialized(){a(d.debug,"route event: initialized"),this.events.emit(j.Initialized,{})}notifyRouteChangeStarted(t){a(d.debug,`route event: started ${t}`),this.events.emit(j.RouteChangeStart,t)}notifyRouteChanged(t){a(d.debug,"route event: changed"),this.events.emit(j.RouteChanged,t)}notifyRouteFinalized(t){a(d.debug,"route event: finalized"),this.events.emit(j.RouteChangeFinish,t)}notifyMatch(t,i){a(d.debug,"route event: matched"),this.events.emit(j.RouteMatched,{route:t,match:i})}notifyMatchExact(t,i){a(d.debug,"route event: matched-exact"),this.events.emit(j.RouteMatchedExact,{route:t,match:i})}handleEventAction(t){switch(l(d.debug,`route-listener: action received ${JSON.stringify(t)}`),t.command){case x.goNext:this.router.goNext();break;case x.goBack:this.router.goBack();break;case x.goToParent:this.router.goToParentRoute();break;case x.goTo:{const{path:i}=t.data;this.router.goToRoute(i);break}case x.back:this.router.history.goBack();break;case x.scrollTo:{const{id:i}=t.data;this.router.scrollToId(i);break}}}destroy(){this.removeSubscription()}}class X{constructor(t){this.win=t,this.scrollPositions=new Map,p("session").then((t=>(this.provider=t,null==t?void 0:t.get("scrollPositions").then((t=>{t&&(this.scrollPositions=new Map(JSON.parse(t)))}))))),t&&"scrollRestoration"in t.history&&(t.history.scrollRestoration="manual")}set(t,i){if(this.scrollPositions.set(t,i),this.provider){const t=[];this.scrollPositions.forEach(((i,s)=>{t.push([s,i])})),this.provider.set("scrollPositions",JSON.stringify(t)).then((()=>{}))}}get(t){return this.scrollPositions.get(t)}has(t){return this.scrollPositions.has(t)}capture(t){this.set(t,[this.win.scrollX,this.win.scrollY])}}class Z{constructor(t,i){this.win=t,this.basename=i,this.allKeys=[],this.events=new o,this.location=this.getDOMLocation(this.getHistoryState()),this.previousLocation=this.location,this.allKeys.push(this.location.key),this.scrollHistory=new X(t),this.win.addEventListener("popstate",(t=>{this.handlePop(this.getDOMLocation(t.state))}))}getHistoryState(){return this.win.history.state||{}}getDOMLocation(t){const{key:i,state:s={}}=t||{},{pathname:e,search:n,hash:o}=this.win.location;let h=e+n+o;return c(!T(h,this.basename),`You are attempting to use a basename on a page whose URL path does not begin with the basename. Expected path "${h}" to begin with "${this.basename}".`),this.basename&&(h=S(h,this.basename)),H(h,s,i||U(6))}handlePop(t){J(this.location,t)||this.setState("POP",t)}push(t,i={}){const s=H(t,i,U(6),this.location),e=this.createHref(s),{key:n}=s;if(J(this.location,s))return;this.win.history.pushState({key:n,state:i},"",e);const o=this.allKeys.indexOf(this.location.key),h=this.allKeys.slice(0,-1===o?0:o+1);h.push(s.key),this.allKeys=h,this.setState("PUSH",s)}replace(t,i={}){const s=H(t,i,U(6),this.location);s.search=this.location.search;const e=this.createHref(s),{key:n}=s;this.win.history.replaceState({key:n,state:i},"",e);const o=this.allKeys.indexOf(this.location.key);-1!==o&&(this.allKeys[o]=s.key),this.setState("REPLACE",s)}createHref(t){return A(function(t){const{pathname:i,search:s,hash:e}=t;let n=i||"/";return s&&"?"!==s&&(n+=(null==s?void 0:s.startsWith("?"))?s:`?${s}`),e&&"#"!==e&&(n+=(null==e?void 0:e.startsWith("#"))?e:`#${e}`),n}(t),this.basename)}setState(t,i){this.scrollHistory.capture(this.location.key),this.previousLocation=this.location,this.location=i,this.location.scrollPosition=this.scrollHistory.get(this.location.key),this.events.emit(t,this.location)}go(t){this.win.history.go(t),this.events.emit("GO",this.location)}goBack(){this.win.history.back(),this.events.emit("BACK",this.location)}goForward(){this.win.history.forward(),this.events.emit("FORWARD",this.location)}listen(t){return t(this.location),this.events.on("*",((i,s)=>{t(s)}))}destroy(){this.events.removeAllListeners()}}class tt{constructor(t){this.accessor=t,this.changed=new o}async get(t){return this.accessor(t)}async set(t,i){}}class it{constructor(t,i,s,e,n="",o="",h="",r="",a="",l=0){if(this.win=t,this.writeTask=i,this.eventBus=s,this.actions=e,this.root=n,this.appTitle=o,this.appDescription=h,this.appKeywords=r,this.transition=a,this.scrollTopOffset=l,this.routes=[],this.history=new Z(t,n),this.listener=new Q(this,this.eventBus,this.actions),d.dataEnabled)this.enableDataProviders();else{const t=v("dataEnabled",(i=>{i&&this.enableDataProviders(),t()}))}this.removeHandler=this.history.listen((t=>{var i;this.location=t,this.listener.notifyRouteChanged(t),null===(i=this.routeData)||void 0===i||i.changed.emit(m.DataChanged,{changed:["route"]})})),this.listener.notifyRouteChanged(this.history.location)}async enableDataProviders(){this.routeData=new tt((t=>{let i={data:this.location.params};return this.hasExactRoute()&&(i=Object.assign(i,this.exactRoute)),i.data[t]||i[t]})),f("route",this.routeData),this.queryData=new tt((t=>this.location.query[t])),f("query",this.queryData),this.queryData=new tt((t=>this.location.query[t])),this.visitData=new tt((async t=>{switch(t){case"all":const t=await C();return JSON.stringify(t).split('"').join("'");case"stored":const i=await O();return JSON.stringify(i).split('"').join("'");case"session":const s=await E();return JSON.stringify(s).split('"').join("'")}})),f("visits",this.visitData)}adjustRootViewUrls(t){let i=this.root&&T(t,this.root)?t.slice(this.root.length):t;return(()=>this.root.includes("."))()?"#"+I(i):I(i)}atRoot(){var t;return(null===(t=this.location)||void 0===t?void 0:t.pathname)==this.root||"/"==this.location.pathname}initialize(t){this.startUrl=t,t&&this.atRoot()&&this.replaceWithRoute(S(t,this.root)),this.captureInnerLinks(this.win.document.body),this.listener.notifyRouterInitialized(),this.routes.every((t=>t.completed))&&this.allRoutesComplete()}allRoutesComplete(){this.listener.notifyRouteFinalized(this.location)}routeCompleted(){this.routes.every((t=>t.completed))&&this.allRoutesComplete()}async goBack(){if(this.exactRoute){const t=await this.exactRoute.getNextRoute();if(t)return void this.goToRoute(null==t?void 0:t.path)}this.listener.notifyRouteChangeStarted(this.history.previousLocation.pathname),this.history.goBack()}async goNext(){if(this.exactRoute){if(!this.exactRoute.isValidForNext())return;const t=await this.exactRoute.getNextRoute();if(t)return void this.goToRoute(t.path)}this.goToParentRoute()}goToParentRoute(){var t;if(this.exactRoute){const t=this.exactRoute.getParentRoute();if(t)return void this.goToRoute(t.path)}const i=null===(t=this.history.location.pathParts)||void 0===t?void 0:t.slice(0,-1);this.goToRoute(i?I(i.join("/")):this.startUrl||"/")}scrollTo(t){this.writeTask((()=>{var i;Array.isArray(null===(i=this.history.location)||void 0===i?void 0:i.scrollPosition)?this.win.scrollTo(this.history.location.scrollPosition[0],this.history.location.scrollPosition[1]):this.win.scrollTo(0,t||0)}))}scrollToId(t){this.writeTask((()=>{const i=this.win.document.querySelector("#"+t);null==i||i.scrollIntoView()}))}goToRoute(t){this.listener.notifyRouteChangeStarted(t);const i=L(t,this.location.pathname);this.history.push(i)}replaceWithRoute(t){this.listener.notifyRouteChangeStarted(t);const i=L(t,this.location.pathname);this.history.replace(i)}matchPath(t={},i=null,s=!0){const e=function(t,i={}){"string"==typeof i&&(i={path:i});const{pathname:s}=t,{path:e="/",exact:n=!1,strict:o=!1}=i,{re:h,keys:r}=((t,i)=>{const s=`${i.end}${i.strict}`,e=Y[s]||(Y[s]={}),n=JSON.stringify(t instanceof RegExp?t.source:t);if(e[n])return e[n];const o=[],h={re:q(t,o,i),keys:o};return G<1e4&&(e[n]=h,G+=1),h})(e,{end:n,strict:o}),a=h.exec(s);if(!a)return null;const[l,...c]=a,u=s===l;if(n&&!u)return null;const d={path:e,url:"/"===e&&""===l?"/":l,isExact:u,params:r.reduce(((t,i,s)=>(t[i.name]=c[s],t)),{})};return d.isExact&&Object.assign(t.params,d.params),d}(this.location,t);return i&&e&&s&&(e.isExact?this.listener.notifyMatchExact(i,e):this.listener.notifyMatch(i,e)),e}resolvePathname(t,i){return L(t,i||this.location.pathname)}normalizeChildUrl(t,i){return A(t,i)}isModifiedEvent(t){return t.metaKey||t.altKey||t.ctrlKey||t.shiftKey}async setPageTags(t){var i,s,e;const{title:n,description:o,keywords:h,robots:r}=t;if(this.win.document){this.win.document.title=[n,this.appTitle].filter((t=>t)).join(" | "),r&&this.win.document.querySelectorAll("meta[name*=bot]").forEach((t=>{t.content=r}));const t=this.win.document.querySelector("link[rel=canonical]");if(t){const{protocol:i,host:s}=document.location,{pathname:e}=this.location;t.href=`${i}//${s}${e}`}this.win.document.querySelectorAll("meta[name*=description]").forEach((t=>{t.content=o||this.appDescription||""})),this.win.document.querySelectorAll("meta[name*=keywords]").forEach((t=>{t.content=h||this.appKeywords||""})),(null===(s=null===(i=this.history)||void 0===i?void 0:i.location)||void 0===s?void 0:s.hash)?this.scrollToId(this.history.location.hash.slice(1)):this.scrollTo((null===(e=this.exactRoute)||void 0===e?void 0:e.scrollTopOffset)||this.scrollTopOffset)}}captureInnerLinks(t,i){!function(t,i,s,e){const n="n-attached-click";Array.from(t.querySelectorAll("a[href]")||[]).map((t=>t)).filter((t=>!t.hasAttribute(n))).forEach((t=>{t.addEventListener(s,(i=>{e(t,i)})),t.setAttribute(n,"")}))}(t,0,"click",((t,s)=>{if(this.isModifiedEvent(s)||!(null==this?void 0:this.history))return!0;if(!t.href.includes(location.origin)||t.target)return!0;s.preventDefault();const e=t.href.replace(location.origin,"");return this.handleRouteLinkClick(e,i||this.location.pathname)}))}get exactRoutes(){return this.routes.filter((t=>{var i;return null===(i=t.match)||void 0===i?void 0:i.isExact}))}get matchedRoutes(){return this.routes.filter((t=>t.match))}get hasRoutes(){return this.routes.length>0}hasExactRoute(){var t;return(null===(t=this.exactRoutes)||void 0===t?void 0:t.length)>0}get exactRoute(){return this.hasExactRoute()?this.exactRoutes[0]:null}handleRouteLinkClick(t,i){var s,e;const n=M(t)?t:this.normalizeChildUrl(t,i||"/");if(i&&n.startsWith(i)&&n.includes("#")){const i=t.substr(t.indexOf("#"));null===(e=null===(s=this.win.document)||void 0===s?void 0:s.querySelector(i))||void 0===e||e.scrollIntoView({behavior:"smooth"})}else this.goToRoute(n)}destroy(){this.removeHandler(),this.listener.destroy(),this.history.destroy(),this.routes.forEach((t=>t.destroy()))}createRoute(t,i,s){let{path:e,exact:n,pageTitle:o,pageDescription:h,pageKeywords:r,pageRobots:a,transition:l,scrollTopOffset:c}=t;const d=(null==i?void 0:i.route)||null;d?(e=this.normalizeChildUrl(t.path,d.path),l=l||(null==d?void 0:d.transition)||l):e=this.adjustRootViewUrls(t.path),t.path=e,this.routes.find((t=>t.path==e))&&u(`route: duplicate route detected for ${e}.`);const v=new _(this,t,e,d,n,{title:o||(null==d?void 0:d.pageData.title),description:h,keywords:r,robots:a||(null==d?void 0:d.pageData.robots)},l||this.transition,c,s,(()=>{this.routes=this.routes.filter((t=>t==v))}));return v}}const st=class{constructor(i){t(this,i),this.root="/",this.startDelay=0,this.debug=!1,this.notFound=!1}componentWillLoad(){let{appTitle:t,appDescription:s,appKeywords:e}=this.el.closest("n-app")||{appTitle:window.document.title};const n=new it(window,i,h,r,this.root,t,s,e,this.transition,this.scrollTopOffset);d.routingEnabled=!0,N.router=n,N.debug=this.debug||d.debug}render(){return s(e,{style:{display:"block"}},s("slot",null),this.notFound?s("div",{hidden:N.hasExactRoute},s("slot",{name:"not-found"})):null)}componentDidLoad(){const t=this.startPath;function i(){var i;null===(i=N.router)||void 0===i||i.initialize(t),l(d.debug,"n-views: initialized")}this.startDelay>0?setTimeout((()=>{i()}),1e3*this.startDelay):i()}disconnectedCallback(){var t;d.routingEnabled=!1,null===(t=N.router)||void 0===t||t.destroy()}get el(){return n(this)}};export{st as n_views}