File: D:/HostingSpaces/SBogers10/shop.komma.nl/resources/js/components/addressFormController.js
class AddressFormController {
/**
* @param {HTMLElement} checkoutFormElement
* @param {ValidationService} validationService
*/
constructor(checkoutFormElement, validationService) {
//Select some elements
if(!checkoutFormElement) {
console.error('AddressFormController: Expecting a element upon construction that holds all address form elements. AddressFormController stopped working');
return;
}
this.checkoutFormElement = checkoutFormElement;
this.validationService = validationService;
this.shippingAddressCheckbox = checkoutFormElement.querySelector('.js-shipping-address-equals');
this.shippingAddressFormGroup = checkoutFormElement.querySelector('.js-shipping-address');
this.shippingAddressFormGroupInputs = this.shippingAddressFormGroup.querySelectorAll('input');
this.shippingAddressCheckboxChanged = this.shippingAddressCheckboxChanged.bind(this);
this.shippingAddressCheckboxChanged();
this.controlListeners(true);
}
/**
* @param {boolean} enabled
*/
controlListeners(enabled)
{
this.shippingAddressCheckbox.removeEventListener('click', this.shippingAddressCheckboxChanged);
if(enabled) {
this.shippingAddressCheckbox.addEventListener('click', this.shippingAddressCheckboxChanged);
}
}
/**
* {MouseEvent|undefined} event
*/
shippingAddressCheckboxChanged(event)
{
if(this.shippingAddressCheckbox.checked) {
this.shippingAddressFormGroup.classList.add('u-hidden');
this.clearShippingAddressFields();
} else {
this.shippingAddressFormGroup.classList.remove('u-hidden');
}
}
clearShippingAddressFields()
{
this.shippingAddressFormGroupInputs.forEach(
/** @param {HTMLInputElement} input */
function(input) {
input.value = '';
}
);
}
}
export { AddressFormController }