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.
30 lines (29 loc) • 13 kB
JavaScript
var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.objectCreate=$jscomp.ASSUME_ES5||"function"==typeof Object.create?Object.create:function(a){var b=function(){};b.prototype=a;return new b};$jscomp.underscoreProtoCanBeSet=function(){var a={a:!0},b={};try{return b.__proto__=a,b.a}catch(c){}return!1};
$jscomp.setPrototypeOf="function"==typeof Object.setPrototypeOf?Object.setPrototypeOf:$jscomp.underscoreProtoCanBeSet()?function(a,b){a.__proto__=b;if(a.__proto__!==b)throw new TypeError(a+" is not extensible");return a}:null;
$jscomp.inherits=function(a,b){a.prototype=$jscomp.objectCreate(b.prototype);a.prototype.constructor=a;if($jscomp.setPrototypeOf){var c=$jscomp.setPrototypeOf;c(a,b)}else for(c in b)if("prototype"!=c)if(Object.defineProperties){var e=Object.getOwnPropertyDescriptor(b,c);e&&Object.defineProperty(a,c,e)}else a[c]=b[c];a.superClass_=b.prototype};
$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,c){a!=Array.prototype&&a!=Object.prototype&&(a[b]=c.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_";$jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};
$jscomp.Symbol=function(){var a=0;return function(b){return $jscomp.SYMBOL_PREFIX+(b||"")+a++}}();$jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var a=$jscomp.global.Symbol.iterator;a||(a=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("iterator"));"function"!=typeof Array.prototype[a]&&$jscomp.defineProperty(Array.prototype,a,{configurable:!0,writable:!0,value:function(){return $jscomp.arrayIterator(this)}});$jscomp.initSymbolIterator=function(){}};
$jscomp.arrayIterator=function(a){var b=0;return $jscomp.iteratorPrototype(function(){return b<a.length?{done:!1,value:a[b++]}:{done:!0}})};$jscomp.iteratorPrototype=function(a){$jscomp.initSymbolIterator();a={next:a};a[$jscomp.global.Symbol.iterator]=function(){return this};return a};$jscomp.makeIterator=function(a){$jscomp.initSymbolIterator();var b=a[Symbol.iterator];return b?b.call(a):$jscomp.arrayIterator(a)};
$jscomp.arrayFromIterator=function(a){for(var b,c=[];!(b=a.next()).done;)c.push(b.value);return c};$jscomp.arrayFromIterable=function(a){return a instanceof Array?a:$jscomp.arrayFromIterator($jscomp.makeIterator(a))};
$jscomp.iteratorFromArray=function(a,b){$jscomp.initSymbolIterator();a instanceof String&&(a+="");var c=0,e={next:function(){if(c<a.length){var d=c++;return{value:b(d,a[d]),done:!1}}e.next=function(){return{done:!0,value:void 0}};return e.next()}};e[Symbol.iterator]=function(){return e};return e};
$jscomp.polyfill=function(a,b,c,e){if(b){c=$jscomp.global;a=a.split(".");for(e=0;e<a.length-1;e++){var d=a[e];d in c||(c[d]={});c=c[d]}a=a[a.length-1];e=c[a];b=b(e);b!=e&&null!=b&&$jscomp.defineProperty(c,a,{configurable:!0,writable:!0,value:b})}};$jscomp.polyfill("Array.prototype.keys",function(a){return a?a:function(){return $jscomp.iteratorFromArray(this,function(a){return a})}},"es6","es3");$jscomp.owns=function(a,b){return Object.prototype.hasOwnProperty.call(a,b)};
$jscomp.assign="function"==typeof Object.assign?Object.assign:function(a,b){for(var c=1;c<arguments.length;c++){var e=arguments[c];if(e)for(var d in e)$jscomp.owns(e,d)&&(a[d]=e[d])}return a};$jscomp.polyfill("Object.assign",function(a){return a||$jscomp.assign},"es6","es3");$jscomp.FORCE_POLYFILL_PROMISE=!1;
$jscomp.polyfill("Promise",function(a){function b(){this.batch_=null}function c(a){return a instanceof d?a:new d(function(b,f){b(a)})}if(a&&!$jscomp.FORCE_POLYFILL_PROMISE)return a;b.prototype.asyncExecute=function(a){null==this.batch_&&(this.batch_=[],this.asyncExecuteBatch_());this.batch_.push(a);return this};b.prototype.asyncExecuteBatch_=function(){var a=this;this.asyncExecuteFunction(function(){a.executeBatch_()})};var e=$jscomp.global.setTimeout;b.prototype.asyncExecuteFunction=function(a){e(a,
0)};b.prototype.executeBatch_=function(){for(;this.batch_&&this.batch_.length;){var a=this.batch_;this.batch_=[];for(var b=0;b<a.length;++b){var c=a[b];delete a[b];try{c()}catch(g){this.asyncThrow_(g)}}}this.batch_=null};b.prototype.asyncThrow_=function(a){this.asyncExecuteFunction(function(){throw a;})};var d=function(a){this.state_=0;this.result_=void 0;this.onSettledCallbacks_=[];var b=this.createResolveAndReject_();try{a(b.resolve,b.reject)}catch(l){b.reject(l)}};d.prototype.createResolveAndReject_=
function(){function a(a){return function(f){c||(c=!0,a.call(b,f))}}var b=this,c=!1;return{resolve:a(this.resolveTo_),reject:a(this.reject_)}};d.prototype.resolveTo_=function(a){if(a===this)this.reject_(new TypeError("A Promise cannot resolve to itself"));else if(a instanceof d)this.settleSameAsPromise_(a);else{a:switch(typeof a){case "object":var b=null!=a;break a;case "function":b=!0;break a;default:b=!1}b?this.resolveToNonPromiseObj_(a):this.fulfill_(a)}};d.prototype.resolveToNonPromiseObj_=function(a){var b=
void 0;try{b=a.then}catch(l){this.reject_(l);return}"function"==typeof b?this.settleSameAsThenable_(b,a):this.fulfill_(a)};d.prototype.reject_=function(a){this.settle_(2,a)};d.prototype.fulfill_=function(a){this.settle_(1,a)};d.prototype.settle_=function(a,b){if(0!=this.state_)throw Error("Cannot settle("+a+", "+b|"): Promise already settled in state"+this.state_);this.state_=a;this.result_=b;this.executeOnSettledCallbacks_()};d.prototype.executeOnSettledCallbacks_=function(){if(null!=this.onSettledCallbacks_){for(var a=
this.onSettledCallbacks_,b=0;b<a.length;++b)a[b].call(),a[b]=null;this.onSettledCallbacks_=null}};var n=new b;d.prototype.settleSameAsPromise_=function(a){var b=this.createResolveAndReject_();a.callWhenSettled_(b.resolve,b.reject)};d.prototype.settleSameAsThenable_=function(a,b){var c=this.createResolveAndReject_();try{a.call(b,c.resolve,c.reject)}catch(g){c.reject(g)}};d.prototype.then=function(a,b){function c(a,b){return"function"==typeof a?function(b){try{e(a(b))}catch(q){f(q)}}:b}var e,f,k=new d(function(a,
b){e=a;f=b});this.callWhenSettled_(c(a,e),c(b,f));return k};d.prototype.catch=function(a){return this.then(void 0,a)};d.prototype.callWhenSettled_=function(a,b){function c(){switch(d.state_){case 1:a(d.result_);break;case 2:b(d.result_);break;default:throw Error("Unexpected state: "+d.state_);}}var d=this;null==this.onSettledCallbacks_?n.asyncExecute(c):this.onSettledCallbacks_.push(function(){n.asyncExecute(c)})};d.resolve=c;d.reject=function(a){return new d(function(b,c){c(a)})};d.race=function(a){return new d(function(b,
d){for(var e=$jscomp.makeIterator(a),f=e.next();!f.done;f=e.next())c(f.value).callWhenSettled_(b,d)})};d.all=function(a){var b=$jscomp.makeIterator(a),e=b.next();return e.done?c([]):new d(function(a,d){function f(b){return function(c){k[b]=c;g--;0==g&&a(k)}}var k=[],g=0;do k.push(void 0),g++,c(e.value).callWhenSettled_(f(k.length-1),d),e=b.next();while(!e.done)})};return d},"es6","es3");
(function(a,b){"object"===typeof exports&&"undefined"!==typeof module?b(exports):"function"===typeof define&&define.amd?define(["exports"],b):b(a["radi-router"]={})})(this,function(a){a.version="0.4.1";a.default=function(a,c){var b=a.r,d=a.l,n=a.headless,f=a.Component,k={},l,g,u,r,v=function(a){window.location.hash=a;return!0},w=function(a){for(var b in a)if(a.hasOwnProperty(b)&&a[b].children){var c=w(a[b].children),d;for(d in c)c.hasOwnProperty(d)&&(a[b+d]=c[d])}return a},z=function(a,b,c,d){var A=
a.split(/[\?&]/).slice(1).map(function(a){return a.split("\x3d")}).reduce(function(a,b){var c={};return Object.assign(a,(c[b[0]]=b[1],c))},{}),B=a.match(b[0]);this.path=a;this.key=d;this.query=A;this.cmp=c[d]||{};this.params=this.cmp.data||{};for(a=0;a<b[1].length;a++)this.params[b[1][a]]=B[a+1]},q=function(a){for(var b=[],c=0;c<arguments.length;++c)b[c-0]=arguments[c];return b.reduce(function(a,b){return b?a.concat(b):a},[])};a=function(a){var b=[];for(var c=0;c<arguments.length;++c)b[c-0]=arguments[c];
b=f.apply(this,[].concat($jscomp.arrayFromIterable(b)))||this;b.state={prefix:null,text:null,suffix:null,seperator:" | "};return b};$jscomp.inherits(a,f);a.prototype.setPrefix=function(a){return this.setState({prefix:a},"setPrefix")};a.prototype.setSuffix=function(a){return this.setState({suffix:a},"setSuffix")};a.prototype.set=function(a){return this.setState({text:a},"set")};a.prototype.setSeperator=function(a){return this.setState({seperator:a},"setSeperator")};a.prototype.onUpdate=function(){var a=
(this.$router.getTitle()||{}).text||c.title&&c.title.text||this.state.text,b=q(c.title&&c.title.prefix||this.state.prefix,a,c.title&&c.title.suffix||this.state.suffix).join(this.state.seperator);b&&document.title!==b&&(document.title=b);this.state.text&&a!==this.state.text&&this.set(null)};var h=function(a){var b=[];for(var c=0;c<arguments.length;++c)b[c-0]=arguments[c];b=f.apply(this,[].concat($jscomp.arrayFromIterable(b)))||this;b.state={location:window.location.hash.substr(1)||"/",params:{},query:{},
last:null,active:null,activeComponent:null,current:{title:null,tags:[],meta:{}}};return b};$jscomp.inherits(h,f);h.prototype.onMount=function(){var a=this;window.onhashchange=function(){return a.setState(a.hashChange()),a.chain()};this.setState(this.hashChange());this.chain()};h.prototype.chain=function(){this.trigger("changed",this.state.active||"",this.state.last)};h.prototype.hashChange=function(){var a=window.location.hash.substr(1)||"/";if(u===a)var b=r;else{l||(l=Object.keys(k.routes));if(!g){b=
l;var c=b.length,d=Array(c);for(--c;0<=c;c--){var e=c;var f=b[c].split("/");for(var h=[],x=[],m=0;m<f.length;m++)58===f[m].charCodeAt(0)?(h.push("([^/]+?)"),x.push(f[m].substr(1))):""!==f[m]&&h.push(f[m]);f=[new RegExp("^/"+h.join("/")+"(?:[/])?(?:[?\x26].*)?$","i"),x];d[e]=f}g=d}b=!1;for(d=0;d<g.length;d++)if(g[d][0].test(a)){r=new z(a,g[d],k.routes,l[d]);b=!0;break}u=a;b=b?r:{key:null}}window.scrollTo(0,0);d=b.cmp&&b.cmp.title;return{last:this.state.active,location:a,params:b.params||{},query:b.query||
{},active:b.key||"",current:{title:"object"===typeof d?d:{text:d},tags:b.cmp&&b.cmp.tags||[],meta:b.cmp&&b.cmp.meta||{}}}};h.prototype.hasTag=function(a){for(var b=[],c=0;c<arguments.length;++c)b[c-0]=arguments[c];var d=this;return b.reduce(function(a,b){return a||0<=d.state.current.tags.indexOf(b)},!1)};h.prototype.getMeta=function(){return this.state.current.meta||{}};h.prototype.getTitle=function(){return this.state.current.title};var t=function(a){var b=[];for(var c=0;c<arguments.length;++c)b[c-
0]=arguments[c];b=f.apply(this,[].concat($jscomp.arrayFromIterable(b)))||this;b.state={to:"/",active:"active",core:!1,class:"",id:null,title:null};return b};$jscomp.inherits(t,f);t.prototype.view=function(){var a=this;return b.apply(null,[].concat(["a",{href:d(this,"to").process(function(a){return"#".concat(a)}),class:d(this,"to").process(function(b){return d(a.$router,"active").process(function(c){return d(a,"class").process(function(d){return[(c===b||a.state.core&&(new RegExp("^"+b)).test(c))&&
a.state.active,d]})})}),id:d(this,"id"),title:d(this,"title")}],$jscomp.arrayFromIterable(this.children)))};var p=function(a){var b=[];for(var c=0;c<arguments.length;++c)b[c-0]=arguments[c];b=f.apply(this,[].concat($jscomp.arrayFromIterable(b)))||this;b.state={active:null};return b};$jscomp.inherits(p,f);p.prototype.onMount=function(){var a=this;this.setState({active:this.$router.state.active});this.$router.on("changed",function(b,c){a.setState({active:b})})};p.prototype.view=function(){var a=this;
return[d(this,"active").process(function(b){return a.extractComponent(b)}),this.children]};p.prototype.extractComponent=function(a,c){var d=this;if(null!==a||"undefined"!==typeof c){var e=k.routes[a],f="object"===typeof e?e.component:e;if(("undefined"===typeof f||"number"===typeof f||!f)&&"undefined"===typeof e)return k.config.errors[f||404]();if("string"===typeof e.redirect)return v(e.redirect);var g=[k.before||null,e.before||null].filter(function(a){return"function"===typeof a});if(0<g.length){var h=
function(b,e){var l=g.pop();return"function"!==typeof l?b(f):l.call(d,a,c,function(a){return"undefined"===typeof a||!0===a?0<g.length?h(b,e):b(f):a?0<g.length?h(b,e):b(a):b(k.config.errors[403]())})};return function(){return new Promise(h)}}return"function"===typeof f&&f.isComponent&&f.isComponent()?b(f):f}};var C=c.beforeEach,D=c.afterEach,y=function(a,b){var d=c.errors&&c.errors[a];return d?"function"===typeof d?d:function(){return d}:b};k={config:{errors:{404:y(404,function(){return b("div",{},
"Error 404: Not Found")}),403:y(403,function(){return b("div",{},"Error 403: Forbidden")})}},before:C,after:D,routes:w(c.routes),write:v,Link:t,Router:p};n("router",h);n("title",a);return k};Object.defineProperty(a,"__esModule",{value:!0})});