| 1 |
'use strict'; |
| 2 |
|
| 3 |
var merchant = merchant || {}; |
| 4 |
|
| 5 |
merchant.modules = merchant.modules || {}; |
| 6 |
|
| 7 |
(function($) { |
| 8 |
|
| 9 |
merchant.modules.realTimeSearch = { |
| 10 |
|
| 11 |
init: function() { |
| 12 |
|
| 13 |
var self = this; |
| 14 |
var fields = document.querySelectorAll('.woocommerce-product-search .wc-search-field, .widget_product_search .search-field, .wp-block-search .wp-block-search__input, .wc-block-product-search-field'); |
| 15 |
|
| 16 |
if ( fields.length ) { |
| 17 |
|
| 18 |
var _loop = function( i ) { |
| 19 |
|
| 20 |
fields[i].setAttribute('autocomplete', 'off'); |
| 21 |
|
| 22 |
fields[i].addEventListener('keyup', self.debounce(function () { |
| 23 |
self.searchFormHandler(fields[i]); |
| 24 |
}, 300)); |
| 25 |
|
| 26 |
fields[i].addEventListener('focus', self.debounce(function () { |
| 27 |
self.searchFormHandler(fields[i]); |
| 28 |
}, 300)); |
| 29 |
|
| 30 |
}; |
| 31 |
|
| 32 |
for (var i = 0; i < fields.length; i++) { |
| 33 |
_loop(i); |
| 34 |
} |
| 35 |
|
| 36 |
document.addEventListener('click', function( e ) { |
| 37 |
if ( e.target.closest('.merchant-ajax-search-wrapper') === null ) { |
| 38 |
self.destroy(); |
| 39 |
} |
| 40 |
}); |
| 41 |
} |
| 42 |
|
| 43 |
}, |
| 44 |
|
| 45 |
searchFormHandler: function( el ) { |
| 46 |
|
| 47 |
if ( el.value.length < 3 ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
var self = this, |
| 52 |
term = el.value, |
| 53 |
clist = el.classList, |
| 54 |
type = clist.contains('wc-block-product-search-field') || clist.contains('wc-search-field') ? 'product' : 'post'; |
| 55 |
|
| 56 |
$.post( window.merchant.setting.ajax_url, { |
| 57 |
action: 'merchant_ajax_search_callback', |
| 58 |
nonce: window.merchant.setting.nonce, |
| 59 |
type: type, |
| 60 |
search_term: term, |
| 61 |
posts_per_page: window.merchant.setting.ajax_search_results_amounth_per_search, |
| 62 |
orderby: window.merchant.setting.ajax_search_results_order_by, |
| 63 |
order: window.merchant.setting.ajax_search_results_order, |
| 64 |
display_categories: window.merchant.setting.ajax_search_results_display_categories, |
| 65 |
enable_search_by_sku: window.merchant.setting.ajax_search_results_enable_search_by_sku, |
| 66 |
}, function( response ) { |
| 67 |
|
| 68 |
var wrapper = el.parentNode.getElementsByClassName('merchant-ajax-search-wrapper')[0]; |
| 69 |
|
| 70 |
if ( typeof wrapper === 'undefined' ) { |
| 71 |
|
| 72 |
wrapper = document.createElement('div'); |
| 73 |
wrapper.className = 'merchant-ajax-search-wrapper'; |
| 74 |
el.parentNode.append(wrapper); |
| 75 |
el.parentNode.classList.add('merchant-ajax-search'); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
wrapper.innerHTML = response.output; |
| 80 |
|
| 81 |
var products_wrapper = document.querySelector('.merchant-ajax-search-products'); |
| 82 |
|
| 83 |
if ( products_wrapper !== null && self.scrollbarVisible(products_wrapper) ) { |
| 84 |
products_wrapper.classList.add('has-scrollbar'); |
| 85 |
} |
| 86 |
|
| 87 |
if ( self.elementIsOutOfScreenHorizontal(wrapper) ) { |
| 88 |
wrapper.classList.add('merchant-reverse'); |
| 89 |
} |
| 90 |
|
| 91 |
}); |
| 92 |
}, |
| 93 |
|
| 94 |
destroy: function() { |
| 95 |
|
| 96 |
var wrappers = document.querySelectorAll('.merchant-ajax-search-wrapper'); |
| 97 |
|
| 98 |
if (wrappers.length) { |
| 99 |
for (var i = 0; i < wrappers.length; i++) { |
| 100 |
wrappers[i].remove(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
}, |
| 105 |
|
| 106 |
debounce: function( callback, wait ) { |
| 107 |
var timeoutId = null; |
| 108 |
return function () { |
| 109 |
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { |
| 110 |
args[_key] = arguments[_key]; |
| 111 |
} |
| 112 |
|
| 113 |
window.clearTimeout(timeoutId); |
| 114 |
timeoutId = window.setTimeout(function () { |
| 115 |
callback.apply(null, args); |
| 116 |
}, wait); |
| 117 |
}; |
| 118 |
}, |
| 119 |
|
| 120 |
scrollbarVisible: function( el ) { |
| 121 |
return el.scrollHeight > el.clientHeight; |
| 122 |
}, |
| 123 |
|
| 124 |
elementIsOutOfScreenHorizontal: function( el ) { |
| 125 |
var rect = el.getBoundingClientRect(); |
| 126 |
return rect.x + rect.width > window.innerWidth; |
| 127 |
} |
| 128 |
|
| 129 |
}; |
| 130 |
|
| 131 |
merchant.modules.realTimeSearch.init(); |
| 132 |
|
| 133 |
}(jQuery)); |