jam-cloud/web/app/assets/javascripts/react-init.js

100 lines
3.4 KiB
JavaScript

window.DID_REACT_INIT_RUN = true;
window.JK = window.JK || {};
window.JK.Actions = {}
window.JK.Stores = {}
window.JK.Components = {}
if (typeof React !== 'undefined' && !React.createClass && window.createReactClass) {
React.createClass = window.createReactClass;
}
(function addLegacyReactCompat() {
if (typeof window.React === 'undefined' || typeof window.ReactDOM === 'undefined') {
return;
}
var React = window.React;
var ReactDOM = window.ReactDOM;
var ReactRailsUJS = window.ReactRailsUJS;
// Legacy app code still calls React.render directly.
if (typeof React.render !== 'function') {
React.render = function renderCompat(element, node) {
if (typeof ReactDOM.render === 'function') {
return ReactDOM.render(element, node);
}
if (typeof ReactDOM.createRoot === 'function') {
if (!node.__jkReactRoot) {
node.__jkReactRoot = ReactDOM.createRoot(node);
}
node.__jkReactRoot.render(element);
return node.__jkReactRoot;
}
throw new Error('No supported ReactDOM render API found');
};
}
// Force react_ujs to use legacy render path to avoid repeated createRoot warnings
// from react-dom UMD integration in this legacy Sprockets stack.
if (ReactRailsUJS && typeof ReactRailsUJS.findOrCreateRoot === 'function') {
ReactRailsUJS.findOrCreateRoot = function findOrCreateRootCompat(node) {
return {
render: function render(component) {
if (typeof ReactDOM.render === 'function') {
return ReactDOM.render(component, node);
}
if (!node.__jkReactRoot && typeof ReactDOM.createRoot === 'function') {
node.__jkReactRoot = ReactDOM.createRoot(node);
}
return node.__jkReactRoot && node.__jkReactRoot.render(component);
},
unmount: function unmount() {
if (typeof ReactDOM.unmountComponentAtNode === 'function') {
return ReactDOM.unmountComponentAtNode(node);
}
if (node.__jkReactRoot && typeof node.__jkReactRoot.unmount === 'function') {
node.__jkReactRoot.unmount();
node.__jkReactRoot = null;
}
}
};
};
}
if (typeof React.unmountComponentAtNode !== 'function' && typeof ReactDOM.unmountComponentAtNode === 'function') {
React.unmountComponentAtNode = ReactDOM.unmountComponentAtNode.bind(ReactDOM);
}
if (typeof React.findDOMNode !== 'function' && typeof ReactDOM.findDOMNode === 'function') {
React.findDOMNode = ReactDOM.findDOMNode.bind(ReactDOM);
}
// Legacy class components frequently call this.getDOMNode().
if (
React.Component &&
React.Component.prototype &&
typeof React.Component.prototype.getDOMNode !== 'function' &&
typeof ReactDOM.findDOMNode === 'function'
) {
React.Component.prototype.getDOMNode = function getDOMNodeCompat() {
return ReactDOM.findDOMNode(this);
};
}
// Legacy createClass components also call this.getDOMNode(); inject into every spec.
if (typeof React.createClass === 'function' && typeof ReactDOM.findDOMNode === 'function') {
var originalCreateClass = React.createClass;
React.createClass = function createClassCompat(spec) {
if (spec && typeof spec.getDOMNode !== 'function') {
spec.getDOMNode = function getDOMNodeCompat() {
return ReactDOM.findDOMNode(this);
};
}
return originalCreateClass(spec);
};
}
})();