katoikia-app/web-ui/web-react/node_modules/react-router/cjs/react-router.js

828 lines
25 KiB
JavaScript

'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = _interopDefault(require('react'));
var PropTypes = _interopDefault(require('prop-types'));
var history = require('history');
var warning = _interopDefault(require('tiny-warning'));
var createContext = _interopDefault(require('mini-create-react-context'));
var invariant = _interopDefault(require('tiny-invariant'));
var pathToRegexp = _interopDefault(require('path-to-regexp'));
var reactIs = require('react-is');
var hoistStatics = _interopDefault(require('hoist-non-react-statics'));
function _extends() {
_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;
};
return _extends.apply(this, arguments);
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
_setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
// TODO: Replace with React.createContext once we can assume React 16+
var createNamedContext = function createNamedContext(name) {
var context = createContext();
context.displayName = name;
return context;
};
var historyContext = /*#__PURE__*/createNamedContext("Router-History");
var context = /*#__PURE__*/createNamedContext("Router");
/**
* The public API for putting history on context.
*/
var Router = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(Router, _React$Component);
Router.computeRootMatch = function computeRootMatch(pathname) {
return {
path: "/",
url: "/",
params: {},
isExact: pathname === "/"
};
};
function Router(props) {
var _this;
_this = _React$Component.call(this, props) || this;
_this.state = {
location: props.history.location
}; // This is a bit of a hack. We have to start listening for location
// changes here in the constructor in case there are any <Redirect>s
// on the initial render. If there are, they will replace/push when
// they mount and since cDM fires in children before parents, we may
// get a new location before the <Router> is mounted.
_this._isMounted = false;
_this._pendingLocation = null;
if (!props.staticContext) {
_this.unlisten = props.history.listen(function (location) {
_this._pendingLocation = location;
});
}
return _this;
}
var _proto = Router.prototype;
_proto.componentDidMount = function componentDidMount() {
var _this2 = this;
this._isMounted = true;
if (this.unlisten) {
// Any pre-mount location changes have been captured at
// this point, so unregister the listener.
this.unlisten();
}
if (!this.props.staticContext) {
this.unlisten = this.props.history.listen(function (location) {
if (_this2._isMounted) {
_this2.setState({
location: location
});
}
});
}
if (this._pendingLocation) {
this.setState({
location: this._pendingLocation
});
}
};
_proto.componentWillUnmount = function componentWillUnmount() {
if (this.unlisten) {
this.unlisten();
this._isMounted = false;
this._pendingLocation = null;
}
};
_proto.render = function render() {
return /*#__PURE__*/React.createElement(context.Provider, {
value: {
history: this.props.history,
location: this.state.location,
match: Router.computeRootMatch(this.state.location.pathname),
staticContext: this.props.staticContext
}
}, /*#__PURE__*/React.createElement(historyContext.Provider, {
children: this.props.children || null,
value: this.props.history
}));
};
return Router;
}(React.Component);
{
Router.propTypes = {
children: PropTypes.node,
history: PropTypes.object.isRequired,
staticContext: PropTypes.object
};
Router.prototype.componentDidUpdate = function (prevProps) {
warning(prevProps.history === this.props.history, "You cannot change <Router history>") ;
};
}
/**
* The public API for a <Router> that stores location in memory.
*/
var MemoryRouter = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(MemoryRouter, _React$Component);
function MemoryRouter() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
_this.history = history.createMemoryHistory(_this.props);
return _this;
}
var _proto = MemoryRouter.prototype;
_proto.render = function render() {
return /*#__PURE__*/React.createElement(Router, {
history: this.history,
children: this.props.children
});
};
return MemoryRouter;
}(React.Component);
{
MemoryRouter.propTypes = {
initialEntries: PropTypes.array,
initialIndex: PropTypes.number,
getUserConfirmation: PropTypes.func,
keyLength: PropTypes.number,
children: PropTypes.node
};
MemoryRouter.prototype.componentDidMount = function () {
warning(!this.props.history, "<MemoryRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { MemoryRouter as Router }`.") ;
};
}
var Lifecycle = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(Lifecycle, _React$Component);
function Lifecycle() {
return _React$Component.apply(this, arguments) || this;
}
var _proto = Lifecycle.prototype;
_proto.componentDidMount = function componentDidMount() {
if (this.props.onMount) this.props.onMount.call(this, this);
};
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
if (this.props.onUpdate) this.props.onUpdate.call(this, this, prevProps);
};
_proto.componentWillUnmount = function componentWillUnmount() {
if (this.props.onUnmount) this.props.onUnmount.call(this, this);
};
_proto.render = function render() {
return null;
};
return Lifecycle;
}(React.Component);
/**
* The public API for prompting the user before navigating away from a screen.
*/
function Prompt(_ref) {
var message = _ref.message,
_ref$when = _ref.when,
when = _ref$when === void 0 ? true : _ref$when;
return /*#__PURE__*/React.createElement(context.Consumer, null, function (context) {
!context ? invariant(false, "You should not use <Prompt> outside a <Router>") : void 0;
if (!when || context.staticContext) return null;
var method = context.history.block;
return /*#__PURE__*/React.createElement(Lifecycle, {
onMount: function onMount(self) {
self.release = method(message);
},
onUpdate: function onUpdate(self, prevProps) {
if (prevProps.message !== message) {
self.release();
self.release = method(message);
}
},
onUnmount: function onUnmount(self) {
self.release();
},
message: message
});
});
}
{
var messageType = PropTypes.oneOfType([PropTypes.func, PropTypes.string]);
Prompt.propTypes = {
when: PropTypes.bool,
message: messageType.isRequired
};
}
var cache = {};
var cacheLimit = 10000;
var cacheCount = 0;
function compilePath(path) {
if (cache[path]) return cache[path];
var generator = pathToRegexp.compile(path);
if (cacheCount < cacheLimit) {
cache[path] = generator;
cacheCount++;
}
return generator;
}
/**
* Public API for generating a URL pathname from a path and parameters.
*/
function generatePath(path, params) {
if (path === void 0) {
path = "/";
}
if (params === void 0) {
params = {};
}
return path === "/" ? path : compilePath(path)(params, {
pretty: true
});
}
/**
* The public API for navigating programmatically with a component.
*/
function Redirect(_ref) {
var computedMatch = _ref.computedMatch,
to = _ref.to,
_ref$push = _ref.push,
push = _ref$push === void 0 ? false : _ref$push;
return /*#__PURE__*/React.createElement(context.Consumer, null, function (context) {
!context ? invariant(false, "You should not use <Redirect> outside a <Router>") : void 0;
var history$1 = context.history,
staticContext = context.staticContext;
var method = push ? history$1.push : history$1.replace;
var location = history.createLocation(computedMatch ? typeof to === "string" ? generatePath(to, computedMatch.params) : _extends({}, to, {
pathname: generatePath(to.pathname, computedMatch.params)
}) : to); // When rendering in a static context,
// set the new location immediately.
if (staticContext) {
method(location);
return null;
}
return /*#__PURE__*/React.createElement(Lifecycle, {
onMount: function onMount() {
method(location);
},
onUpdate: function onUpdate(self, prevProps) {
var prevLocation = history.createLocation(prevProps.to);
if (!history.locationsAreEqual(prevLocation, _extends({}, location, {
key: prevLocation.key
}))) {
method(location);
}
},
to: to
});
});
}
{
Redirect.propTypes = {
push: PropTypes.bool,
from: PropTypes.string,
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired
};
}
var cache$1 = {};
var cacheLimit$1 = 10000;
var cacheCount$1 = 0;
function compilePath$1(path, options) {
var cacheKey = "" + options.end + options.strict + options.sensitive;
var pathCache = cache$1[cacheKey] || (cache$1[cacheKey] = {});
if (pathCache[path]) return pathCache[path];
var keys = [];
var regexp = pathToRegexp(path, keys, options);
var result = {
regexp: regexp,
keys: keys
};
if (cacheCount$1 < cacheLimit$1) {
pathCache[path] = result;
cacheCount$1++;
}
return result;
}
/**
* Public API for matching a URL pathname to a path.
*/
function matchPath(pathname, options) {
if (options === void 0) {
options = {};
}
if (typeof options === "string" || Array.isArray(options)) {
options = {
path: options
};
}
var _options = options,
path = _options.path,
_options$exact = _options.exact,
exact = _options$exact === void 0 ? false : _options$exact,
_options$strict = _options.strict,
strict = _options$strict === void 0 ? false : _options$strict,
_options$sensitive = _options.sensitive,
sensitive = _options$sensitive === void 0 ? false : _options$sensitive;
var paths = [].concat(path);
return paths.reduce(function (matched, path) {
if (!path && path !== "") return null;
if (matched) return matched;
var _compilePath = compilePath$1(path, {
end: exact,
strict: strict,
sensitive: sensitive
}),
regexp = _compilePath.regexp,
keys = _compilePath.keys;
var match = regexp.exec(pathname);
if (!match) return null;
var url = match[0],
values = match.slice(1);
var isExact = pathname === url;
if (exact && !isExact) return null;
return {
path: path,
// the path used to match
url: path === "/" && url === "" ? "/" : url,
// the matched portion of the URL
isExact: isExact,
// whether or not we matched exactly
params: keys.reduce(function (memo, key, index) {
memo[key.name] = values[index];
return memo;
}, {})
};
}, null);
}
function isEmptyChildren(children) {
return React.Children.count(children) === 0;
}
function evalChildrenDev(children, props, path) {
var value = children(props);
warning(value !== undefined, "You returned `undefined` from the `children` function of " + ("<Route" + (path ? " path=\"" + path + "\"" : "") + ">, but you ") + "should have returned a React element or `null`") ;
return value || null;
}
/**
* The public API for matching a single path and rendering.
*/
var Route = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(Route, _React$Component);
function Route() {
return _React$Component.apply(this, arguments) || this;
}
var _proto = Route.prototype;
_proto.render = function render() {
var _this = this;
return /*#__PURE__*/React.createElement(context.Consumer, null, function (context$1) {
!context$1 ? invariant(false, "You should not use <Route> outside a <Router>") : void 0;
var location = _this.props.location || context$1.location;
var match = _this.props.computedMatch ? _this.props.computedMatch // <Switch> already computed the match for us
: _this.props.path ? matchPath(location.pathname, _this.props) : context$1.match;
var props = _extends({}, context$1, {
location: location,
match: match
});
var _this$props = _this.props,
children = _this$props.children,
component = _this$props.component,
render = _this$props.render; // Preact uses an empty array as children by
// default, so use null if that's the case.
if (Array.isArray(children) && isEmptyChildren(children)) {
children = null;
}
return /*#__PURE__*/React.createElement(context.Provider, {
value: props
}, props.match ? children ? typeof children === "function" ? evalChildrenDev(children, props, _this.props.path) : children : component ? /*#__PURE__*/React.createElement(component, props) : render ? render(props) : null : typeof children === "function" ? evalChildrenDev(children, props, _this.props.path) : null);
});
};
return Route;
}(React.Component);
{
Route.propTypes = {
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
component: function component(props, propName) {
if (props[propName] && !reactIs.isValidElementType(props[propName])) {
return new Error("Invalid prop 'component' supplied to 'Route': the prop is not a valid React component");
}
},
exact: PropTypes.bool,
location: PropTypes.object,
path: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
render: PropTypes.func,
sensitive: PropTypes.bool,
strict: PropTypes.bool
};
Route.prototype.componentDidMount = function () {
warning(!(this.props.children && !isEmptyChildren(this.props.children) && this.props.component), "You should not use <Route component> and <Route children> in the same route; <Route component> will be ignored") ;
warning(!(this.props.children && !isEmptyChildren(this.props.children) && this.props.render), "You should not use <Route render> and <Route children> in the same route; <Route render> will be ignored") ;
warning(!(this.props.component && this.props.render), "You should not use <Route component> and <Route render> in the same route; <Route render> will be ignored") ;
};
Route.prototype.componentDidUpdate = function (prevProps) {
warning(!(this.props.location && !prevProps.location), '<Route> elements should not change from uncontrolled to controlled (or vice versa). You initially used no "location" prop and then provided one on a subsequent render.') ;
warning(!(!this.props.location && prevProps.location), '<Route> elements should not change from controlled to uncontrolled (or vice versa). You provided a "location" prop initially but omitted it on a subsequent render.') ;
};
}
function addLeadingSlash(path) {
return path.charAt(0) === "/" ? path : "/" + path;
}
function addBasename(basename, location) {
if (!basename) return location;
return _extends({}, location, {
pathname: addLeadingSlash(basename) + location.pathname
});
}
function stripBasename(basename, location) {
if (!basename) return location;
var base = addLeadingSlash(basename);
if (location.pathname.indexOf(base) !== 0) return location;
return _extends({}, location, {
pathname: location.pathname.substr(base.length)
});
}
function createURL(location) {
return typeof location === "string" ? location : history.createPath(location);
}
function staticHandler(methodName) {
return function () {
invariant(false, "You cannot %s with <StaticRouter>", methodName) ;
};
}
function noop() {}
/**
* The public top-level API for a "static" <Router>, so-called because it
* can't actually change the current location. Instead, it just records
* location changes in a context object. Useful mainly in testing and
* server-rendering scenarios.
*/
var StaticRouter = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(StaticRouter, _React$Component);
function StaticRouter() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
_this.handlePush = function (location) {
return _this.navigateTo(location, "PUSH");
};
_this.handleReplace = function (location) {
return _this.navigateTo(location, "REPLACE");
};
_this.handleListen = function () {
return noop;
};
_this.handleBlock = function () {
return noop;
};
return _this;
}
var _proto = StaticRouter.prototype;
_proto.navigateTo = function navigateTo(location, action) {
var _this$props = this.props,
_this$props$basename = _this$props.basename,
basename = _this$props$basename === void 0 ? "" : _this$props$basename,
_this$props$context = _this$props.context,
context = _this$props$context === void 0 ? {} : _this$props$context;
context.action = action;
context.location = addBasename(basename, history.createLocation(location));
context.url = createURL(context.location);
};
_proto.render = function render() {
var _this$props2 = this.props,
_this$props2$basename = _this$props2.basename,
basename = _this$props2$basename === void 0 ? "" : _this$props2$basename,
_this$props2$context = _this$props2.context,
context = _this$props2$context === void 0 ? {} : _this$props2$context,
_this$props2$location = _this$props2.location,
location = _this$props2$location === void 0 ? "/" : _this$props2$location,
rest = _objectWithoutPropertiesLoose(_this$props2, ["basename", "context", "location"]);
var history$1 = {
createHref: function createHref(path) {
return addLeadingSlash(basename + createURL(path));
},
action: "POP",
location: stripBasename(basename, history.createLocation(location)),
push: this.handlePush,
replace: this.handleReplace,
go: staticHandler("go"),
goBack: staticHandler("goBack"),
goForward: staticHandler("goForward"),
listen: this.handleListen,
block: this.handleBlock
};
return /*#__PURE__*/React.createElement(Router, _extends({}, rest, {
history: history$1,
staticContext: context
}));
};
return StaticRouter;
}(React.Component);
{
StaticRouter.propTypes = {
basename: PropTypes.string,
context: PropTypes.object,
location: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
};
StaticRouter.prototype.componentDidMount = function () {
warning(!this.props.history, "<StaticRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { StaticRouter as Router }`.") ;
};
}
/**
* The public API for rendering the first <Route> that matches.
*/
var Switch = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(Switch, _React$Component);
function Switch() {
return _React$Component.apply(this, arguments) || this;
}
var _proto = Switch.prototype;
_proto.render = function render() {
var _this = this;
return /*#__PURE__*/React.createElement(context.Consumer, null, function (context) {
!context ? invariant(false, "You should not use <Switch> outside a <Router>") : void 0;
var location = _this.props.location || context.location;
var element, match; // We use React.Children.forEach instead of React.Children.toArray().find()
// here because toArray adds keys to all child elements and we do not want
// to trigger an unmount/remount for two <Route>s that render the same
// component at different URLs.
React.Children.forEach(_this.props.children, function (child) {
if (match == null && /*#__PURE__*/React.isValidElement(child)) {
element = child;
var path = child.props.path || child.props.from;
match = path ? matchPath(location.pathname, _extends({}, child.props, {
path: path
})) : context.match;
}
});
return match ? /*#__PURE__*/React.cloneElement(element, {
location: location,
computedMatch: match
}) : null;
});
};
return Switch;
}(React.Component);
{
Switch.propTypes = {
children: PropTypes.node,
location: PropTypes.object
};
Switch.prototype.componentDidUpdate = function (prevProps) {
warning(!(this.props.location && !prevProps.location), '<Switch> elements should not change from uncontrolled to controlled (or vice versa). You initially used no "location" prop and then provided one on a subsequent render.') ;
warning(!(!this.props.location && prevProps.location), '<Switch> elements should not change from controlled to uncontrolled (or vice versa). You provided a "location" prop initially but omitted it on a subsequent render.') ;
};
}
/**
* A public higher-order component to access the imperative API
*/
function withRouter(Component) {
var displayName = "withRouter(" + (Component.displayName || Component.name) + ")";
var C = function C(props) {
var wrappedComponentRef = props.wrappedComponentRef,
remainingProps = _objectWithoutPropertiesLoose(props, ["wrappedComponentRef"]);
return /*#__PURE__*/React.createElement(context.Consumer, null, function (context) {
!context ? invariant(false, "You should not use <" + displayName + " /> outside a <Router>") : void 0;
return /*#__PURE__*/React.createElement(Component, _extends({}, remainingProps, context, {
ref: wrappedComponentRef
}));
});
};
C.displayName = displayName;
C.WrappedComponent = Component;
{
C.propTypes = {
wrappedComponentRef: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object])
};
}
return hoistStatics(C, Component);
}
var useContext = React.useContext;
function useHistory() {
{
!(typeof useContext === "function") ? invariant(false, "You must use React >= 16.8 in order to use useHistory()") : void 0;
}
return useContext(historyContext);
}
function useLocation() {
{
!(typeof useContext === "function") ? invariant(false, "You must use React >= 16.8 in order to use useLocation()") : void 0;
}
return useContext(context).location;
}
function useParams() {
{
!(typeof useContext === "function") ? invariant(false, "You must use React >= 16.8 in order to use useParams()") : void 0;
}
var match = useContext(context).match;
return match ? match.params : {};
}
function useRouteMatch(path) {
{
!(typeof useContext === "function") ? invariant(false, "You must use React >= 16.8 in order to use useRouteMatch()") : void 0;
}
var location = useLocation();
var match = useContext(context).match;
return path ? matchPath(location.pathname, path) : match;
}
{
if (typeof window !== "undefined") {
var global = window;
var key = "__react_router_build__";
var buildNames = {
cjs: "CommonJS",
esm: "ES modules",
umd: "UMD"
};
if (global[key] && global[key] !== "cjs") {
var initialBuildName = buildNames[global[key]];
var secondaryBuildName = buildNames["cjs"]; // TODO: Add link to article that explains in detail how to avoid
// loading 2 different builds.
throw new Error("You are loading the " + secondaryBuildName + " build of React Router " + ("on a page that is already running the " + initialBuildName + " ") + "build, so things won't work right.");
}
global[key] = "cjs";
}
}
exports.MemoryRouter = MemoryRouter;
exports.Prompt = Prompt;
exports.Redirect = Redirect;
exports.Route = Route;
exports.Router = Router;
exports.StaticRouter = StaticRouter;
exports.Switch = Switch;
exports.__HistoryContext = historyContext;
exports.__RouterContext = context;
exports.generatePath = generatePath;
exports.matchPath = matchPath;
exports.useHistory = useHistory;
exports.useLocation = useLocation;
exports.useParams = useParams;
exports.useRouteMatch = useRouteMatch;
exports.withRouter = withRouter;
//# sourceMappingURL=react-router.js.map