react-load-script
Version:
react-load-script enables you to easily create components which depend on third party JS scripts
177 lines (137 loc) • 6.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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; }
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; }
var Script = function (_React$Component) {
_inherits(Script, _React$Component);
// A dictionary mapping script URL to a boolean value indicating if the script
// has failed to load.
// A dictionary mapping script URLs to a dictionary mapping
// component key to component for all components that are waiting
// for the script to load.
function Script(props) {
_classCallCheck(this, Script);
var _this = _possibleConstructorReturn(this, (Script.__proto__ || Object.getPrototypeOf(Script)).call(this, props));
_this.scriptLoaderId = 'id' + _this.constructor.idCount++; // eslint-disable-line space-unary-ops, no-plusplus
return _this;
}
// A counter used to generate a unique id for each component that uses
// ScriptLoaderMixin.
// A dictionary mapping script URL to a boolean value indicating if the script
// has already been loaded.
_createClass(Script, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _props = this.props,
onError = _props.onError,
onLoad = _props.onLoad,
url = _props.url;
if (this.constructor.loadedScripts[url]) {
onLoad();
return;
}
if (this.constructor.erroredScripts[url]) {
onError();
return;
}
// If the script is loading, add the component to the script's observers
// and return. Otherwise, initialize the script's observers with the component
// and start loading the script.
if (this.constructor.scriptObservers[url]) {
this.constructor.scriptObservers[url][this.scriptLoaderId] = this.props;
return;
}
this.constructor.scriptObservers[url] = _defineProperty({}, this.scriptLoaderId, this.props);
this.createScript();
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
var url = this.props.url;
var observers = this.constructor.scriptObservers[url];
// If the component is waiting for the script to load, remove the
// component from the script's observers before unmounting the component.
if (observers) {
delete observers[this.scriptLoaderId];
}
}
}, {
key: 'createScript',
value: function createScript() {
var _this2 = this;
var _props2 = this.props,
onCreate = _props2.onCreate,
url = _props2.url,
attributes = _props2.attributes;
var script = document.createElement('script');
onCreate();
// add 'data-' or non standard attributes to the script tag
if (attributes) {
Object.keys(attributes).forEach(function (prop) {
return script.setAttribute(prop, attributes[prop]);
});
}
script.src = url;
// default async to true if not set with custom attributes
if (!script.hasAttribute('async')) {
script.async = 1;
}
var callObserverFuncAndRemoveObserver = function callObserverFuncAndRemoveObserver(shouldRemoveObserver) {
var observers = _this2.constructor.scriptObservers[url];
Object.keys(observers).forEach(function (key) {
if (shouldRemoveObserver(observers[key])) {
delete _this2.constructor.scriptObservers[url][_this2.scriptLoaderId];
}
});
};
script.onload = function () {
_this2.constructor.loadedScripts[url] = true;
callObserverFuncAndRemoveObserver(function (observer) {
observer.onLoad();
return true;
});
};
script.onerror = function () {
_this2.constructor.erroredScripts[url] = true;
callObserverFuncAndRemoveObserver(function (observer) {
observer.onError();
return true;
});
};
document.body.appendChild(script);
}
}, {
key: 'render',
value: function render() {
return null;
}
}]);
return Script;
}(_react2.default.Component);
Script.propTypes = {
attributes: _propTypes.PropTypes.object, // eslint-disable-line react/forbid-prop-types
onCreate: _propTypes.PropTypes.func,
onError: _propTypes.PropTypes.func.isRequired,
onLoad: _propTypes.PropTypes.func.isRequired,
url: _propTypes.PropTypes.string.isRequired
};
Script.defaultProps = {
attributes: {},
onCreate: function onCreate() {},
onError: function onError() {},
onLoad: function onLoad() {} };
Script.scriptObservers = {};
Script.loadedScripts = {};
Script.erroredScripts = {};
Script.idCount = 0;
exports.default = Script;
module.exports = exports['default'];