1'use strict'; 2 3class Husky { 4 constructor(input, data = {}) { 5 this.searched_value = ''; 6 this.current_page = 0; 7 this.data = Object.assign({}, data); 8 this.input = input; 9 this.init_input(); 10 this.container = document.createElement('div'); 11 this.container.className = 'woof_husky_txt'; 12 this.input.insertAdjacentElement('afterend', this.container); 13 14 this.fetch_timer = null; 15 this.fetch_controller = null; 16 17 document.addEventListener('click', ev => { 18 if (ev.target !== this.input) { 19 this._show(false); 20 } 21 }); 22 } 23 24 init_input() { 25 26 Object.keys(this.data).forEach((marker) => { 27 if (this.input.hasAttribute(`data-${marker}`)) { 28 this.data[marker] = this.input.getAttribute(`data-${marker}`); 29 } 30 }); 31 32 this.input.value = this.data.s; 33 if (!this.input.classList.contains('woof_husky_txt-input')) { 34 this.input.classList.add('woof_husky_txt-input'); 35 } 36 37 this.input.setAttribute('autocomplete', 'off'); 38 39 //+++ 40 41 let _this = this; 42 43 if (this.input.closest('div').querySelector('.woof_text_search_go') ) { 44 this.input.closest('div').querySelector('.woof_text_search_go').addEventListener('click', function (e) { 45 e.stopPropagation(); 46 e.preventDefault(); 47 let search_text = _this.input.value; 48 woof_current_values['woof_text'] = search_text; 49 woof_ajax_page_num = 1; 50 woof_submit_link(woof_get_submit_link(), 0); 51 52 return false; 53 }); 54 } 55 if (this.input.closest('form[role=search]')) { 56 this.input.closest('form[role=search]').addEventListener('submit', function (e) { 57 e.stopPropagation(); 58 e.preventDefault(); 59 60 let search_text = _this.input.value; 61 let min_symbols = (typeof _this.data.min_symbols) ? _this.data.min_symbols : 3; 62 if (search_text.length >= min_symbols) { 63 woof_current_values['woof_text'] = search_text; 64 woof_ajax_page_num = 1; 65 // woof_text_do_submit = false; 66 woof_submit_link(woof_get_submit_link(), 0); 67 68 } 69 70 return false; 71 }); 72 } 73 74 this.input.addEventListener('keyup', function (e) { 75 e.stopPropagation(); 76 e.preventDefault(); 77 _this.searched_value = this.value; 78 _this._search(1, e.key); 79 80 return false; 81 }); 82 83 this.input.addEventListener('focus', (e) => { 84 if (this._get_options_container()) { 85 this._get_options_container().style.display = 'block'; 86 } 87 }); 88 } 89 90 _search(current_page = 1, ekey = null) { 91 92 let _this = this; 93 let search_text = this.searched_value; 94 95 let min_symbols = (typeof this.data.min_symbols) ? this.data.min_symbols : 3; 96 this._loader(false); 97 if (this.fetch_timer) { 98 clearTimeout(this.fetch_timer); 99 } 100 101 if (search_text.length === 0) { 102 this._cross(false); 103 } 104 if (search_text.length >= min_symbols) { 105 jQuery('.woof_text_search_go').show(222); 106 } else { 107 jQuery('.woof_text_search_go').hide(); 108 } 109 110 woof_current_values['woof_text'] = search_text; 111 if (ekey === 'Enter') { 112 this._loader(); 113 // woof_current_values['woof_text'] = search_text; 114 woof_ajax_page_num = 1; 115 woof_submit_link(woof_get_submit_link(), 0); 116 this._loader(false); 117 return true; 118 } 119 120 121 122 if (parseInt(_this.data.autocomplete)== 0) { 123 return false; 124 } 125 126 if (this.searched_value === search_text && current_page === this.current_page) { 127 //return true; - TODO for pagination clicking 128 } 129 130 this.current_page = current_page; 131 132 //+++ 133 134 let delay_time = 777; 135 if (ekey === 'Enter' || ekey === 'Paged') { 136 delay_time = 1; 137 } 138 139 this.fetch_timer = setTimeout(() => { 140 141 let do_fetch = true; 142 143 if (search_text.length < min_symbols) { 144 do_fetch = false;
145 146 this._reset(); 147 } 148 this._loader(); 149 if (do_fetch) { 150 //check current tax 151 var cur_tax = false; 152 if (Object.keys(woof_really_curr_tax).length > 0) { 153 cur_tax = woof_really_curr_tax.term_id + '-' + woof_really_curr_tax.taxonomy; 154 } 155 156 let request_data = { 157 action: 'woof_text_search', 158 value: search_text, 159 link: woof_get_submit_link(), 160 cur_tax: cur_tax, 161 page: current_page - 1, 162 ...this.data 163 }; 164 165 this._cross(); 166 167 if (this.fetch_controller) { 168 //cancel ajax request if user is too quick 169 this.fetch_controller.abort(); 170 } 171 172 this.fetch_controller = new AbortController(); 173 const signal = this.fetch_controller.signal; 174 175 fetch(woof_husky_txt.ajax_url, {...{ 176 method: 'POST', 177 credentials: 'same-origin', 178 body: (function (data) { 179 const formData = new FormData(); 180 181 Object.keys(data).forEach(function (k) { 182 formData.append(k, data[k]); 183 }); 184 185 return formData; 186 })(request_data) 187 }, signal}).then(response => response.json()).then(response => { 188 189 this._reset(); 190 this.searched_value = search_text; 191 this._loader(false); 192 let answer = document.createElement('div'); 193 this.answer = answer; 194 this.answer.className = 'woof_husky_txt-container'; 195 this.container.appendChild(answer); 196 197 if (response.options.length > 0) { 198 response.options.forEach(function (row) { 199 let option = document.createElement('div'); 200 option.className = 'woof_husky_txt-option'; 201 option.innerHTML = row; 202 203 //+++ 204 205 let title = option.querySelector('.woof_husky_txt-option-title'); 206 let title_link = title.querySelector('a'); 207 208 209 if (parseInt(_this.data.title_light)) { 210 let pattern = new RegExp('(' + search_text + ')', 'ig'); 211 title_link.innerHTML = title.innerText.replace(pattern, `<span class='woof_husky_txt-highlight'>$1</span>`); 212 } 213 214 215 // if (parseInt(_this.data.how_to_open_links)) { 216// option.classList.add('husky-option-clickable'); 217// option.addEventListener('click', (e) => { 218// e.stopPropagation(); 219// window.open(title_link.href, _this.data.click_target); 220// return false; 221// }); 222 // } 223 224 answer.appendChild(option); 225 }); 226 227 if (response.pagination.pages > 1) { 228 this._draw_pagination(answer, response); 229 } 230 } else { 231 let option = document.createElement('div'); 232 option.className = 'woof_husky_txt-option'; 233 234 let content_container = document.createElement('div'); 235 236 let title = document.createElement('div'); 237 title.className = 'woof_husky_txt-option-title'; 238 title.textContent = woof_husky_txt.not_found; 239 content_container.appendChild(title); 240 option.appendChild(content_container); 241 answer.appendChild(option); 242 } 243 244 this._show(); 245 246 }).catch((err) => { 247 console.log(err); 248 }); 249 250 } else { 251 this._loader(false); 252 } 253 }, delay_time); 254 255 return true; 256 } 257
258 _show(is = true) { 259 if (is) { 260 //animation 261 let counter = 1; 262 let container = this._get_options_container(); 263 let timer = setInterval(() => { 264 let max_height = 0; 265 266 if (typeof this.data.max_open_height !== 'undefined') { 267 max_height = parseInt(this.data.max_open_height); 268 } else { 269 container.querySelectorAll('.woof_husky_txt-option').forEach(function (item) { 270 max_height += item.offsetHeight; 271 }); 272 } 273 274 //growing 275 container.style.maxHeight = parseFloat(0.05 * counter) * max_height + 'px'; 276 277 if (parseInt(container.style.maxHeight) >= max_height) { 278 clearInterval(timer); 279 if (typeof this.data.max_open_height !== 'undefined') { 280 container.style.maxHeight = max_height + 'px'; 281 container.style.overflow = 'auto'; 282 } else { 283 container.style.maxHeight = '100vh'; 284 } 285 } 286 counter++; 287 }, 10); 288 } else { 289 290 if (this._get_options_container()) { 291 let container = this._get_options_container(); 292 container.style.display = 'none'; 293 294 if (typeof container.style.overflow !== 'undefined') { 295 //container.scrollTop = 0;//!! 296 //container.style.overflow = null; 297 } 298 } 299 300 } 301 } 302 303 _loader(show = true) { 304 if (show) { 305 this.loader = document.createElement('div'); 306 this.loader.className = 'woof_husky_txt-loader'; 307 this.loader.style.width = this.loader.style.height = this.input.offsetHeight + 'px'; 308 this.loader.style.top = '-' + (this.input.offsetHeight - 1) + 'px'; 309 this.container.appendChild(this.loader); 310 } else { 311 if (this.loader) { 312 this.loader.remove(); 313 } 314 } 315 } 316 317 _cross(show = true) { 318 319 if (!this.cross) { 320 this.cross = document.createElement('span'); 321 this.cross.className = 'woof_husky_txt-cross'; 322 this.cross.innerText = 'x'; 323 this.cross.style.width = this.cross.style.height = (this.input.offsetHeight / 2) + 'px'; 324 this.cross.style.top = '-' + (this.input.offsetHeight / 2) + 'px'; 325 this.cross.style.right = '-23px'; 326 327 this.container.appendChild(this.cross); 328 329 let _this = this; 330 this.cross.addEventListener('click', function (e) { 331 e.stopPropagation(); 332 _this.input.value = ''; 333 _this._cross(false); 334 if (_this.answer) { 335 _this.answer.remove(); 336 } 337 if (_this.fetch_controller) { 338 //cancel ajax request if user is too quick 339 _this.fetch_controller.abort(); 340 } 341 if (_this.fetch_timer) { 342 clearTimeout(_this.fetch_timer); 343 } 344 345 return true; 346 }); 347 } 348 349 350 if (show) { 351 this.cross.style.display = 'inline'; 352 } else { 353 this.cross.style.display = 'none'; 354 } 355 } 356 357 _reset() { 358 if (this._get_options_container()) { 359 this._get_options_container().remove(); 360 } 361 } 362 363 _get_options_container() { 364 return this.container.querySelector('.woof_husky_txt-container'); 365 } 366 367 _draw_pagination(answer, response) { 368 let _this = this; 369 let option = document.createElement('div'); 370 option.className = 'woof_husky_txt-option woof_husky_txt-option-pagination'; 371 let p_container = document.createElement('div'); 372 p_container.className = 'husky-pagination'; 373 option.appendChild(p_container); 374 answer.appendChild(option); 375 376 //+++ 377 let a = null; 378 379 //algo 380 //1 2 !3! 4 5 381 for (let p = 1; p <= response.pagination.pages; p++) { 382 383 if (response.pagination.pages > 12) { 384 385 if (p === 1 || p === response.pagination.pages) { 386 this._draw_pagination_btn(response.pagination.page, p, p_container); 387 continue; 388 } 389 390 if (p === response.pagination.page || 391 p === response.pagination.page - 1 || 392 p === response.pagination.page - 2 || 393 p === response.pagination.page - 3 || 394 p === response.pagination.page + 1 || 395 p === response.pagination.page + 2 || 396 p === response.pagination.page + 3) { 397 this._draw_pagination_btn(response.pagination.page, p, p_container); 398 } 399 400 if (p === response.pagination.page - 4 || p === response.pagination.page + 4) { 401 a = document.createElement('i'); 402 a.innerText = ' ... '; 403 p_container.appendChild(a); 404 } 405 406 } else { 407 this._draw_pagination_btn(response.pagination.page, p, p_container); 408 } 409 410 } 411 412 //+++ 413 414 if (response.pagination.page > 1) { 415 a = document.createElement('a'); 416 a.href = '#'; 417 a.innerText = woof_husky_txt.prev; 418 419 a.addEventListener('click', function (e) { 420 e.preventDefault(); 421 e.stopPropagation(); 422 _this._search(response.pagination.page - 1, 'Paged'); 423 return false;
424 }); 425 426 p_container.prepend(a); 427 } 428 429 if (response.pagination.page < response.pagination.pages) { 430 a = document.createElement('a'); 431 a.href = '#'; 432 a.innerText = woof_husky_txt.next; 433 434 a.addEventListener('click', function (e) { 435 e.preventDefault(); 436 e.stopPropagation(); 437 _this._search(response.pagination.page + 1, 'Paged'); 438 return false; 439 }); 440 441 p_container.appendChild(a); 442 } 443 } 444 445 _draw_pagination_btn(page, p, p_container) { 446 let a = null; 447 let _this = this; 448 449 if (parseInt(page) === p) { 450 a = document.createElement('b'); 451 a.innerText = p; 452 } else { 453 454 a = document.createElement('a'); 455 a.href = '#'; 456 a.innerText = a.dataset.page = p; 457 458 a.addEventListener('click', function (e) { 459 e.preventDefault(); 460 e.stopPropagation(); 461 462 _this._search(this.dataset.page, 'Paged'); 463 464 return false; 465 }); 466 467 } 468 469 p_container.appendChild(a); 470 } 471} 472
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.