webpack-bootstrap-ui-kit/src/js_old/_components/_ui.nocaptcha.js

98 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-08-18 20:51:15 +02:00
"use strict";
2020-12-24 23:42:33 +01:00
2021-08-18 20:51:15 +02:00
import $ from "jquery";
import Events from "../lib/_events";
2019-06-08 17:20:51 +02:00
const NoCaptcha = (($) => {
// Constants
2021-04-28 17:15:49 +02:00
const $window = $(window);
2019-06-08 17:20:51 +02:00
const D = document;
2021-08-18 20:51:15 +02:00
const $Body = $("body");
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
const NAME = "jsNoCaptcha";
2019-06-08 17:20:51 +02:00
class NoCaptcha {
static init() {
const ui = this;
ui.dispose();
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: init`);
2021-01-15 15:52:46 +01:00
2021-08-18 20:51:15 +02:00
if (
$(".g-recaptcha").length &&
typeof $window[0].grecaptcha === "undefined"
) {
2021-01-15 15:52:46 +01:00
console.log(`${NAME}: Loading Captcha API`);
$.getScript(
2021-08-18 20:51:15 +02:00
"https://www.google.com/recaptcha/api.js?render=explicit&hl=en&onload=noCaptchaFieldRender",
2021-01-15 15:52:46 +01:00
() => {
this.renderCaptcha();
2021-08-18 20:51:15 +02:00
}
2021-01-15 15:52:46 +01:00
);
} else {
this.renderCaptcha();
}
2019-06-08 17:20:51 +02:00
}
static dispose() {
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: dispose`);
2019-06-08 17:20:51 +02:00
}
static renderCaptcha() {
2021-04-28 17:15:49 +02:00
const grecaptcha = $window[0].grecaptcha;
2021-08-18 20:51:15 +02:00
if (
typeof grecaptcha === "undefined" ||
typeof grecaptcha.render === "undefined"
) {
2021-04-28 17:15:49 +02:00
return;
}
2019-06-08 17:20:51 +02:00
2021-04-28 17:15:49 +02:00
console.log(`${NAME}: Rendering Captcha`);
2021-08-18 20:51:15 +02:00
const $_noCaptchaFields = $(".g-recaptcha");
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
if (!$(".g-recaptcha").length) {
2021-01-15 15:52:46 +01:00
console.log(`${NAME}: No Captcha fields`);
return;
}
2019-06-08 17:20:51 +02:00
const submitListener = (e) => {
2021-08-18 20:51:15 +02:00
const $field = $(e.currentTarget).find(".g-recaptcha");
grecaptcha.execute($field.data("widgetid"));
2019-06-08 17:20:51 +02:00
};
$_noCaptchaFields.each((i, field) => {
const $field = $(field);
2021-08-18 20:51:15 +02:00
if ($field.data("widgetid") || $field.html().length) {
2019-06-08 17:20:51 +02:00
return;
}
2021-08-18 20:51:15 +02:00
const $form = $field.data("form")
? $(`#${$field.data("form")}`)
: $field.parents("form");
2019-06-08 17:20:51 +02:00
2019-10-20 01:40:40 +02:00
const widget_id = grecaptcha.render(field, $field.data());
2021-08-18 20:51:15 +02:00
$field.data("widgetid", widget_id);
2019-10-20 01:40:40 +02:00
// For the invisible captcha we need to setup some callback listeners
2021-08-18 20:51:15 +02:00
if ($field.data("size") === "invisible" && !$field.data("callback")) {
2019-10-20 01:40:40 +02:00
grecaptcha.execute(widget_id);
2021-08-18 20:51:15 +02:00
$form.on("submit", submitListener);
2019-06-08 17:20:51 +02:00
}
});
}
}
2021-04-28 17:15:49 +02:00
$window.on(`${NAME}.init ${Events.AJAX} ${Events.LOADED}`, () => {
2019-06-08 17:20:51 +02:00
NoCaptcha.init();
});
2021-04-28 17:15:49 +02:00
$window.data(`${NAME}`, NoCaptcha);
$window[0].noCaptchaFieldRender = NoCaptcha.renderCaptcha;
2019-06-08 17:20:51 +02:00
return NoCaptcha;
})($);
export default NoCaptcha;