1const setDBAutocomplete = (inp) => { 2 if( !ds_locations.autocomplete_list || !inp ) 3 return; 4 5 const arr = ds_locations.autocomplete_list; 6 const renderAutocompleteListItem = (title,address) => { 7 return `<h3>${title}</h3><p>${address}</p>` 8 }; 9 const highlightPart = (str,val) => { 10 if(!str.toLowerCase().includes(val.toLowerCase())){ 11 return str; 12 } 13 const matchStart = str.toLowerCase().indexOf(val.toLowerCase()); 14 const matchEnd = matchStart + val.length; 15 return `${str.substring(0, matchStart)}<strong>${str.substring(matchStart, matchEnd)}</strong>${str.substring(matchEnd)}`; 16 } 17 18 inp.addEventListener('input', function(e) { 19 let a, b, i, val = this.value; 20 this.removeAttribute('data-search'); 21 closeAllLists(); 22 if (!val) { return false; } 23 currentFocus = -1; 24 a = document.createElement('ul'); 25 a.setAttribute('id', this.id + '-autocomplete-list'); 26 a.setAttribute('class', 'autocomplete-items'); 27 let container = document.getElementsByClassName('location-search__main'); 28 container[0].parentNode.appendChild(a); 29 30 for (i = 0; i < arr.length; i++) { 31 // Check if the item contains the search term anywhere in the string 32 let {title,address} = arr[i]; 33 if (title.toLowerCase().includes(val.toLowerCase()) || address.toLowerCase().includes(val.toLowerCase())) { 34 b = document.createElement('li'); 35 b.setAttribute('class', 'autocomplete-items__item'); 36 b.innerHTML = renderAutocompleteListItem(highlightPart(title,val),highlightPart(address,val)); 37 b.addEventListener('click', function(e) { 38 39 if(e.target.tagName ==='H3' || e.target.parentElement.tagName ==='H3'){ 40 inp.value=title; 41 inp.setAttribute('data-search','title') 42 } 43 if(e.target.tagName ==='P' || e.target.parentElement.tagName ==='P'){ 44 inp.value=address; 45 inp.setAttribute('data-search','address') 46 } 47 if(e.target !== e.currentTarget){ 48 closeAllLists(); 49 runSearch(); 50 } 51 }); 52 a.appendChild(b); 53 } 54 } 55 }); 56 57 inp.addEventListener('keydown', function(e) { 58 let x = document.getElementById(this.id + 'autocomplete-list'); 59 if (x) x = x.getElementsByTagName('div'); 60 if (e.keyCode == 40) { 61 currentFocus++; 62 addActive(x); 63 } else if (e.keyCode == 38) { 64 currentFocus--; 65 addActive(x); 66 } else if (e.keyCode == 13) { 67 e.preventDefault(); 68 if (currentFocus > -1) { 69 if (x) x[currentFocus].click(); 70 } 71 closeAllLists(); 72 } 73 }); 74 75 function addActive(x) { 76 if (!x) return false; 77 removeActive(x); 78 if (currentFocus >= x.length) currentFocus = 0; 79 if (currentFocus < 0) currentFocus = (x.length - 1); 80 x[currentFocus].classList.add('autocomplete-active'); 81 } 82 83 function removeActive(x) { 84 for (let i = 0; i < x.length; i++) { 85 x[i].classList.remove('autocomplete-active'); 86 } 87 } 88 89 function closeAllLists(elmnt) { 90 let x = document.getElementsByClassName('autocomplete-items'); 91 for (let i = 0; i < x.length; i++) { 92 if (elmnt != x[i] && elmnt != inp) { 93 x[i].parentNode.removeChild(x[i]); 94 } 95 } 96 } 97 98 document.addEventListener('click', function (e) { 99 closeAllLists(e.target); 100 }); 101} 102 103// export { setDBAutocomplete };
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.