-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ItinerisLtd/Fix-Bugs
Fix and Update!
- Loading branch information
Showing
3 changed files
with
135 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,66 @@ | ||
function gfGiftAidOnInputChange(elem) { | ||
if (!(elem instanceof HTMLElement)) { | ||
return; | ||
} | ||
document.addEventListener('DOMContentLoaded', initGiftAid); | ||
|
||
if (! elem.classList.contains('ginput_total') && ! elem.classList.contains('ginput_amount')) { | ||
/** | ||
* Updates the gift aid display, based on a chosen field changing. | ||
*/ | ||
function initGiftAid() { | ||
const gravityForm = document.querySelector('.gform_wrapper'); | ||
if (!(gravityForm instanceof HTMLElement)) { | ||
return; | ||
} | ||
|
||
const parentForm = elem.closest('form'); | ||
if (!(parentForm instanceof HTMLFormElement)) { | ||
const totalSelector = totalFieldSelector(gravityForm, '.ginput_amount'); | ||
if (!totalSelector) { | ||
return; | ||
} | ||
window.gform.addAction('gform_input_change', function (elem) { | ||
const donationValue = parseFloat(elem.value); | ||
const giftAidValue = donationValue * 1.25; | ||
const donationRounded = donationValue.toFixed(2); | ||
const giftAidRounded = giftAidValue.toFixed(2); | ||
if (!donationRounded || !giftAidRounded) { | ||
return; | ||
} | ||
updateGiftAidDisplay(gravityForm, donationRounded, giftAidRounded); | ||
}, 10); | ||
} | ||
|
||
const totalEl = parentForm.querySelector('.ginput_total'); | ||
if (totalEl instanceof HTMLElement) { | ||
elem = totalEl; | ||
/** | ||
* Gets the selector of the total field, selected in the gift aid field settings. | ||
* Will be in the format 'field_<form_id>_<field_id>' and will be an id so the selector will be prefixed by #. | ||
* @param {HTMLElement} gravityForm The form element. | ||
* @param {string} fallback fallback selector if the field is not found. | ||
* @returns {string} The class of the total field. | ||
*/ | ||
function totalFieldSelector(gravityForm, fallback) { | ||
const giftAidComponent = gravityForm.querySelector('.gift-box-wrapper'); | ||
if (!(giftAidComponent instanceof HTMLElement)) { | ||
return fallback; | ||
} | ||
const selectedPriceFieldId = giftAidComponent.dataset.selectedPriceFieldId; | ||
if (!selectedPriceFieldId) { | ||
return fallback; | ||
} | ||
return `#${selectedPriceFieldId}`; | ||
} | ||
|
||
const spanTotal = parentForm.querySelectorAll('.gform_donation_total'); | ||
if (!(spanTotal instanceof NodeList) || 0 === spanTotal.length) { | ||
/** | ||
* Updates the gift aid display. | ||
* @param {HTMLElement} gravityForm The form element. | ||
* @param {object} The donation and gift aid total. | ||
* @returns {void} | ||
*/ | ||
function updateGiftAidDisplay(gravityform, donation, giftAidAmount) { | ||
if (!donation || !giftAidAmount || !(gravityform instanceof HTMLElement)) { | ||
return; | ||
} | ||
|
||
const spanTotalGift = parentForm.querySelectorAll( | ||
'.gform_donation_gifttotal', | ||
); | ||
if (!(spanTotalGift instanceof NodeList) || 0 === spanTotalGift.length) { | ||
const spanTotal = gravityform.querySelector('.gform_donation_total'); | ||
if (!(spanTotal instanceof HTMLSpanElement)) { | ||
return; | ||
} | ||
|
||
const extractFloat = (str) => { | ||
// Regular expression to match floating-point numbers | ||
const regex = /[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/g; | ||
|
||
// Use match() to find all matching numbers in the string | ||
const matches = str.match(regex); | ||
|
||
// If there are matches, convert them to floats and return them; otherwise, return the original str. | ||
return matches ? matches.map(Number) : str; | ||
}; | ||
|
||
const val = extractFloat(elem.value); | ||
const total = parseFloat(val).toFixed(2); | ||
const totalGift = parseFloat(val * 1.25).toFixed(2); | ||
|
||
Array.from(spanTotal).forEach((item) => { | ||
item.innerHTML = total; | ||
}); | ||
|
||
Array.from(spanTotalGift).forEach((item) => { | ||
item.innerHTML = totalGift; | ||
}); | ||
const spanTotalGift = gravityform.querySelector('.gform_donation_gifttotal'); | ||
if (!(spanTotalGift instanceof HTMLSpanElement)) { | ||
return; | ||
} | ||
spanTotal.innerHTML = donation; | ||
spanTotalGift.innerHTML = giftAidAmount; | ||
} | ||
document.addEventListener('DOMContentLoaded', () => { | ||
window.gform.addAction('gform_input_change', gfGiftAidOnInputChange, 10); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters