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

72 lines
1.7 KiB
JavaScript
Raw Normal View History

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();
console.log(`Initializing: ${NAME}`);
this.renderCaptcha();
}
static dispose() {
console.log(`Destroying: ${NAME}`);
}
static renderCaptcha() {
console.log(`Rendering Captcha: ${NAME}`);
if (typeof grecaptcha === 'undefined') {
console.log('Captcha API isn\'t available yet');
}
const $_noCaptchaFields = $('.g-recaptcha');
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);
if ($field.data('widgetid')) {
return;
}
const $form = $field.data('form') ? $(`#${ $field.data('form')}`) : $field.parents('form');
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);
}
});
}
}
$(W).on(`${Events.AJAX}`, () => {
NoCaptcha.init();
});
W.NoCaptcha = NoCaptcha;
W.noCaptchaFieldRender = NoCaptcha.renderCaptcha;
return NoCaptcha;
})($);
export default NoCaptcha;