jQuery(document).ready(function ($){
const $searchForm=$("#global-search-form");
if(!$searchForm.length){
return;
}
let currentRequest=null;
const $mobileFilterOverlay=$(".mobile-filter-overlay");
const $paginationControls=$("#pagination-controls");
const $globalSearchTextHeading=$("#global-search-text");
const $globalSearchTextInput=$("#item_text_search");
const $filteredItems=$(".filtered-items");
const $itemFilterContainer=$(".item-filter-container");
const filterVisibilityMapping={
all: ["#sdg-content"],
newsposts: ["#sdg-content", "#message-type-content"],
newsletter_post: [],
project: ["#sdg-content"],
event: ["#sdg-content", "#event-location-content"],
document: ["#sdg-content", "#medienart-content"]
};
const setActivePostTypeButton=()=> {
const selectedPostType=$('input[name="post_type"]:checked').val();
$(".post-type-button").removeClass("embla__slide--active");
$(`.post-type-button input[value="${selectedPostType}"]`).closest(".post-type-button").addClass("embla__slide--active");
};
const updateFilterVisibility=()=> {
const selectedPostType=$('input[name="post_type"]:checked').val();
const allFilterSelectors=["#sdg-content", "#medienart-content", "#event-location-content", "#message-type-content"];
allFilterSelectors.forEach((selector)=> {
$(selector).closest(".accordion__item").hide();
});
const visibleFilters=filterVisibilityMapping[selectedPostType]||[];
visibleFilters.forEach((selector)=> {
$(selector).closest(".accordion__item").show();
});
};
const ajaxSearch=(paged=1)=> {
const serializedData=$searchForm.serializeArray();
const formDataObj={};
serializedData.forEach((item)=> {
if(formDataObj[item.name]){
if(!Array.isArray(formDataObj[item.name])){
formDataObj[item.name]=[formDataObj[item.name]];
}
formDataObj[item.name].push(item.value);
}else{
formDataObj[item.name]=item.value;
}});
formDataObj.action="ajax_get_search_results";
formDataObj.nonce=global_search_params.nonce;
formDataObj.paged=paged;
const historyParams=new URLSearchParams(formDataObj);
historyParams.delete("action");
historyParams.delete("nonce");
historyParams.set("paged", paged);
const historyQueryString=historyParams.toString();
const newUrl=historyQueryString ? `${window.location.pathname}?${historyQueryString}`:window.location.pathname;
window.history.pushState({ path: newUrl }, "", newUrl);
if(currentRequest&&currentRequest.readyState!==4){
currentRequest.abort();
}
currentRequest=$.ajax({
url: global_search_params.ajax_url,
type: "GET",
dataType: "json",
data: formDataObj, // 'action', 'nonce', and 'paged' are included here
beforeSend: function (){
const searchText=`${window.rne_translations.common.global_search_text_translation} "${$globalSearchTextInput.val()}"`;
$globalSearchTextHeading.text(searchText);
$filteredItems.html('<div class="loading-spinner"></div>');
setActivePostTypeButton();
updateFilterVisibility();
$paginationControls.hide();
},
success: function (response){
$filteredItems.html(response.content);
$paginationControls.html(response.pagination).show();
updateFilterVisibility();
if(window.PlyrInit&&typeof window.PlyrInit.init==="function"){
window.PlyrInit.init();
}},
error: function (jqXHR, textStatus){
if(textStatus!=="abort"){
$paginationControls.show();
$filteredItems.html("<p>Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut.</p>");
}}
});
};
const isMobileMenuOpen=()=> {
return $mobileFilterOverlay.attr("data-open")==="true";
};
const handleFormSubmit=(e)=> {
e.preventDefault();
ajaxSearch();
};
const handleFormChange=(e)=> {
const $target=$(e.target);
const inputName=$target.attr("name");
const targetNames=["audio", "pdf", "video"];
if(targetNames.includes(inputName)){
$target.closest(".accordion__header").next(".accordion__content").find("input[type=checkbox]").prop("checked", false);
}
if($target.closest(".term-checkbox").length){
$target.closest(".accordion__item").find(".accordion__header input[type=checkbox]").prop("checked", true);
}
if(!isMobileMenuOpen()){
ajaxSearch();
}};
const handlePaginationClick=function (e){
e.preventDefault();
let paged=1;
if($(this).data("page")){
paged=$(this).data("page");
}else{
const href=$(this).attr("href");
const match=href.match(/paged=([0-9]+)/);
if(match&&match[1]){
paged=parseInt(match[1], 10);
}}
ajaxSearch(paged);
const formOffset=$searchForm.offset().top;
$("html, body").animate({ scrollTop: formOffset }, 500);
};
const handleMobileApplyButtonClick=function (e){
e.preventDefault();
ajaxSearch();
$mobileFilterOverlay.attr("data-open", "false");
$mobileFilterOverlay.removeClass("open");
$("body").css("overflow", "auto");
};
const initEventListeners=()=> {
$searchForm.on("submit", handleFormSubmit);
$searchForm.on("change rne-select-change", "input, select", handleFormChange);
$paginationControls.on("click", "a", handlePaginationClick);
$(".item-search-button")
.not(".item-search-button--mobile")
.on("click", function (e){
e.preventDefault();
ajaxSearch();
});
$(".item-search-button--mobile").on("click", handleMobileApplyButtonClick);
if($mobileFilterOverlay.length){
window.bindMobileFilterMenuEvents($searchForm[0], $mobileFilterOverlay[0]);
}};
const handlePopState=(event)=> {
if(event.state){
location.reload();
}};
const init=()=> {
initEventListeners();
setActivePostTypeButton();
updateFilterVisibility();
window.onpopstate=handlePopState;
};
init();
});