| @@ -1,9 +1,15 @@ | ||
| 1 | 1 | ( function() { |
| 2 | + 'use strict'; | |
| 2 | 3 | |
| 4 | + const globalVars = { | |
| 5 | + sendTestEmailModal: null | |
| 6 | + }; | |
| 7 | + | |
| 3 | 8 | function addEventListeners() { |
| 4 | 9 | document.addEventListener( 'change', handleChangeEvent ); |
| 5 | 10 | document.addEventListener( 'keydown', handleKeyDownEvent ); |
| 11 | + document.addEventListener( 'click', handleClickEvent ); | |
| 6 | 12 | } |
| 7 | 13 | |
| 8 | 14 | function handleChangeEvent( e ) { |
| 9 | 15 | if ( 'INPUT' === e.target.nodeName && 'checkbox' === e.target.type && e.target.parentNode.classList.contains( 'frm_toggle_block' ) ) { |
| @@ -8,12 +14,16 @@ | ||
| 8 | 14 | function handleChangeEvent( e ) { |
| 9 | 15 | if ( 'INPUT' === e.target.nodeName && 'checkbox' === e.target.type && e.target.parentNode.classList.contains( 'frm_toggle_block' ) ) { |
| 10 | 16 | handleToggleChangeEvent( e ); |
| 11 | 17 | } |
| 18 | + | |
| 19 | + if ( 'frm_currency' === e.target.id ) { | |
| 20 | + syncCurrencyOptions( e.target ); | |
| 21 | + } | |
| 12 | 22 | } |
| 13 | 23 | |
| 14 | 24 | function handleKeyDownEvent( e ) { |
| 15 | - switch ( e.key ) { | |
| 25 | + switch ( e.key ) { // eslint-disable-line sonarjs/no-small-switch | |
| 16 | 26 | case ' ': |
| 17 | 27 | handleSpaceDownEvent( e ); |
| 18 | 28 | break; |
| 19 | 29 | } |
| @@ -20,8 +30,84 @@ | ||
| 20 | 30 | } |
| 21 | 31 | |
| 22 | 32 | function handleToggleChangeEvent( e ) { |
| 23 | 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; | |
| 24 | 110 | } |
| 25 | 111 | |
| 26 | 112 | function handleSpaceDownEvent( e ) { |
| 27 | 113 | if ( e.target.classList.contains( 'frm_toggle' ) ) { |