eecs448-project-4/documentation/generated/frontend_src_module_routing.js.html

162 lines
5.8 KiB
HTML
Raw Permalink Normal View History

2020-11-01 18:53:08 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: frontend/src/module/routing.js</title>
2020-11-01 18:53:08 +00:00
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: frontend/src/module/routing.js</h1>
2020-11-01 18:53:08 +00:00
<section>
<article>
<pre class="prettyprint source linenums"><code>/** @module routing */
/**
* A bare-bones history-api based SPA router.
*/
class Router {
/**
* Arguments for the current route.
* @type {undefined|*}
*/
route_args = undefined
/** The currently navigated route. */
current_route = undefined
2020-11-01 18:53:08 +00:00
/**
* List of callback functions listening for route changes.
* @type {function[]}
*/
subscribers = []
/**
* Array of router history records.
* @type {object[]}
*/
history = []
/** Initialize the router and determine the current page. */
constructor() {
try {
const route = location.href.split(APP_BASE_PATH).filter(Boolean)[0].split(/[#?]/)[0]
if ( route &amp;&amp; !route.toLowerCase().includes('http://') &amp;&amp; !route.toLowerCase().includes('https://') ) {
console.log('resume route', route)
this.navigate(route, {})
}
} catch (e) {}
}
2020-11-01 18:53:08 +00:00
/**
* Returns the APP_BASE_PATH of the application.
* @return {string}
*/
get base_path() {
return APP_BASE_PATH
}
/**
* Navigate the app to the given path with the given args.
* @param {string} path
* @param {*} args
*/
navigate(path, args) {
this.route_args = args
this.history.push({path, args})
window.history.pushState({}, path, this.build_url(path))
this.current_route = path
2020-11-01 18:53:08 +00:00
this.subscribers.forEach(sub => sub(path, args))
}
/**
* Navigate back one route.
*/
back() {
window.history.back()
if ( this.history.length &lt; 2 ) return;
this.history.pop()
const { path, args } = this.history[this.history.length - 1]
this.subscribers.forEach(sub => sub(path, args))
}
/**
* Subscribe to listen for route changes. Returns an object with an unsubscribe() property.
* @param {function} handler - callback called when the route changes
* @return {object} - subscription manager
*/
subscribe(handler) {
if ( !this.subscribers.includes(handler) ) {
this.subscribers.push(handler)
}
return {
unsubscribe: () => {
this.subscribers = this.subscribers.filter(handler)
}
}
}
/**
* Given an array of route parts, build a joined URL route.
* @param {...string} parts
* @return {string}
*/
build_url(...parts) {
parts = [this.base_path, ...parts].map(part => {
if ( part.endsWith('/') ) part = part.slice(0, -1)
if ( part.startsWith('/') ) part = part.slice(1)
return part
})
return parts.join('/')
}
}
/**
* Global router instance.
* @type {Router}
*/
const router = new Router()
export { router }
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fake_data.html">fake_data</a></li><li><a href="module-routing.html">routing</a></li><li><a href="module-util.html">util</a></li></ul><h3>Classes</h3><ul><li><a href="ActiveScope.html">ActiveScope</a></li><li><a href="AddPlayersComponent.html">AddPlayersComponent</a></li><li><a href="DraftBoard.html">DraftBoard</a></li><li><a href="DraftBoardComponent.html">DraftBoardComponent</a></li><li><a href="FrontendUnit.html">FrontendUnit</a></li><li><a href="GenerateMatchupsForWeekPatch.html">GenerateMatchupsForWeekPatch</a></li><li><a href="GenerateWeeklyResultsPatch.html">GenerateWeeklyResultsPatch</a></li><li><a href="GridActionButtonComponent.html">GridActionButtonComponent</a></li><li><a href="GridComponent.html">GridComponent</a></li><li><a href="Home.html">Home</a></li><li><a href="InjectUserTeam.html">InjectUserTeam</a></li><li><a href="LeagueComponent.html">LeagueComponent</a></li><li><a href="Lineup.html">Lineup</a></li><li><a href="LinkComponent.html">LinkComponent</a></li><li><a href="Matchup.html">Matchup</a></li><li><a href="module-routing-Router.html">Router</a></li><li><a href="MyTeamComponent.html">MyTeamComponent</a></li><li><a href="Player.html">Player</a></li><li><a href="ScoresComponent.html">ScoresComponent</a></li><li><a href="ScoresController.html">ScoresController</a></li><li><a href="SeedAPIDataPatch.html">SeedAPIDataPatch</a></li><li><a href="SeedWeeklyPlayerDataPatch.html">SeedWeeklyPlayerDataPatch</a></li><li><a href="SportsDataService.html">SportsDataService</a></li><li><a href="Team.html">Team</a></li><li><a href="Teams.html">Teams</a></li><li><a href="TopLevelComponent.html">TopLevelComponent</a></li><li><a href="User.html">User</a></li><li><a href="WeeklyPlayerStat.html">WeeklyPlayerStat</a></li><li><a href="WeeklyTeamStat.html">WeeklyTeamStat</a></li></ul><h3>Global</h3><ul><li><a href="global.html#GridCellRenderType">GridCellRenderType</a></li></ul>
2020-11-01 18:53:08 +00:00
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Sun Nov 08 2020 14:34:32 GMT-0600 (Central Standard Time)
2020-11-01 18:53:08 +00:00
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>