1( function( $ ) { 2 "use strict"; 3 4 // HashMap for searched keys and their post titles. 5 var searchCache = {}; 6 7 // track the currect AJAX request. 8 var sameAjaxRequest; 9 10 // get cached post titles from the params object of wp_localize_script. 11 var cachedPostTitles = ( false !== params.cached_post_titles && params.cached_post_titles.length ) ? params.cached_post_titles : false; 12 13 $( "#nds-advanced-search-form #nds-search-box" ).autocomplete({ 14 delay: 300, 15 source: function( request, response ) { 16 17 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp. 18 var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" ); 19 20 var searchTitlesForSuggestions = function( searchTitles ) { 21 22 // function used to search the key in the post titles. 23 var suggestions = $.grep( searchTitles, function( item ) { 24 return matcher.test( item ); 25 }); 26 27 // cache the search term with its data in a hash map. 28 cachedPostTitles = searchTitles; 29 searchCache[request.term] = suggestions; 30 31 return suggestions; 32 }; 33 34 // check if the search key was already cached in the HashMap. 35 if ( request.term in searchCache ) { 36 37 // return suggestions using the cache object. 38 response( searchCache[request.term] ); 39 40 // exit and avoid an ajax call as we can use data that was cached in earlier ajax calls. 41 return; 42 43 } // else check if cached post tiles exists. 44 else if ( cachedPostTitles ) { 45 46 // cachedPostTitles array may have been set in previous AJAX call or inititally by wp_localize_script. 47 var searchSuggestions = searchTitlesForSuggestions( cachedPostTitles ); 48 49 // return the suggestions for the search term. 50 response( searchSuggestions ); 51 52 // exit and avoid an ajax call as we can use data that was cached in earlier ajax calls. 53 return; 54 55 } 56 57 // Else Make an AJAX Request. 58 59 // AJAX call is made if wp_localize_script sent an empty array for post titles. 60 sameAjaxRequest = $.ajax ({ 61 62 url: params.ajaxurl, // domain/wp-admin/admin-ajax.php 63 type: "POST", 64 dataType: "json", 65 data: { 66 action: "nds_advanced_search_autosuggest", 67 ajaxRequest: "yes", 68 term: request.term 69 } 70 }) 71 72 // on success. 73 .done( function( data, textStatus, jqXHR ) { 74 75 if ( jqXHR === sameAjaxRequest && null !== data && "undefined" !== typeof( data ) ) { 76 77 // data contains the post titles sent by the AJAX handler. 78 var searchSuggestions = searchTitlesForSuggestions( data ); 79 80 // return the suggestions for the search term. 81 response( searchSuggestions ); 82 83 } 84 }) 85 86 // on failure. 87 .fail( function( xhr, status, errorThrown ) { 88 89 $( "#nds-search-box" ).css( "background-color", "yellow" ); 90 $( "#nds-search-box" ).val( "An error occurred ..." ); 91 }) 92 93 // after all this time? 94 .always( function( xhr, status ) { 95 }); 96 97 98 }, 99 minLength: 3 100 }); 101 102})( 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.