var AddressEditorComponent = class AddressEditor
{
constructor(divId, callbackFunction)
{
this.divId = divId;
this.country = null;
this.state = null;
this.stateCode = null;
this.zipCode = null;
this.city = null;
this.addressLine1 = null;
this.addressLine2 = null;
this.phone = null;
this.phoneCountryCode = null;
this.addressEditorConfig = null;
this.countryConfiguration = null;
this.callbackFunction = (callbackFunction != undefined && callbackFunction != null) ? callbackFunction : null;
addressForms.push(this);
}
paintAddressForm(addressEditorConfig)
{
let addressForm = document.getElementById(this.divId);
this.addressEditorConfig = addressEditorConfig;
if(currentAddressEditor == null)
{
for (let i in addressForms)
{
if (addressForms[i].divId == this.divId)
{
currentAddressEditor = addressForms[i];
break;
}
}
}
if(addressForm == null)
{
return;
}
addressForm.style.display = "block";
let html = '';
if(addressEditorConfig != null && addressEditorConfig.address.isRequested)
{
html += this.paintAddress(addressEditorConfig);
if(!addressEditorConfig.address.isMandatory && addressEditorConfig.phone.isMandatory)
{
html += this.paintPhone()
}
}
else
{
if(addressEditorConfig != null && addressEditorConfig.phone.isRequested)
{
html += this.paintPhone();
}
if(addressEditorConfig != null && addressEditorConfig.cp.isRequested)
{
html += this.paintCp();
}
}
addressForm.innerHTML = html;
this.checkPhoneInputLocation(addressEditorConfig);
this.requireMandatoryFields(addressEditorConfig);
$("textarea[id=" + this.divId + "_input]").val(this.buildAddressText(false));
$(".form-error-address").hide();
}
validateAddress()
{
if($("#popup_address").hasClass('show'))
{
return;
}
}
validatePhone(event)
{
event.preventDefault();
currentAddressEditor.phone = event.target.value;
let phoneCountryElement = document.querySelector('#' + currentAddressEditor.divId + ' #countriesPhoneCode input');
if(phoneCountryElement != null)
{
if(currentAddressEditor.country == null)
{
currentAddressEditor.country = phoneCountryElement.value.toUpperCase();
}
currentAddressEditor.phoneCountryCode = $("#select-contry-" + phoneCountryElement.value).data("prefix");
}
}
validateZipCode(event)
{
event.preventDefault();
currentAddressEditor.zipCode = event.target.value;
}
paintAddress(addressEditorConfig)
{
let html = '
'
html += '
'
html += '
'
html += '
';
return html;
}
paintPhone()
{
let html = ''
+ '
'
+ '
'
+ '
'
+ '
';
return html;
}
paintCp()
{
let html = '';
return html;
}
checkPhoneInputLocation(addressEditorConfig)
{
let phoneElem = document.getElementById("telefono"+this.divId);
if (typeof phoneElem === "undefined" || phoneElem == null)
{
phoneElem = document.getElementById("telefono");
}
if(!addressEditorConfig.phone.isRequested)
{
//$('#telefono').parent().remove();
phoneElem.parentNode.style.display = 'none';
}
else
{
phoneElem.parentNode.style.display = 'block';
phoneElem.addEventListener("focusout", this.validatePhone, false);
let phoneCountryElement = document.querySelector('#' + this.divId + ' #countriesPhoneCode');
if(phoneCountryElement != null)
{
phoneCountryElement.addEventListener("focusout", this.validatePhoneCountry, false);
}
}
if(!addressEditorConfig.address.isMandatory && addressEditorConfig.phone.isMandatory)
{
if(document.querySelector('#formAddressEdition #telefono') != null)
{
document.querySelector('#formAddressEdition #telefono').parentNode.style.display = "none";
}
}
}
requireMandatoryFields(addressEditorConfig)
{
let isAddressSetToMandatoryInTheEventConfig = false;
if(typeof isAddressMandatoryToBuyThisEvent != 'undefined') isAddressSetToMandatoryInTheEventConfig = isAddressMandatoryToBuyThisEvent;
if(isAddressSetToMandatoryInTheEventConfig || addressEditorConfig.address.isMandatory)
{
$('#' + this.divId + "_input").attr("required", true);
if(isAddressSetToMandatoryInTheEventConfig || currentAddressEditor != null && currentAddressEditor.countryConfiguration != null && !currentAddressEditor.countryConfiguration.fields.province.optional && !currentAddressEditor.countryConfiguration.fields.province.hidden)
{
document.querySelector('#stateInput input').required = true;
}
else
{
document.querySelector('#stateInput input').required = false;
}
if(isAddressSetToMandatoryInTheEventConfig || currentAddressEditor != null && currentAddressEditor.countryConfiguration != null && !currentAddressEditor.countryConfiguration.fields.locality.optional && !currentAddressEditor.countryConfiguration.fields.locality.hidden)
{
$('#city').attr("required", true);
}
else
{
$('#city').attr("required", false);
}
if(currentAddressEditor != null && currentAddressEditor.countryConfiguration != null && !currentAddressEditor.countryConfiguration.fields.postal_code.optional && !currentAddressEditor.countryConfiguration.fields.postal_code.hidden)
{
$('#pc').attr("required", true);
}
else
{
$('#pc').attr("required", false);
}
if(isAddressSetToMandatoryInTheEventConfig || currentAddressEditor != null && currentAddressEditor.countryConfiguration != null && !currentAddressEditor.countryConfiguration.fields.address_1.optional && !currentAddressEditor.countryConfiguration.fields.address_1.hidden)
{
$('#addressLine1').attr("required", true);
}
else
{
$('#addressLine1').attr("required", false);
}
if(currentAddressEditor != null && currentAddressEditor.countryConfiguration != null && !currentAddressEditor.countryConfiguration.fields.address_2.hidden)
{
$('#addressLine2').attr("required", currentAddressEditor.countryConfiguration.fields.address_2.required);
}
}
else
{
$('#' + this.divId + "_input").attr("required", false);
document.querySelector('#stateInput input').required = false;
$('#city').attr("required", false);
$('#pc').attr("required", false);
$('#district').attr("required", false);
$('#addressLine1').attr("required", false);
$('#addressLine2').attr("required", false);
}
let phoneElem = document.getElementById("telefono"+this.divId);
if (typeof phoneElem === "undefined" || phoneElem == null)
{
phoneElem = document.getElementById("telefono");
}
if(addressEditorConfig.phone.isMandatory)
{
phoneElem.required = true;
}
else
{
phoneElem.required = false;
}
if(addressEditorConfig.cp.isMandatory)
{
$('#pc').attr("required", true);
if(!addressEditorConfig.address.isMandatory)
{
document.getElementById('pc').addEventListener("focusout", this.validateZipCode, false);
}
}
else
{
if(!addressEditorConfig.address.isMandatory)
{
$('#pc').attr("required", false);
}
}
}
openSelectedAddressModal()
{
document.getElementById('formAddressEdition').reset();
let selectedCountry = this.country;
initCountrySelect('#countryDiv','#country', selectedCountry, false);
if(selectedCountry == null || selectedCountry.length == 0)
{
document.querySelector('#formAddressEdition .text').innerHTML = polyglot.t("global.Please_select_a_country");
}
if((this.state == null || this.state.length == 0) && !setToNoValidate)
{
if(document.querySelector('#formAddressEdition #stateDiv .text') != null)
{
document.querySelector('#formAddressEdition #stateDiv .text').innerHTML = polyglot.t("global.Please_select_a_state_province");
}
}
$("#popup_address").addClass("show");
this.checkPhoneInputLocation(this.addressEditorConfig);
this.requireMandatoryFields(this.addressEditorConfig);
this.fillAddressModal($("textarea[name=address]").val());
}
countrySelected(countrySelected)
{
this.country = countrySelected;
this.enableAddressInputs();
this.loadStates(countrySelected, '#stateDiv', geolocationUrl);
}
fillAddressModal(addressText)
{
if(this.country == null || this.country.length == 0)
{
if(typeof serverProgrammedCountry != 'undefined' && serverProgrammedCountry != null && serverProgrammedCountry.length > 0)
{
initCountrySelect('#countryDiv','#country', serverProgrammedCountry, false);
}
else if(typeof serverLanguage != 'undefined' && serverLanguage != null && serverLanguage.length > 0)
{
this.getCountryFromServerLanguage();
}
else
{
$("#formAddressEdition input[name='country']").val('');
this.disableAddressInputs();
}
}
else
{
this.enableAddressInputs();
$("#formAddressEdition input[name='country']").val(this.country);
}
$("#formAddressEdition input[name='state']").val(this.state);
$("#formAddressEdition input[name='district']").val(this.district);
$("#formAddressEdition input[name='city']").val(this.city);
$("#formAddressEdition input[name='pc']").val(this.zipCode);
$("#formAddressEdition input[name='addressLine1']").val(this.addressLine1);
$("#formAddressEdition input[name='addressLine2']").val(this.addressLine2);
$("#formAddressEdition input[name='phone']").val(this.phone);
}
getCountryFromServerLanguage()
{
let country = null;
for(let i = 0; i < COUNTRIES.length; i++)
{
country = COUNTRIES[i];
if(country.languages != null && country.languages.length > 0)
{
let countryLanguages = country.languages.split(",");
if(countryLanguages != null && countryLanguages.length > 0 && countryLanguages.includes(serverLanguage.replace("_", "-")))
{
initCountrySelect('#countryDiv','#country', country.iso, false);
break;
}
}
}
}
showModal(elemento)
{
$(".modal").hide();
$(elemento).show();
$('.modal-background-general').fadeIn(300);
}
resetAddress()
{
this.country = null;
this.state = null;
this.stateCode = null;
this.district = null;
this.zipCode = null;
this.city = null;
this.addressLine1 = null;
this.addressLine2 = null;
this.phone = null;
}
enableAddressInputs()
{
this.changeAddressInputs(false);
}
disableAddressInputs()
{
this.changeAddressInputs(true);
}
changeAddressInputs(isDisabled)
{
$("#formAddressEdition input[name='state']").attr("disabled", isDisabled);
$("#formAddressEdition input[name='province']").attr("disabled", isDisabled);
$("#formAddressEdition input[name='district']").attr("disabled", isDisabled);
$("#formAddressEdition input[name='city']").attr("disabled", isDisabled);
$("#formAddressEdition input[name='pc']").attr("disabled", isDisabled);
$("#formAddressEdition input[name='addressLine1']").attr("disabled", isDisabled);
$("#formAddressEdition input[name='phone']").attr("disabled", isDisabled);
$("#formAddressEdition input[name='addressLine2']").attr("disabled", isDisabled);
}
buildAddressText(isPalceholder)
{
let addressText = '';
let addressValue = null;
if(this.countryConfiguration == null)
{
if(this.country == null)
{
this.countryConfiguration = getCountry('ES');
}
else
{
this.countryConfiguration = getCountry(this.country);
}
}
addressValue = this.checkAddressValue(this.addressLine1, addressText)
if((addressEditorConfig == null || addressEditorConfig.address.isMandatory) && currentAddressEditor.countryConfiguration != null && !currentAddressEditor.countryConfiguration.fields.address_1.optional && !currentAddressEditor.countryConfiguration.fields.address_1.hidden && !addressValue && !isPalceholder)
{
return '';
}
addressText += addressValue;
addressText += this.checkAddressValue(this.addressLine2, addressText)
addressValue = this.checkAddressValue(this.city, addressText)
if((addressEditorConfig == null || addressEditorConfig.address.isMandatory) && currentAddressEditor.countryConfiguration != null && !currentAddressEditor.countryConfiguration.fields.locality.optional && !currentAddressEditor.countryConfiguration.fields.locality.hidden && !addressValue && !isPalceholder)
{
return '';
}
addressText += addressValue;
addressValue = this.checkAddressValue(this.state, addressText)
if((addressEditorConfig == null || addressEditorConfig.address.isMandatory) && currentAddressEditor.countryConfiguration != null && !currentAddressEditor.countryConfiguration.fields.province.optional && !currentAddressEditor.countryConfiguration.fields.province.hidden && !addressValue && !isPalceholder)
{
return '';
}
addressText += addressValue;
addressValue = this.checkAddressValue(this.country, addressText)
if(!addressValue && !isPalceholder)
{
return '';
}
addressText += addressValue;
addressValue = this.checkAddressValue(this.zipCode, addressText)
if((addressEditorConfig == null || addressEditorConfig.address.isMandatory) && currentAddressEditor.countryConfiguration != null && !currentAddressEditor.countryConfiguration.fields.postal_code.optional && !currentAddressEditor.countryConfiguration.fields.postal_code.hidden && !addressValue && !isPalceholder)
{
return '';
}
else if(this.countryConfiguration != null && this.countryConfiguration.fields != null && this.countryConfiguration.fields.postal_code != null && !this.countryConfiguration.fields.postal_code.hidden && addressValue && !isPalceholder)
{
addressText += addressValue;
}
return addressText;
}
checkAddressValue(addressValue, addressText)
{
let text ='';
if(addressValue != undefined && addressValue!= null && addressValue.length > 0)
{
if(addressText.length !== 0)
{
text += ", ";
}
text += addressValue;
}
return text;
}
validate()
{
this.country = $("#formAddressEdition input[name='country']").val().toUpperCase();
this.state = $("#formAddressEdition input[name='state']").val();
this.district = $("#formAddressEdition input[name='district']").val();
this.city = $("#formAddressEdition input[name='city']").val();
this.zipCode = $("#pc").val();
this.addressLine1 = $("#formAddressEdition input[name='addressLine1']").val();
const phoneElem = document.getElementById("telefono"+this.divId);
if (phoneElem)
{
this.phone = phoneElem.value.trim().replaceAll(' ', '');;
}
else
{
this.phone = $("#formAddressEdition #telefono").val().trim().replaceAll(' ', '');
}
this.addressLine2 = $("#formAddressEdition input[name='addressLine2']").val();
if(!this.checkValidity())
{
return false;
}
let searchJSON = JSON.stringify(this.getSearchTerm());
this.connectToGeolocationDatabase(searchJSON, geolocationUrl);
}
saveSuggestedAddress()
{
let address = suggestedAddresses[selectedAddressPosition];
this.zipCode = address.postal_code;
if(address.country_code == 'ES')
{
this.city = address.place_name;
this.state = address.admin_name_2;
this.stateCode = address.admin_code_2;
}
else if(address.country_code == 'MX')
{
this.city = address.admin_name_2;
this.state = address.admin_name_1;
this.stateCode = address.admin_code_1;
this.district = address.place_name;
}
else
{
this.city = address.place_name;
this.state = address.admin_name_1 ?? this.state;
this.stateCode = address.admin_code_1 ?? this.stateCode;
this.district = address.admin_name_2;
}
$("textarea[id=" + this.divId + "_input]").val(this.buildAddressText(false));
if(this.callbackFunction != null)
{
this.callbackFunction(this);
}
this.phoneCountryCode = phonePrefix;
if(this.phoneCountryCode != null && this.phone != null && this.phone.startsWith(this.phoneCountryCode))
{
this.phone = this.phone.replace(this.phoneCountryCode, '');
}
hideAddressValidationModal();
}
getSearchTerm()
{
let searchTerm = '';
searchTerm = this.addToSearchTerm(this.state, searchTerm);
searchTerm = this.addToSearchTerm(this.city, searchTerm);
searchTerm = this.addToSearchTerm(this.zipCode, searchTerm);
searchTerm = this.addToSearchTerm(this.district, searchTerm);
if(!this.addressEditorConfig.address.isMandatory && (this.state == null || this.city == null || this.country == null))
{
setToNoValidate = true;
}
return {
searchTerm: searchTerm,
countryCode: this.country
};
}
addToSearchTerm(addressData, searchTerm)
{
let addressFormatted = null;
if(addressData != null && addressData.length > 0)
{
addressFormatted = addressData.replaceAll(" ", " & ");
if(searchTerm.length == 0)
{
searchTerm += addressFormatted;
}
else
{
searchTerm += " & " + addressFormatted;
}
}
return searchTerm;
}
checkValidity()
{
let isAddressSetToMandatoryInTheEventConfig = false;
if(typeof isAddressMandatoryToBuyThisEvent != 'undefined') isAddressSetToMandatoryInTheEventConfig = isAddressMandatoryToBuyThisEvent;
if(!isAddressSetToMandatoryInTheEventConfig && !currentAddressEditor.countryConfiguration.fields.validate_address)
{
return true;
}
if(this.addressEditorConfig.address.isMandatory)
{
if(this.country === null || this.country.length === 0 ||
(!this.countryConfiguration.fields.province.hidden && (this.state == null || this.state.trim().length == 0)) ||
(!this.countryConfiguration.fields.postal_code.optional && !this.countryConfiguration.fields.postal_code.hidden && (this.zipCode == null || this.zipCode.trim().length == 0)) ||
(!this.countryConfiguration.fields.locality.hidden && (this.city == null || this.city.trim().length == 0)) ||
(!this.countryConfiguration.fields.address_1.hidden && (this.addressLine1 == null || this.addressLine1.trim().length == 0)))
{
return false;
}
}
if(this.addressEditorConfig.address.isMandatory && this.addressEditorConfig.phone.isMandatory)
{
if(this.phone === null || this.phone === undefined || this.phone.trim().length === 0)
{
return false;
}
}
const phonePattern = /^(\+\d{1,3})?(\(\d{0,15}\))?([0-9\-\ ]{0,25})$/;
if (this.phone != null && this.phone != "" && !phonePattern.test(this.phone))
{
return false;
}
return true;
}
connectToGeolocationDatabase(addressString, url)
{
geolocationUrl = url;
if(geolocationUrl == null)
{
AddressEditorAjax.getGeolocationEndpoint((url) => this.connectToGeolocationDatabase(addressString, url));
return;
}
url += 'validate';
let req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/json; charset=utf-8');
req.addEventListener('error', function ()
{
currentAddressEditor.saveAddressWhenSetToNoValidate();
});
req.onload = function ()
{
if((!req.responseText.startsWith('[{') && req.responseText != '[]') || setToNoValidate || currentAddressEditor.country == 'MX')
{
currentAddressEditor.saveAddressWhenSetToNoValidate();
return;
}
let response = JSON.parse(req.responseText);
if (req.readyState == 4 && req.status == "200")
{
if(response.length == 0)
{
confirmarMensaje("Warning", polyglot.t("addressEditor.Address_not_found"), polyglot.t("addressEditor.Save"), polyglot.t("addressEditor.Change"), () => saveWithoutValidation(), null);
return false;
}
hideAddressModal();
if(!currentAddressEditor.countryConfiguration.fields.validate_address)
{
let cityInput = document.getElementById('city');
let suggestedCities = currentAddressEditor.checkResponseOptions(response);
let stateInputValue = document.querySelector('#stateInput input').value;
if(stateInputValue == null || stateInputValue.length == 0 || stateInputValue == "--")
{
currentAddressEditor.saveAddressWhenSetToNoValidate();
return;
}
if(cityInput.value == null || cityInput.value.length == 0 || suggestedCities.filter(sc => sc.city == cityInput.value).length > 0)
{
currentAddressEditor.saveAddressWhenSetToNoValidate();
return;
}
let suggestedCitiesDiv = document.getElementById('citiesSuggested');
suggestedCitiesDiv.innerHTML = currentAddressEditor.paintCitiesSuggested(suggestedCities);
$("#city_suggestion_popup").addClass("show");
}
else
{
$("#popup_address_validation").addClass("show");
currentAddressEditor.showValidationResults(response);
}
}
else
{
alertaKO(response.data);
}
}
req.send(JSON.stringify({country: this.country, state: this.state, city: this.city, zipCode: this.zipCode, district: this.district}));
}
checkResponseOptions(response)
{
let suggestedCities = [];
let responseAddress = null;
for(let i = 0; i < response.length; i++)
{
responseAddress = response[i];
if(suggestedCities.filter(sc => sc.city == responseAddress.place_name).length == 0)
{
suggestedCities.push({ "city": responseAddress.place_name, "state": responseAddress.admin_name_1});
}
}
return suggestedCities;
}
selectSuggestedCity(position)
{
selectedCityPosition = position;
}
saveSuggestedCity()
{
if(selectedCityPosition != null)
{
currentAddressEditor.city = suggestedCities[selectedCityPosition].city;
currentAddressEditor.saveAddressWhenSetToNoValidate();
}
}
paintCitiesSuggested(citiesSuggested)
{
let html = "";
let cityOption = null;
suggestedCities = citiesSuggested;
for(let i = 0; i < citiesSuggested.length; i++)
{
cityOption = citiesSuggested[i];
if(i == 0) html += '' + polyglot.t("addressEditor.Did_you_mean_any_of_these_cities") + '
';
html += '';
}
return html;
}
saveAddressWhenSetToNoValidate()
{
$("textarea[id=" + currentAddressEditor.divId + "_input]").val(currentAddressEditor.buildAddressText(false));
this.phoneCountryCode = phonePrefix;
if(this.phoneCountryCode != null && this.phone != null && this.phone.startsWith(this.phoneCountryCode))
{
this.phone = this.phone.replace(this.phoneCountryCode, '');
}
if(stateCodeSelectedNotSaved != null)
{
currentAddressEditor.stateCode = stateCodeSelectedNotSaved;
}
if(this.callbackFunction != null)
{
this.callbackFunction(this);
}
hideAddressModal();
hideCitySuggestionModal();
cambiarVentanaEsperaOK();
}
showValidationResults(addresses)
{
let i = 0;
let html = '';
suggestedAddresses = [];
for(let address of addresses)
{
suggestedAddresses.push(address);
html += '' +
'
';
if(i == 0)
{
html += '';
}
else
{
html += '';
}
html += '';
html += '';
}
else if(address.country_code == 'MX')
{
html += this.addressLine1 + ', ' + address.admin_name_2 + ', ' + address.admin_name_1 + ', ' + address.place_name + ', ' + address.postal_code + ', ' + address.country_code + '';
}
else
{
html += this.addressLine1 + ', ' + address.place_name + ', ' + (address.admin_name_1 ?? this.state) + ', ';
if(this.countryConfiguration != null && this.countryConfiguration.fields != null && this.countryConfiguration.fields.postal_code != null && (this.countryConfiguration.fields.postal_code.hidden == null || !this.countryConfiguration.fields.postal_code.hidden))
{
html += address.postal_code + ', ';
}
html += address.country_code + '';
}
html += '
';
}
this.selectSuggestedAddress(0);
document.getElementById('addressesSuggested').innerHTML = html;
}
selectSuggestedAddress(position)
{
selectedAddressPosition = position;
}
loadStates(countrySelected, statesContainer, url)
{
geolocationUrl = url;
if(geolocationUrl == null)
{
AddressEditorAjax.getGeolocationEndpoint((url) => this.loadStates(countrySelected, statesContainer, url));
return;
}
setToNoValidate = false;
let stateInputHtml = '';
document.getElementById('stateInput').innerHTML = stateInputHtml;
url += 'getStates';
let reqBody = {
countryCode: countrySelected
}
let req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/json; charset=utf-8');
req.addEventListener('error', function ()
{
currentAddressEditor.changeStateSelectToInputAndSetToNoValidate();
if(currentAddressEditor.addressEditorConfig != null && currentAddressEditor.addressEditorConfig.address.isMandatory && !currentAddressEditor.countryConfiguration.fields.province.optional)
{
document.querySelector('#stateInput input').required = true;
}
});
req.onload = function ()
{
if(!req.responseText.startsWith('[{'))
{
currentAddressEditor.changeStateSelectToInputAndSetToNoValidate();
return;
}
let response = JSON.parse(req.responseText);
if (req.readyState == 4 && req.status == "200")
{
currentAddressEditor.setZipcodePattern(response, countrySelected);
currentAddressEditor.loadStatesOnStateSelect(statesContainer, countrySelected, response);
let stateLabel = document.querySelector('label#stateInput')
if(stateLabel != null)
{
stateLabel.parentElement.classList.remove('error');
}
if(currentAddressEditor.addressEditorConfig != null && currentAddressEditor.addressEditorConfig.address.isMandatory && !currentAddressEditor.countryConfiguration.fields.province.optional)
{
document.querySelector('#stateInput input').required = true;
}
currentAddressEditor.checkCountryHasValidation(response);
if(currentAddressEditor.stateCode != null)
{
$('#select-state-' + currentAddressEditor.stateCode).trigger('click');
}
}
else
{
alertaKO(response.data);
}
}
req.send(JSON.stringify(reqBody));
}
changeStateSelectToInputAndSetToNoValidate()
{
let stateInputHTML = ''
+ ''
+ polyglot.t('error.state')
+ '';
document.getElementById('stateInput').innerHTML = stateInputHTML;
$(stateSelect).val(currentAddressEditor.state);
stateCodeSelectedNotSaved = null;
setToNoValidate = true;
$('#pc').attr('pattern', 'alpha_numeric');
}
setZipcodePattern(response, countrySelected)
{
if(response.length == 0)
{
return;
}
let zipcodePattern = response[0].postal_code;
if(currentAddressEditor.countryConfiguration.fields.postal_code.hidden)
{
zipcodePattern = 'alpha_numeric';
}
$('#pc').attr('pattern', zipcodePattern.replace('d','[0-9]'));
}
loadStatesOnStateSelect(selector, countrySelected, statesList)
{
let code = '';
statesList.forEach(c => {
code += ''+c.admin_name_1+'
'
})
$("#state").html(code);
$('.ui.dropdown').dropdown({
on: 'click',
onChange: function(value, text, $selectedItem) {
}
});
//Default selected
if(code.length > 0 && (this.state == null || this.state.length == 0))
{
document.querySelector('#formAddressEdition #stateDiv .text').innerHTML = polyglot.t("global.Please_select_a_state_province");
}
else if(code.length == 0)
{
document.querySelector('#formAddressEdition #stateDiv .text').innerHTML = polyglot.t("--");
}
if(currentAddressEditor.addressEditorConfig != null && currentAddressEditor.addressEditorConfig.address.isMandatory)
{
$(selector).attr('required', true);
}
}
checkCountryHasValidation(response)
{
setToNoValidate = !response[0].has_validation;
}
getAddress(addressToFill)
{
addressToFill.pais = this.country;
addressToFill.direccion = this.addressLine1;
addressToFill.address2 = this.addressLine2;
addressToFill.poblacion = this.city;
addressToFill.cp = this.zipCode;
addressToFill.provincia = this.state;
addressToFill.distrito = this.district;
addressToFill.stateCode = this.stateCode;
addressToFill.telefono = this.phone;
addressToFill.phoneCountryCode = this.phoneCountryCode;
}
getAddressFromAddressClass(addressToFill)
{
addressToFill.country = this.country;
addressToFill.addressLine = this.addressLine1;
addressToFill.addressLine2 = this.addressLine2;
addressToFill.city = this.city;
addressToFill.zipCode = this.zipCode;
addressToFill.countryState = this.state;
addressToFill.stateCode = this.stateCode;
addressToFill.district = this.district;
addressToFill.telephone = this.phone;
}
loadAddress(address)
{
this.country = address.country != null ? address.country.toUpperCase() : null;
this.state = address.countryState;
this.stateCode = address.stateCode;
this.city = address.city;
this.zipCode = address.zipCode;
this.addressLine1 = address.addressLine;
this.addressLine2 = address.addressLine2;
this.phone = address.phone;
this.district = address.district;
if(this.phone === null || this.phone === undefined)
{
this.phone = address.telephone;
}
this.phoneCountryCode = address.phoneCountryCode;
$("textarea[id=" + this.divId + "_input]").val(this.buildAddressText(false));
}
}
window.createAddress = function (divId, callbackSelected)
{
return new AddressEditorComponent(divId, callbackSelected);
}
let addressForms = [];
let suggestedAddresses = [];
let suggestedCities = [];
let lastInputCode = null;
let selectedAddressPosition = null;
let selectedCityPosition = 0;
window.setToNoValidate = false;
window.stateCodeSelectedNotSaved = null
let geolocationUrl = null;
window.openSelectedAddressModal = function(divId)
{
if($("#popup_address").hasClass('show'))
{
return;
}
setToNoValidate = false;
for (let i in addressForms)
{
if (addressForms[i].divId == divId)
{
currentAddressEditor = addressForms[i];
addressForms[i].openSelectedAddressModal();
break;
}
}
}