1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | 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; }; }();
|
8 |
|
9 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
10 |
|
11 | 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; }
|
12 |
|
13 | 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; }
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var React = require('react');
|
19 | var ReactDOM = require('react-dom');
|
20 | var PropTypes = React.PropTypes;
|
21 |
|
22 | var ContextBridge = require('./ContextBridge').ContextBridge;
|
23 | var ErrorUtils = require('./ErrorUtils').default;
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var SandboxedComponent = exports.SandboxedComponent = function (_React$Component) {
|
31 | _inherits(SandboxedComponent, _React$Component);
|
32 |
|
33 | function SandboxedComponent(props) {
|
34 | _classCallCheck(this, SandboxedComponent);
|
35 |
|
36 | var _this = _possibleConstructorReturn(this, (SandboxedComponent.__proto__ || Object.getPrototypeOf(SandboxedComponent)).call(this, props));
|
37 |
|
38 | _this.domNode = null;
|
39 | return _this;
|
40 | }
|
41 |
|
42 | _createClass(SandboxedComponent, [{
|
43 | key: 'componentWillReceiveProps',
|
44 | value: function componentWillReceiveProps(nextProps) {
|
45 | if (nextProps.children !== this.props.children) {
|
46 | this._cleanupChild();
|
47 | this._renderChild(nextProps);
|
48 | }
|
49 | }
|
50 | }, {
|
51 | key: 'componentDidMount',
|
52 | value: function componentDidMount() {
|
53 | this._renderChild(this.props);
|
54 | }
|
55 | }, {
|
56 | key: 'componentWillUnmount',
|
57 | value: function componentWillUnmount() {
|
58 | this._cleanupChild();
|
59 | this.domNode = null;
|
60 | }
|
61 | }, {
|
62 | key: '_renderChild',
|
63 | value: function _renderChild(props) {
|
64 | if (!this.domNode) {
|
65 | return null;
|
66 | }
|
67 |
|
68 | try {
|
69 | var contextValuesAsProps = {
|
70 | config: this.context.config,
|
71 | router: this.context.router
|
72 | };
|
73 | var bridgedComponent = React.createElement(ContextBridge, contextValuesAsProps, props.children);
|
74 | ReactDOM.render(bridgedComponent, this.domNode);
|
75 | } catch (e) {
|
76 | console.warn('error rendering: ', e);
|
77 | ReactDOM.render(ErrorUtils.errorToElement(e), this.domNode);
|
78 | }
|
79 | }
|
80 | }, {
|
81 | key: '_cleanupChild',
|
82 | value: function _cleanupChild() {
|
83 | if (this.domNode) {
|
84 | try {
|
85 | ReactDOM.unmountComponentAtNode(this.domNode);
|
86 | } catch (err) {
|
87 | console.log("Error unmounting component", err);
|
88 | }
|
89 | }
|
90 | }
|
91 | }, {
|
92 | key: 'render',
|
93 | value: function render() {
|
94 | var _this2 = this;
|
95 |
|
96 | var extraClass = this.props.className || '';
|
97 |
|
98 | return React.createElement('div', { className: 'sandbox-component ' + extraClass, ref: function ref(node) {
|
99 | _this2.domNode = node;
|
100 | } });
|
101 | }
|
102 | }]);
|
103 |
|
104 | return SandboxedComponent;
|
105 | }(React.Component);
|
106 |
|
107 | SandboxedComponent.propTypes = {
|
108 | children: PropTypes.node,
|
109 | className: PropTypes.string
|
110 | };
|
111 |
|
112 | SandboxedComponent.contextTypes = {
|
113 | router: PropTypes.object,
|
114 | config: PropTypes.object
|
115 | }; |
\ | No newline at end of file |