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

98 lines
2.3 KiB
JavaScript
Raw Normal View History

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