Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

StaticRouter.js 6.2 KiB

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. "use strict";
  2. exports.__esModule = true;
  3. 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; };
  4. var _warning = require("warning");
  5. var _warning2 = _interopRequireDefault(_warning);
  6. var _invariant = require("invariant");
  7. var _invariant2 = _interopRequireDefault(_invariant);
  8. var _react = require("react");
  9. var _react2 = _interopRequireDefault(_react);
  10. var _propTypes = require("prop-types");
  11. var _propTypes2 = _interopRequireDefault(_propTypes);
  12. var _history = require("history");
  13. var _Router = require("./Router");
  14. var _Router2 = _interopRequireDefault(_Router);
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
  17. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  18. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  19. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  20. var addLeadingSlash = function addLeadingSlash(path) {
  21. return path.charAt(0) === "/" ? path : "/" + path;
  22. };
  23. var addBasename = function addBasename(basename, location) {
  24. if (!basename) return location;
  25. return _extends({}, location, {
  26. pathname: addLeadingSlash(basename) + location.pathname
  27. });
  28. };
  29. var stripBasename = function stripBasename(basename, location) {
  30. if (!basename) return location;
  31. var base = addLeadingSlash(basename);
  32. if (location.pathname.indexOf(base) !== 0) return location;
  33. return _extends({}, location, {
  34. pathname: location.pathname.substr(base.length)
  35. });
  36. };
  37. var createURL = function createURL(location) {
  38. return typeof location === "string" ? location : (0, _history.createPath)(location);
  39. };
  40. var staticHandler = function staticHandler(methodName) {
  41. return function () {
  42. (0, _invariant2.default)(false, "You cannot %s with <StaticRouter>", methodName);
  43. };
  44. };
  45. var noop = function noop() {};
  46. /**
  47. * The public top-level API for a "static" <Router>, so-called because it
  48. * can't actually change the current location. Instead, it just records
  49. * location changes in a context object. Useful mainly in testing and
  50. * server-rendering scenarios.
  51. */
  52. var StaticRouter = function (_React$Component) {
  53. _inherits(StaticRouter, _React$Component);
  54. function StaticRouter() {
  55. var _temp, _this, _ret;
  56. _classCallCheck(this, StaticRouter);
  57. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  58. args[_key] = arguments[_key];
  59. }
  60. return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.createHref = function (path) {
  61. return addLeadingSlash(_this.props.basename + createURL(path));
  62. }, _this.handlePush = function (location) {
  63. var _this$props = _this.props,
  64. basename = _this$props.basename,
  65. context = _this$props.context;
  66. context.action = "PUSH";
  67. context.location = addBasename(basename, (0, _history.createLocation)(location));
  68. context.url = createURL(context.location);
  69. }, _this.handleReplace = function (location) {
  70. var _this$props2 = _this.props,
  71. basename = _this$props2.basename,
  72. context = _this$props2.context;
  73. context.action = "REPLACE";
  74. context.location = addBasename(basename, (0, _history.createLocation)(location));
  75. context.url = createURL(context.location);
  76. }, _this.handleListen = function () {
  77. return noop;
  78. }, _this.handleBlock = function () {
  79. return noop;
  80. }, _temp), _possibleConstructorReturn(_this, _ret);
  81. }
  82. StaticRouter.prototype.getChildContext = function getChildContext() {
  83. return {
  84. router: {
  85. staticContext: this.props.context
  86. }
  87. };
  88. };
  89. StaticRouter.prototype.componentWillMount = function componentWillMount() {
  90. (0, _warning2.default)(!this.props.history, "<StaticRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { StaticRouter as Router }`.");
  91. };
  92. StaticRouter.prototype.render = function render() {
  93. var _props = this.props,
  94. basename = _props.basename,
  95. context = _props.context,
  96. location = _props.location,
  97. props = _objectWithoutProperties(_props, ["basename", "context", "location"]);
  98. var history = {
  99. createHref: this.createHref,
  100. action: "POP",
  101. location: stripBasename(basename, (0, _history.createLocation)(location)),
  102. push: this.handlePush,
  103. replace: this.handleReplace,
  104. go: staticHandler("go"),
  105. goBack: staticHandler("goBack"),
  106. goForward: staticHandler("goForward"),
  107. listen: this.handleListen,
  108. block: this.handleBlock
  109. };
  110. return _react2.default.createElement(_Router2.default, _extends({}, props, { history: history }));
  111. };
  112. return StaticRouter;
  113. }(_react2.default.Component);
  114. StaticRouter.propTypes = {
  115. basename: _propTypes2.default.string,
  116. context: _propTypes2.default.object.isRequired,
  117. location: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.object])
  118. };
  119. StaticRouter.defaultProps = {
  120. basename: "",
  121. location: "/"
  122. };
  123. StaticRouter.childContextTypes = {
  124. router: _propTypes2.default.object.isRequired
  125. };
  126. exports.default = StaticRouter;