Skip to content

Commit

Permalink
Add dynamic display for custom themes settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Jul 30, 2023
1 parent f9e3433 commit a3181a0
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion assets/js/general.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global jQuery, HCaptchaGeneralObject */
/* global jQuery, HCaptchaGeneralObject, HCaptchaMainObject */

/**
* @param HCaptchaGeneralObject.ajaxUrl
Expand All @@ -12,6 +12,7 @@
* @param HCaptchaGeneralObject.modeTestPublisherSiteKey
* @param HCaptchaGeneralObject.modeTestEnterpriseSafeEndUserSiteKey
* @param HCaptchaGeneralObject.modeTestEnterpriseBotDetectedSiteKey
* @param HCaptchaMainObject.params
*/

/**
Expand All @@ -23,6 +24,9 @@ const general = function( $ ) {
const msgSelector = '#hcaptcha-message';
const $message = $( msgSelector );
const $language = $( '[name="hcaptcha_settings[language]"]' );
const $customThemes = $( '[name="hcaptcha_settings[custom_themes][]"]' );
const $configParams = $( '[name="hcaptcha_settings[config_params]"]' );
const $submit = $( '#submit' );
const modes = {};

modes[ HCaptchaGeneralObject.modeLive ] = HCaptchaGeneralObject.siteKey;
Expand Down Expand Up @@ -82,6 +86,29 @@ const general = function( $ ) {
t.parentNode.insertBefore( s, t );
}

function applyCustomThemes() {
let paramsJson = $configParams.val().trim();
let params;

paramsJson = paramsJson ? paramsJson : null;

try {
params = JSON.parse( paramsJson );
} catch ( e ) {
$configParams.css( 'background-color', '#ffabaf' );
$submit.attr( 'disabled', true );
showErrorMessage( 'Bad JSON!' );

return;
}

params = $customThemes.prop( 'checked' ) ? params : null;

HCaptchaMainObject.params = JSON.stringify( params );

hCaptchaGeneralReset();
}

$( '#check_config' ).on( 'click', function( event ) {
event.preventDefault();
clearMessage();
Expand Down Expand Up @@ -154,6 +181,19 @@ const general = function( $ ) {
$( '.h-captcha' ).attr( 'data-sitekey', modes[ mode ] );
hCaptchaGeneralReset();
} );

$customThemes.on( 'change', function() {
applyCustomThemes();
} );

$configParams.on( 'blur', function() {
applyCustomThemes();
} );

$configParams.on( 'focus', function() {
$configParams.css( 'background-color', 'unset' );
$submit.attr( 'disabled', false );
} );
};

window.hCaptchaGeneral = general;
Expand Down

0 comments on commit a3181a0

Please sign in to comment.