File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/resources/js/site/components/checkoutDataController.js
/**
* Controls the checkout form
*/
class CheckoutDataController {
constructor() {
this.checkoutDataForm = document.querySelector('.js-checkout-data-form');
if(!isset(this.checkoutDataForm)) return;
this.differentEmailAddressForInvoiceCheckbox = this.checkoutDataForm.querySelector('#checkout_send_invoice_to_other_email');
this.clientTypeRadioButtons = this.checkoutDataForm.getElementsByClassName('js-client-type');
if(this.differentEmailAddressForInvoiceCheckbox) this.toggleDifferentEmailAddressField(this.differentEmailAddressForInvoiceCheckbox.checked);
//Bind event listeners to this class.
this.controlListeners = this.controlListeners.bind(this);
this.triggerToggleOtherEmailAddressForInvoice = this.triggerToggleOtherEmailAddressForInvoice.bind(this);
this.toggleDifferentEmailAddressField = this.toggleDifferentEmailAddressField.bind(this);
this.clientTypeChanged = this.clientTypeChanged.bind(this);
this.changeClientType = this.changeClientType.bind(this);
//Enable the listeners.
this.controlListeners(true);
}
/**
* @param {boolean} enabled
*/
controlListeners(enabled) {
this.differentEmailAddressForInvoiceCheckbox.removeEventListener('change', this.triggerToggleOtherEmailAddressForInvoice);
const clientTypeRadioButtonsLength = this.clientTypeRadioButtons.length;
for (let index = 0; index < clientTypeRadioButtonsLength; index++) {
const clientTypeRadioButton = this.clientTypeRadioButtons[index];
clientTypeRadioButton.removeEventListener('change', this.clientTypeChanged);
if( clientTypeRadioButton.checked ) {
this.changeClientType(clientTypeRadioButton.value);
}
}
if (enabled) {
this.differentEmailAddressForInvoiceCheckbox.addEventListener('change', this.triggerToggleOtherEmailAddressForInvoice);
for (let index = 0; index < clientTypeRadioButtonsLength; index++) this.clientTypeRadioButtons[index].addEventListener('change', this.clientTypeChanged);
}
}
/**
* Toggle the shipping address fields
*
* @param event
*/
triggerToggleOtherEmailAddressForInvoice(event)
{
const input = event.currentTarget;
this.toggleDifferentEmailAddressField(input.checked);
}
toggleDifferentEmailAddressField(value)
{
const otherEmailAddressField = this.checkoutDataForm.querySelector('.js-other-email-for-invoice-area');
// Hidden is the inverted value of the checked
otherEmailAddressField.hidden = !value;
}
clientTypeChanged(event)
{
const changedRadioButton = event.currentTarget;
this.changeClientType(changedRadioButton.value);
}
/**
* Note: The client type is the index of the site.client_types config.
* 0 = business
* 1 = private
* 2 = organisation
*
* @param clientTypeId
*/
changeClientType(clientTypeId)
{
const companyFields = this.checkoutDataForm.querySelector('.js-company-area');
companyFields.hidden = (clientTypeId == 1);
}
}
export { CheckoutDataController }