| 1 |
( function() { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
const globalVars = { |
| 5 |
sendTestEmailModal: null |
| 6 |
}; |
| 7 |
|
| 8 |
function addEventListeners() { |
| 9 |
document.addEventListener( 'change', handleChangeEvent ); |
| 10 |
document.addEventListener( 'keydown', handleKeyDownEvent ); |
| 11 |
document.addEventListener( 'click', handleClickEvent ); |
| 12 |
} |
| 13 |
|
| 14 |
function handleChangeEvent( e ) { |
| 15 |
if ( 'INPUT' === e.target.nodeName && 'checkbox' === e.target.type && e.target.parentNode.classList.contains( 'frm_toggle_block' ) ) { |
| 16 |
handleToggleChangeEvent( e ); |
| 17 |
} |
| 18 |
|
| 19 |
if ( 'frm_currency' === e.target.id ) { |
| 20 |
syncCurrencyOptions( e.target ); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
function handleKeyDownEvent( e ) { |
| 25 |
switch ( e.key ) { // eslint-disable-line sonarjs/no-small-switch |
| 26 |
case ' ': |
| 27 |
handleSpaceDownEvent( e ); |
| 28 |
break; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
function handleToggleChangeEvent( e ) { |
| 33 |
e.target.nextElementSibling.setAttribute( 'aria-checked', e.target.checked ? 'true' : 'false' ); |
| 34 |
} |
| 35 |
|
| 36 |
function handleClickEvent( e ) { |
| 37 |
if ( 'BUTTON' === e.target.nodeName && 'choose' === e.target.dataset.action && e.target.closest( '.frm-email-style' ) ) { |
| 38 |
handleClickChooseEmailStyle( e ); |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
if ( 'frm-send-test-email' === e.target.id ) { |
| 43 |
showSendTestEmailModal(); |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
if ( 'frm-send-test-email-btn' === e.target.id ) { |
| 48 |
handleClickSendTestEmailBtn(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function handleClickChooseEmailStyle( e ) { |
| 53 |
const styleEls = document.querySelectorAll( '.frm-email-style' ); |
| 54 |
styleEls.forEach( el => { |
| 55 |
el.classList.remove( 'frm-email-style--selected' ); |
| 56 |
} ); |
| 57 |
|
| 58 |
const styleEl = e.target.closest( '.frm-email-style' ); |
| 59 |
styleEl.classList.add( 'frm-email-style--selected' ); |
| 60 |
|
| 61 |
const { styleKey } = styleEl.dataset; |
| 62 |
document.getElementById( 'frm-email-style-value' ).value = styleKey; |
| 63 |
} |
| 64 |
|
| 65 |
function showSendTestEmailModal() { |
| 66 |
if ( ! globalVars.sendTestEmailModal ) { |
| 67 |
globalVars.sendTestEmailModal = frmAdminBuild.initModal( '#frm-send-test-email-modal', '400px' ); |
| 68 |
} |
| 69 |
|
| 70 |
globalVars.sendTestEmailModal.dialog( 'open' ); |
| 71 |
} |
| 72 |
|
| 73 |
function handleClickSendTestEmailBtn() { |
| 74 |
const emailInput = document.getElementById( 'frm-test-email-address' ); |
| 75 |
const resultEl = document.getElementById( 'frm-send-test-email-result' ); |
| 76 |
|
| 77 |
const showResult = ( msg, success ) => { |
| 78 |
resultEl.textContent = msg; |
| 79 |
resultEl.classList.add( success ? 'frm_updated_message' : 'frm_error_style' ); |
| 80 |
}; |
| 81 |
|
| 82 |
resultEl.textContent = ''; |
| 83 |
resultEl.classList.remove( 'frm_error_style', 'frm_updated_message' ); |
| 84 |
|
| 85 |
if ( ! emailInput.value ) { |
| 86 |
showResult( 'Empty email address' ); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
const data = new FormData(); |
| 91 |
data.append( 'emails_str', emailInput.value ); |
| 92 |
frmDom.ajax.doJsonPost( 'send_test_email', data ).then( response => { |
| 93 |
showResult( response, true ); |
| 94 |
} ).catch( error => { |
| 95 |
showResult( error ); |
| 96 |
} ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Updates the currency formatting options based on the selected currency. |
| 101 |
* |
| 102 |
* @param {HTMLSelectElement} currencySelect The currency select element. |
| 103 |
*/ |
| 104 |
function syncCurrencyOptions( currencySelect ) { |
| 105 |
const currency = frmSettings.currencies[ currencySelect.value ]; |
| 106 |
|
| 107 |
document.getElementById( 'frm_thousand_separator' ).value = currency.thousand_separator; |
| 108 |
document.getElementById( 'frm_decimal_separator' ).value = currency.decimal_separator; |
| 109 |
document.getElementById( 'frm_decimals' ).value = currency.decimals; |
| 110 |
} |
| 111 |
|
| 112 |
function handleSpaceDownEvent( e ) { |
| 113 |
if ( e.target.classList.contains( 'frm_toggle' ) ) { |
| 114 |
e.preventDefault(); // Prevent automatic browser scroll when space is pressed. |
| 115 |
e.target.click(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
addEventListeners(); |
| 120 |
}() ); |
| 121 |
|