1(function() { 2 const electionSearchBtn = document.getElementById('avow-voter--search-btn'); 3 const resetSearchBtn = document.getElementById('avow-voter--reset-btn'); 4 const addressField = document.getElementById('address'); 5 const cityField = document.getElementById('city'); 6 const zipField = document.getElementById('zipcode'); 7 8 const cachedResults = {}; 9 10 electionSearchBtn.addEventListener('click', getAddressResults); 11 resetSearchBtn.addEventListener('click', resetAddressResults); 12 13 function getAddressResults() { 14 // Get form data 15 const address = addressField.value.trim(); 16 const city = cityField.value.trim(); 17 const zipcode = zipField.value.trim(); 18 19 // Construct the API endpoint 20 const apiEndpoint = `https://rws.capitol.texas.gov/api/MatchAddress?Address=${encodeURIComponent(address)}&City=${encodeURIComponent(city)}&Zip=${encodeURIComponent(zipcode)}&DistType=A`; 21 22 // Check if the results are already cached 23 if (cachedResults[apiEndpoint]) { 24 displayResults(cachedResults[apiEndpoint]); 25 } else { 26 // Fetch data from the API endpoint 27 fetch(apiEndpoint) 28 .then(response => { 29 if (!response.ok) { 30 throw new Error('Network response was not ok'); 31 } 32 return response.json(); 33 }) 34 .then(data => { 35 // Cache the results 36 cachedResults[apiEndpoint] = data; 37 displayResults(data); 38 }) 39 .catch(error => { 40 console.error('There has been a problem with your fetch operation:', error); 41 }); 42 } 43 } 44 45 function displayResults(data) { 46 // Ensure data is an array for iteration 47 data = Array.isArray(data) ? data : [data]; 48 49 electionSearchBtn.setAttribute('disabled', 'disabled'); 50 resetSearchBtn.removeAttribute('disabled'); 51 52 // Define the positions and their display names 53 const positions = [ 54 { key: 'Congress', displayName: 'U.S. Congressional District' }, 55 { key: 'Senate', displayName: 'Texas Senate District' }, 56 { key: 'House', displayName: 'Texas House District' }, 57 { key: 'Sboe', displayName: 'State Board of Education District' } 58 ]; 59 60 // Track matching IDs 61 const matchingIds = []; 62 63 // Add list items 64 data.forEach(item => { 65 positions.forEach(position => { 66 const member = item[position.key] && item[position.key][0].Member; 67 if (member && member.formalName) { 68 const displayNameWithDistrict = `${position.displayName} ${item[position.key][0]['District']}` 69 .replace(/\s+/g, '-') 70 .replace(/\./g, '') 71 .toLowerCase(); 72 matchingIds.push(displayNameWithDistrict); 73 } 74 }); 75 }); 76 77 console.log('Matching IDs:', matchingIds); 78 // Always include U.S. Senate 79 matchingIds.push('us-senate'); 80 81 // Hide all elements with the data-race attribute that do not have a matching ID 82 const allElements = document.querySelectorAll('[data-race]'); 83 allElements.forEach(element => { 84 if (element.getAttribute('data-race') === 'governor') { 85 element.style.display = 'block'; 86 } else if (!matchingIds.includes(element.getAttribute('data-race'))) { 87 element.style.display = 'none'; 88 } else { 89 element.style.display = 'block'; 90 } 91 }); 92 } 93 94 function resetAddressResults() { 95 electionSearchBtn.removeAttribute('disabled'); 96 resetSearchBtn.setAttribute('disabled', 'disabled'); 97 98 addressField.value = ''; 99 cityField.value = ''; 100 zipField.value = ''; 101 102 const allElements = document.querySelectorAll('[data-race]');
103 allElements.forEach(element => { 104 if (element.getAttribute('data-race')) { 105 element.style.display = 'block'; 106 } 107 }); 108 } 109})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.