/* global woodmart_settings */ (function($) { woodmartThemeModule.$document.on('wdShopPageInit', function () { woodmartThemeModule.ajaxSearch(); }); $.each([ 'frontend/element_ready/wd_search.default' ], function(index, value) { woodmartThemeModule.wdElementorAddAction(value, function() { woodmartThemeModule.ajaxSearch(); }); }); woodmartThemeModule.ajaxSearch = function() { if (typeof ($.fn.devbridgeAutocomplete) == 'undefined') { return; } var escapeRegExChars = function(value) { return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); }; $('form.woodmart-ajax-search').each(function() { var $this = $(this), number = parseInt($this.data('count')), thumbnail = parseInt($this.data('thumbnail')), symbols_count = parseInt($this.data('symbols_count')), productCat = $this.find('[name="product_cat"]'), $results = $this.parent().find('.wd-dropdown-results > .wd-scroll-content'), postType = $this.data('post_type'), url = woodmart_settings.ajaxurl + '?action=woodmart_ajax_search', price = parseInt($this.data('price')), sku = $this.data('sku'); if (number > 0) { url += '&number=' + number; } url += '&post_type=' + postType; $results.on('click', '.view-all-results', function() { $this.submit(); }); if (productCat.length && productCat.val() !== '') { url += '&product_cat=' + productCat.val(); } $this.find('[type="text"]').on('focus keyup', function() { var $input = $(this); if ($input.hasClass('wd-search-inited')) { return; } $input.devbridgeAutocomplete({ serviceUrl : url, appendTo : $results, minChars : symbols_count, deferRequestBy : woodmart_settings.ajax_search_delay, onSelect : function(suggestion) { if (suggestion.permalink.length > 0) { window.location.href = suggestion.permalink; } }, onHide : function() { $results.parent().removeClass('wd-opened'); }, onSearchStart : function() { $this.addClass('search-loading'); }, beforeRender : function(container) { $(container).find('.suggestion-divider-title').parent().addClass('suggestion-divider'); if (container[0].childElementCount > 2) { $(container).append('
' + suggestion.sku + '
'; } if (price && suggestion.price) { returnValue += '' + suggestion.price + '
'; } if (suggestion.value) { returnValue += '