Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function initAutoComplete() {
- var $searchFormWrapper = $('.search-form');
- var $searchInput = $searchFormWrapper.find('[name="s"]');
- var $searchResults = $('.search-form__results');
- var $searchResultsLoader = $('.search-form__results-loader');
- $searchInput.keyup(function() {
- var search_query = $searchInput.val();
- if (search_query.length < 3) return;
- // prevent flooding
- var me = $(this);
- if ( me.data('requestRunning') ) { return; }
- me.data('requestRunning', true);
- $('.search-form__results-list').empty();
- $searchResults.slideDown(220);
- setTimeout(function() {
- $searchResultsLoader.addClass('show');
- },220);
- $.ajax({
- type: 'post',
- url: ajaxurl,
- dataType: 'json',
- data: { action: 'search_results_form', search_query: search_query },
- success: function (data) {
- $searchResultsLoader.removeClass('show');
- console.log(data);
- if (data.result == 1) {
- $.each(data.posts, function (key, postObject) {
- console.log(postObject.post_link);
- var htmlResultPost = `<div class="search-form__result" data-post-id="${postObject.post_id}">
- <h3 class="search-form__result-title">
- <a href="${postObject.post_link.url}" target="${postObject.post_link.target}"><span class="title">${postObject.post_title}</span></a>
- <span class="search-form__result-category">${postObject.post_type}</span>
- </h3>
- <div class="search-form__result-text"><a href="${postObject.post_permalink}">${postObject.post_excerpt}</a></div>
- </div>`;
- $('.search-form__results-list').append(htmlResultPost);
- });
- }
- else {
- console.log('Got Result');
- $('.search-form__results-list').append('<div>No Results Found</div>');
- }
- },
- complete: function() {
- me.data('requestRunning', false);
- }
- }); // eof $.ajax
- });
- // close search results on click outside of element
- $(document).mouseup(function(e) {
- var container = $searchFormWrapper;
- if (!container.is(e.target) && container.has(e.target).length === 0) {
- closeSearchResults();
- }
- });
- function closeSearchResults() {
- $searchResults.slideUp(220);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement