Skip to content

Commit

Permalink
feat: react to manual change in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jul 3, 2015
1 parent 5d77885 commit 1e35d18
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 33 deletions.
27 changes: 19 additions & 8 deletions dist/browser/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,11 @@ var Router5 = (function () {
key: 'onPopState',
value: function onPopState(evt) {
// Do nothing if no state or if last know state is poped state (it should never happen)
if (!evt.state) return;
if (this.lastKnownState && this.areStatesEqual(evt.state, this.lastKnownState)) return;
var state = evt.state || this.matchPath(this.getWindowPath());
if (!state) return;
if (this.lastKnownState && this.areStatesEqual(state, this.lastKnownState)) return;

var canTransition = this._transition(evt.state, this.lastKnownState);
if (canTransition) this.lastKnownState = evt.state;
var canTransition = this._transition(state, this.lastKnownState);
}
}, {
key: 'start',
Expand All @@ -497,11 +497,11 @@ var Router5 = (function () {
this.started = true;

// Try to match starting path name
var startPath = this.options.useHash ? window.location.hash.replace(/^#/, '') : window.location.pathname;
var startMatch = this.rootNode.matchPath(startPath);
var startPath = this.getWindowPath();
var startState = this.matchPath(startPath);

if (startMatch) {
this.lastKnownState = makeState(startMatch.name, startMatch.params, startPath);
if (startState) {
this.lastKnownState = startState;
window.history.replaceState(this.lastKnownState, '', this.options.useHash ? '#' + startPath : startPath);
} else if (this.options.defaultRoute) {
this.navigate(this.options.defaultRoute, this.options.defaultParams, { replace: true });
Expand Down Expand Up @@ -573,6 +573,11 @@ var Router5 = (function () {
// return window.history.state
;
}
}, {
key: 'getWindowPath',
value: function getWindowPath() {
return this.options.useHash ? window.location.hash.replace(/^#/, '') : window.location.pathname;
}
}, {
key: 'areStatesEqual',
value: function areStatesEqual(state1, state2) {
Expand Down Expand Up @@ -624,6 +629,12 @@ var Router5 = (function () {
value: function buildPath(route, params) {
return (this.options.useHash ? '#' : '') + this.rootNode.buildPath(route, params);
}
}, {
key: 'matchPath',
value: function matchPath(path) {
var match = this.rootNode.matchPath(path);
return match ? makeState(match.name, match.params, path) : null;
}
}, {
key: 'navigate',
value: function navigate(name) {
Expand Down

0 comments on commit 1e35d18

Please sign in to comment.