Fix standalone router bugs

This commit is contained in:
Colin McLeod
2016-03-06 22:39:33 -08:00
parent 32795ea678
commit f060eb1b62
2 changed files with 23 additions and 10 deletions

View File

@@ -56,7 +56,7 @@ export default class Coriolis extends React.Component {
this.state = { this.state = {
page: null, page: null,
language: getLanguage(Persist.getLangCode()), language: getLanguage(Persist.getLangCode()),
route: null, route: {},
sizeRatio: Persist.getSizeRatio() sizeRatio: Persist.getSizeRatio()
}; };

View File

@@ -1,16 +1,21 @@
import Persist from './stores/Persist'; import Persist from './stores/Persist';
let standalone = undefined;
/** /**
* Determine if the app is running in mobile/tablet 'standalone' mode * Determine if the app is running in mobile/tablet 'standalone' mode
* @return {Boolean} True if the app is in standalone mode * @return {Boolean} True if the app is in standalone mode
*/ */
function isStandAlone() { function isStandAlone() {
if (standalone === undefined) {
try { try {
return window.navigator.standalone || (window.external && window.external.msIsSiteMode && window.external.msIsSiteMode()); standalone = window.navigator.standalone || (window.external && window.external.msIsSiteMode && window.external.msIsSiteMode());
} catch (ex) { } catch (ex) {
return false; standalone = false;
} }
} }
return standalone;
}
/** /**
* Register path with callback fn(), or route path`, or Router.start(). * Register path with callback fn(), or route path`, or Router.start().
@@ -44,14 +49,14 @@ Router.start = function() {
if (isStandAlone()) { if (isStandAlone()) {
let state = Persist.getState(); let state = Persist.getState();
// If a previous state has been stored, load that state // If a previous state has been stored, load that state
if (state && state.name && state.params) { if (state && state.path) {
Router(this.props.initialPath || '/'); Router.replace(state.path, null, true);
} else { } else {
Router('/'); Router.replace('/', null, true);
} }
} else { } else {
let url = location.pathname + location.search; let url = location.pathname + location.search;
Router.replace(url, null, true, true); Router.replace(url, null, true);
} }
}; };
@@ -68,6 +73,9 @@ Router.go = function(path, state) {
let ctx = new Context(path, state); let ctx = new Context(path, state);
Router.dispatch(ctx); Router.dispatch(ctx);
if (!ctx.unhandled) { if (!ctx.unhandled) {
if (isStandAlone()) {
Persist.setState(ctx);
}
history.pushState(ctx.state, ctx.title, ctx.canonicalPath); history.pushState(ctx.state, ctx.title, ctx.canonicalPath);
} }
return ctx; return ctx;
@@ -85,7 +93,12 @@ Router.go = function(path, state) {
Router.replace = function(path, state, dispatch) { Router.replace = function(path, state, dispatch) {
gaTrack(path); gaTrack(path);
let ctx = new Context(path, state); let ctx = new Context(path, state);
if (dispatch) Router.dispatch(ctx); if (dispatch) {
Router.dispatch(ctx);
}
if (isStandAlone()) {
Persist.setState(ctx);
}
history.replaceState(ctx.state, ctx.title, ctx.canonicalPath); history.replaceState(ctx.state, ctx.title, ctx.canonicalPath);
return ctx; return ctx;
}; };