Skip to content

Commit

Permalink
[added] getPathname to Router.State
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Nov 27, 2014
1 parent c7c1fb5 commit b9079c9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
14 changes: 5 additions & 9 deletions modules/mixins/Scrolling.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
var invariant = require('react/lib/invariant');
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
var getWindowScrollPosition = require('../utils/getWindowScrollPosition');
var Path = require('../utils/Path');

function shouldUpdateScroll(state, prevState) {
if (!prevState) {
if (!prevState)
return true;
}

var path = state.path;
// Don't update scroll position when only the query has changed.
if (state.pathname === prevState.pathname)
return false;

var routes = state.routes;
var prevPath = prevState.path;
var prevRoutes = prevState.routes;

if (Path.withoutQuery(path) === Path.withoutQuery(prevPath)) {
return false;
}

var sharedAncestorRoutes = routes.filter(function (route) {
return prevRoutes.indexOf(route) !== -1;
});
Expand Down
8 changes: 8 additions & 0 deletions modules/mixins/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var State = {
contextTypes: {
getCurrentPath: React.PropTypes.func.isRequired,
getCurrentRoutes: React.PropTypes.func.isRequired,
getCurrentPathname: React.PropTypes.func.isRequired,
getCurrentParams: React.PropTypes.func.isRequired,
getCurrentQuery: React.PropTypes.func.isRequired,
isActive: React.PropTypes.func.isRequired
Expand All @@ -42,6 +43,13 @@ var State = {
return this.context.getCurrentRoutes();
},

/**
* Returns the current URL path without the query string.
*/
getPathname: function () {
return this.context.getCurrentPathname();
},

/**
* Returns an object of the URL params that are currently active.
*/
Expand Down
9 changes: 9 additions & 0 deletions modules/mixins/StateContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ var StateContext = {
return this.state.routes.slice(0);
},

/**
* Returns the current URL path without the query string.
*/
getCurrentPathname: function () {
return this.state.pathname;
},

/**
* Returns a read-only object of the currently active URL parameters.
*/
Expand Down Expand Up @@ -72,6 +79,7 @@ var StateContext = {
childContextTypes: {
getCurrentPath: React.PropTypes.func.isRequired,
getCurrentRoutes: React.PropTypes.func.isRequired,
getCurrentPathname: React.PropTypes.func.isRequired,
getCurrentParams: React.PropTypes.func.isRequired,
getCurrentQuery: React.PropTypes.func.isRequired,
isActive: React.PropTypes.func.isRequired
Expand All @@ -81,6 +89,7 @@ var StateContext = {
return {
getCurrentPath: this.getCurrentPath,
getCurrentRoutes: this.getCurrentRoutes,
getCurrentPathname: this.getCurrentPathname,
getCurrentParams: this.getCurrentParams,
getCurrentQuery: this.getCurrentQuery,
isActive: this.isActive
Expand Down
8 changes: 5 additions & 3 deletions modules/utils/createRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ function createRouter(options) {
* Performs a match of the given pathname against this router and returns an object
* with the { routes, params } that match. Returns null if no match can be made.
*/
match: function (path) {
return findMatch(Path.withoutQuery(path), routes, this.defaultRoute, this.notFoundRoute) || null;
match: function (pathname) {
return findMatch(pathname, routes, this.defaultRoute, this.notFoundRoute) || null;
},

/**
Expand Down Expand Up @@ -289,7 +289,8 @@ function createRouter(options) {
this.recordScrollPosition(prevPath);
}

var match = this.match(path);
var pathname = Path.withoutQuery(path);
var match = this.match(pathname);

warning(
match != null,
Expand Down Expand Up @@ -334,6 +335,7 @@ function createRouter(options) {

nextState.path = path;
nextState.action = action;
nextState.pathname = pathname;
nextState.routes = nextRoutes;
nextState.params = nextParams;
nextState.query = nextQuery;
Expand Down

0 comments on commit b9079c9

Please sign in to comment.