var selector = '#search input'; var selector2 = '#content input[name=search]'; var queryMinLength = '3'; var catTitle = 'Категории'; var prodTitle = 'Товары'; var viewAllTitle = 'Посмотреть все'; var noResTitle = 'Ничего не найдено'; $(document).ready(function() { var delay = (function () { var timer = 0; return function (callback, ms) { clearTimeout(timer); timer = setTimeout(callback, ms); }; })(); var timer = null; $(selector2).keyup(function(){ delay(function(){ sphinxAutocomplete(selector2) }, 300); }); $(selector).keyup(function(){ delay(function(){ sphinxAutocomplete(selector) }, 300); }); }); $(document).mouseup(function (e) { if (!$('.sphinxsearch').is(e.target) && $('.sphinxsearch').has(e.target).length === 0) { $('.sphinxsearch').hide(); $(selector).removeClass('no-bottom-borders'); return false; } if (!$('.sphinxsearch').is(e.target) && $('.sphinxsearch').has(e.target).length === 0) { $('.sphinxsearch').hide(); $(selector2).removeClass('no-bottom-borders'); return false; } }); function sphinxAutocomplete(input) { if($(input).val() == '' || $(input).val().length < queryMinLength) { $('.sphinxsearch').hide(); $(selector).removeClass('no-bottom-borders'); return false; } $.ajax({ url: 'https://vseelki.ru/index.php?route=common/sphinx/autocompleate&search=' + $(input).val(), dataType: 'json', success: function(json) { var html = ''; //Categories if (json.categories.length) { html += '
'+catTitle+''; var categories = json.categories; for (i = 0; i < categories.length; i++) { html += ''; if(categories[i]['image'] != '') { html += ''; } html += categories[i]['name']; html += '
'; html += '
'; } html += '
'; } //Products if (json.products.length) { if (json.categories.length) { html += '
'+prodTitle+''; } var products = json.products; for (i = 0; i < products.length; i++) { html += ''; html += ''; html += products[i]['name']; html += '
'; html += '
'; } if (json.categories.length) { html += '
'; } html += '
' + ''+viewAllTitle+'' + '
'; } else { html = '
'+noResTitle+'
'; } $('ul.dropdown-menu, .sphinxsearch').remove(); $(input).after('
'+ html +'
'); $('.sphinxsearch').show(); $(input).addClass('no-bottom-borders'); } }); }