react-oc
Version:
A React component that allows OpenComponents to operate within a react application.
151 lines (110 loc) • 6.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Prefetched = void 0;
var _react = _interopRequireDefault(require("react"));
var _OCContext = require("./OCContext");
var _dangerousHtml = require("./lib/dangerousHtml");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var Prefetched =
/*#__PURE__*/
function (_React$Component) {
_inherits(Prefetched, _React$Component);
function Prefetched(props) {
var _this;
_classCallCheck(this, Prefetched);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Prefetched).call(this, props));
_this.ref = _react.default.createRef();
return _this;
}
_createClass(Prefetched, [{
key: "render",
value: function render() {
var _this2 = this;
var _this$props = this.props,
id = _this$props.id,
className = _this$props.className,
prefetchKey = _this$props.prefetchKey,
captureAs = _this$props.captureAs;
if (!prefetchKey) {
throw new Error("Mandatory prop 'prefetchKey' was not provided.");
}
return _react.default.createElement(_OCContext.OCContext.Consumer, null, function (context) {
if (!context) {
throw new Error('OpenComponent.Prefetched must be nested within a <ComponentContext />');
}
_this2.oc = context.oc;
_this2.saveElements = context.saveElements;
_this2.capturedElements = _this2.props.captureAs && context.getElements(_this2.props.captureAs);
_this2.html = _this2.capturedElements ? '' : context.getHtml(prefetchKey);
var suppressHydrationWarning = false;
if (_this2.html === undefined) {
_this2.html = _this2.props.fallback || '';
suppressHydrationWarning = true;
}
var html = (0, _dangerousHtml.dangerousHtml)(_this2.html);
return _react.default.createElement("div", {
ref: _this2.ref,
id: id,
className: className,
dangerouslySetInnerHTML: html,
suppressHydrationWarning: suppressHydrationWarning
});
});
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
var _this3 = this;
if (this.capturedElements) {
this.ref.current.innerHTML = '';
for (var i = 0; i < this.capturedElements.length; i++) {
var el = this.capturedElements[i];
this.ref.current.appendChild(el);
}
delete this.capturedElements;
return;
}
var div = this.ref.current;
if (!div) {
return;
}
var markupWasFromContext = this.html && div.innerHTML === this.html;
if (markupWasFromContext) {
// if not hydrating... TODO: DETECTION ALGORITHM
var keepScripts = true;
var elements = this.oc.$.parseHTML(this.html, keepScripts);
this.oc.$(div).html(elements);
}
if (this.props.captureAs) {
this.saveElements(this.props.captureAs, _toConsumableArray(div.childNodes));
}
var containsUnrenderedComponent = div.childNodes.length === 1 && div.childNodes[0].tagName && div.childNodes[0].tagName.toLowerCase() === 'oc-component' && div.childNodes[0].getAttribute('data-rendered') !== 'true';
if (containsUnrenderedComponent) {
var element = this.oc.$(div.childNodes[0]);
this.oc.renderNestedComponent(element, function () {
if (_this3.props.captureAs) {
_this3.saveElements(_this3.props.captureAs, _toConsumableArray(div.childNodes));
}
});
}
}
}]);
return Prefetched;
}(_react.default.Component);
exports.Prefetched = Prefetched;
//# sourceMappingURL=OpenComponent.Prefetched.js.map