(function($){jQuery('document').ready(function($){var finalTemplate=$('#pfd-search-result-template'),outputTemplate=_.template(finalTemplate.html()),wrapper='#search-wrapper',resultList='#result-list',results='#search-results',searchLoader='#search-loading',searchInput='#search-input',notice='.pf-docs-helper',endPointError=pf_docs_search.end_point_error,errorDiv=".pf-docs-error-results-wrap",remote_url=$(searchInput).data('remote-url'),typingTimer,doneTypingInterval=4000;request_status='';var search_page=$('.search_page').val();init();function init(){$(searchInput).on('keyup',function(event){if(event.keyCode===13){processSearchResults(!1)}});$(searchInput).on('click',function(event){if(!$('.pf-docs-search-results-wrap').hasClass('pf-docs-show')){processSearchResults(!1)}});$('.pf-docs-search-container .pf-svg-search').on('click',function(event){processSearchResults(!1)});$(searchInput).on('keyup',function(event){if(event.keyCode!==13){clearTimeout(typingTimer);processSearchResults(!1,event);typingTimer=setTimeout(function(){doneTyping(event)},doneTypingInterval)}});$(searchInput).on('keydown',function(){clearTimeout(typingTimer)});function doneTyping(event){val=$.trim($(searchInput).val()) if(request_status!=''&&val.length>=3){searchResultUpdateInDb(val,request_status)}}} function processSearchResults(updateDatabase=!0,event=''){let subtype='subtype[]=docs';let source=[];if(typeof pfd_option.pfd_post_type!='undefined'){let post_types=[];source=pfd_option.pfd_post_type.split(',');source.forEach((type)=>{post_types.push('subtype[]='+type.trim())}) subtype=post_types.join('&')} var key=event.which,that=$(searchInput),val=$.trim($(searchInput).val()),valIs=val==$(that).val(),notEmpty=''!==val,type=$(searchInput).data('object-type'),max=$(wrapper).data('number'),url=remote_url+'/wp-json/wp/v2/search?'+subtype+'&search='+val+'&per_page='+max+'&orderby=relevance&order=asc';if(!valIs&&!notEmpty){return!1} $(results).parent().addClass('pf-docs-hide').removeClass('pf-docs-show');if(val==''){destroySearch(1)}else if(val.length==2){destroySearch();$('.search-results-text,.no-search-results-text').removeClass('pf-docs-show').addClass('pf-docs-hide');$(results).parent().addClass('pf-docs-show').removeClass('pf-docs-hide');$('.pf-docs-helper').removeClass('pf-docs-hide').addClass('pf-docs-show')} if(val.length>=3){$('.pf-docs-helper,.pf-svg-search').addClass('pf-docs-hide').removeClass('pf-docs-show');$(searchLoader).removeClass('pf-docs-hide').addClass('pf-docs-show');var jqxhr=$.getJSON(url,function(response){$(resultList).children().remove();$(resultList).removeClass('rs-full').addClass('rs-empty');$(searchLoader).removeClass('pf-docs-show').addClass('pf-docs-hide');$('.pf-svg-search').addClass('pf-docs-show').removeClass('pf-docs-hide');if(response.length==0){$(results).closest(wrapper).addClass('pf-docs-no-results');$(results).find('.no-search-results-text').removeClass('pf-docs-hide');$(results).find('.search-results-text').addClass('pf-docs-hide');request_status='fail'}else{$(results).closest(wrapper).removeClass('pf-docs-no-results');$(results).find('.search-results-text').removeClass('pf-docs-hide');$(results).find('.no-search-results-text').addClass('pf-docs-hide');grouped_results=group_posts(response,'subtype');source.forEach((type)=>{heading=type;if(type=='post'){heading='Blog Posts'} if(typeof grouped_results[type]!=='undefined'&&grouped_results[type].length>0){$(resultList).append("
  • "+heading+"
  • ");$.each(grouped_results[type],function(i,result){$(resultList).append(outputTemplate({post:result})).removeClass('rs-empty').addClass('rs-full')})} if(pfd_option.support_link!=''){$(resultList).append("
  • "+pfd_option.support_link+"
  • ")}}) request_status='pass'} $(results).parent().removeClass('pf-docs-hide').addClass('pf-docs-show');if(!0===updateDatabase){searchResultUpdateInDb(val,request_status)}}).done(function(){$(document).trigger("after")}).fail(function(){$(errorDiv).text(endPointError);$(searchLoader).removeClass('pf-docs-show').addClass('pf-docs-hide')})}} const group_posts=(array,key)=>{return array.reduce((result,currentValue)=>{(result[currentValue[key]]=result[currentValue[key]]||[]).push(currentValue);return result},{})} function destroySearch(clear_input=0){$(resultList).children().remove();if(clear_input==1){$(searchInput).val('')} $(results).parent().removeClass('pf-docs-show').addClass('pf-docs-hide');$(resultList).removeClass('rs-full').addClass('rs-empty');$(notice).removeClass('pf-docs-show').addClass('pf-docs-hide');$(errorDiv).empty();$('.pf-docs-after-wrapper').fadeOut()} function searchResultUpdateInDb(search_term,request_status){$.ajax({url:pf_docs_search.ajaxurl,type:'POST',dataType:'json',data:{'action':'pf_docs_search_result','search_term':search_term,'request_status':request_status,'page':search_page,'nonce':pf_docs_search.pfd_ajax_nonce},}).fail(function(jqXHR){}).done(function(results){})} jQuery('body').click(function(e){if(!jQuery(e.target).closest('.pf-docs-search-results-wrap').length&&e.target.id!='search-input'){destroySearch()}})})})(jQuery)