UNPKG

react-crowdriff-gallery

Version:

React component to put a CrowdRiff embed code in a React app

102 lines (78 loc) 4.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.CrowdRiffGallery = void 0; var _react = _interopRequireDefault(require("react")); 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 _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 defaultProps = { scriptUrl: 'https://starling.crowdriff.com/js/crowdriff.js' }; var CrowdRiffGallery = /*#__PURE__*/ function (_React$Component) { _inherits(CrowdRiffGallery, _React$Component); function CrowdRiffGallery() { _classCallCheck(this, CrowdRiffGallery); return _possibleConstructorReturn(this, _getPrototypeOf(CrowdRiffGallery).apply(this, arguments)); } _createClass(CrowdRiffGallery, [{ key: "componentDidMount", value: function componentDidMount() { var _this$props = this.props, scriptUrl = _this$props.scriptUrl, hash = _this$props.hash; // Warn if this script is being used outside of platform without the correct scriptUrl if (process.env.NODE_ENV !== 'development' && scriptUrl.indexOf('crowdriff.com') === -1 && window.location.href.indexOf('//platform.') === -1) { console.warn('Invalid scriptUrl prop passed to CrowdRiffGallery component.'); } var script = document.createElement('script'); script.src = scriptUrl; script.id = "cr-init__".concat(hash); script.async = true; this.wrapper.appendChild(script); if (window.crowdriff__gallery && !window.crowdriff__gallery.interval) { window.crowdriff__gallery.interval = setInterval(window.crowdriff__gallery.processQueue, 300); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var _this = this; window.crowdriff__gallery.rendered = window.crowdriff__gallery.rendered.filter(function (id) { return id !== "cr__".concat(_this.props.hash); }); } }, { key: "shouldComponentUpdate", value: function shouldComponentUpdate(nextProps) { if (nextProps.allowUpdate) return true; return false; } }, { key: "render", value: function render() { var _this2 = this; if (!this.props.hash) console.warn('No gallery hash passed to CrowdRiff gallery component'); return _react.default.createElement("div", { ref: function ref(c) { return _this2.wrapper = c; }, id: "gallery-wrapper-".concat(this.props.hash) }); } }]); return CrowdRiffGallery; }(_react.default.Component); exports.CrowdRiffGallery = CrowdRiffGallery; CrowdRiffGallery.defaultProps = defaultProps; var _default = CrowdRiffGallery; exports.default = _default;