UNPKG

radi-router

Version:

`radi-router` is the official router for [Radi.js](https://radi.js.org). It deeply integrates with Radi for seamless application building.

1 lines 4.52 kB
let version='0.4.1';var index=({r:t,l:e,mount:r,headless:n,Component:u},o)=>{let s={},i=58;47;var c,a,l,f;let h=t=>{var e=t.split('/'),r=[],n=[];for(var u=0;u<e.length;u++)i===e[u].charCodeAt(0)?(r.push('([^/]+?)'),n.push(e[u].substr(1))):e[u]!==''&&r.push(e[u]);return[new RegExp('^/'+r.join('/')+'(?:[/])?(?:[?&].*)?$','i'),n]},p=t=>{var e=t.length,r=new Array(e);for(var n=e-1;n>=0;n--)r[n]=h(t[n]);return r},d=t=>{return s.config.errors[t]()},g=t=>{window.location.hash=t;return!0},m=t=>{let e=t;for(let n in t){if(t.hasOwnProperty(n)&&t[n].children){let u=m(t[n].children);for(var r in u)u.hasOwnProperty(r)&&(e[n+r]=u[r])}}return e},x=t=>{if(l===t)return f;c||(c=Object.keys(s.routes));a||(a=p(c));var e=!1;for(var r=0;r<a.length;r++){if(a[r][0].test(t)){f=new b(t,a[r],s.routes,c[r]);e=!0;break}}l=t;return!e?{key:null}:f};class b{constructor(t,e,r,n){let u=t.split(/[\?\&]/).slice(1).map(t=>t.split('=')).reduce((t,e)=>Object.assign(t,{[e[0]]:e[1]}),{});var o=t.match(e[0]);this.path=t;this.key=n;this.query=u;this.cmp=r[n]||{};this.params=this.cmp.data||{};for(var s=0;s<e[1].length;s++)this.params[e[1][s]]=o[s+1]}}let w=(...t)=>(t.reduce((t,e)=>e?t.concat(e):t,[]));class C extends u{constructor(...t){super(...t);this.state={prefix:null,text:null,suffix:null,seperator:' | '}}setPrefix(t){return this.setState({prefix:t},'setPrefix')}setSuffix(t){return this.setState({suffix:t},'setSuffix')}set(t){return this.setState({text:t},'set')}setSeperator(t){return this.setState({seperator:t},'setSeperator')}onUpdate(){let t=this.$router.getTitle()||{},e=o.title&&o.title.prefix||this.state.prefix,r=t.text||o.title&&o.title.text||this.state.text,n=o.title&&o.title.suffix||this.state.suffix,u=w(e,r,n).join(this.state.seperator);u&&document.title!==u&&(document.title=u);this.state.text&&r!==this.state.text&&this.set(null)}}class S extends u{constructor(...t){super(...t);this.state={location:window.location.hash.substr(1)||'/',params:{},query:{},last:null,active:null,activeComponent:null,current:{title:null,tags:[],meta:{}}}}onMount(){window.onhashchange=()=>(this.setState(this.hashChange()), this.chain());this.setState(this.hashChange());this.chain()}chain(){this.trigger('changed',this.state.active||'',this.state.last)}hashChange(){var t=window.location.hash.substr(1)||'/',e=x(t);window.scrollTo(0,0);let r=e.cmp&&e.cmp.title;return{last:this.state.active,location:t,params:e.params||{},query:e.query||{},active:e.key||'',current:{title:typeof r==='object'?r:{text:r}||null,tags:e.cmp&&e.cmp.tags||[],meta:e.cmp&&e.cmp.meta||{}}}}hasTag(...t){return t.reduce((t,e)=>t||this.state.current.tags.indexOf(e)>=0,!1)}getMeta(){return this.state.current.meta||{}}getTitle(){let t=this.state.current.title;return t}}class v extends u{constructor(...t){super(...t);this.state={to:'/',active:'active',core:!1,class:'',id:null,title:null}}view(){return t('a',{href:e(this,'to').process(t=>'#'.concat(t)),class:e(this,'to').process(t=>e(this.$router,'active').process(r=>e(this,'class').process(e=>([(r===t||this.state.core&&new RegExp('^'+t).test(r))&&this.state.active,e])))),id:e(this,'id'),title:e(this,'title')},...this.children)}}class y extends u{constructor(...t){super(...t);this.state={active:null}}onMount(){this.setState({active:this.$router.state.active});this.$router.on('changed',(t,e)=>{this.setState({active:t})})}view(){return[e(this,'active').process(t=>this.extractComponent(t)),this.children]}extractComponent(e,r){if(e===null&&typeof r==='undefined')return;let n=s.routes[e],u=typeof n==='object'?n.component:n;if((typeof u==='undefined'||typeof u==='number'||!u)&&typeof n==='undefined')return d(u||404);if(typeof n.redirect==='string')return g(n.redirect);let o=[s.before||null,n.before||null].filter(t=>typeof t==='function');if(o.length>0){let t=(n,s)=>{let i=o.pop();if(typeof i!=='function'){return n(u)}return i.call(this,e,r,e=>{if(typeof e==='undefined'||e===!0){if(o.length>0){return t(n,s)}else{return n(u)}}if(e){if(o.length>0){return t(n,s)}else{return n(e)}}return n(d(403))})};return()=>new Promise(t)}if(typeof u==='function'){if(u.isComponent&&u.isComponent())return t(u);return u}return u}}let j=o.beforeEach,$=o.afterEach,E=(t,e)=>{let r=o.errors&&o.errors[t];if(r){return typeof r==='function'?r:()=>r}return e};s={config:{errors:{404:E(404,()=>t('div',{},'Error 404: Not Found')),403:E(403,()=>t('div',{},'Error 403: Forbidden'))}},before:j,after:$,routes:m(o.routes),write:g,Link:v,Router:y};n('router',S);n('title',C);return s};export default index;export { version }