// Facet search functions -- Jeremy // Todo: specify content type, recipe/article/both (function($) { $(function(){ HFG.searching = false; if (HFG.isRecipeSearchPage()) { $("#facit_search_box_wrapper .recipe-search").submit(HFG.searchDelay); $("#facit_search_box_wrapper .recipe-search input[type=text]").keyup(HFG.searchDelay); //console.log('is recipi page'); $("body").on('click', '.menu_term', HFG.search); } if (HFG.isBlogPage()) { $("body").on('click', '.lazyload_term', HFG.search); console.log('is blog page'); } if (HFG.isArticleArchive()) { $("body").on('click', '.lazyload_term', HFG.search); console.log('is article archive page'); } // Lazy load results if ($('.lazy-load').length > 0) { console.log("Initialising lazy loading"); HFG.initLazyLoad(); } // Set up destination for search forms // $('#searchform').on('submit', function() { // if ($("#searchform input[name=search_post_type]:checked").val() == 'recipe') { // location.href = '/recipes?search=' + $('#s').val(); // return false; // } // return true; // }); }); var filtering = false; HFG = { searchTimeout: false, lazyLoadPageCount: 2, noMoreContent: false, isBlogPage: function() { return $('.facit_search_blog').length > 0; }, isRecipeSearchPage: function() { return $('.facit_search').length > 0; }, isArticleArchive: function() { return $('.facet_search_articles').length > 0 }, initLazyLoad: function() { window.loading = false; HFG.noMoreContent = false; $(window).scroll(function(){ // Trigger when we're 2 screens from the bottom if (($(window).scrollTop() > $(document).height() - 3 * $(window).height()) && !window.loading){ if(!HFG.noMoreContent) { window.loading = true; HFG.loadContent(HFG.lazyLoadPageCount); HFG.lazyLoadPageCount++; } } }); }, searchDelay: function() { if (HFG.searchTimeout) { clearTimeout(HFG.searchTimeout); } HFG.searchTimeout = setTimeout(HFG.search, 1000); return false; }, search: function() { HFG.noMoreContent = false; HFG.lazyLoadPageCount = 2; var post_type = 'recipe'; $('#loading-bar').css('display','flex'); $('#goto_search_result').remove(); if (HFG.isBlogPage()) post_type = 'blog'; if (HFG.isArticleArchive()) post_type = 'post'; var term = $("#s").val().replace("Search for", ""); //$.get('http://localhost/healthyfood/wp-admin/admin-ajax.php', { $.get('/wp-admin/admin-ajax.php', { action: "hfg_search", term: term, categories: HFG.getSearchCategories(), post_type: post_type }, function(response) { $('#loading-bar').css('display','none'); //$('#searchsubmit').after('View results below'); $('div.recipe-content, div.article-content').html(response); if (response.match('no_results_found')) HFG.noMoreContent = true; if (HFG.isBlogPage()){ console.log($('.lazyload_term.on').html()); $('h1.title').html($('.lazyload_term.on').html()); } if (HFG.isArticleArchive()){ console.log($('.lazyload_term.on').html()); $('h1.title').html($('.lazyload_term.on').html()); } }); return false; }, getSearchCategories: function() { categories = {}; if (HFG.isBlogPage()) { $('.lazyload_term.on').each(function() { filtering = true; slug = $(this).attr('data-taxonomy-slug'); categories[slug] = categories[slug] || []; categories[slug].push($(this).attr('tid')); }); } else if (HFG.isArticleArchive()) { $('.lazyload_term.on').each(function() { filtering = true; slug = $(this).attr('data-taxonomy-slug'); categories[slug] = categories[slug] || []; categories[slug].push($(this).attr('tid')); }); } else { $('.menu_term.on').each(function() { slug = $(this).parents("*[data-taxonomy-slug]").attr('data-taxonomy-slug'); if (slug) { categories[slug] = categories[slug] || []; categories[slug].push($(this).attr('tid')); } }); } return categories; }, loadContent: function(page){ //the code from here to line 126 doesn't actually seem to have any bearing on what get loaded. Check template-search.php instead to debug or add new content types var this_url = window.location.href; var query_string = window.location.search; query = $('.lazy-load').data('query'); var search_post_type = 'articles'; if (this_url.indexOf('recipes') > -1) search_post_type = 'recipes'; if (query_string.indexOf('recipe') > -1) search_post_type = 'recipes'; if (($('.tax-health_information').length > 0)||($('.tax-meals_and_occasions').length > 0)||($('.tax-ingredient').length > 0)||($('.tax-seasons').length > 0)||($('.tax-vegetable_serves').length > 0)) search_post_type = 'recipes'; if (this_url.indexOf('videos') > -1) search_post_type = 'videos'; if (this_url.indexOf('video-type') > -1) { search_post_type = 'videos'; console.log('video type'); } if (this_url.indexOf('blogs') > -1) search_post_type = 'blogs'; if (this_url.indexOf('collections/') > -1) search_post_type = 'hfg_collection'; if (this_url.indexOf('competitions') > -1) search_post_type = 'competition'; if (this_url.indexOf('expert-advice') > -1) { search_post_type = 'articles'; } if (query!=undefined) if (query.indexOf(query) > -1) { //post_type=post&seasons=autumn var this_arr = query.split('&'); if (this_arr[0]!= undefined){ var this_arr2 = this_arr[0].split('='); search_post_type = this_arr2[1]; } } if(HFG.isArticleArchive()) { search_post_type = 'articles'; } if (HFG.isRecipeSearchPage()) { //console.log(HFG.getSearchCategories()); ajaxParams = { action: "hfg_search", term: $("#s").val(), categories: HFG.getSearchCategories(), page: page }; } else if ((HFG.isBlogPage())&&(filtering)) { ajaxParams = { action: "hfg_search", term: $("#s").val(), categories: HFG.getSearchCategories(), page: page, post_type : 'blog' }; } else if ((HFG.isArticleArchive())&&(filtering)) { ajaxParams = { action: "hfg_search", term: $("#s").val(), categories: HFG.getSearchCategories(), page: page, post_type : 'post' }; } else if (this_url.indexOf('expert-advice') > -1) { loop = $('.lazy-load').data('loop-file') || "loop"; search_post_type = 'articles'; ajaxParams = { "action": "lazy_load", "page_number": page, "loop_file": loop, "query": query, "search_post_type": search_post_type }; } else { loop = $('.lazy-load').data('loop-file') || "loop"; ajaxParams = { "action": "lazy_load", "page_number": page, "loop_file": loop, "query": query, "search_post_type": search_post_type }; } console.log('search_post_type:'+search_post_type); $.ajax({ url: ajaxurl, type:'POST', data: ajaxParams, success: function(html) { window.loading = false; if(html) { $content = $(html); jQuery('.lazy-load').append($content);//.isotope('appended', $content); if (html.match('no_results_found')) HFG.noMoreContent = true; } else { HFG.noMoreContent = true; } }, complete: function() { window.loading = false; } }); } }; }(window.jQuery));