UNPKG

9.17 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends2 = require('babel-runtime/helpers/extends');
8
9var _extends3 = _interopRequireDefault(_extends2);
10
11var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
12
13var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
14
15var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
16
17var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
18
19var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
20
21var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
22
23var _createClass2 = require('babel-runtime/helpers/createClass');
24
25var _createClass3 = _interopRequireDefault(_createClass2);
26
27var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
28
29var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
30
31var _inherits2 = require('babel-runtime/helpers/inherits');
32
33var _inherits3 = _interopRequireDefault(_inherits2);
34
35var _react = require('react');
36
37var _react2 = _interopRequireDefault(_react);
38
39var _reactRouter = require('react-router');
40
41function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
42
43/**
44 * <Router />
45 *
46 * This is adapted from <ReactRouter.Router /> to enable routes hot reloading
47 * and strip away unneeded functionality.
48 *
49 * @copyright 2016-present, Sitegen team
50 */
51
52var INITIAL_STATE = {
53 location: null,
54 routes: null,
55 params: null,
56 components: null
57};
58
59var Router = function (_React$Component) {
60 (0, _inherits3.default)(Router, _React$Component);
61
62 function Router(props) {
63 (0, _classCallCheck3.default)(this, Router);
64
65 var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(Router).call(this, props));
66
67 _this.state = INITIAL_STATE;
68 return _this;
69 }
70
71 (0, _createClass3.default)(Router, [{
72 key: 'render',
73 value: function render() {
74 var _state = this.state;
75 var location = _state.location;
76 var routes = _state.routes;
77 var params = _state.params;
78 var components = _state.components;
79 var _props = this.props;
80 var _props$matchContext = _props.matchContext;
81 var history = _props$matchContext.history;
82 var router = _props$matchContext.router;
83 var props = (0, _objectWithoutProperties3.default)(_props, ['matchContext']);
84
85
86 if (location == null) {
87 // Async match
88 return null;
89 }
90
91 return _react2.default.createElement(_reactRouter.RouterContext, (0, _extends3.default)({}, props, {
92 history: history,
93 router: router,
94 location: location,
95 routes: routes,
96 params: params,
97 components: components
98 }));
99 }
100 }, {
101 key: 'componentWillMount',
102 value: function componentWillMount() {
103 this.initializeRouter();
104 }
105 }, {
106 key: 'componentWillReceiveProps',
107 value: function componentWillReceiveProps(nextProps) {
108 if (nextProps.history !== this.props.history || nextProps.routes !== this.props.routes) {
109 this.setState(INITIAL_STATE);
110 this.initializeRouter();
111 }
112 }
113 }, {
114 key: 'componentWillUnmount',
115 value: function componentWillUnmount() {
116 if (this._unlisten) {
117 this._unlisten();
118 }
119 }
120 }, {
121 key: 'handleError',
122 value: function handleError(error) {
123 if (this.props.onError) {
124 this.props.onError.call(this, error);
125 } else {
126 // Throw errors by default so we don't silently swallow them!
127 // This error probably occurred in getChildRoutes or getComponents.
128 throw error;
129 }
130 }
131 }, {
132 key: 'initializeRouter',
133 value: function initializeRouter() {
134 var _this2 = this;
135
136 var transitionManager = this.props.matchContext.transitionManager;
137
138
139 if (this._unlisten) {
140 this._unlisten();
141 }
142
143 this._unlisten = transitionManager.listen(function (error, state) {
144 if (error) {
145 _this2.handleError(error);
146 } else {
147 _this2.setState(state, _this2.props.onUpdate);
148 }
149 });
150 }
151 }]);
152 return Router;
153}(_react2.default.Component);
154
155exports.default = Router;
156//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9Vc2Vycy9hbmRyZXlwb3BwL1dvcmtzcGFjZS9zaXRlZ2VuL3NyYy9Sb3V0ZXIuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7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VxuICAgICAgICBjb21wb25lbnRzPXtjb21wb25lbnRzfVxuICAgICAgICAvPlxuICAgICk7XG4gIH1cblxuICBjb21wb25lbnRXaWxsTW91bnQoKSB7XG4gICAgdGhpcy5pbml0aWFsaXplUm91dGVyKCk7XG4gIH1cblxuICBjb21wb25lbnRXaWxsUmVjZWl2ZVByb3BzKG5leHRQcm9wcykge1xuICAgIGlmIChcbiAgICAgIG5leHRQcm9wcy5oaXN0b3J5ICE9PSB0aGlzLnByb3BzLmhpc3RvcnkgfHxcbiAgICAgIG5leHRQcm9wcy5yb3V0ZXMgIT09IHRoaXMucHJvcHMucm91dGVzXG4gICAgKSB7XG4gICAgICB0aGlzLnNldFN0YXRlKElOSVRJQUxfU1RBVEUpO1xuICAgICAgdGhpcy5pbml0aWFsaXplUm91dGVyKCk7XG4gICAgfVxuICB9XG5cbiAgY29tcG9uZW50V2lsbFVubW91bnQoKSB7XG4gICAgaWYgKHRoaXMuX3VubGlzdGVuKSB7XG4gICAgICB0aGlzLl91bmxpc3RlbigpO1xuICAgIH1cbiAgfVxuXG4gIGhhbmRsZUVycm9yKGVycm9yKSB7XG4gICAgaWYgKHRoaXMucHJvcHMub25FcnJvcikge1xuICAgICAgdGhpcy5wcm9wcy5vbkVycm9yLmNhbGwodGhpcywgZXJyb3IpO1xuICAgIH0gZWxzZSB7XG4gICAgICAvLyBUaHJvdyBlcnJvcnMgYnkgZGVmYXVsdCBzbyB3ZSBkb24ndCBzaWxlbnRseSBzd2FsbG93IHRoZW0hXG4gICAgICAvLyBUaGlzIGVycm9yIHByb2JhYmx5IG9jY3VycmVkIGluIGdldENoaWxkUm91dGVzIG9yIGdldENvbXBvbmVudHMuXG4gICAgICB0aHJvdyBlcnJvcjtcbiAgICB9XG4gIH1cblxuICBpbml0aWFsaXplUm91dGVyKCkge1xuICAgIGxldCB7dHJhbnNpdGlvbk1hbmFnZXJ9ID0gdGhpcy5wcm9wcy5tYXRjaENvbnRleHQ7XG5cbiAgICBpZiAodGhpc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