"use strict";
document.addEventListener("DOMContentLoaded", function (){
const headers=document.querySelectorAll("section h3, section h4, section h5, section h6");
let activeContent=null;
let activeHeader=null;
function openContent(content){
content.classList.add("active");
content.style.maxHeight=content.scrollHeight + "px";
activeContent=content;
}
function closeContent(content){
content.style.maxHeight=content.scrollHeight + "px";
requestAnimationFrame(()=> {
content.style.maxHeight="0";
content.classList.remove("active");
});
activeContent=null;
}
headers.forEach(header=> {
header.addEventListener("click", function (){
const content=this.closest("section").querySelector(".saswp_faq_tiny_content");
if(activeContent&&activeContent!==content){
closeContent(activeContent);
if(activeHeader) activeHeader.classList.remove("active");
}
if(content.classList.contains("active")){
closeContent(content);
this.classList.remove("active");
activeHeader=null;
}else{
openContent(content);
this.classList.add("active");
activeHeader=this;
}});
});
const loadMoreBtn=document.querySelector(".load-more");
if(loadMoreBtn){
loadMoreBtn.addEventListener("click", function (event){
event.preventDefault();
let page=this.getAttribute("data-page") ? parseInt(this.getAttribute("data-page"), 10):2;
const formData=new FormData();
formData.append("action", "getposts");
formData.append("page", page);
formData.append("nonce", MyAjax.nonce);
this.setAttribute("data-page", page + 1);
this.classList.add("loading");
fetch("/wp-admin/admin-ajax.php", {
method: "POST",
body: formData,
credentials: "same-origin"
})
.then((response)=> {
if(!response.ok) throw new Error("Network response was not ok");
return response.json();
})
.then((resp)=> {
if(!resp||resp.success!==true||!resp.data){
throw new Error("Bad response structure");
}
const { html, total_pages, total_posts, page }=resp.data;
const postsContainer=document.querySelector(".posts-companies");
if(postsContainer&&html){
postsContainer.insertAdjacentHTML("beforeend", html);
this.classList.remove("loading");
}
if(page >=total_pages){
loadMoreBtn.style.display="none";
}else{
loadMoreBtn.setAttribute("data-page", page + 1);
}})
.catch((error)=> {
console.error("Ошибка:", error);
});
});
}
function findNextFieldBlock(el){
let node=el.parentElement;
while (node&&node.nextElementSibling){
node=node.nextElementSibling;
if(node.classList.contains('kadence-blocks-form-field')){
return node;
}}
return null;
}
document.querySelectorAll('.kb-radio-field').forEach(group=> {
const nextFieldBlock=findNextFieldBlock(group);
if(!nextFieldBlock) return;
const textarea=nextFieldBlock.querySelector('textarea.kb-textarea-field');
if(!textarea) return;
textarea.dataset.toggleByRadio='1';
textarea.style.display='none';
const radios=group.querySelectorAll('input[type="radio"]');
if(!radios.length) return;
const lastRadio=radios[radios.length - 1];
function updateVisibility(){
const checked=group.querySelector('input[type="radio"]:checked');
if(checked===lastRadio){
textarea.style.display='block';
}else{
textarea.style.display='none';
}}
radios.forEach(r=> r.addEventListener('change', updateVisibility));
updateVisibility();
});
});