1(function ($) { 2 3 $(document).ready(function () { 4 $('input[data-search="search"]').attr('maxlength',120) 5 var language = $('[data-sf-role="language"]').first().val(); 6 if (language == 'en') { 7 $('input[data-search="search"]').on('input', function () { 8 var char = this.selectionStart, 9 regex = /[^a-zA-Z ]/gi, 10 value = $(this).val(); 11 if (regex.test(value)) { 12 $(this).val(value.replace(regex, '')); 13 char--; 14 } 15 this.setSelectionRange(char, char); 16 17 }); 18 } else { 19 $('.search-box').on('input', function () { 20 var char = this.selectionStart, 21 regex = /[^\u0600-\u06FF ]/gi, 22 value = $(this).val(); 23 if (regex.test(value)) { 24 $(this).val(value.replace(regex, '')); 25 char--; 26 } 27 this.setSelectionRange(char, char); 28 29 }); 30 } 31 var searchBoxIdFields = $('[data-sf-role="searchTextBoxId"]'); 32 var search = $('[data-search="search"]'); 33 for (var i = 0; i < searchBoxIdFields.length; i++) { 34 var searchBoxIdField = $(searchBoxIdFields[i]); 35 var controlServerData = { 36 37 resultsUrl: searchBoxIdField.siblings('[data-sf-role="resultsUrl"]').first().val(), 38 indexCatalogue: searchBoxIdField.siblings('[data-sf-role="indexCatalogue"]').first().val(), 39 wordsMode: searchBoxIdField.siblings('[data-sf-role="wordsMode"]').first().val(), 40 disableSuggestions: $.parseJSON(searchBoxIdField.siblings('[data-sf-role="disableSuggestions"]').first().val()), 41 minSuggestionLength: searchBoxIdField.siblings('[data-sf-role="minSuggestionLength"]').first().val(), 42 suggestionFields: searchBoxIdField.siblings('[data-sf-role="suggestionFields"]').first().val(), 43 language: searchBoxIdField.siblings('[data-sf-role="language"]').first().val(), 44 suggestionsRoute: searchBoxIdField.siblings('[data-sf-role="suggestionsRoute"]').first().val(), 45 searchTextBoxSelector: searchBoxIdField.val(), 46 searchButtonSelector: searchBoxIdField.siblings('[data-sf-role="searchButtonId"]').first().val() 47 48 }; 49 featherSearchBoxWidget(controlServerData); 50 } 51 52 function featherSearchBoxWidget(serverData) { 53 54 var searchTextBox = $(serverData.searchTextBoxSelector), 55 searchButton = $(serverData.searchButtonSelector); 56 57 if (!serverData.disableSuggestions) { 58 59 searchTextBox.keyup(keyupHandler); 60 61 try { 62 searchTextBox.autocomplete({ 63 source: [], 64 messages: 65 { 66 noResults: '', 67 results: function () { } 68 }, 69 select: function (event, ui) { 70 searchTextBox.val(ui.item.value); 71 navigateToResults(event); 72 }, 73 }).autocomplete("widget").addClass("sf-autocomplete"); 74 } catch (e) { 75 // Fixes jQuery bug, causing IE7 to throw error "script3 member not found". 76 // The try/catch can be removed when the bug is fixed. 77 } 78 } 79 80 function suggestionsSuccessHandler(result, args) { 81 console.log(result); 82 var dataSource = result.Suggestions; 83 if (dataSource.length > 0) { 84 generateMarkup(dataSource); 85 } 86 87 } 88 89 function generateMarkup(dataSource) { 90 var markup = ""; 91 92 var searchResult = $("#searchResult"); 93 for (var i = 0; i < dataSource.length; i++) { 94 var data = dataSource[i]; 95 markup += "<li><a href='javascript:void(0)'>" + data + "</a></li>"; 96 97 } 98 searchResult.html(markup); 99 clickResult(); 100 } 101 $("#search-close").click(function () { 102 $("#searchResult").html(""); 103 $(".form-group__input").val(""); 104 var listUrlPath = $("#listUrl").val(); 105 106 //$.ajax({
107 // type: "Get", 108 // url: listUrlPath, 109 // datatype: "html", 110 // success: successHandlerPoplularSearch, 111 // error: function (xhr, ajaxOptions, thrownError) { 112 113 // console.log(thrownError); 114 115 // } 116 //}); 117 }); 118 function clickResult() { 119 $("#searchResult li a").click(function () { 120 121 var query = $(this).text(); 122 if (query && query.trim() && serverData.indexCatalogue) { 123 // sendSentence(); 124 setCookieValue(query); 125 window.location = getLocation(query); 126 } 127 }); 128 } 129 $("#searchButton").click(function () { 130 131 var query = searchTextBox.val().trim(); 132 133 if (query && query.trim() && serverData.indexCatalogue) { 134 // sendSentence(); 135 setCookieValue(query); 136 window.location = getLocation(query); 137 } 138 }); 139 140 141 function keyupHandler(e) { 142 143 if (e.keyCode != 38 && // up arrow 144 e.keyCode != 40 && // down arrow 145 e.keyCode != 27) { // esc 146 // When the auto complete menu is shown, only this event is detected 147 if (e.keyCode == 13) { 148 // when enter is pressed 149 navigateToResults(e); 150 } 151 152 var request = {}; 153 var searchText = searchTextBox.val().trim(); 154 if (searchText.length >= serverData.minSuggestionLength) { 155 request.IndexName = serverData.indexCatalogue; 156 request.SuggestionFields = serverData.suggestionFields; 157 request.Text = searchText; 158 request.Language = serverData.language; 159 160 $.ajax({
161 type: "GET", 162 url: serverData.suggestionsRoute, 163 dataType: 'json', 164 data: request, 165 success: suggestionsSuccessHandler 166 }); 167 } 168 } 169 } 170 171 /* Helper methods */ 172 function navigateToResults(e) { 173 if (!e) 174 e = window.event; 175 176 if (e.stopPropagation) { 177 e.stopPropagation(); 178 } 179 else { 180 e.cancelBubble = true; 181 } 182 if (e.preventDefault) { 183 e.preventDefault(); 184 } 185 else { 186 e.returnValue = false; 187 } 188 189 var query = searchTextBox.val(); 190 191 if (query && query.trim() && serverData.indexCatalogue) { 192 sendSentence(); 193 setCookieValue(query); 194 window.location = getLocation(); 195 } 196 } 197 198 function getLocation(query) { 199 if (query == undefined) { 200 query = searchTextBox.val().trim(); 201 } 202 203 204 var separator = (serverData.resultsUrl.indexOf("?") == -1) ? "?" : "&"; 205 206 var catalogueParam = separator + "indexCatalogue=" + encodeURIComponent(serverData.indexCatalogue), 207 searchQueryParam = "&searchQuery=" + encodeURIComponent(query), 208 wordsModeParam = "&wordsMode=" + serverData.wordsMode; 209 210 var url = serverData.resultsUrl + catalogueParam + searchQueryParam + wordsModeParam; 211 212 return url; 213 } 214 215 function sendSentence() { 216 if (window.DataIntelligenceSubmitScript) { 217 DataIntelligenceSubmitScript._client.sentenceClient.writeSentence({ 218 predicate: "Search for", 219 object: searchTextBox.val(), 220 objectMetadata: [{ 221 'K': 'PageUrl', 222 'V': location.href 223 }] 224 }); 225 } 226 } 227 228 function getCookieValue() { 229 var cookieValue = $.cookie('searchArrayCookie'); 230 if (cookieValue != '' && cookieValue != undefined) { 231 // Convert the cookie value back to an array 232 var searchArray = JSON.parse(cookieValue) || []; 233 return searchArray; 234 } 235 return ""; 236 } 237 function setCookieValue(searchValue) { 238 var searchArray = []; 239 if ($.cookie('searchArrayCookie')) { 240 // The cookie exists 241 // Retrieve the cookie value 242 var cookieValue = $.cookie('searchArrayCookie'); 243 244 // Convert the cookie value back to an array 245 var searchArray = JSON.parse(cookieValue) || []; 246 } 247 248 249 // Add a new item to the array 250 if (searchArray.indexOf(searchValue) < 0) { 251 searchArray.push(searchValue); 252 253 // Limit the array to a maximum of 3 items 254 if (searchArray.length > 3) { 255 256 searchArray.shift(); 257 } 258 259 // Convert the array to a string 260 var arrayString = JSON.stringify(searchArray); 261 262 // Set the cookie with the updated array string 263 $.cookie('searchArrayCookie', arrayString); 264 } 265 266 } 267 function setRecentSearch() { 268 var cookieValueArray = getCookieValue(); 269 270 if (cookieValueArray.length != 0 && cookieValueArray != null) { 271 $("#recentSearchSpan").show(); 272 var html = ' '; 273 for (var i = 0; i < cookieValueArray.length; i++) { 274 275 html += '<a href="javascript:void(0)">' + cookieValueArray[i] + '</a>'; 276 if (i !== cookieValueArray.length - 1) { 277 html += " , " 278 } 279 280 } 281 282 $("#recentSearch").html(html); 283 } 284 else { 285 $("#recentSearchSpan").hide(); 286 $("#recentSearch").html(''); 287 } 288 } 289 setRecentSearch(); 290 $("#recentSearch a").click(function () { 291 292 var recentSearch = $(this).text(); 293 searchTextBox.val(recentSearch); 294 295 var query = searchTextBox.val().trim(); 296 297 if (query && query.trim() && serverData.indexCatalogue) { 298 // sendSentence();
299 setCookieValue(query); 300 window.location = getLocation(query); 301 } 302 }); 303 } 304 $("#btnCloseSearch").click(function () { 305 search.val(""); 306 $("#searchResult").html(''); 307 }); 308 $("#clearSearch").click(function () { 309 search.val(""); 310 $("#searchResult").html(''); 311 }); 312 313 314 }); 315}(jQuery));
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.