Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

105 строки
3.8 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getContainerRenderMixin;
  6. var _reactDom = _interopRequireDefault(require("react-dom"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  9. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  10. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  11. function defaultGetContainer() {
  12. var container = document.createElement('div');
  13. document.body.appendChild(container);
  14. return container;
  15. }
  16. function getContainerRenderMixin(config) {
  17. var _config$autoMount = config.autoMount,
  18. autoMount = _config$autoMount === void 0 ? true : _config$autoMount,
  19. _config$autoDestroy = config.autoDestroy,
  20. autoDestroy = _config$autoDestroy === void 0 ? true : _config$autoDestroy,
  21. isVisible = config.isVisible,
  22. isForceRender = config.isForceRender,
  23. getComponent = config.getComponent,
  24. _config$getContainer = config.getContainer,
  25. getContainer = _config$getContainer === void 0 ? defaultGetContainer : _config$getContainer;
  26. var mixin;
  27. function _renderComponent(instance, componentArg, ready) {
  28. if (!isVisible || instance._component || isVisible(instance) || isForceRender && isForceRender(instance)) {
  29. if (!instance._container) {
  30. instance._container = getContainer(instance);
  31. }
  32. var component;
  33. if (instance.getComponent) {
  34. component = instance.getComponent(componentArg);
  35. } else {
  36. component = getComponent(instance, componentArg);
  37. }
  38. _reactDom.default.unstable_renderSubtreeIntoContainer(instance, component, instance._container, function callback() {
  39. instance._component = this;
  40. if (ready) {
  41. ready.call(this);
  42. }
  43. });
  44. }
  45. }
  46. if (autoMount) {
  47. mixin = _objectSpread({}, mixin, {
  48. componentDidMount: function componentDidMount() {
  49. _renderComponent(this);
  50. },
  51. componentDidUpdate: function componentDidUpdate() {
  52. _renderComponent(this);
  53. }
  54. });
  55. }
  56. if (!autoMount || !autoDestroy) {
  57. mixin = _objectSpread({}, mixin, {
  58. renderComponent: function renderComponent(componentArg, ready) {
  59. _renderComponent(this, componentArg, ready);
  60. }
  61. });
  62. }
  63. function _removeContainer(instance) {
  64. if (instance._container) {
  65. var container = instance._container;
  66. _reactDom.default.unmountComponentAtNode(container);
  67. container.parentNode.removeChild(container);
  68. instance._container = null;
  69. }
  70. }
  71. if (autoDestroy) {
  72. mixin = _objectSpread({}, mixin, {
  73. componentWillUnmount: function componentWillUnmount() {
  74. _removeContainer(this);
  75. }
  76. });
  77. } else {
  78. mixin = _objectSpread({}, mixin, {
  79. removeContainer: function removeContainer() {
  80. _removeContainer(this);
  81. }
  82. });
  83. }
  84. return mixin;
  85. }