| 1 |
class LiveSearch { |
| 2 |
constructor(){ |
| 3 |
var $ = jQuery; |
| 4 |
|
| 5 |
//live search |
| 6 |
this.request; |
| 7 |
this.searchForm = $(".betterdocs-searchform"); |
| 8 |
this.searchField = $(".betterdocs-search-field"); |
| 9 |
this.searchCategory = $(".betterdocs-search-category"); |
| 10 |
this.popularSearch = $( |
| 11 |
".betterdocs-popular-search-keyword .popular-keyword" |
| 12 |
); |
| 13 |
this.searchClose = $(".docs-search-close"); |
| 14 |
|
| 15 |
|
| 16 |
this.liveSearch(); |
| 17 |
} |
| 18 |
|
| 19 |
liveSearch() { |
| 20 |
var $ = jQuery; |
| 21 |
var $this = this; |
| 22 |
// disable from submit on enter |
| 23 |
this.popularSearch.on("click", function (e) { |
| 24 |
e.preventDefault(); |
| 25 |
let popularKeyword = $(this).text(); |
| 26 |
$(this) |
| 27 |
.parent(".betterdocs-popular-search-keyword") |
| 28 |
.siblings(".betterdocs-searchform") |
| 29 |
.find(".betterdocs-search-field") |
| 30 |
.val(popularKeyword) |
| 31 |
.trigger("propertychange"); |
| 32 |
}); |
| 33 |
|
| 34 |
this.searchForm.on("keyup keypress", function (e) { |
| 35 |
let keyCode = e.keyCode || e.which; |
| 36 |
if (keyCode === 13) { |
| 37 |
e.preventDefault(); |
| 38 |
return false; |
| 39 |
} |
| 40 |
}); |
| 41 |
|
| 42 |
// ajax load titles on keyup to searchbox |
| 43 |
this.searchField.on("input propertychange paste", function (e) { |
| 44 |
let thisEvent = $(this); |
| 45 |
let inputVal = $(this).val(); |
| 46 |
let inputCat = thisEvent |
| 47 |
.parent(".betterdocs-searchform-input-wrap") |
| 48 |
.siblings(".betterdocs-search-category") |
| 49 |
.find(":selected") |
| 50 |
.val(); |
| 51 |
let resultWrapper = thisEvent |
| 52 |
.parent() |
| 53 |
.parent(".betterdocs-searchform"); |
| 54 |
let kbSlug = thisEvent |
| 55 |
.parent() |
| 56 |
.parent(".betterdocs-searchform") |
| 57 |
.find(".betterdocs-search-kbslug") |
| 58 |
.val(); |
| 59 |
|
| 60 |
//Before the result is fetched, we have to remove the search result wrapper based on letter limit characters |
| 61 |
if (!inputVal.length) { |
| 62 |
var nodeResults = thisEvent |
| 63 |
.parents(".betterdocs-live-search") |
| 64 |
.find(".betterdocs-search-result-wrap"); |
| 65 |
if (nodeResults.length > 0) { |
| 66 |
nodeResults.each(function (item) { |
| 67 |
$(nodeResults[item]).remove(); |
| 68 |
}); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$this.liveSearchAction( |
| 73 |
e, |
| 74 |
thisEvent, |
| 75 |
inputVal, |
| 76 |
inputCat, |
| 77 |
resultWrapper, |
| 78 |
kbSlug |
| 79 |
); |
| 80 |
}); |
| 81 |
|
| 82 |
this.searchCategory.on("change", function (e) { |
| 83 |
let thisEvent = $(this); |
| 84 |
let inputVal = thisEvent |
| 85 |
.siblings(".betterdocs-searchform-input-wrap") |
| 86 |
.children(".betterdocs-search-field") |
| 87 |
.val(); |
| 88 |
let inputCat = $(this).find(":selected").val(); |
| 89 |
let resultWrapper = thisEvent.parent(".betterdocs-searchform"); |
| 90 |
let kbSlug = thisEvent |
| 91 |
.parent(".betterdocs-searchform") |
| 92 |
.find(".betterdocs-search-kbslug") |
| 93 |
.val(); |
| 94 |
$this.liveSearchAction( |
| 95 |
e, |
| 96 |
thisEvent, |
| 97 |
inputVal, |
| 98 |
inputCat, |
| 99 |
resultWrapper, |
| 100 |
kbSlug |
| 101 |
); |
| 102 |
}); |
| 103 |
|
| 104 |
this.liveSearchAction = function ( |
| 105 |
e, |
| 106 |
thisEvent, |
| 107 |
inputVal, |
| 108 |
inputCat, |
| 109 |
resultWrapper, |
| 110 |
kbSlug |
| 111 |
) { |
| 112 |
let $ = jQuery; |
| 113 |
let resultList = thisEvent |
| 114 |
.parent(".betterdocs-searchform") |
| 115 |
.find(".betterdocs-search-result-wrap"); |
| 116 |
let searchLoader = thisEvent.parent().find(".docs-search-loader"); |
| 117 |
let searchClose = thisEvent.parent().find(".docs-search-close"); |
| 118 |
let search_letter_limit = betterdocsSearchConfig.search_letter_limit; |
| 119 |
if ( |
| 120 |
e.key != "a" && |
| 121 |
e.keyCode != 17 && |
| 122 |
e.keyCode != 91 && |
| 123 |
inputVal.length >= search_letter_limit |
| 124 |
) { |
| 125 |
$this.delay(function () { |
| 126 |
$this.ajaxLoad( |
| 127 |
inputVal, |
| 128 |
inputCat, |
| 129 |
kbSlug, |
| 130 |
resultWrapper, |
| 131 |
resultList, |
| 132 |
searchLoader, |
| 133 |
searchClose, |
| 134 |
thisEvent |
| 135 |
); |
| 136 |
}, 400); |
| 137 |
} else if (!inputVal.length) { |
| 138 |
thisEvent |
| 139 |
.parent() |
| 140 |
.parent(".betterdocs-live-search") |
| 141 |
.find(".betterdocs-search-result-wrap") |
| 142 |
.addClass("hideArrow"); |
| 143 |
thisEvent |
| 144 |
.parent() |
| 145 |
.parent(".betterdocs-live-search") |
| 146 |
.find(".docs-search-result") |
| 147 |
.slideUp(300); |
| 148 |
searchLoader.hide(); |
| 149 |
searchClose.hide(); |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
this.searchClose.on("click", function () { |
| 154 |
$(this).hide(); |
| 155 |
$(this).parent() |
| 156 |
.parent() |
| 157 |
.find(".betterdocs-search-result-wrap") |
| 158 |
.addClass("hideArrow"); |
| 159 |
$(this).parent() |
| 160 |
.parent() |
| 161 |
.find(".docs-search-result") |
| 162 |
.slideUp(300); |
| 163 |
$(this).siblings( ".betterdocs-search-field" ).val(""); |
| 164 |
}); |
| 165 |
|
| 166 |
this.delay = (function (callback, ms) { |
| 167 |
let timer = 0; |
| 168 |
return function (callback, ms) { |
| 169 |
clearTimeout(timer); |
| 170 |
timer = setTimeout(callback, ms); |
| 171 |
return timer; |
| 172 |
}; |
| 173 |
})(); |
| 174 |
|
| 175 |
this.ajaxLoad = function ( |
| 176 |
inputVal, |
| 177 |
inputCat, |
| 178 |
kbSlug, |
| 179 |
resultWrapper, |
| 180 |
resultList, |
| 181 |
searchLoader, |
| 182 |
searchClose, |
| 183 |
inputEvent |
| 184 |
) { |
| 185 |
var $ = jQuery; |
| 186 |
let request; |
| 187 |
if (request) { |
| 188 |
request.abort(); |
| 189 |
} |
| 190 |
request = $.ajax({ |
| 191 |
url: betterdocsSearchConfig.ajax_url, |
| 192 |
type: "post", |
| 193 |
data: { |
| 194 |
action: "betterdocs_get_search_result", |
| 195 |
search_input: inputVal, |
| 196 |
search_cat: inputCat, |
| 197 |
kb_slug: kbSlug || betterdocsSearchConfigTwo?.kb_based_search || '', |
| 198 |
is_post_type_archive:betterdocsSearchConfigTwo?.is_post_type_archive, |
| 199 |
}, |
| 200 |
beforeSend: function () { |
| 201 |
searchLoader.show(); |
| 202 |
searchClose.hide(); |
| 203 |
resultList.addClass("hideArrow"); |
| 204 |
$(".betterdocs-live-search .docs-search-result").slideUp( |
| 205 |
400 |
| 206 |
); |
| 207 |
}, |
| 208 |
success: function (response) { |
| 209 |
resultList.remove(); |
| 210 |
searchLoader.hide(); |
| 211 |
searchClose.show(); |
| 212 |
|
| 213 |
let search_letter_limit = |
| 214 |
betterdocsSearchConfig.search_letter_limit; |
| 215 |
var inputVal2 = inputEvent.val(); |
| 216 |
|
| 217 |
//After the result is fetched, we have to remove the search result wrapper |
| 218 |
if (inputVal2.length < search_letter_limit) { |
| 219 |
var nodeResults = $(".betterdocs-live-search").find( |
| 220 |
".betterdocs-search-result-wrap" |
| 221 |
); |
| 222 |
if (nodeResults.length > 0) { |
| 223 |
nodeResults.each(function (item) { |
| 224 |
$(nodeResults[item]).slideUp(400); |
| 225 |
}); |
| 226 |
} |
| 227 |
searchClose.hide(); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
resultWrapper.append(response.data.post_lists); |
| 232 |
}, |
| 233 |
}); |
| 234 |
}; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
new LiveSearch; |
| 239 |
|