|
import Router5 from 'router5' |
|
import browserPluginFactory from 'router5/plugins/browser' |
|
import listenersPlugin from 'router5/plugins/listeners' |
|
import {seq} from '../promises' |
|
|
|
const router5 = new Router5([], { |
|
strictQueryParams: true, |
|
autoCleanUp: true |
|
}) |
|
const broswerPlugin = browserPluginFactory({ |
|
useHash: true |
|
}) |
|
router5.usePlugin(broswerPlugin) |
|
.usePlugin(listenersPlugin()) |
|
|
|
class RouterSingleton { |
|
constructor() { |
|
this._onStart = [] |
|
} |
|
|
|
/*setTransition(to, from) { |
|
extendObservable(this._previousState, from) |
|
extendObservable(this._currentState, to) |
|
}*/ |
|
|
|
get currentSegment() { |
|
return router5.getState().name |
|
} |
|
|
|
segmentMatches(segment) { |
|
if(typeof segment === 'string') { |
|
return new RegExp(`^${segment.replace('.', '\\.')}$`).test(this.currentSegment) |
|
} |
|
else { |
|
return segment.test(this.currentSegment) |
|
} |
|
} |
|
|
|
/** |
|
* |
|
* Match a route, and navigate to it. Do not reload browser page |
|
* |
|
* router.navigate('profile.redeem-code', {code: 'XXX'}) |
|
* |
|
* ^ this will change browser location to '/redeem-code/XXX' |
|
* but *will not* reload the page |
|
* |
|
* @param args : {route: string, params: object} |
|
* @returns {any} |
|
*/ |
|
navigate(...args) { |
|
return router5.navigate.apply(router5, args) |
|
} |
|
|
|
transitionTo(...args) { |
|
return router5.navigate.apply(router5, args) |
|
} |
|
|
|
/** |
|
* |
|
* If path matches an existing route, navigate to it without reloading |
|
* the page |
|
* |
|
* If path doesn't match, make browser go to this url |
|
* |
|
* E.g. |
|
* |
|
* router.navigateToPath('/redeem-code/XXX') |
|
* |
|
* ^ this will change path to '/redeem-code/XXX' and *will not* |
|
* reload the page because it matches |
|
* <Route name='redeem-code' path='/redeem-code/:code'> |
|
* |
|
* router.navigateToPath('/browse-book') |
|
* |
|
* ^ this will force the browser to go to '/browse-book' because |
|
* no such path has been configured |
|
* |
|
* @param path : string |
|
*/ |
|
navigateToPath(path) { |
|
const match = router5.matchPath(path) |
|
if(match === null) { |
|
window.location.href = path |
|
return |
|
} |
|
this.navigate(match.name, match.params) |
|
} |
|
|
|
/** |
|
* |
|
* Match a route and navigate to it, but force browser reload |
|
* |
|
* router.navigate('profile.redeem-code', {code: 'XXX'}) |
|
* |
|
* ^ this will make browser load location '/redeem-code/XXX' |
|
* |
|
* @param args : {route: string, params: object} |
|
*/ |
|
forceNavigate(...args) { |
|
const route = router5.buildPath.apply(router5, args) |
|
window.location.href = route |
|
} |
|
|
|
addListener(...args) { |
|
return router5.addListener.apply(router5, args) |
|
} |
|
|
|
buildPath(...args) { |
|
return router5.buildPath.apply(router5, args) |
|
} |
|
|
|
matchPath(...args) { |
|
return router5.matchPath.apply(router5, args) |
|
} |
|
|
|
matchUrl(...args) { |
|
return router5.matchUrl.apply(router5, args) |
|
} |
|
|
|
_setRouterIsStarted() { |
|
this._isStarted = true |
|
} |
|
|
|
start() { |
|
return router5.start.apply(router5, [(err, args) => { |
|
seq(this._onStart |
|
.map((callback) => () => callback(err, args))) |
|
.then(() => this._setRouterIsStarted()) |
|
.catch(() => this._setRouterIsStarted()) |
|
}]) |
|
} |
|
|
|
onStart(callback) { |
|
this._onStart.push(callback) |
|
} |
|
|
|
get isStarted() { |
|
return router5.isStarted() |
|
} |
|
|
|
add(...args) { |
|
return router5.add.apply(router5, args) |
|
} |
|
|
|
getParams() { |
|
const state = router5.getState() |
|
return state.params |
|
} |
|
|
|
getState() { |
|
return router5.getState() |
|
} |
|
|
|
getPath() { |
|
return router5.getState().path |
|
} |
|
|
|
getActiveRoute() { |
|
return this.currentSegment |
|
} |
|
} |
|
|
|
const router = new RouterSingleton() |
|
export default router |