react-router
Version:
A complete routing library for React.js
1,769 lines (1,338 loc) • 77.4 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("react"));
else if(typeof define === 'function' && define.amd)
define(["react"], factory);
else if(typeof exports === 'object')
exports["ReactRouter"] = factory(require("react"));
else
root["ReactRouter"] = factory(root["React"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
/* components */
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _Router2 = __webpack_require__(17);
var _Router3 = _interopRequireDefault(_Router2);
exports.Router = _Router3['default'];
var _Link2 = __webpack_require__(13);
var _Link3 = _interopRequireDefault(_Link2);
exports.Link = _Link3['default'];
/* components (configuration) */
var _Redirect2 = __webpack_require__(15);
var _Redirect3 = _interopRequireDefault(_Redirect2);
exports.Redirect = _Redirect3['default'];
var _Route2 = __webpack_require__(16);
var _Route3 = _interopRequireDefault(_Route2);
exports.Route = _Route3['default'];
/* mixins */
var _Navigation2 = __webpack_require__(14);
var _Navigation3 = _interopRequireDefault(_Navigation2);
exports.Navigation = _Navigation3['default'];
var _TransitionHook2 = __webpack_require__(23);
var _TransitionHook3 = _interopRequireDefault(_TransitionHook2);
exports.TransitionHook = _TransitionHook3['default'];
var _State2 = __webpack_require__(21);
var _State3 = _interopRequireDefault(_State2);
exports.State = _State3['default'];
/* utils */
var _RouteUtils = __webpack_require__(3);
Object.defineProperty(exports, 'createRoutesFromReactChildren', {
enumerable: true,
get: function get() {
return _RouteUtils.createRoutesFromReactChildren;
}
});
var _PropTypes2 = __webpack_require__(4);
var _PropTypes3 = _interopRequireDefault(_PropTypes2);
exports.PropTypes = _PropTypes3['default'];
var _Router4 = _interopRequireDefault(_Router2);
exports['default'] = _Router4['default'];
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
module.exports = __WEBPACK_EXTERNAL_MODULE_1__;
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule invariant
*/
'use strict';
/**
* Use invariant() to assert state which your program assumes to be true.
*
* Provide sprintf-style format (only %s is supported) and arguments
* to provide information about what broke and what you were
* expecting.
*
* The invariant message will be stripped in production, but the invariant
* will remain to ensure logic does not differ in production.
*/
var invariant = function(condition, format, a, b, c, d, e, f) {
if (false) {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
}
if (!condition) {
var error;
if (format === undefined) {
error = new Error(
'Minified exception occurred; use the non-minified dev environment ' +
'for the full error message and additional helpful warnings.'
);
} else {
var args = [a, b, c, d, e, f];
var argIndex = 0;
error = new Error(
'Invariant Violation: ' +
format.replace(/%s/g, function() { return args[argIndex++]; })
);
}
error.framesToPop = 1; // we don't care about invariant's own frame
throw error;
}
};
module.exports = invariant;
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.isReactChildren = isReactChildren;
exports.createRouteFromReactElement = createRouteFromReactElement;
exports.createRoutesFromReactChildren = createRoutesFromReactChildren;
exports.createRoutes = createRoutes;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var _warning = __webpack_require__(7);
var _warning2 = _interopRequireDefault(_warning);
function isValidChild(object) {
return object == null || (0, _react.isValidElement)(object);
}
function isReactChildren(object) {
return isValidChild(object) || Array.isArray(object) && object.every(isValidChild);
}
function checkPropTypes(componentName, propTypes, props) {
componentName = componentName || 'UnknownComponent';
for (var propName in propTypes) {
if (propTypes.hasOwnProperty(propName)) {
var error = propTypes[propName](props, propName, componentName);
if (error instanceof Error) (0, _warning2['default'])(false, error.message);
}
}
}
function createRouteFromReactElement(element) {
var type = element.type;
var route = _extends({}, type.defaultProps, element.props);
if (type.propTypes) checkPropTypes(type.displayName || type.name, type.propTypes, route);
if (route.children) {
route.childRoutes = createRoutesFromReactChildren(route.children);
delete route.children;
}
return route;
}
/**
* Creates and returns a routes object from the given ReactChildren. JSX
* provides a convenient way to visualize how routes in the hierarchy are
* nested.
*
* import { Route, createRoutesFromReactChildren } from 'react-router';
*
* var routes = createRoutesFromReactChildren(
* <Route component={App}>
* <Route path="home" component={Dashboard}/>
* <Route path="news" component={NewsFeed}/>
* </Route>
* );
*
* Note: This method is automatically used when you provide <Route> children
* to a <Router> component.
*/
function createRoutesFromReactChildren(children) {
var routes = [];
_react2['default'].Children.forEach(children, function (element) {
if ((0, _react.isValidElement)(element)) {
// Component classes may have a static create* method.
if (element.type.createRouteFromReactElement) {
routes.push(element.type.createRouteFromReactElement(element));
} else {
routes.push(createRouteFromReactElement(element));
}
}
});
return routes;
}
/**
* Creates and returns an array of routes from the given object which
* may be a JSX route, a plain object route, or an array of either.
*/
function createRoutes(routes) {
if (isReactChildren(routes)) {
routes = createRoutesFromReactChildren(routes);
} else if (!Array.isArray(routes)) {
routes = [routes];
}
return routes;
}
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var _Location = __webpack_require__(5);
var _Location2 = _interopRequireDefault(_Location);
var _History = __webpack_require__(12);
var _History2 = _interopRequireDefault(_History);
var _React$PropTypes = _react2['default'].PropTypes;
var func = _React$PropTypes.func;
var object = _React$PropTypes.object;
var arrayOf = _React$PropTypes.arrayOf;
var instanceOf = _React$PropTypes.instanceOf;
var oneOfType = _React$PropTypes.oneOfType;
var element = _React$PropTypes.element;
function falsy(props, propName, componentName) {
if (props[propName]) return new Error('<' + componentName + '> should not have a "' + propName + '" prop');
}
var component = func;
var components = oneOfType([component, object]);
var history = instanceOf(_History2['default']);
var location = instanceOf(_Location2['default']);
var route = oneOfType([object, element]);
var routes = oneOfType([route, arrayOf(route)]);
module.exports = {
falsy: falsy,
component: component,
components: components,
history: history,
location: location,
route: route,
routes: routes
};
/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _NavigationTypes = __webpack_require__(9);
var _NavigationTypes2 = _interopRequireDefault(_NavigationTypes);
/**
* A Location answers two important questions:
*
* 1. Where am I?
* 2. How did I get here?
*/
var Location = (function () {
function Location() {
var pathname = arguments[0] === undefined ? '/' : arguments[0];
var query = arguments[1] === undefined ? null : arguments[1];
var state = arguments[2] === undefined ? null : arguments[2];
var navigationType = arguments[3] === undefined ? _NavigationTypes2['default'].POP : arguments[3];
_classCallCheck(this, Location);
this.pathname = pathname;
this.query = query;
this.state = state;
this.navigationType = navigationType;
}
_createClass(Location, null, [{
key: 'isLocation',
value: function isLocation(object) {
return object instanceof Location;
}
}]);
return Location;
})();
exports['default'] = Location;
module.exports = exports['default'];
/***/ },
/* 6 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.stringifyQuery = stringifyQuery;
exports.getPathname = getPathname;
exports.getQueryString = getQueryString;
exports.stripLeadingSlashes = stripLeadingSlashes;
exports.isAbsolutePath = isAbsolutePath;
exports.compilePattern = compilePattern;
exports.matchPattern = matchPattern;
exports.getParamNames = getParamNames;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _qs = __webpack_require__(25);
var _qs2 = _interopRequireDefault(_qs);
var parseQueryString = _qs2['default'].parse;
exports.parseQueryString = parseQueryString;
function stringifyQuery(query) {
return _qs2['default'].stringify(query, { arrayFormat: 'brackets' });
}
var queryMatcher = /\?(.*)$/;
function getPathname(path) {
return path.replace(queryMatcher, '');
}
function getQueryString(path) {
var match = path.match(queryMatcher);
return match ? match[1] : '';
}
function stripLeadingSlashes(path) {
return path ? path.replace(/^\/+/, '') : '';
}
function isAbsolutePath(path) {
return typeof path === 'string' && path.charAt(0) === '/';
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function escapeSource(string) {
return escapeRegExp(string).replace(/\/+/g, '/+');
}
function _compilePattern(pattern) {
var regexpSource = '';
var paramNames = [];
var tokens = [];
var match,
lastIndex = 0,
matcher = /:([a-zA-Z_$][a-zA-Z0-9_$]*)|\*|\(|\)/g;
while (match = matcher.exec(pattern)) {
if (match.index !== lastIndex) {
tokens.push(pattern.slice(lastIndex, match.index));
regexpSource += escapeSource(pattern.slice(lastIndex, match.index));
}
if (match[1]) {
regexpSource += '([^/?#]+)';
paramNames.push(match[1]);
} else if (match[0] === '*') {
regexpSource += '(.*?)';
paramNames.push('splat');
} else if (match[0] === '(') {
regexpSource += '(?:';
} else if (match[0] === ')') {
regexpSource += ')?';
}
tokens.push(match[0]);
lastIndex = matcher.lastIndex;
}
if (lastIndex !== pattern.length) {
tokens.push(pattern.slice(lastIndex, pattern.length));
regexpSource += escapeSource(pattern.slice(lastIndex, pattern.length));
}
return {
pattern: pattern,
regexpSource: regexpSource,
paramNames: paramNames,
tokens: tokens
};
}
var CompiledPatternsCache = {};
function compilePattern(pattern) {
if (!(pattern in CompiledPatternsCache)) CompiledPatternsCache[pattern] = _compilePattern(pattern);
return CompiledPatternsCache[pattern];
}
/**
* Attempts to match a pattern on the given pathname. Patterns may use
* the following special characters:
*
* - :paramName Matches a URL segment up to the next /, ?, or #. The
* captured string is considered a "param"
* - () Wraps a segment of the URL that is optional
* - * Consumes (non-greedy) all characters up to the next
* character in the pattern, or to the end of the URL if
* there is none
*
* The return value is an object with the following properties:
*
* - remainingPathname
* - paramNames
* - paramValues
*/
function matchPattern(pattern, pathname) {
var _compilePattern2 = compilePattern(stripLeadingSlashes(pattern));
var regexpSource = _compilePattern2.regexpSource;
var paramNames = _compilePattern2.paramNames;
var tokens = _compilePattern2.tokens;
regexpSource += '/*'; // Ignore trailing slashes
var captureRemaining = tokens[tokens.length - 1] !== '*';
if (captureRemaining) regexpSource += '(.*?)';
var match = pathname.match(new RegExp('^' + regexpSource + '$', 'i'));
var remainingPathname, paramValues;
if (match != null) {
paramValues = Array.prototype.slice.call(match, 1);
if (captureRemaining) {
remainingPathname = paramValues.pop();
} else {
remainingPathname = pathname.replace(match[0], '');
}
}
return {
remainingPathname: remainingPathname,
paramNames: paramNames,
paramValues: paramValues
};
}
function getParamNames(pattern) {
return compilePattern(pattern).paramNames;
}
/***/ },
/* 7 */
/***/ function(module, exports, __webpack_require__) {
/**
* Copyright 2014-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule warning
*/
'use strict';
/**
* Similar to invariant but only logs a warning if the condition is not met.
* This can be used to log issues in development environments in critical
* paths. Removing the logging code for production environments will keep the
* same logic and follow the same code paths.
*/
var __DEV__ = ("production") !== 'production';
var warning = function() {};
if (__DEV__) {
warning = function(condition, format, args) {
var len = arguments.length;
args = new Array(len > 2 ? len - 2 : 0);
for (var key = 2; key < len; key++) {
args[key - 2] = arguments[key];
}
if (format === undefined) {
throw new Error(
'`warning(condition, format, ...args)` requires a warning ' +
'message argument'
);
}
if (format.length < 10 || (/^[s\W]*$/).test(format)) {
throw new Error(
'The warning format should be able to uniquely identify this ' +
'warning. Please, use a more descriptive format than: ' + format
);
}
if (!condition) {
var argIndex = 0;
var message = 'Warning: ' +
format.replace(/%s/g, function() {
args[argIndex++];
});
if (typeof console !== 'undefined') {
console.error(message);
}
try {
// --- Welcome to debugging React ---
// This error was thrown as a convenience so that you can use this stack
// to find the callsite that caused this warning to fire.
throw new Error(message);
} catch(x) {}
}
};
}
module.exports = warning;
/***/ },
/* 8 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.loopAsync = loopAsync;
exports.mapAsync = mapAsync;
exports.hashAsync = hashAsync;
function loopAsync(turns, work, callback) {
var currentTurn = 0;
var isDone = false;
function done() {
isDone = true;
callback.apply(this, arguments);
}
function next() {
if (isDone) return;
if (currentTurn < turns) {
currentTurn += 1;
work.call(this, currentTurn - 1, next, done);
} else {
done.apply(this, arguments);
}
}
next();
}
function mapAsync(array, work, callback) {
var length = array.length;
var values = [];
if (length === 0) return callback(null, values);
var isDone = false;
var doneCount = 0;
function done(index, error, value) {
if (isDone) return;
if (error) {
isDone = true;
callback(error);
} else {
values[index] = value;
isDone = ++doneCount === length;
if (isDone) callback(null, values);
}
}
array.forEach(function (item, index) {
work(item, index, function (error, value) {
done(index, error, value);
});
});
}
function hashAsync(object, work, callback) {
var keys = Object.keys(object);
mapAsync(keys, function (key, index, callback) {
work(object[key], callback);
}, function (error, valuesArray) {
if (error) {
callback(error);
} else {
var values = valuesArray.reduce(function (memo, results, index) {
memo[keys[index]] = results;
return memo;
}, {});
callback(null, values);
}
});
}
/***/ },
/* 9 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _keymirror = __webpack_require__(24);
var _keymirror2 = _interopRequireDefault(_keymirror);
var NavigationTypes = (0, _keymirror2['default'])({
/**
* Indicates that navigation was caused by a call to history.push.
*/
PUSH: null,
/**
* Indicates that navigation was caused by a call to history.replace.
*/
REPLACE: null,
/**
* Indicates that navigation was caused by some other action such
* as using a browser's back/forward buttons and/or manually manipulating
* the URL in a browser's location bar. This is the default.
*
* See https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
* for more information.
*/
POP: null
});
exports['default'] = NavigationTypes;
module.exports = exports['default'];
/***/ },
/* 10 */
/***/ function(module, exports, __webpack_require__) {
// Load modules
// Declare internals
var internals = {};
exports.arrayToObject = function (source) {
var obj = {};
for (var i = 0, il = source.length; i < il; ++i) {
if (typeof source[i] !== 'undefined') {
obj[i] = source[i];
}
}
return obj;
};
exports.merge = function (target, source) {
if (!source) {
return target;
}
if (typeof source !== 'object') {
if (Array.isArray(target)) {
target.push(source);
}
else {
target[source] = true;
}
return target;
}
if (typeof target !== 'object') {
target = [target].concat(source);
return target;
}
if (Array.isArray(target) &&
!Array.isArray(source)) {
target = exports.arrayToObject(target);
}
var keys = Object.keys(source);
for (var k = 0, kl = keys.length; k < kl; ++k) {
var key = keys[k];
var value = source[key];
if (!target[key]) {
target[key] = value;
}
else {
target[key] = exports.merge(target[key], value);
}
}
return target;
};
exports.decode = function (str) {
try {
return decodeURIComponent(str.replace(/\+/g, ' '));
} catch (e) {
return str;
}
};
exports.compact = function (obj, refs) {
if (typeof obj !== 'object' ||
obj === null) {
return obj;
}
refs = refs || [];
var lookup = refs.indexOf(obj);
if (lookup !== -1) {
return refs[lookup];
}
refs.push(obj);
if (Array.isArray(obj)) {
var compacted = [];
for (var i = 0, il = obj.length; i < il; ++i) {
if (typeof obj[i] !== 'undefined') {
compacted.push(obj[i]);
}
}
return compacted;
}
var keys = Object.keys(obj);
for (i = 0, il = keys.length; i < il; ++i) {
var key = keys[i];
obj[key] = exports.compact(obj[key], refs);
}
return obj;
};
exports.isRegExp = function (obj) {
return Object.prototype.toString.call(obj) === '[object RegExp]';
};
exports.isBuffer = function (obj) {
if (obj === null ||
typeof obj === 'undefined') {
return false;
}
return !!(obj.constructor &&
obj.constructor.isBuffer &&
obj.constructor.isBuffer(obj));
};
/***/ },
/* 11 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.getHashPath = getHashPath;
exports.replaceHashPath = replaceHashPath;
exports.getWindowPath = getWindowPath;
exports.getWindowScrollPosition = getWindowScrollPosition;
exports.setWindowScrollPosition = setWindowScrollPosition;
exports.supportsHistory = supportsHistory;
var canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement);
exports.canUseDOM = canUseDOM;
function getHashPath() {
return decodeURI(
// We can't use window.location.hash here because it's not
// consistent across browsers - Firefox will pre-decode it!
window.location.href.split('#')[1] || '');
}
function replaceHashPath(path) {
window.location.replace(window.location.pathname + window.location.search + '#' + path);
}
function getWindowPath() {
return decodeURI(window.location.pathname + window.location.search);
}
function getWindowScrollPosition() {
return {
scrollX: window.pageXOffset || document.documentElement.scrollLeft,
scrollY: window.pageYOffset || document.documentElement.scrollTop
};
}
function setWindowScrollPosition(scrollX, scrollY) {
window.scrollTo(scrollX, scrollY);
}
/**
* taken from modernizr
* https://github.com/Modernizr/Modernizr/blob/master/LICENSE
* https://github.com/Modernizr/Modernizr/blob/master/feature-detects/history.js
* changed to avoid false negatives for Windows Phones: https://github.com/rackt/react-router/issues/586
*/
function supportsHistory() {
var ua = navigator.userAgent;
if ((ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) && ua.indexOf('Mobile Safari') !== -1 && ua.indexOf('Chrome') === -1 && ua.indexOf('Windows Phone') === -1) {
return false;
}
return window.history && 'pushState' in window.history;
}
/***/ },
/* 12 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _invariant = __webpack_require__(2);
var _invariant2 = _interopRequireDefault(_invariant);
var _URLUtils = __webpack_require__(6);
var _Location = __webpack_require__(5);
var _Location2 = _interopRequireDefault(_Location);
var RequiredHistorySubclassMethods = ['pushState', 'replaceState', 'go'];
function createRandomKey() {
return Math.random().toString(36).substr(2);
}
/**
* A history interface that normalizes the differences across
* various environments and implementations. Requires concrete
* subclasses to implement the following methods:
*
* - pushState(state, path)
* - replaceState(state, path)
* - go(n)
*/
var History = (function () {
function History() {
var options = arguments[0] === undefined ? {} : arguments[0];
_classCallCheck(this, History);
RequiredHistorySubclassMethods.forEach(function (method) {
(0, _invariant2['default'])(typeof this[method] === 'function', '%s needs a "%s" method', this.constructor.name, method);
}, this);
this.parseQueryString = options.parseQueryString || _URLUtils.parseQueryString;
this.changeListeners = [];
this.location = null;
}
_createClass(History, [{
key: '_notifyChange',
value: function _notifyChange() {
for (var i = 0, len = this.changeListeners.length; i < len; ++i) this.changeListeners[i].call(this);
}
}, {
key: 'addChangeListener',
value: function addChangeListener(listener) {
this.changeListeners.push(listener);
}
}, {
key: 'removeChangeListener',
value: function removeChangeListener(listener) {
this.changeListeners = this.changeListeners.filter(function (li) {
return li !== listener;
});
}
}, {
key: 'back',
value: function back() {
this.go(-1);
}
}, {
key: 'forward',
value: function forward() {
this.go(1);
}
}, {
key: '_createState',
value: function _createState(state) {
state = state || {};
if (!state.key) state.key = createRandomKey();
return state;
}
}, {
key: 'createLocation',
value: function createLocation(path, state, navigationType) {
var pathname = (0, _URLUtils.getPathname)(path);
var queryString = (0, _URLUtils.getQueryString)(path);
var query = queryString ? this.parseQueryString(queryString) : null;
return new _Location2['default'](pathname, query, state, navigationType);
}
}]);
return History;
})();
exports['default'] = History;
module.exports = exports['default'];
/***/ },
/* 13 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var _React$PropTypes = _react2['default'].PropTypes;
var object = _React$PropTypes.object;
var string = _React$PropTypes.string;
var func = _React$PropTypes.func;
function isLeftClickEvent(event) {
return event.button === 0;
}
function isModifiedEvent(event) {
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
}
/**
* <Link> components are used to create an <a> element that links to a route.
* When that route is active, the link gets an "active" class name (or the
* value of its `activeClassName` prop).
*
* For example, assuming you have the following route:
*
* <Route name="showPost" path="/posts/:postID" handler={Post}/>
*
* You could use the following component to link to that route:
*
* <Link to="showPost" params={{ postID: "123" }} />
*
* In addition to params, links may pass along query string parameters
* using the `query` prop.
*
* <Link to="showPost" params={{ postID: "123" }} query={{ show:true }}/>
*/
var Link = _react2['default'].createClass({
displayName: 'Link',
contextTypes: {
router: object
},
propTypes: {
activeStyle: object,
activeClassName: string,
to: string.isRequired,
query: object,
state: object,
onClick: func
},
getDefaultProps: function getDefaultProps() {
return {
className: '',
activeClassName: 'active',
style: {}
};
},
handleClick: function handleClick(event) {
var allowTransition = true;
var clickResult;
if (this.props.onClick) clickResult = this.props.onClick(event);
if (isModifiedEvent(event) || !isLeftClickEvent(event)) return;
if (clickResult === false || event.defaultPrevented === true) allowTransition = false;
event.preventDefault();
if (allowTransition) this.context.router.transitionTo(this.props.to, this.props.query, this.props.state);
},
render: function render() {
var router = this.context.router;
var _props = this.props;
var to = _props.to;
var query = _props.query;
var props = _extends({}, this.props, {
href: router.makeHref(to, query),
onClick: this.handleClick
});
// ignore if rendered outside of the context of a router, simplifies unit testing
if (router && router.isActive(to, query)) {
if (props.activeClassName) props.className += ' ' + props.activeClassName;
if (props.activeStyle) _extends(props.style, props.activeStyle);
}
return _react2['default'].createElement('a', props);
}
});
exports.Link = Link;
exports['default'] = Link;
/***/ },
/* 14 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var object = _react2['default'].PropTypes.object;
/**
* A mixin for components that modify the URL.
*
* Example:
*
* import { Navigation } from 'react-router';
*
* var MyLink = React.createClass({
* mixins: [ Navigation ],
* handleClick(event) {
* event.preventDefault();
* this.transitionTo('aRoute', { the: 'params' }, { the: 'query' });
* },
* render() {
* return (
* <a onClick={this.handleClick}>Click me!</a>
* );
* }
* });
*/
var Navigation = {
contextTypes: {
router: object.isRequired
}
};
var RouterNavigationMethods = ['makePath', 'makeHref', 'transitionTo', 'replaceWith', 'go', 'goBack', 'goForward'];
RouterNavigationMethods.forEach(function (method) {
Navigation[method] = function () {
var router = this.context.router;
return router[method].apply(router, arguments);
};
});
exports['default'] = Navigation;
module.exports = exports['default'];
/***/ },
/* 15 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var _invariant = __webpack_require__(2);
var _invariant2 = _interopRequireDefault(_invariant);
var _RouteUtils = __webpack_require__(3);
var _PropTypes = __webpack_require__(4);
var string = _react2['default'].PropTypes.string;
var Redirect = _react2['default'].createClass({
displayName: 'Redirect',
statics: {
createRouteFromReactElement: function createRouteFromReactElement(element) {
var route = (0, _RouteUtils.createRouteFromReactElement)(element);
if (route.from) route.path = route.from;
route.onEnter = function (nextState, router) {
router.replaceWith(route.to, nextState.query);
};
return route;
}
},
propTypes: {
from: string,
to: string.isRequired,
onEnter: _PropTypes.falsy,
children: _PropTypes.falsy
},
render: function render() {
(0, _invariant2['default'])(false, '<Redirect> elements are for router configuration only and should not be rendered');
}
});
exports.Redirect = Redirect;
exports['default'] = Redirect;
/***/ },
/* 16 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var _invariant = __webpack_require__(2);
var _invariant2 = _interopRequireDefault(_invariant);
var _RouteUtils = __webpack_require__(3);
var _PropTypes = __webpack_require__(4);
var _React$PropTypes = _react2['default'].PropTypes;
var string = _React$PropTypes.string;
var bool = _React$PropTypes.bool;
var func = _React$PropTypes.func;
/**
* A <Route> is used to declare which components are rendered to the page when
* the URL matches a given pattern.
*
* Routes are arranged in a nested tree structure. When a new URL is requested,
* the tree is searched depth-first to find a route whose path matches the URL.
* When one is found, all routes in the tree that lead to it are considered
* "active" and their components are rendered into the DOM, nested in the same
* order as they are in the tree.
*/
var Route = _react2['default'].createClass({
displayName: 'Route',
statics: {
createRouteFromReactElement: function createRouteFromReactElement(element) {
var route = (0, _RouteUtils.createRouteFromReactElement)(element);
if (route.handler) {
warning(false, '<Route handler> is deprecated, use <Route component> instead');
route.component = route.handler;
delete route.handler;
}
return route;
}
},
propTypes: {
path: string,
ignoreScrollBehavior: bool,
handler: _PropTypes.component,
component: _PropTypes.component,
components: _PropTypes.components,
getComponents: func
},
render: function render() {
(0, _invariant2['default'])(false, '<Route> elements are for router configuration only and should not be rendered');
}
});
exports.Route = Route;
exports['default'] = Route;
/***/ },
/* 17 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var _warning = __webpack_require__(7);
var _warning2 = _interopRequireDefault(_warning);
var _invariant = __webpack_require__(2);
var _invariant2 = _interopRequireDefault(_invariant);
var _AsyncUtils = __webpack_require__(8);
var _RouteUtils = __webpack_require__(3);
var _RoutingUtils = __webpack_require__(19);
var _PropTypes = __webpack_require__(4);
var _RouterContextMixin = __webpack_require__(18);
var _RouterContextMixin2 = _interopRequireDefault(_RouterContextMixin);
var _ScrollManagementMixin = __webpack_require__(20);
var _ScrollManagementMixin2 = _interopRequireDefault(_ScrollManagementMixin);
var _Location = __webpack_require__(5);
var _Transition = __webpack_require__(22);
var _Transition2 = _interopRequireDefault(_Transition);
var _React$PropTypes = _react2['default'].PropTypes;
var arrayOf = _React$PropTypes.arrayOf;
var func = _React$PropTypes.func;
var object = _React$PropTypes.object;
function runTransition(prevState, routes, location, hooks, callback) {
var transition = new _Transition2['default']();
(0, _RoutingUtils.getState)(routes, location, function (error, nextState) {
if (error || nextState == null || transition.isCancelled) {
callback(error, null, transition);
} else {
nextState.location = location;
var transitionHooks = (0, _RoutingUtils.getTransitionHooks)(prevState, nextState);
if (Array.isArray(hooks)) transitionHooks.unshift.apply(transitionHooks, hooks);
(0, _AsyncUtils.loopAsync)(transitionHooks.length, function (index, next, done) {
transitionHooks[index](nextState, transition, function (error) {
if (error || transition.isCancelled) {
done(error); // No need to continue.
} else {
next();
}
});
}, function (error) {
if (error || transition.isCancelled) {
callback(error, null, transition);
} else {
(0, _RoutingUtils.getComponents)(nextState.branch, function (error, components) {
if (error || transition.isCancelled) {
callback(error, null, transition);
} else {
nextState.components = components;
callback(null, nextState, transition);
}
});
}
});
}
});
}
var Router = _react2['default'].createClass({
displayName: 'Router',
mixins: [_RouterContextMixin2['default'], _ScrollManagementMixin2['default']],
statics: {
/**
* Runs a transition to the given location using the given routes and
* transition hooks (optional) and calls callback(error, state, transition)
* when finished. This is primarily useful for server-side rendering.
*/
run: function run(routes, location, transitionHooks, callback) {
if (typeof transitionHooks === 'function') {
callback = transitionHooks;
transitionHooks = null;
}
(0, _invariant2['default'])(typeof callback === 'function', 'Router.run needs a callback');
runTransition(null, routes, location, transitionHooks, callback);
}
},
propTypes: {
createElement: func.isRequired,
onAbort: func,
onError: func,
onUpdate: func,
// Client-side
history: _PropTypes.history,
routes: _PropTypes.routes,
// Routes may also be given as children (JSX)
children: _PropTypes.routes,
// Server-side
location: _PropTypes.location,
branch: _PropTypes.routes,
params: object,
components: arrayOf(_PropTypes.components)
},
getDefaultProps: function getDefaultProps() {
return {
createElement: _react.createElement
};
},
getInitialState: function getInitialState() {
return {
isTransitioning: false,
location: null,
branch: null,
params: null,
components: null
};
},
_updateState: function _updateState(location) {
var _this = this;
(0, _invariant2['default'])((0, _Location.isLocation)(location), 'A <Router> needs a valid Location');
var hooks = this.transitionHooks;
if (hooks) hooks = hooks.map(function (hook) {
return (0, _RoutingUtils.createTransitionHook)(hook, _this);
});
this.setState({ isTransitioning: true });
runTransition(this.state, this.routes, location, hooks, function (error, state, transition) {
if (error) {
_this.handleError(error);
} else if (transition.isCancelled) {
if (transition.redirectInfo) {
var _transition$redirectInfo = transition.redirectInfo;
var pathname = _transition$redirectInfo.pathname;
var query = _transition$redirectInfo.query;
var state = _transition$redirectInfo.state;
_this.replaceWith(pathname, query, state);
} else {
(0, _invariant2['default'])(_this.state.location, 'You may not abort the initial transition');
_this.handleAbort(reason);
}
} else if (state == null) {
(0, _warning2['default'])(false, 'Location "%s" did not match any routes', location.pathname);
} else {
_this.setState(state, _this.props.onUpdate);
}
_this.setState({ isTransitioning: false });
});
},
/**
* Adds a transition hook that runs before all route hooks in a
* transition. The signature is the same as route transition hooks.
*/
addTransitionHook: function addTransitionHook(hook) {
if (!this.transitionHooks) this.transitionHooks = [];
this.transitionHooks.push(hook);
},
/**
* Removes the given transition hook.
*/
removeTransitionHook: function removeTransitionHook(hook) {
if (this.transitionHooks) this.transitionHooks = this.transitionHooks.filter(function (h) {
return h !== hook;
});
},
handleAbort: function handleAbort(reason) {
if (this.props.onAbort) {
this.props.onAbort.call(this, reason);
} else {
// The best we can do here is goBack so the location state reverts
// to what it was. However, we also set a flag so that we know not
// to run through _updateState again since state did not change.
this._ignoreNextHistoryChange = true;
this.goBack();
}
},
handleError: function handleError(error) {
if (this.props.onError) {
this.props.onError.call(this, error);
} else {
// Throw errors by default so we don't silently swallow them!
throw error; // This error probably originated in getChildRoutes or getComponents.
}
},
handleHistoryChange: function handleHistoryChange() {
if (this._ignoreNextHistoryChange) {
this._ignoreNextHistoryChange = false;
} else {
this._updateState(this.props.history.location);
}
},
componentWillMount: function componentWillMount() {
var _props = this.props;
var history = _props.history;
var routes = _props.routes;
var children = _props.children;
var location = _props.location;
var branch = _props.branch;
var params = _props.params;
var components = _props.components;
if (history) {
(0, _invariant2['default'])(routes || children, 'Client-side <Router>s need routes. Try using <Router routes> or ' + 'passing your routes as nested <Route> children');
this.routes = (0, _RouteUtils.createRoutes)(routes || children);
if (typeof history.setup === 'function') history.setup();
// We need to listen first in case we redirect immediately.
if (history.addChangeListener) history.addChangeListener(this.handleHistoryChange);
this._updateState(history.location);
} else {
(0, _invariant2['default'])(location && branch && params && components, 'Server-side <Router>s need location, branch, params, and components ' + 'props. Try using Router.run to get all the props you need');
this.setState({ location: location, branch: branch, params: params, components: components });
}
},
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
(0, _invariant2['default'])(this.props.history === nextProps.history, '<Router history> may not be changed');
if (nextProps.history) {
var currentRoutes = this.props.routes || this.props.children;
var nextRoutes = nextProps.routes || nextProps.children;
if (currentRoutes !== nextRoutes) {
this.routes = (0, _RouteUtils.createRoutes)(nextRoutes);
// Call this here because _updateState
// uses this.routes to determine state.
if (nextProps.history.location) this._updateState(nextProps.history.location);
}
}
},
componentWillUnmount: function componentWillUnmount() {
var history = this.props.history;
if (history && history.removeChangeListener) history.removeChangeListener(this.handleHistoryChange);
},
_createElement: function _createElement(component, props) {
return typeof component === 'function' ? this.props.createElement(component, props) : null;
},
render: function render() {
var _this2 = this;
var _state = this.state;
var location = _state.location;
var branch = _state.branch;
var params = _state.params;
var components = _state.components;
var isTransitioning = _state.isTransitioning;
var element = null;
if (components) {
element = components.reduceRight(function (element, components, index) {
if (components == null) return element; // Don't create new children; use the grandchildren.
var route = branch[index];
var routeParams = (0, _RoutingUtils.getRouteParams)(route, params);
var props = { location: location, params: params, route: route, routeParams: routeParams, isTransitioning: isTransitioning };
if ((0, _react.isValidElement)(element)) {
props.children = element;
} else if (element) {
// In render, do var { header, sidebar } = this.props;
_extends(props, element);
}
if (typeof components === 'object') {
var elements = {};
for (var key in components) if (components.hasOwnProperty(key)) elements[key] = _this2._createElement(components[key], props);
return elements;
}
return _this2._createElement(components, props);
}, element);
}
(0, _invariant2['default'])(element === null || element === false || (0, _react.isValidElement)(element), 'The root route must render a single element');
return element;
}
});
exports['default'] = Router;
module.exports = exports['default'];
/***/ },
/* 18 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var _invariant = __webpack_require__(2);
var _invariant2 = _interopRequireDefault(_invariant);
var _URLUtils = __webpack_require__(6);
var _React$PropTypes = _react2['default'].PropTypes;
var func = _React$PropTypes.func;
var object = _React$PropTypes.object;
function pathnameIsActive(pathname, activePathname) {
if ((0, _URLUtils.stripLeadingSlashes)(activePathname).indexOf((0, _URLUtils.stripLeadingSlashes)(pathname)) === 0) return true; // This quick comparison satisfies most use cases.
// TODO: Implement a more stringent comparison that checks
// to see if the pathname matches any routes (and params)
// in the currently active branch.
return false;
}