|
1 | 1 | var React = require('react');
|
2 | 2 | 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; |
5 | 4 | var goBack = require('../helpers/goBack');
|
| 5 | +var mergeProperties = require('../helpers/mergeProperties'); |
6 | 6 | var replaceWith = require('../helpers/replaceWith');
|
7 | 7 | var transitionTo = require('../helpers/transitionTo');
|
8 |
| -var Location = require('../helpers/Location'); |
9 | 8 | var Route = require('../components/Route');
|
10 | 9 | var Path = require('../helpers/Path');
|
| 10 | +var HashLocation = require('../locations/HashLocation'); |
| 11 | +var HistoryLocation = require('../locations/HistoryLocation'); |
| 12 | +var RefreshLocation = require('../locations/RefreshLocation'); |
11 | 13 | var ActiveStore = require('../stores/ActiveStore');
|
| 14 | +var PathStore = require('../stores/PathStore'); |
12 | 15 | var RouteStore = require('../stores/RouteStore');
|
13 |
| -var URLStore = require('../stores/URLStore'); |
14 |
| -var Promise = require('es6-promise').Promise; |
15 | 16 |
|
16 | 17 | /**
|
17 | 18 | * The ref name that can be used to reference the active route component.
|
18 | 19 | */
|
19 | 20 | var REF_NAME = '__activeRoute__';
|
20 | 21 |
|
| 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 | + |
21 | 32 | /**
|
22 | 33 | * The <Routes> component configures the route hierarchy and renders the
|
23 | 34 | * route matching the current location when rendered into a document.
|
@@ -55,48 +66,54 @@ var Routes = React.createClass({
|
55 | 66 |
|
56 | 67 | propTypes: {
|
57 | 68 | preserveScrollPosition: React.PropTypes.bool,
|
58 |
| - location: function(props, propName, componentName) { |
| 69 | + location: function (props, propName, componentName) { |
59 | 70 | 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); |
65 | 74 | }
|
66 | 75 | },
|
67 | 76 |
|
68 | 77 | getDefaultProps: function () {
|
69 | 78 | return {
|
70 |
| - location: 'hash', |
71 |
| - preserveScrollPosition: false |
| 79 | + preserveScrollPosition: false, |
| 80 | + location: HashLocation |
72 | 81 | };
|
73 | 82 | },
|
74 | 83 |
|
75 | 84 | getInitialState: function () {
|
76 | 85 | return {};
|
77 | 86 | },
|
78 | 87 |
|
| 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 | + |
79 | 97 | componentWillMount: function () {
|
80 | 98 | React.Children.forEach(this.props.children, function (child) {
|
81 | 99 | RouteStore.registerRoute(child);
|
82 | 100 | });
|
83 | 101 |
|
84 |
| - if (!URLStore.isSetup() && ExecutionEnvironment.canUseDOM) |
85 |
| - URLStore.setup(this.props.location); |
| 102 | + PathStore.setup(this.getLocation()); |
86 | 103 |
|
87 |
| - URLStore.addChangeListener(this.handleRouteChange); |
| 104 | + PathStore.addChangeListener(this.handlePathChange); |
88 | 105 | },
|
89 | 106 |
|
90 | 107 | componentDidMount: function () {
|
91 |
| - this.dispatch(URLStore.getCurrentPath()); |
| 108 | + this.handlePathChange(); |
92 | 109 | },
|
93 | 110 |
|
94 | 111 | componentWillUnmount: function () {
|
95 |
| - URLStore.removeChangeListener(this.handleRouteChange); |
| 112 | + PathStore.removeChangeListener(this.handlePathChange); |
96 | 113 | },
|
97 | 114 |
|
98 |
| - handleRouteChange: function () { |
99 |
| - this.dispatch(URLStore.getCurrentPath()); |
| 115 | + handlePathChange: function () { |
| 116 | + this.dispatch(PathStore.getCurrentPath()); |
100 | 117 | },
|
101 | 118 |
|
102 | 119 | /**
|
|
0 commit comments