12345 |
- var _extends=Object.assign||function(a){for(var b,c=1;c<arguments.length;c++)for(var d in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,d)&&(a[d]=b[d]);return a};import React from'react';import Navigo from'navigo';import Callback from'../helpers/callbackjs';class RoutingSystem{constructor(a={}){// Defaults to: false
- // Defaults to: '#'
- this._root=a.root||window.location.protocol+'//'+window.location.host,this._useHash=!!a.useHash,this._hash=a.hash||'#',this.router=new Navigo(this._root,this._useHash,this._hash),this.router.notFound(()=>this._notFound()),this.router.on(()=>this._home()),this.routes={},this.routeNames={},this.routesByName={},this.mods={},this.handlers={},this.currentView='root',this.currentState={},this.callbacks=Callback('RoutingSystem')}_notFound(a,b){console.log('URL not found!',a,b),this.currentView='root',this._routeChanged()}_home(){// this.currentView = 'root';
- this.router.navigate('/home')}_handler(a,b,c){return console.log('Handler',a,b),this.currentView=c,this._routeChanged(),!0}onRouteChange(a,b,c='\\w+'){this.callbacks.add(a,b,c)}_routeChanged(){let a=this.getRoute();this.callbacks.match(a.url)}addRoute(a,b,c){// if(this.routes[route])return;
- this.mods[b],this.routeNames[a]=c,this.routesByName[c]=a,this.routes[a]=b,this.mods[b]=a,this.handlers[a]=(a,c)=>this._handler(a,c,b),this.router.on(a,this.handlers[a])}removeRoute(a,b){let c=b||this.mods[a];console.log('Removed route',c,' from mod with id:',a,' and route: ',c),this.router.off(this.handlers[c]),delete this.handlers[c],delete this.routes[c],delete this.routesByName[this.routeNames[c]],delete this.routeNames[c],delete this.mods[a]}resolve(){this.router.resolve()}go(a,b={},c=!1){this.currentState=_extends({},b),c&&this.router.pause(),this.router.navigate(a),c&&this.router.resume()}goName(a){return!!this.routesByName[a]&&(this.router.navigate(this.routesByName[a]),!0)}getURL(a){let b=this.getNameURL(a);return b||a}getNameURL(a){return this.routesByName[a]}getCurrentView(){return this.currentView}getView(a){return this.routes[a]}getRouteName(a){return this.routeNames[a]}getRoute(){return this.router.lastRouteResolved()}getRoutes(){return Object.keys(this.routes).map(a=>({route:a,id:this.routes[a],routeName:this.routeNames[a]}))}getRoutesRegs(){let a=[];for(var b in this.routes){let c=b,d=/:[a-z]*/,e=c.split(d),f=e.join('[a-z0-9]+'),g=new RegExp(f);a.push({url:c,reg:g,id:this.routes[b]})}return a}clean(){this.routes={},this.router.destroy(),this.router=new Navigo(this._root,this._useHash,this._hash),this.router.notFound(()=>this._notFound()),this.router.on(()=>this._home()),this.routes={},this.routeNames={},this.routesByName={},this.mods={},this.handlers={}}import(a){for(var b in a.routes)this.addRoute(b,a.routes[b],a.routeNames[b]||a.routes[b])}export(){return{routes:this.routes,routeNames:this.routeNames}}}export default RoutingSystem;
|