Skip to content

Commit cc606fa

Browse files
committed
Clean up PR #169
Adds the ability to specify <Routes location={HashLocation}> instead of just using a string. Also, use Browserify's events module instead of event-emitter. Also, replaces URLStore with PathStore which takes a location object instead of using a string to identify which location it's using.
1 parent 0e7a182 commit cc606fa

22 files changed

+428
-503
lines changed

modules/components/Routes.js

+37-20
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
var React = require('react');
22
var warning = require('react/lib/warning');
3-
var ExecutionEnvironment = require('react/lib/ExecutionEnvironment');
4-
var mergeProperties = require('../helpers/mergeProperties');
3+
var Promise = require('es6-promise').Promise;
54
var goBack = require('../helpers/goBack');
5+
var mergeProperties = require('../helpers/mergeProperties');
66
var replaceWith = require('../helpers/replaceWith');
77
var transitionTo = require('../helpers/transitionTo');
8-
var Location = require('../helpers/Location');
98
var Route = require('../components/Route');
109
var Path = require('../helpers/Path');
10+
var HashLocation = require('../locations/HashLocation');
11+
var HistoryLocation = require('../locations/HistoryLocation');
12+
var RefreshLocation = require('../locations/RefreshLocation');
1113
var ActiveStore = require('../stores/ActiveStore');
14+
var PathStore = require('../stores/PathStore');
1215
var RouteStore = require('../stores/RouteStore');
13-
var URLStore = require('../stores/URLStore');
14-
var Promise = require('es6-promise').Promise;
1516

1617
/**
1718
* The ref name that can be used to reference the active route component.
1819
*/
1920
var REF_NAME = '__activeRoute__';
2021

22+
/**
23+
* A hash of { name, location } pairs of all locations.
24+
*/
25+
var NAMED_LOCATIONS = {
26+
hash: HashLocation,
27+
history: HistoryLocation,
28+
refresh: RefreshLocation,
29+
disabled: RefreshLocation // TODO: Remove
30+
};
31+
2132
/**
2233
* The <Routes> component configures the route hierarchy and renders the
2334
* route matching the current location when rendered into a document.
@@ -55,48 +66,54 @@ var Routes = React.createClass({
5566

5667
propTypes: {
5768
preserveScrollPosition: React.PropTypes.bool,
58-
location: function(props, propName, componentName) {
69+
location: function (props, propName, componentName) {
5970
var location = props[propName];
60-
if (!Location[location]) {
61-
return new Error('No matching location: "' + location +
62-
'". Must be one of: ' + Object.keys(Location) +
63-
'. See: ' + componentName);
64-
}
71+
72+
if (typeof location === 'string' && !(location in NAMED_LOCATIONS))
73+
return new Error('Unknown location "' + location + '", see ' + componentName);
6574
}
6675
},
6776

6877
getDefaultProps: function () {
6978
return {
70-
location: 'hash',
71-
preserveScrollPosition: false
79+
preserveScrollPosition: false,
80+
location: HashLocation
7281
};
7382
},
7483

7584
getInitialState: function () {
7685
return {};
7786
},
7887

88+
getLocation: function () {
89+
var location = this.props.location;
90+
91+
if (typeof location === 'string')
92+
return NAMED_LOCATIONS[location];
93+
94+
return location;
95+
},
96+
7997
componentWillMount: function () {
8098
React.Children.forEach(this.props.children, function (child) {
8199
RouteStore.registerRoute(child);
82100
});
83101

84-
if (!URLStore.isSetup() && ExecutionEnvironment.canUseDOM)
85-
URLStore.setup(this.props.location);
102+
PathStore.setup(this.getLocation());
86103

87-
URLStore.addChangeListener(this.handleRouteChange);
104+
PathStore.addChangeListener(this.handlePathChange);
88105
},
89106

90107
componentDidMount: function () {
91-
this.dispatch(URLStore.getCurrentPath());
108+
this.handlePathChange();
92109
},
93110

94111
componentWillUnmount: function () {
95-
URLStore.removeChangeListener(this.handleRouteChange);
112+
PathStore.removeChangeListener(this.handlePathChange);
96113
},
97114

98-
handleRouteChange: function () {
99-
this.dispatch(URLStore.getCurrentPath());
115+
handlePathChange: function () {
116+
this.dispatch(PathStore.getCurrentPath());
100117
},
101118

102119
/**

modules/helpers/DisabledLocation.js

-34
This file was deleted.

modules/helpers/HashLocation.js

-60
This file was deleted.

modules/helpers/HistoryLocation.js

-50
This file was deleted.

modules/helpers/Location.js

-10
This file was deleted.

modules/helpers/MemoryLocation.js

-53
This file was deleted.

modules/helpers/goBack.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var URLStore = require('../stores/URLStore');
1+
var PathStore = require('../stores/PathStore');
22

3+
/**
4+
* Transitions to the previous URL.
5+
*/
36
function goBack() {
4-
URLStore.back();
7+
PathStore.pop();
58
}
69

710
module.exports = goBack;

modules/helpers/makeHref.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
var URLStore = require('../stores/URLStore');
1+
var HashLocation = require('../locations/HashLocation');
2+
var PathStore = require('../stores/PathStore');
23
var makePath = require('./makePath');
34

45
/**
56
* Returns a string that may safely be used as the href of a
67
* link to the route with the given name.
78
*/
8-
function makeHref(routeName, params, query) {
9-
var path = makePath(routeName, params, query);
9+
function makeHref(to, params, query) {
10+
var path = makePath(to, params, query);
1011

11-
if (URLStore.getLocation() === 'hash')
12+
if (PathStore.getLocation() === HashLocation)
1213
return '#' + path;
1314

1415
return path;

modules/helpers/replaceWith.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
var URLStore = require('../stores/URLStore');
1+
var PathStore = require('../stores/PathStore');
22
var makePath = require('./makePath');
33

44
/**
55
* Transitions to the URL specified in the arguments by replacing
66
* the current URL in the history stack.
77
*/
88
function replaceWith(to, params, query) {
9-
URLStore.replace(makePath(to, params, query));
9+
PathStore.replace(makePath(to, params, query));
1010
}
1111

1212
module.exports = replaceWith;

modules/helpers/supportsHistory.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function supportsHistory() {
2+
/*! taken from modernizr
3+
* https://github.com/Modernizr/Modernizr/blob/master/LICENSE
4+
* https://github.com/Modernizr/Modernizr/blob/master/feature-detects/history.js
5+
*/
6+
var ua = navigator.userAgent;
7+
if ((ua.indexOf('Android 2.') !== -1 ||
8+
(ua.indexOf('Android 4.0') !== -1)) &&
9+
ua.indexOf('Mobile Safari') !== -1 &&
10+
ua.indexOf('Chrome') === -1) {
11+
return false;
12+
}
13+
return (window.history && 'pushState' in window.history);
14+
}
15+
16+
module.exports = supportsHistory;

0 commit comments

Comments
 (0)