"use strict"; var BYD; (function (BYD) { var Validation; (function (Validation) { function showCountryControl(countryControlId, show) { if (show) $("#" + countryControlId).show(); else $("#" + countryControlId).remove(); } Validation.showCountryControl = showCountryControl; function onLoad(config) { BYD.Validation.checkAccess(config); var override = false; BYD.Validation.setAutoComplete(config); if ($("#" + config.ui.data).val() === "" || $("#" + config.ui.data).val() === null) BYD.ValidationClient.SetDefaultValue(config); if (config.overrideAddressField !== undefined && config.overrideAddressField !== "") { override = BYD.ValidationClient.GetValue(config.overrideAddressField, "boolean"); if (override === null) override = false; if (override && config.allowOverride !== undefined && config.allowOverride !== null && config.allowOverride === false) override = false; } if (override) { BYD.ValidationClient.RevertToManualAddress(config, override, null); } else { if (config.errorHandling.showSection !== undefined && config.errorHandling.showSection !== null && config.errorHandling.showSection !== "") { BYD.ValidationClient.showHideAddressSection(config.errorHandling.showSection, false); } if (config.errorHandling.showWebResource !== undefined && config.errorHandling.showWebResource !== null && config.errorHandling.showWebResource !== "") { BYD.ValidationClient.showHideAddressResource(config.errorHandling.showWebResource, false); } } } Validation.onLoad = onLoad; function setAutoComplete(config) { var showCountry = false; if (config.showCountry) showCountry = true; BYD.Validation.showCountryControl(config.ui.country, showCountry); $("#" + config.ui.data).autocomplete({ minLength: 4, delay: 200, source: function (request, response) { BYD.Validation.hideError(config); if (config.resolvedAddressField !== undefined && config.resolvedAddressField !== "") BYD.ValidationClient.SetValue(false, config.resolvedAddressField, "boolean"); if (config.addressMatchTypeField !== undefined && config.addressMatchTypeField !== "") BYD.ValidationClient.SetValue(null, config.addressMatchTypeField, "number"); if (config.addressConfidenceField !== undefined && config.addressConfidenceField !== "") BYD.ValidationClient.SetValue(null, config.addressConfidenceField, "string"); let addressSearch = { CountryCode: getCountryCode(config.ui.country), AddressLine: request.term, ResultLimit: config.ResultLimit, clientId: config.ClientId, requestStarted: new Date().toISOString() }; fetch(config.EndpointURL.replace("{0}", "SearchAddress"), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(addressSearch) }).then(function (searchResponse) { if (searchResponse.status == 200) { searchResponse.json().then(function (output) { var array = []; array = $.map(output, function (p) { p.label = p.AddressLine + ", " + p.Locality + ", " + p.StateOrProvince + ", " + p.Postcode; p.value = p.RecordId; return p; }); response(array); }); } else if (searchResponse.status == 401 || searchResponse.status == 404) { searchResponse.text().then(function (text) { BYD.Validation.showError(config, text); }); } else { searchResponse.text().then(function (text) { BYD.Validation.showError(config, text); }); } }); }, focus: function (event, ui) { event.preventDefault(); }, select: function (event, ui) { let addressResolve = { RetrieveGeoCoding: config.RetrieveGeoCoding, clientId: config.ClientId, Country: getCountryCode(config.ui.country), requestStarted: new Date().toISOString(), RecordId: ui.item.value }; fetch(config.EndpointURL.replace("{0}", "ResolveAddress"), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(addressResolve) }).then(function (resolveResponse) { if (resolveResponse.status == 200) { resolveResponse.json().then(function (output) { populateAddress(output, config); }); } else if (resolveResponse.status == 401 || resolveResponse.status == 404) { resolveResponse.text().then(function (text) { BYD.Validation.showError(config, text); }); } else { resolveResponse.text().then(function (text) { BYD.Validation.showError(config, text); }); } }); event.preventDefault(); } }); } Validation.setAutoComplete = setAutoComplete; function showError(config, error) { var errorControl = $("#" + config.ui.error); errorControl.html(error); errorControl.show(); } Validation.showError = showError; function hideError(config) { $("#" + config.ui.error).hide(); } Validation.hideError = hideError; function getCountryCode(controlId) { var selectCountry = $("#" + controlId + " option:selected"); if (selectCountry == undefined || selectCountry.length == 0) { return 'AU'; } else { return selectCountry.val(); } } Validation.getCountryCode = getCountryCode; function checkAccess(config) { let checkAccess = { clientId: config.ClientId, requestStarted: new Date().toISOString() }; var previousResponse = window.sessionStorage.getItem("accessResponse"); if (previousResponse !== undefined && previousResponse !== null) { if (previousResponse === "200") { return; } return; } fetch(config.EndpointURL.replace("{0}", "CheckAccess"), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(checkAccess) }).then(function (resolveResponse) { window.sessionStorage.setItem("accessResponse", resolveResponse.status.toString()); if (resolveResponse.status === 200) { } else if (resolveResponse.status === 401 || resolveResponse.status === 404) { BYD.ValidationClient.RevertToManualAddress(config, true, null); } else { resolveResponse.text().then(function (text) { $("#" + config.ui.error).html(text); }); } }); } Validation.checkAccess = checkAccess; function populateAddress(output, config) { config.fieldMappings.forEach(function (value, index) { BYD.ValidationClient.SetAddressValue(output, value.crmField, value.addressField); }); if (config.resolvedAddressField !== undefined && config.resolvedAddressField !== "") { if (config.acceptableMatchTypes !== undefined && config.acceptableMatchTypes !== "") { BYD.ValidationClient.SetValue(config.acceptableMatchTypes.split(",").indexOf(output.MatchType.toString()) !== -1, config.resolvedAddressField, "boolean"); } else if (output.MatchType === 0) { BYD.ValidationClient.SetValue(true, config.resolvedAddressField, "boolean"); } } if (config.overrideAddressField !== undefined && config.overrideAddressField !== "") BYD.ValidationClient.SetValue(false, config.overrideAddressField, "boolean"); if (config.addressMatchTypeField !== undefined && config.addressMatchTypeField !== "") BYD.ValidationClient.SetValue(output.MatchType, config.addressMatchTypeField, "number"); if (config.addressConfidenceField !== undefined && config.addressConfidenceField !== "") BYD.ValidationClient.SetValue(output.ConfidenceScore, config.addressConfidenceField, "string"); BYD.ValidationClient.SetDefaultValue(config); } Validation.populateAddress = populateAddress; })(Validation = BYD.Validation || (BYD.Validation = {})); })(BYD || (BYD = {})); //# sourceURL=validationAddress.js