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

89 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-12-24 23:42:33 +01:00
'use strict';
2019-06-08 17:20:51 +02:00
import $ from 'jquery';
import Events from '../_events';
import Spinner from './_ui.spinner';
const NoCaptcha = (($) => {
// Constants
const W = window;
const D = document;
const $Body = $('body');
const NAME = 'NoCaptcha';
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
if ($('.g-recaptcha').length && typeof grecaptcha === 'undefined') {
console.log(`${NAME}: Loading Captcha API`);
$.getScript(
'https://www.google.com/recaptcha/api.js?render=explicit&hl=en&onload=noCaptchaFieldRender',
() => {
this.renderCaptcha();
},
);
} 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() {
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: Rendering Captcha`);
2019-06-08 17:20:51 +02:00
const $_noCaptchaFields = $('.g-recaptcha');
2021-01-15 15:52:46 +01:00
if (!$('.g-recaptcha').length) {
console.log(`${NAME}: No Captcha fields`);
return;
}
2019-06-08 17:20:51 +02:00
const submitListener = (e) => {
2019-10-20 01:40:40 +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-01-15 15:52:46 +01:00
if ($field.data('widgetid') || $field.html().length) {
2019-06-08 17:20:51 +02:00
return;
}
2020-12-24 23:42:33 +01: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());
$field.data('widgetid', widget_id);
// For the invisible captcha we need to setup some callback listeners
2019-06-08 17:20:51 +02:00
if ($field.data('size') === 'invisible' && !$field.data('callback')) {
2019-10-20 01:40:40 +02:00
grecaptcha.execute(widget_id);
2019-06-08 17:20:51 +02:00
$form.on('submit', submitListener);
}
});
}
}
2021-01-15 15:52:46 +01:00
$(W).on(`${NAME}.init ${Events.AJAX} ${Events.LOADED}`, () => {
2019-06-08 17:20:51 +02:00
NoCaptcha.init();
});
W.NoCaptcha = NoCaptcha;
W.noCaptchaFieldRender = NoCaptcha.renderCaptcha;
return NoCaptcha;
})($);
export default NoCaptcha;