1var re_ajax_cache = { 2 data: {}, 3 remove: function (cache_id) { 4 delete re_ajax_cache.data[cache_id]; 5 }, 6 exist: function (cache_id) { 7 if(jQuery('.custom_search_box').length){ 8 return false; 9 } 10 return re_ajax_cache.data.hasOwnProperty(cache_id) && re_ajax_cache.data[cache_id] !== null; 11 }, 12 get: function (cache_id) { 13 return re_ajax_cache.data[cache_id]; 14 }, 15 set: function (cache_id, cachedData) { 16 re_ajax_cache.remove(cache_id); 17 re_ajax_cache.data[cache_id] = cachedData; 18 } 19}; 20 21var re_ajax_search = { 22 23 // Some variables 24 _current_selection_index:0, 25 _last_request_results_count:0, 26 _first_down_up:true, 27 28 init: function init() { 29 30 // keydown on the text box 31 jQuery('.re-ajax-search').on("keydown", jQuery.debounce(250, function(event) { 32 var ajaxsearchitem = jQuery(this); 33 if ( 34 (event.which && event.which == 39) 35 || (event.keyCode && event.keyCode == 39) 36 || (event.which && event.which == 37) 37 || (event.keyCode && event.keyCode == 37)) 38 { 39 //do nothing on left and right arrows 40 re_ajax_search.re_ajax_set_focus(ajaxsearchitem); 41 return; 42 } 43 44 if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) { 45 // on enter 46 var re_ajax_search_cur = jQuery(this).parent().parent().find('.re-sch-cur-element'); 47 if (re_ajax_search_cur.length > 0) { 48 var re_searchopen_url = re_ajax_search_cur.find('.re-search-result-title a').attr('href'); 49 window.location = re_searchopen_url; 50 } else { 51 jQuery(this).parent().submit(); 52 } 53 return false; //redirect for search on enter 54 } else { 55 56 if ((event.which && event.which == 40) || (event.keyCode && event.keyCode == 40)) { 57 // down 58 re_ajax_search.re_aj_search_move_key_down(ajaxsearchitem); 59 return false; //disable the envent 60 61 } else if((event.which && event.which == 38) || (event.keyCode && event.keyCode == 38)) { 62 //up 63 re_ajax_search.re_aj_search_move_key_up(ajaxsearchitem); 64 return false; //disable the envent 65 } else { 66 67 //for backspace we have to check if the search query is empty and if so, clear the list 68 if ((event.which && event.which == 8) || (event.keyCode && event.keyCode == 8)) { 69 //if we have just one character left, that means it will be deleted now and we also have to clear the search results list 70 var search_query = jQuery(this).val(); 71 if (search_query.length == 1) { 72 jQuery(this).parent().parent().find('.re-aj-search-wrap').removeClass( 're-aj-search-open' ).empty(); 73 } 74 75 } 76 77 //various keys 78 re_ajax_search.re_ajax_set_focus(ajaxsearchitem); 79 //jQuery('.re-aj-search-wrap').empty(); 80 setTimeout(function(){ 81 re_ajax_search.do_ajax_call(ajaxsearchitem); 82 }, 100); 83 } 84 return true; 85 } 86 })); 87 88 }, 89 90 /** 91 * moves the select up 92 */ 93 re_aj_search_move_key_up: function re_aj_search_move_key_up(elem) { 94 if (re_ajax_search._first_down_up === true) { 95 re_ajax_search._first_down_up = false; 96 if (re_ajax_search._current_selection_index === 0) { 97 re_ajax_search._current_selection_index = re_ajax_search._last_request_results_count - 1; 98 } else { 99 re_ajax_search._current_selection_index--; 100 } 101 } else { 102 if (re_ajax_search._current_selection_index === 0) { 103 re_ajax_search._current_selection_index = re_ajax_search._last_request_results_count; 104 } else { 105 re_ajax_search._current_selection_index--; 106 } 107 } 108 elem.parent().parent().find('.re-search-result-div').removeClass('re-sch-cur-element'); 109 if (re_ajax_search._current_selection_index > re_ajax_search._last_request_results_count -1) { 110 //the input is selected 111 elem.closest('form').fadeTo(100, 1); 112 } else { 113 re_ajax_search.re_search_input_remove_focus(elem); 114 elem.parent().parent().find('.re-search-result-div').eq(re_ajax_search._current_selection_index).addClass('re-sch-cur-element'); 115 } 116 }, 117 118 /** 119 * moves the select prompt down 120 */ 121 re_aj_search_move_key_down: function re_aj_search_move_key_down(elem) { 122 if (re_ajax_search._first_down_up === true) { 123 re_ajax_search._first_down_up = false; 124 } else { 125 if (re_ajax_search._current_selection_index === re_ajax_search._last_request_results_count) { 126 re_ajax_search._current_selection_index = 0; 127 } else { 128 re_ajax_search._current_selection_index++; 129 } 130 } 131 elem.parent().parent().find('.re-search-result-div').removeClass('re-sch-cur-element'); 132 if (re_ajax_search._current_selection_index > re_ajax_search._last_request_results_count - 1 ) { 133 //the input is selected 134 elem.closest('form').fadeTo(100, 1); 135 } else { 136 re_ajax_search.re_search_input_remove_focus(elem); 137 elem.parent().parent().find('.re-search-result-div').eq(re_ajax_search._current_selection_index).addClass('re-sch-cur-element'); 138 } 139 }, 140 141 /** 142 * puts the focus on the input box 143 */ 144 re_ajax_set_focus: function re_ajax_set_focus(elem) { 145 re_ajax_search._current_selection_index = 0; 146 re_ajax_search._first_down_up = true; 147 elem.closest('form').fadeTo(100, 1); 148 elem.parent().parent().find('.re-search-result-div').removeClass('re-sch-cur-element'); 149 }, 150 151 /** 152 * removes the focus from the input box 153 */ 154 re_search_input_remove_focus: function re_search_input_remove_focus(elem) { 155 if (re_ajax_search._last_request_results_count !== 0) { 156 elem.closest('form').css('opacity', 0.5); 157 } 158 }, 159 160 /** 161 * AJAX: process the response from the server 162 */ 163 process_ajax_response: function (data, callelem) { 164 var current_query = callelem.val().trim(); 165 166 167 //the search is empty - drop results 168 if (current_query == '') { 169 callelem.parent().parent().find('.re-aj-search-wrap').empty(); 170 return; 171 } 172 173 var td_data_object = JSON.parse(data); //get the data object 174 //drop the result - it's from a old query 175 if (td_data_object.re_search_query !== current_query) { 176 return; 177 } 178 179 //reset the current selection and total posts 180 re_ajax_search._current_selection_index = 0; 181 re_ajax_search._last_request_results_count = td_data_object.re_total_inlist; 182 re_ajax_search._first_down_up = true; 183 184 185 //update the query 186 callelem.parent().parent().find('.re-aj-search-wrap').addClass( 're-aj-search-open' ).html(td_data_object.re_data); 187 var iconsearch = callelem.parent().find('.rhi-sync'); 188 iconsearch.removeClass('rhi-sync fa-spin').addClass('rhi-search'); 189 callelem.removeClass('searching-now'); 190 191 }, 192 193 /**
194 * AJAX: do the ajax request 195 */ 196 do_ajax_call: function do_ajax_call(elem) { 197 var posttypes = elem.data('posttype'); 198 var enable_compare = elem.data('enable_compare'); 199 var aff = elem.data('aff'); 200 if(elem.prevObject == undefined){ 201 var catid = elem.data('catid'); 202 }else{ 203 var catid = elem.attr('data-catid'); 204 } 205 var callelem = elem; 206 if (elem.val() == '') { 207 re_ajax_search.re_ajax_set_focus(callelem); 208 return; 209 } 210 211 var search_query = elem.val(); 212 213 //do we have a cache hit 214 if (re_ajax_cache.exist(search_query)) { 215 re_ajax_search.process_ajax_response(re_ajax_cache.get(search_query), callelem); 216 return; 217 } 218 219 var iconsearch = elem.parent().find('.rhi-search'); 220 iconsearch.removeClass('rhi-search').addClass('rhi-sync fa-spin'); 221 elem.addClass('searching-now'); 222 223 jQuery.ajax({ 224 type: 'POST', 225 url: rhscriptvars.ajax_url, 226 data: { 227 action: 'rehub_ajax_search', 228 re_string: search_query, 229 posttypesearch: posttypes, 230 enable_compare : enable_compare, 231 aff_link: aff, 232 catid : catid, 233 security : rhscriptvars.searchnonce, 234 } 235 }).done(function(data, textStatus, XMLHttpRequest){ 236 re_ajax_cache.set(search_query, data); 237 re_ajax_search.process_ajax_response(data, callelem); 238 }); 239 } 240}; 241 242jQuery(document).ready(function($) { 243 'use strict'; 244 re_ajax_search.init(); 245});
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.