$(document).ready(function () { $("#seniors_addresssearch").val(""); }); (function(webapi, $){ function safeAjax(ajaxOptions) { var deferredAjax = $.Deferred(); shell.getTokenDeferred().done(function (token) { // add headers for AJAX if (!ajaxOptions.headers) { $.extend(ajaxOptions, { headers: { "__RequestVerificationToken": token } }); } else { ajaxOptions.headers["__RequestVerificationToken"] = token; } $.ajax(ajaxOptions) .done(function(data, textStatus, jqXHR) { validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve); }).fail(deferredAjax.reject); //AJAX }).fail(function () { deferredAjax.rejectWith(this, arguments); // on token failure pass the token AJAX and args }); return deferredAjax.promise(); } webapi.safeAjax = safeAjax; })(window.webapi = window.webapi || {}, jQuery) function initializeAddressAutocomplete() { var inputField = document.getElementById("seniors_addresssearch"); if (!inputField) { console.error("seniors_addresssearch field not found."); return; } inputField.addEventListener("input", debounce(async function (event) { const inputValue = event.target.value; if (inputValue.trim().length < 3) return; await getAddressSuggestions(inputValue); }, 300)); } async function getAddressSuggestions(inputValue) { try { const result = await webapi.safeAjax({ type: "POST", url: "/_api/seniors_addressvalidationrequests", contentType: "application/json", data: JSON.stringify({ "seniors_addressinput": inputValue }), success: function (res, status, xhr) { const recordId = xhr.getResponseHeader("entityid"); console.log("Created entity ID:", recordId); setTimeout(async () => { const data = await webapi.safeAjax({ type: "GET", url: `/_api/seniors_addressvalidationrequests(${recordId})?$select=seniors_addressresponse,seniors_processed`, contentType: "application/json" }); if (data.seniors_processed && data.seniors_addressresponse) { const parsed = JSON.parse(data.seniors_addressresponse); displaySuggestions(parsed.data || [], document.getElementById("seniors_addresssearch")); } }, 1000); } }); } catch (err) { console.error("Failed to create/retrieve address validation record:", err); } } function displaySuggestions(addresses, inputElement) { const suggestionBoxId = "suggestions-box"; let suggestionBox = document.getElementById(suggestionBoxId); if (!suggestionBox) { suggestionBox = document.createElement("div"); suggestionBox.id = suggestionBoxId; suggestionBox.style.border = "1px solid #ccc"; suggestionBox.style.background = "#fff"; suggestionBox.style.position = "absolute"; suggestionBox.style.zIndex = "1000"; suggestionBox.style.maxHeight = "200px"; suggestionBox.style.overflowY = "auto"; document.body.appendChild(suggestionBox); } suggestionBox.innerHTML = ""; console.log("addresses.length : ", addresses.length ); if (addresses.length == 0) { suggestionBox.textContent = "No Address Found"; suggestionBox.style.display = "block"; } else { addresses.forEach(addr => { const div = document.createElement("div"); div.textContent = addr.suggestion || addr.fullAddress || ""; div.style.padding = "8px"; div.style.cursor = "pointer"; div.addEventListener("click", () => { inputElement.value = addr.suggestion || addr.fullAddress || ""; suggestionBox.style.display = "none"; sendAddressToPortalForm(addr.lookupData || {}); }); suggestionBox.appendChild(div); }); } const rect = inputElement.getBoundingClientRect(); suggestionBox.style.top = `${rect.bottom + window.scrollY}px`; suggestionBox.style.left = `${rect.left + window.scrollX}px`; suggestionBox.style.width = `${rect.width}px`; suggestionBox.style.display = "block"; } function debounce(func, delay) { let timer; return function (...args) { clearTimeout(timer); timer = setTimeout(() => func.apply(this, args), delay); }; } function getAntiForgeryToken() { return $('input[name="__RequestVerificationToken"]').val(); } function waitForWebApi(callback) { if (window.webapi && typeof window.webapi.safeAjax === "function") { callback(); } else { setTimeout(() => waitForWebApi(callback), 100); } } document.addEventListener("DOMContentLoaded", () => { waitForWebApi(initializeAddressAutocomplete); });