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