25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

StaticRouter.js 5.6 KiB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 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; };
  2. 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; }
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. 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; }
  5. 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; }
  6. import warning from "warning";
  7. import invariant from "invariant";
  8. import React from "react";
  9. import PropTypes from "prop-types";
  10. import { createLocation, createPath } from "history";
  11. import Router from "./Router";
  12. var addLeadingSlash = function addLeadingSlash(path) {
  13. return path.charAt(0) === "/" ? path : "/" + path;
  14. };
  15. var addBasename = function addBasename(basename, location) {
  16. if (!basename) return location;
  17. return _extends({}, location, {
  18. pathname: addLeadingSlash(basename) + location.pathname
  19. });
  20. };
  21. var stripBasename = function stripBasename(basename, location) {
  22. if (!basename) return location;
  23. var base = addLeadingSlash(basename);
  24. if (location.pathname.indexOf(base) !== 0) return location;
  25. return _extends({}, location, {
  26. pathname: location.pathname.substr(base.length)
  27. });
  28. };
  29. var createURL = function createURL(location) {
  30. return typeof location === "string" ? location : createPath(location);
  31. };
  32. var staticHandler = function staticHandler(methodName) {
  33. return function () {
  34. invariant(false, "You cannot %s with <StaticRouter>", methodName);
  35. };
  36. };
  37. var noop = function noop() {};
  38. /**
  39. * The public top-level API for a "static" <Router>, so-called because it
  40. * can't actually change the current location. Instead, it just records
  41. * location changes in a context object. Useful mainly in testing and
  42. * server-rendering scenarios.
  43. */
  44. var StaticRouter = function (_React$Component) {
  45. _inherits(StaticRouter, _React$Component);
  46. function StaticRouter() {
  47. var _temp, _this, _ret;
  48. _classCallCheck(this, StaticRouter);
  49. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  50. args[_key] = arguments[_key];
  51. }
  52. return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.createHref = function (path) {
  53. return addLeadingSlash(_this.props.basename + createURL(path));
  54. }, _this.handlePush = function (location) {
  55. var _this$props = _this.props,
  56. basename = _this$props.basename,
  57. context = _this$props.context;
  58. context.action = "PUSH";
  59. context.location = addBasename(basename, createLocation(location));
  60. context.url = createURL(context.location);
  61. }, _this.handleReplace = function (location) {
  62. var _this$props2 = _this.props,
  63. basename = _this$props2.basename,
  64. context = _this$props2.context;
  65. context.action = "REPLACE";
  66. context.location = addBasename(basename, createLocation(location));
  67. context.url = createURL(context.location);
  68. }, _this.handleListen = function () {
  69. return noop;
  70. }, _this.handleBlock = function () {
  71. return noop;
  72. }, _temp), _possibleConstructorReturn(_this, _ret);
  73. }
  74. StaticRouter.prototype.getChildContext = function getChildContext() {
  75. return {
  76. router: {
  77. staticContext: this.props.context
  78. }
  79. };
  80. };
  81. StaticRouter.prototype.componentWillMount = function componentWillMount() {
  82. warning(!this.props.history, "<StaticRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { StaticRouter as Router }`.");
  83. };
  84. StaticRouter.prototype.render = function render() {
  85. var _props = this.props,
  86. basename = _props.basename,
  87. context = _props.context,
  88. location = _props.location,
  89. props = _objectWithoutProperties(_props, ["basename", "context", "location"]);
  90. var history = {
  91. createHref: this.createHref,
  92. action: "POP",
  93. location: stripBasename(basename, createLocation(location)),
  94. push: this.handlePush,
  95. replace: this.handleReplace,
  96. go: staticHandler("go"),
  97. goBack: staticHandler("goBack"),
  98. goForward: staticHandler("goForward"),
  99. listen: this.handleListen,
  100. block: this.handleBlock
  101. };
  102. return React.createElement(Router, _extends({}, props, { history: history }));
  103. };
  104. return StaticRouter;
  105. }(React.Component);
  106. StaticRouter.propTypes = {
  107. basename: PropTypes.string,
  108. context: PropTypes.object.isRequired,
  109. location: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
  110. };
  111. StaticRouter.defaultProps = {
  112. basename: "",
  113. location: "/"
  114. };
  115. StaticRouter.childContextTypes = {
  116. router: PropTypes.object.isRequired
  117. };
  118. export default StaticRouter;