var Mfn_livesearch={
that: this,
postsLoaded: [],
dom:{
ajaxFetchedPage: '',
get resultsFromPage(){ return this.ajaxFetchedPage; },
isItem(e){ return e.target.closest('.mfn-live-search-wrapper') },
itemAttr(attrName){
const wrapper=document.querySelector('.mfn-live-search-wrapper');
return parseInt(wrapper.getAttribute('data-'+attrName));
},
get searchForm(){
return Array.from(document.querySelectorAll('.search_wrapper .form-searchform, .top_bar_right .form-searchform, #Side_slide #side-form, .mfn-live-search-wrapper .mfn-live-searchform'));
},
get searchField(){
return Array.from(document.querySelectorAll('.search_wrapper input[type=text], .top_bar_right .form-searchform input[type=text], #Side_slide #side-form input[type=text], .mfn-live-search-wrapper input[type=text]'));
},
get liveSearchNoResults(){
return Array.from(document.querySelectorAll('.mfn-live-search-box .mfn-live-search-noresults'));
},
get liveSearchBox(){
return Array.from(document.querySelectorAll('.mfn-live-search-box'));
},
get liveSearchResultsList(){
return Array.from(document.querySelectorAll('.mfn-live-search-list'));
},
get liveSearchResultsListShop(){
return Array.from(document.querySelectorAll('.mfn-live-search-list-shop'));
},
get liveSearchResultsListBlog(){
return Array.from(document.querySelectorAll('.mfn-live-search-list-blog'));
},
get liveSearchResultsListPortfolio(){
return Array.from(document.querySelectorAll('.mfn-live-search-list-portfolio'));
},
get liveSearchResultsListPages(){
return Array.from(document.querySelectorAll('.mfn-live-search-list-pages'));
},
get liveSearchResultsListCategories(){
return Array.from(document.querySelectorAll('.mfn-live-search-list-categories'));
},
},
create: {
that: this,
linkToLivesearch: (inputValue)=> `${mfn.home_url_lang}?s=${inputValue}&mfn_livesearch`,
linkToLivesearch2: (inputValue,postType)=> `${mfn.home_url_lang}?s=${inputValue}&mfn_livesearch&searchpage${postType}`,
Li: ()=> document.createElement("li"),
Heading: (post)=> post.querySelector('.post-title'),
Link:  (post)=> post.querySelector('.post-title a'),
Excerpt: (post)=> post.querySelector('.post-excerpt p'),
WooPrice: (post)=> post.querySelector('.post-product-price'),
Image(post){
const imgDom=post.querySelector('.live-search-thumbnail');
if(imgDom){
let imgDomCreate=document.createElement('img');
imgDomCreate.src=imgDom.src;
return imgDomCreate;
}},
Category(post){
switch(true){
case post.classList.contains('product'):
return 'product';
case post.classList.contains('page'):
return 'page';
case post.classList.contains('portfolio'):
return 'portfolio';
case post.classList.contains('post'):
return 'post';
}},
Textbox(heading, link, excerpt, wooPrice){
let headingCreate=document.createElement('a');
let excerptCreate=document.createElement("p");
let wooPriceCreate=document.createElement("span");
let container=document.createElement("div");
container.classList.add("mfn-live-search-texts");
if(heading.textContent&&link.href){
headingCreate.innerHTML=heading.textContent;
headingCreate.href=link.href;
container.appendChild(headingCreate);
}
if(wooPrice){
wooPriceCreate.classList.add("mfn-ls-price");
wooPriceCreate.innerHTML=wooPrice.innerHTML;
container.appendChild(wooPriceCreate);
}
if(excerpt!=null&&excerpt.textContent.match(/\w/)){
let finalExcerpt='';
const letterLimit=90;
const sentence=excerpt.innerHTML;
if(letterLimit >=sentence.length){
finalExcerpt=sentence;
}else{
finalExcerpt=`${sentence.substr(0, sentence.lastIndexOf(' ', letterLimit))}...`;
}
excerptCreate.innerHTML=finalExcerpt;
container.appendChild(excerptCreate);
}
return container;
},
readyList(e){
var that=this.that;
let loadPostsAmount=mfn.livesearch.loadPosts;
let remotePageSource=that.Mfn_livesearch.dom.resultsFromPage;
remotePageSource=jQuery(remotePageSource).find('.posts_group');
if(remotePageSource.length){
const [{ children: posts }]=remotePageSource;
Array.from(posts).forEach(post=> {
if(loadPostsAmount > 0){
let Li=this.Li();
this.postId=post.id.match(/\d+/g).toString();
if(_.isObject(this.Image(post))) Li.appendChild(this.Image(post) );
const textbox=this.Textbox(this.Heading(post), this.Link(post), this.Excerpt(post), this.WooPrice(post));
Li.setAttribute('data-category', this.Category(post));
Li.appendChild(textbox);
that.Mfn_livesearch.postsLoaded.push(Li);
}
loadPostsAmount--;
});
}else if(e.target.value.length&&!that.Mfn_livesearch.postsLoaded.length){
jQuery(that.Mfn_livesearch.dom.liveSearchNoResults).fadeIn();
}},
categoryPills(actualInput){
var that=this.that;
if(mfn_livesearch_categories){
const regex=new RegExp(`[a-zA-Z]*${actualInput}[a-zA-Z]*`, 'gi');
const similarResults=Object.values(mfn_livesearch_categories).filter(function(category){
return category.match(regex);
});
similarResults.forEach(category=> {
let Li=this.Li();
Li.setAttribute('data-category', 'category');
let text=document.createElement('a');
text.innerHTML=category;
text.href=Object.keys(mfn_livesearch_categories).find(key=> mfn_livesearch_categories[key]===category);
Li.appendChild(text);
that.Mfn_livesearch.postsLoaded.push(Li);
});
}}
},
ajaxSearch(that, e){
let howManyChars=that.Mfn_livesearch.dom.isItem(e)!==null ? that.Mfn_livesearch.dom.itemAttr('char'):mfn.livesearch.minChar;
if(e.target.value.length >=howManyChars){
jQuery(that.Mfn_livesearch.dom.searchForm).addClass('mfn-livesearch-loading');
let trimmedSearchingSentence=e.target.value.trim();
jQuery.ajax({
url: this.Mfn_livesearch.create.linkToLivesearch(trimmedSearchingSentence),
type: "GET",
success: function (response){
that.Mfn_livesearch.dom.ajaxFetchedPage=response;
setTimeout(function(){
jQuery(that.Mfn_livesearch.dom.searchForm).removeClass('mfn-livesearch-loading');
that.Mfn_livesearch.postsLoaded=[];
jQuery(that.Mfn_livesearch.dom.liveSearchNoResults).fadeOut();
that.Mfn_livesearch.create.categoryPills(e.target.value);
that.Mfn_livesearch.create.readyList(e);
that.Mfn_livesearch.refreshCategoryContainers();
that.Mfn_livesearch.assignToProperContainer(that.Mfn_livesearch.postsLoaded);
that.Mfn_livesearch.hideNotUsedCategories();
that.Mfn_livesearch.toggleDropdown(e);
that.Mfn_livesearch.toggleMoreResultsButton(e);
}, 0);
}});
}else{
that.Mfn_livesearch.toggleDropdown(e);
}},
refreshCategoryContainers(){
const containers=this.that.Mfn_livesearch.dom;
const { pages, categories, portfolio, post, products }=mfn.livesearch.translation;
jQuery(containers.liveSearchResultsListShop).html(`<ul><li class="mfn-live-search-heading" data-category="info"> ${products} </li></ul>`);
jQuery(containers.liveSearchResultsListPages).html(`<ul><li class="mfn-live-search-heading" data-category="info"> ${pages} </li></ul>`);
jQuery(containers.liveSearchResultsListPortfolio).html(`<ul><li class="mfn-live-search-heading" data-category="info"> ${portfolio} </li></ul>`);
jQuery(containers.liveSearchResultsListBlog).html(`<ul><li class="mfn-live-search-heading" data-category="info"> ${post} </li></ul>`);
jQuery(containers.liveSearchResultsListCategories).html(`<ul><li class="mfn-live-search-heading" data-category="info"> ${categories} </li></ul>`);
},
assignToProperContainer(posts){
var that=this.that;
posts.forEach(post=>{
switch(post.getAttribute('data-category')){
case 'product':
jQuery(that.Mfn_livesearch.dom.liveSearchResultsListShop).children('ul').append(post);
break;
case 'page':
jQuery(that.Mfn_livesearch.dom.liveSearchResultsListPages).children('ul').append(post);
break;
case 'portfolio':
jQuery(that.Mfn_livesearch.dom.liveSearchResultsListPortfolio).children('ul').append(post);
break;
case 'post':
jQuery(that.Mfn_livesearch.dom.liveSearchResultsListBlog).children('ul').append(post);
break;
case 'category':
jQuery(that.Mfn_livesearch.dom.liveSearchResultsListCategories).children('ul').append(post);
break;
}});
},
hideNotUsedCategories(){
var that=this.that;
that.Mfn_livesearch.dom.liveSearchResultsList.forEach(resultsList=>{
Array.from(resultsList.children).forEach(category=> {
let content=category.querySelectorAll('ul li[data-category]');
if(content.length===1){
category.style.display="none";
}else{
category.style.display="block";
}});
});
},
toggleDropdown(e){
let focusedSearchBox;
let howManyChars=this.that.Mfn_livesearch.dom.isItem(e)!==null ? this.that.Mfn_livesearch.dom.itemAttr('char'):mfn.livesearch.minChar;
if(this.dom.isItem(e)){
focusedSearchBox=document.querySelector('.mfn-live-search-wrapper .mfn-live-search-box');
if(!this.dom.itemAttr('featured')){
const featuredImages=document.querySelectorAll('.mfn-live-search-wrapper img');
Array.from(featuredImages).forEach(image=> {
image.style.display='none';
});
}}else if(document.querySelector('#Side_slide')&&document.querySelector('#Side_slide').style.right==='0px'){
focusedSearchBox=document.querySelector('#Side_slide .mfn-live-search-box');
}else if(document.querySelector('.search_wrapper')&&document.querySelector('.search_wrapper').style.display==='block'){
focusedSearchBox=document.querySelector('.search_wrapper .mfn-live-search-box');
}else if(document.querySelector('.mfn-header-tmpl')){
focusedSearchBox=jQuery('.search_wrapper input:focus').closest('.search_wrapper').find('.mfn-live-search-box');
jQuery('.search_wrapper input:focus').closest('.mcb-wrap').css('z-index', 3);
}else{
focusedSearchBox=document.querySelector('.top_bar_right .mfn-live-search-box');
}
if(e.target.value.length < howManyChars){
return jQuery(focusedSearchBox).slideUp(300);
}
jQuery(focusedSearchBox).slideDown(300);
},
toggleMoreResultsButton(e){
this.dom.liveSearchBox.forEach(searchBox=> {
const getMoreResultsButton=searchBox.querySelector('a.button');
const howManyItems=mfn.livesearch.loadPosts;
var postType='';
if(this.postsLoaded.length >=howManyItems&&this.postsLoaded.length){
getMoreResultsButton.classList.remove('hidden');
if(e.target.parentNode.querySelector('input[name="post_type"]')){
postType='&post_type=' + e.target.parentNode.querySelector('input[name="post_type"]').value;
}
getMoreResultsButton.href=this.create.linkToLivesearch2(e.target.value, postType);
}else{
getMoreResultsButton.classList.add('hidden');
}});
},
closeBoxOnClick(){
this.dom.searchForm.forEach((x)=> {
if(jQuery(x).siblings('.mfn-live-search-box').css('display')!=='none'||jQuery(x).closest('.mfn-loaded').length){
x.setAttribute('mfn-livesearch-dropdown', true);
}else{
x.setAttribute('mfn-livesearch-dropdown', false);
}})
let item=jQuery('[mfn-livesearch-dropdown=true]');
if(item.closest('.mfn-loaded').length){
jQuery(item).find('.icon_close').click();
item.attr('mfn-livesearch-dropdown', false);
}else{
jQuery(item).find('.mfn-live-search-box').fadeOut(300);
jQuery(item).siblings('.mfn-live-search-box').fadeOut(300);
}
this.refreshCategoryContainers();
this.hideNotUsedCategories();
item.siblings('.mfn-live-search-box').find('.button').addClass('hidden');
item.closest('.mcb-wrap').css('z-index', 2);
},
events(){
var inputDebounce=_.debounce(this.ajaxSearch, 300);
this.dom.searchForm.forEach(searchForm=> {
searchForm.addEventListener("submit", (e)=> {
e.preventDefault();
var postType='';
const { value }=e.target.querySelector('.field');
if(e.target.querySelector('input[name="post_type"]')){
postType='&post_type=' + e.target.querySelector('input[name="post_type"]').value;
}
window.location.href=this.create.linkToLivesearch2(value, postType);
});
});
document.addEventListener("keyup", (e)=> {
if(e.key==='Escape'){
this.closeBoxOnClick();
e.stopPropagation();
}});
this.dom.searchField.forEach(searchField=> {
searchField.addEventListener("click", (e)=> { inputDebounce(this.that, e); });
searchField.addEventListener("input", (e)=> inputDebounce(this.that, e) );
searchField.addEventListener("click", (e)=> {
if(this.that.Mfn_livesearch.dom.isItem(e)){
jQuery(".search_wrapper .mfn-live-search-box, .top_bar_right .mfn-live-search-box, #Side_slide .mfn-live-search-box").slideUp(300);
}else{
jQuery(".mfn-live-search-wrapper .mfn-live-search-box").slideUp(300);
e.stopPropagation();
}});
});
jQuery('.mfn-live-search-box').on('click', 'li[data-category]', function(){
let a=jQuery(this).find('a');
if(a.length){
window.location.href=a.attr('href');
}});
this.dom.liveSearchBox.forEach(searchBox=> {
searchBox.addEventListener("click", (e)=> e.stopPropagation());
});
document.addEventListener("click", (e)=> {
this.closeBoxOnClick();
});
},
init(){
this.events();
}};
Mfn_livesearch.init();