const bodyScrollLock = require('body-scroll-lock'); const disableBodyScroll = bodyScrollLock.disableBodyScroll; const enableBodyScroll = bodyScrollLock.enableBodyScroll; var MobileDetect = require('mobile-detect'), md = new MobileDetect(window.navigator.userAgent); function setSearchResultsMaxHeight() { if($('.main-header-helper').length === 0) { return; } $('.core-search-form-suggestions').css('max-height', $(window).height() - $('.main-header-helper').height()); } setSearchResultsMaxHeight(); $(window).on('resize', function() { setSearchResultsMaxHeight(); }); var request = null; var value = $('.search_suggestion_form input[name="search"]').val(); var timer = null; $(document).on('focusin', '.search_suggestion_form input[name="search"]', function() { $('.previous-searches').remove(); let previousSearches = readCookie('previous-searches'); let items = previousSearches ? previousSearches.split(/,/) : new Array(); if(items.length > 0) { let form = $(this).closest('.search_suggestion_form'); let html = '
' + form.data('tag-previous-searches') + '
'; let url = form.attr('action'); for(let i = 0; i < items.length; i++) { html += '' + items[i] + ' '; } $(this).after($('
'+ html +'
').addClass('previous-searches').css({ 'top': $(this).position().top + 40, })); } }); // search suggestion $('.search_suggestion_form input[name="search"]').on('keyup', function(){ var val = $(this).val(); let that = $(this); $('.search .search-holder .search-button').show(); $('.search .search-holder .clear-button').hide(); if (value != val || val.length < 3) { if (request) { request.abort(); } if (timer) { clearTimeout(timer); } } if(val.length > 0) { $('.search_suggestion_form .btn-erase-search-input').show(); } else { $('.search_suggestion_form .btn-erase-search-input').hide(); } $('.previous-searches').remove(); if (val.length < 3) { $('.core-search-form-suggestions').fadeOut(200); } else if(value != val) { value = val; timer = setTimeout(function () { request = $.ajax({ url: '/ajax/core-ajax-suggestions.php', type: 'POST', dataType: 'json', data: { userInput : val }, success: function(data){ $('.core-ajax-suggestions').html(data.bottom); $('.core-ajax-suggestions-text') .html(data.top) .wrapInTag({"words" : [val], tag: 'span'}); if(md.phone()) { disableBodyScroll(document.querySelector('.core-search-form-suggestions')); } if((data.total.products + data.total.text) > 0) { val = $.trim(val); $('.search .search-holder .search-button').hide(); $('.search .search-holder .clear-button').show(); if(data.total.products == 0 || data.total.text == 0) { $('.core-ajax-suggestions-text').removeClass('border-bottom'); } else { $('.core-ajax-suggestions-text').addClass('border-bottom'); } if(md.is('iPhone')) { that.blur(); } let previousSearches = readCookie('previous-searches'); var items = previousSearches ? previousSearches.split(/,/) : new Array(); if(!items.find(function(element) { return $.trim(element) === val; })) { items.push(val); } items = items.slice(-3); createCookie('previous-searches', items.join(','), 7); $('.core-search-form-suggestions').fadeIn(200); } else { if(md.phone()) { enableBodyScroll(document.querySelector('.core-search-form-suggestions')); } $('.core-search-form-suggestions').fadeOut(200); } } }); }, 200); } }); $('.search_suggestion_form input[name="search"]').on('focusout', function(){ if(!md.is('iPhone')) { $('.core-search-form-suggestions').fadeOut(200); enableBodyScroll(document.querySelector('.core-search-form-suggestions')); } }); $(document).on('click', '.search .clear-button', function() { let inputElem = $(this).closest('.search').find('.search_suggestion_form input[name="search"]'); inputElem.val(''); inputElem.trigger('keyup'); if(md.phone()) { enableBodyScroll(document.querySelector('.core-search-form-suggestions')); } }); $(document).on('submit', '.search_suggestion_form', function() { if(ScarabQueue) { if($(this).find('input[name="search"]').length) { ScarabQueue.push(['searchTerm', $(this).find('input[name="search"]').val()]); ScarabQueue.push(['go']); } } }); function createCookie(name, value, days) { var expires; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toGMTString(); } else expires = ""; document.cookie = name + "=" + value + expires + "; path=/"; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; } function eraseCookie(name) { createCookie(name, "", -1); } function areCookiesEnabled() { var r = false; createCookie("testing", "Hello", 1); if (readCookie("testing") != null) { r = true; eraseCookie("testing"); } return r; }