@mopinion/survey
Version:
Collect customer feedback with the Mopinion survey library
178 lines (177 loc) • 9.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _preact = require("preact");
var _excluded = ["sitekey", "onChange", "theme", "type", "tabindex", "onExpired", "size", "stoken", "grecaptcha", "badge"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var lang = typeof window !== 'undefined' && window.recaptchaOptions && window.recaptchaOptions.lang ? "&hl=".concat(window.recaptchaOptions.lang) : '';
var URL = "https://www.google.com/recaptcha/api.js?&onload=recaptchaLoaded&render=explicit".concat(lang);
var isRecaptchaLoaded = function isRecaptchaLoaded() {
return typeof window !== 'undefined' && typeof window.grecaptcha !== 'undefined';
};
var ReCaptcha = exports["default"] = function (_Component) {
function ReCaptcha() {
var _this;
_classCallCheck(this, ReCaptcha);
_this = _callSuper(this, ReCaptcha);
_defineProperty(_this, "execute", function () {
if (isRecaptchaLoaded() && _this.element && _this.recaptchaId !== null && _this.props.size === 'invisible') {
grecaptcha.execute(_this.recaptchaId);
return true;
}
return false;
});
_defineProperty(_this, "reset", function () {
if (isRecaptchaLoaded() && _this.element && _this.recaptchaId !== null) {
grecaptcha.reset(_this.recaptchaId);
return true;
}
return false;
});
_defineProperty(_this, "getResponse", function () {
if (isRecaptchaLoaded() && _this.element && _this.recaptchaId !== null) {
return grecaptcha.getResponse(_this.recaptchaId);
}
return null;
});
_defineProperty(_this, "handleChange", function (response) {
if (_this.props.onChange) {
_this.props.onChange(response);
}
});
_defineProperty(_this, "handleExpired", function () {
if (_this.props.onExpired) {
_this.props.onExpired();
}
});
_defineProperty(_this, "removeRecaptcha", function () {
grecaptcha.reset(_this.recaptchaId);
});
_defineProperty(_this, "renderRecaptcha", function () {
_this.recaptchaId = grecaptcha.render(_this.element, {
sitekey: _this.props.sitekey,
callback: _this._handleChange,
theme: _this.props.theme,
type: _this.props.type,
tabindex: _this.props.tabindex,
'expired-callback': _this._handleExpired,
size: _this.props.size,
stoken: _this.props.stoken,
badge: _this.props.badge
});
});
_defineProperty(_this, "captureRef", function (elem) {
_this.element = elem;
});
_defineProperty(_this, "initializeRecaptcha", function () {
if (isRecaptchaLoaded()) {
_this.clearTimer();
_this.renderRecaptcha();
} else if (!_this.isRecaptchaJSLibInjected()) {
var script = document.createElement('script');
window.recaptchaLoaded = _this.initializeRecaptcha.bind(_this);
script.src = URL;
script.async = 1;
script.defer = 1;
script.onerror = function () {
console.log('Error loading google recaptcha!');
};
document.body.appendChild(script);
} else {
_this.startTimer();
}
});
_defineProperty(_this, "isRecaptchaJSLibInjected", function () {
if (typeof window === 'undefined') {
return false;
}
if (!document.scripts || document.scripts.length === 0) {
return false;
}
var scripts = Object.assign([], document.scripts);
return scripts.filter(function (script) {
return script.src.match(/((http|https):)?\/\/(www\.)?google\.com\/recaptcha\/(.*)/i) !== null;
}).length > 0;
});
_defineProperty(_this, "startTimer", function () {
_this.waitingTimer = setTimeout(_this._initializeRecaptcha, 200);
});
_defineProperty(_this, "clearTimer", function () {
if (_this.waitingTimer !== null) {
clearInterval(_this.waitingTimer);
}
});
_this.waitingTimer = null;
_this.element = null;
_this.recaptchaId = null;
_this._captureRef = _this.captureRef.bind(_this);
_this._initializeRecaptcha = _this.initializeRecaptcha.bind(_this);
_this._handleExpired = _this.handleExpired.bind(_this);
_this._handleChange = _this.handleChange.bind(_this);
return _this;
}
_inherits(ReCaptcha, _Component);
return _createClass(ReCaptcha, [{
key: "componentDidMount",
value: function componentDidMount() {
this.initializeRecaptcha();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.clearTimer();
if (isRecaptchaLoaded() && this.element && this.recaptchaId !== null) {
this.removeRecaptcha();
}
if (window.___grecaptcha_cfg) {
delete window.___grecaptcha_cfg.clients[this.recaptchaId];
window.___grecaptcha_cfg.count--;
}
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
sitekey = _this$props.sitekey,
onChange = _this$props.onChange,
theme = _this$props.theme,
type = _this$props.type,
tabindex = _this$props.tabindex,
onExpired = _this$props.onExpired,
size = _this$props.size,
stoken = _this$props.stoken,
grecaptcha = _this$props.grecaptcha,
badge = _this$props.badge,
childProps = _objectWithoutProperties(_this$props, _excluded);
return (0, _preact.h)("div", _extends({}, childProps, {
ref: this._captureRef
}));
}
}]);
}(_preact.Component);
ReCaptcha.defaultProps = {
theme: 'light',
type: 'image',
tabindex: 0,
size: 'normal',
badge: 'bottomright'
};
//# sourceMappingURL=ReCaptcha.js.map