All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.primefaces.captcha.captcha.js Maven / Gradle / Ivy

There is a newer version: 14.0.0
Show newest version
/**
 * __PrimeFaces Captcha Widget__
 *
 * Captcha is a form validation component based on Recaptcha API V2.
 *
 * @typedef {"auto" | "light" | "dark"} PrimeFaces.widget.Captcha.Theme Captcha features light and dark modes for theme.
 *
 * @interface {PrimeFaces.widget.CaptchaCfg} cfg The configuration for the {@link  Captcha| Captcha widget}.
 * You can access this configuration via {@link PrimeFaces.widget.BaseWidget.cfg|BaseWidget.cfg}. Please note that this
 * configuration is usually meant to be read-only and should not be modified.
 * @extends {PrimeFaces.widget.BaseWidgetCfg} cfg
 *
 * @prop {string} cfg.callback Name of a function in the global scope. Callback to be executed when the user submits a
 * successful CAPTCHA response. The user's response, goggle recaptcha-response, will be the input for your callback
 * function.
 * @prop {string} cfg.expired Name of a function in the global scope. The callback executed when the captcha response
 * expires and the user needs to solve a new captcha.
 * @prop {string} cfg.language Language in which information about this captcha is shown.
 * @prop {string} cfg.sitekey Public recaptcha key for a specific domain (deprecated).
 * @prop {string} cfg.size Size of the recaptcha.
 * @prop {string} cfg.sourceUrl URL for the ReCaptcha JavaScript file. Some countries do not have access to Google.
 * @prop {number} cfg.tabindex Position of the input element in the tabbing order.
 * @prop {PrimeFaces.widget.Captcha.Theme} cfg.theme Theme of the captcha.
 */
PrimeFaces.widget.Captcha = PrimeFaces.widget.BaseWidget.extend({

    /**
     * @override
     * @inheritdoc
     * @param {TCfg} cfg
     */
    init: function(cfg) {
        this._super(cfg);
        this.cfg.language = this.cfg.language||'en';
        this.cfg.theme = this.cfg.theme || PrimeFaces.env.getThemeContrast();

        var $this = this;

        window[this.getInitCallbackName()] = function() {
            $this.render();
        };

        this.appendScript();
    },
    
    /**
     * Appends the script to the document body.
     * @private
     */
    appendScript : function() {
        var nonce = '';
        if (PrimeFaces.csp.NONCE_VALUE) {
            nonce = 'nonce="'+PrimeFaces.csp.NONCE_VALUE+'"';
        }
        var sourceUrl = this.cfg.sourceUrl || 'https://www.google.com/recaptcha/api.js';
        $(document.body).append('