🇷🇺 browser-russian-router 👾
Here is the javascript router for browser environment based on russian-router.
- 🐯 Installation
- 🐵 API
- 🦊 Examples
- 🐷 Contributors
🐯 Installation
To install the current version with npm use the command below:
npm install --save browser-russian-router
Or if you prefer yarn:
yarn add browser-russian-router
Now the package is installed and you can start using it in different environments.
For ES6 modules:
;
For CommonJS modules:
const BrowserRussianRouter = ;
Or you can add UMD bundle just to your HTML code:
<!-- Minified version is available browser-russian-router/dist/browser-russian-router.min.js -->
🐵 API
Since browser-russian-router only extends capabilities of russian-router, it's strongly recommended to read original documentation before usage.
Browser router additionally works with the browser history. It pushes and replaces uris, emits events when uri is changed and even restores the scroll position. Also browser-russian-router substitutes keys in match objects. See examples section to learn how it works exactly.
Another important thing is methods pushUri
and replaceUri
, provided by browser-russian-router. You have to use them instead native analogues to inform the router about changes, because pushState
and replaceState
don't emit any events.
new BrowserRussianRouter(rawRoutes, rawOptions)
Returns a new instance of BrowserRussianRouter
. At the moment all the instances use the same global native history, but there is an intention to change it in the future.
Also one additional option appears. It's scrollRestoration
, that can be set to one of the values manual
, auto
or reset
. Not confuse it with native property history.scrollRestoration
.
- Default value
manual
does nothing, scroll works as before.history.scrollRestoration
isn't affected. - If
auto
was chosen, the router handles scroll position.history.scrollRestoration
is set tomanual
. - If
reset
was chosen, the router resets scroll position, when uri is changed.history.scrollRestoration
is set tomanual
.
router.destructor()
Returns nothing, but must be called before router is deleted from the code. The method removes all the listeners. If you don't call destructor, your memory is leaking.
router.resolveUri(rawUri)
Transforms any rawUri
to uri, that has an absolute path. The result depends on the current location (uri from the addressbar).
router.matchUri(rawUri)
Firstly resolves uri using router.resolveUri
, then matches routes. So the method always matches uri, that has an absolute path.
router.generateUri(routeName, userParams)
Firstly generates uri, then resolves it using router.resolveUri
. Returns a uri (string), that has an absolute path.
router.getMatchObjects()
Returns already cached array of match objects. The result depends on current location (uri from the addressbar).
The router replaces a key property of match object. The original key could be presented by a function like
(matchObject) => matchObject.params.id
or by a string likeuserPage:{key}
(key will be replaced to navigation key).
router.getNavigationKey()
Returns current navigation key, that is changed every time, when navigation was occured (uri was changed).
router.addListener(eventType, eventHandler)
Adds new event listener. The next event types are available: pushstate
, replacestate
, popstate
, change
.
router.removeListener(eventType, eventHandler)
Removes event listener.
router.restoreScroll()
Restores the scroll after uri was changed. It has no effect, if option scrollRestoration
is manual
(by default).
Note you need to call the method manually. It's because the router only knows that uri is changed, but absolutely has no idea when to update your application. react-russian-router cares about scroll restoration and calls this method.
router.pushRoute(routeName, userParams)
Generates uri and pushes it to the history.
router.replaceRoute(routeName, userParams)
Generates uri and replaces the current state with it.
router.pushUri(rawUri)
Alias for pushUri
.
router.replaceUri(rawUri)
Alias for replaceUri
.
router.back()
Alias for back
.
router.forward()
Alias for forward
.
router.go(stepNumber)
Alias for go
.
pushUri(rawUri)
Pushes uri to history like history.pushState
. Also the method affects so called navigation key and emits an event. You have to use it instead native pushState
.
replaceUri(rawUri)
Replaces state with uri like history.replaceState
. Also the method affects so called navigation key and emits an event. You have to use it instead native replaceState
.
back()
Alias for native history.back
.
forward()
Alias for native history.forward
.
go(stepNumber)
Alias for native history.go
.
🦊 Examples
Look at the examples how to use router in some cases. If you want to use react, check out react-russian-router.
Demo Example
See examples/demo/src/routes.js
index: uri: '/' // {key} will be replaced with navigation key key: 'index.{key}' user: uri: '/user/{id}' params: id: /\d+/ { return 'user.' + matchObjectparamsid } about: uri: '/about' hello: // Note the relative path here, that's not recommended to use uri: '?hello={entity}' params: entity: /\w+/ ;
See examples/demo/src/index.js
;; // Reset the uri before the demonstrationhistory; const options = {};const router = routes options; const currentMatchObjects = router;console; // 1console; // 'User/user.123' const indexMatchObjects = router;console; // 'User/index.0' const aboutMatchObjects = router;console; // 'RussianRouter/about' console; // '/user/123/delete'console; // '/user/123?xyz=777'console; // '/user/123#матрёшка'console; // '/user/123?xyz=777#матрёшка'console; // '/alrady/resolved/' const helloMatchObjects = router;console; // 2console; // 'user'console; // 'hello'console; // '/user/123'console; // {hello: 'world'}console; // 'world' console; // '/about'console; // '/user/123?hello=world' console; // 0router;console; // 1
SPA Example
See examples/spa/src/routes.js
index: uri: '/' user: uri: '/user/{id}' params: id: /\d+/ list: uri: '/list/?filter={filter}' params: filter: 'A' 'B' 'C' ;
See examples/spa/src/index.js
;; // Reset the uri before the demonstrationhistory; const options = scrollRestoration: 'auto';const router = routes options; // Render JSON stringified match objects to containerconst codeNode = document; { codeNodeinnerHTML = JSON;} // Render the content of entry point, that is matched nowconst contentNode = document; { const html = matchObjects; contentNodeinnerHTML = html;} // Handler is called every time, when uri is changed { console; const matchObjects = router; ; ; // Scroll should be restored manually after uri is changed router;} // You can start/stop observing uri changeslet routingState = false;const routingNode = document; { routingState = !routingState; if routingState routingNodeinnerHTML = 'Stop routing'; ; router; else routingNodeinnerHTML = 'Start routing'; router; } // Click handler to prevent default link's action and implement SPA { const linkNode = eventtarget; if !linkNode || linkNodetagName !== 'A' return; event; const fn = linkNode; if fn === 'back' ; // router.back is an alias return; else if fn === 'forward' ; // router.forward is an alias return; const action = linkNode; const name = linkNode; if name const params = linkNode; if action === 'replace' router; else router; return; const href = linkNode; if action === 'replace' ; // router.replaceUri is an alias else ; // router.pushUri is an alias } document;routingNode;;
🐷 Contributors
Pull requests are welcome 🐾 Let improve the package together. But, please, respect the code style.
If you don't understand how to use the router or you have additional questions about internal structure, be free to write me at enet@protonmail.ch. Also if you are looking for front-end software developer, be aware that I'm looking for a job. Check out my portfolio at https://zhevak.name 🐤