1// //search-box-o 2// 3// XA.component.headerSearchBox = (function($) { 4// var api = {}; 5// 6// api.init = function() { 7// var searchBoxs = $('.search-box-o:not(.initialized)'); 8// 9// searchBoxs.each(function() { 10// var searchBox = $(this); 11// var input = $(searchBox.find('input')); 12// var button = $(searchBox.find('button')); 13// 14// button.on('click', function() { 15// if (input.val() !== '') { 16// var query = "q=" + input.val(); 17// window.location = '/search?' + query; 18// } 19// }); 20// 21// input.on('keypress', function(e) { 22// if (e.keyCode === 13) { 23// if (input.val() !== '') { 24// var query = "q=" + input.val(); 25// window.location = '/search?' + query; 26// } 27// } 28// }) 29// searchBox.addClass('initialized'); 30// }) 31// 32// 33// }; 34// 35// return api; 36// })(jQuery); 37// 38// XA.register("headerSearchBox", XA.component.headerSearchBox); 39 40 41 42//search-box-o 43 44XA.component.headerSearchBox = (function($) { 45 var api = {}; 46 const defaultHeaderConfig = { 47 widgetId: 'library', 48 sourceId: '1193142', 49 suggestions: 'title_auto_complete', 50 facets: ["category", "subcategory"], 51 queryParamName: 'q', 52 sortorder: "title_descending", 53 filterConfigs: { 54 }, 55 //apiKey: "01-fe26bba7-ada5189dca3155fbacc4df5f5d432e8af793b577",//staging 56 apiKey: "01-867118b8-59015babebaf6ab3c88617d286a229e1f2109841", 57 //apiUrl: "https://discover-apse2.sitecorecloud.io/discover/v2/6226372680",//staging 58 apiUrl: "https://discover-apse2.sitecorecloud.io/discover/v2/62263726", 59 defaultPageSize: 6, 60 selectors: { 61 suggestionsWidgets: ["surgeon_full_name_suggestions"] 62 }, 63 permanentFilters: [] 64 }; 65 66 api.init = function() { 67 var searchBoxs = $('.search-box-o:not(.initialized)'); 68 69 searchBoxs.each(function() { 70 var searchBox = $(this); 71 var input = $(searchBox.find('input')); 72 var button = $(searchBox.find('button'));
73 var suggestionsContainer = $('<div class="suggestions-container"></div>'); 74 75 // Append suggestions container after the input 76 input.after(suggestionsContainer); 77 78 // Style the suggestions container 79 suggestionsContainer.css({ 80 'position': 'fixed', 81 'width': '90%', 82 'background': '#fff', 83 'z-index': '1000', 84 'box-shadow': '0 2px 5px rgba(0,0,0,0.2)', 85 'display': 'none' 86 }); 87 88 // Configure sitecoreSearch component with required settings 89 // We need to set the suggestions field explicitly 90 if (XA.component.sitecoreSearch) { 91 var currentConfig = XA.component.sitecoreSearch.config || {}; 92 93 // Make sure suggestions is set - this is what was causing our issue 94 if (!currentConfig.suggestions) { 95 // Configure with the default suggestion field 96 XA.component.sitecoreSearch.configure(defaultHeaderConfig); 97 } 98 } 99 100 button.on('click', function() { 101 if (input.val() !== '') { 102 var query = "q=" + input.val(); 103 window.location = '/search?' + query; 104 } 105 }); 106 107 input.on('keypress', function(e) { 108 if (e.keyCode === 13) { 109 if (input.val() !== '') { 110 var query = "q=" + input.val(); 111 window.location = '/search?' + query; 112 } 113 } 114 }); 115 116 // Add input handler for suggestions with debounce 117 var debounceDelay = 300; // 300ms delay 118 var debouncedSearch = _.debounce(function(query) { 119 if (query.length >= 2) { 120 XA.component.sitecoreSearch.getSuggestions(query) 121 .then(function(response) { 122 suggestionsContainer.empty(); 123 124 if (!response.widgets || response.widgets.length === 0) { 125 suggestionsContainer.hide(); 126 return; 127 } 128 129 // Extract suggestions from the response 130 let suggestions = []; 131 try { 132 const widgetData = response.widgets[0]; 133 if (widgetData && widgetData.suggestion && widgetData.suggestion.title_auto_complete) { 134 suggestions = widgetData.suggestion.title_auto_complete; 135 } 136 } catch (e) { 137 console.error('Error parsing suggestions:', e); 138 } 139 140 if (suggestions.length === 0) { 141 suggestionsContainer.hide(); 142 return; 143 } 144 145 // Create suggestion elements 146 suggestions.forEach(function(suggestion) { 147 let query = input.val(); 148 let text = suggestion.text; 149 150 const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); 151 const regex = new RegExp(`(${escapedQuery})`, 'gi'); 152 let displayText = text.replace(regex, '<mark style="background: #004987; color: white; padding: 5px;" >$1</mark>'); 153 154 const suggestionItem = $('<div class="suggestion-item"></div>'); 155 suggestionItem.html(displayText); 156 suggestionItem.data('query', suggestion.text); 157 suggestionItem.css({ 158 'padding': '8px 12px', 159 'cursor': 'pointer' 160 }); 161 162 // Add hover effect 163 suggestionItem.hover( 164 function() { $(this).css('background-color', '#f0f0f0'); }, 165 function() { $(this).css('background-color', 'transparent'); } 166 ); 167 168 // Add click handler 169 suggestionItem.on('click', function() { 170 input.val($(this).data('query')); 171 suggestionsContainer.hide(); 172 var query = "q=" + suggestion.text; 173 window.location = '/search?' + query; 174 }); 175 176 suggestionsContainer.append(suggestionItem); 177 }); 178 179 suggestionsContainer.show(); 180 }) 181 .catch(function(error) { 182 console.error('Error fetching suggestions:', error); 183 suggestionsContainer.hide(); 184 }); 185 } else { 186 suggestionsContainer.empty().hide(); 187 } 188 }, debounceDelay); 189 190 input.on('input', function() { 191 var query = $(this).val(); 192 debouncedSearch(query); 193 }); 194 195 // Hide suggestions when clicking outside 196 $(document).on('click', function(e) {
197 if (!$(e.target).closest(searchBox).length) { 198 suggestionsContainer.hide(); 199 } 200 }); 201 202 searchBox.addClass('initialized'); 203 }) 204 }; 205 206 return api; 207})(jQuery); 208 209XA.register("headerSearchBox", XA.component.headerSearchBox);
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.