Add ability to resume route on page reload

This commit is contained in:
Garrett Mills 2020-11-04 20:54:09 -06:00
parent 31ff08e6d0
commit d1c085dbf2
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
2 changed files with 16 additions and 0 deletions

View File

@ -59,6 +59,7 @@ class TopLevelComponent extends Component {
async vue_on_create() { async vue_on_create() {
// Listen for navigation changes. // Listen for navigation changes.
this.router_subscription = router.subscribe((path, args) => this.on_route_change(path, args)) this.router_subscription = router.subscribe((path, args) => this.on_route_change(path, args))
this.current_route = router.current_route
const url_params = new URLSearchParams(window.location.search) const url_params = new URLSearchParams(window.location.search)
if ( url_params.has('then') ) { if ( url_params.has('then') ) {

View File

@ -10,6 +10,9 @@ class Router {
*/ */
route_args = undefined route_args = undefined
/** The currently navigated route. */
current_route = undefined
/** /**
* List of callback functions listening for route changes. * List of callback functions listening for route changes.
* @type {function[]} * @type {function[]}
@ -22,6 +25,17 @@ class Router {
*/ */
history = [] 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 ) {
this.navigate(route, {})
}
} catch (e) {}
}
/** /**
* Returns the APP_BASE_PATH of the application. * Returns the APP_BASE_PATH of the application.
* @return {string} * @return {string}
@ -39,6 +53,7 @@ class Router {
this.route_args = args this.route_args = args
this.history.push({path, args}) this.history.push({path, args})
window.history.pushState({}, path, this.build_url(path)) window.history.pushState({}, path, this.build_url(path))
this.current_route = path
this.subscribers.forEach(sub => sub(path, args)) this.subscribers.forEach(sub => sub(path, args))
} }