(function($) {
console.log("Get FDat: Custom JS script loaded.");
// Ensure the DOM is ready before trying to manipulate elements
$(document).ready(function() {
console.log("Get FDat: DOM is ready.");
// --- IMPORTANT: Ensure RTCL's JS is recognized as active ---
// The presence of 'rtcl-no-js' indicates a potential issue with RTCL's core JS loading.
// We'll try to manually correct this, though ideally RTCL should manage this itself.
if ($('body').hasClass('rtcl-no-js')) {
$('body').removeClass('rtcl-no-js').addClass('rtcl-js');
console.warn("Get FDat: Manually removed 'rtcl-no-js' and added 'rtcl-js' to the body. Please check for underlying RTCL JavaScript loading issues if problems persist.");
}
// Selectors for the main search form and its elements (header)
const mainSearchForm = $('form.rtcl-widget-search-form');
const locationInputHeader = mainSearchForm.find('#rtc-geo-search');
const categorySelectHeader = mainSearchForm.find('select.rtcl-category-search[name="rtcl_category"]');
const subcategorySelectHeader = mainSearchForm.find('.rtcl-child-terms select.rtcl-category-search');
const searchButtonHeader = mainSearchForm.find('.rtin-search-btn, .rtcl-btn');
// Selectors for the sidebar filter form and its elements
const sidebarFilterForm = $('.rtcl-filter-form');
const sidebarLocationInput = sidebarFilterForm.find('#rtc-geo-search');
const sidebarCategorySelect = sidebarFilterForm.find('select.rtcl-category-search[name="rtcl_category"]');
const sidebarSubcategorySelect = sidebarFilterForm.find('.rtcl-child-terms select.rtcl-category-search');
const filterApplyButton = sidebarFilterForm.find('.rtcl-filter-btn'); // "Apply filters" button in sidebar
// Selector for the "Load More" button
const loadMoreButton = $('#el_load_more');
console.log("Get FDat: Selected elements status:", {
mainSearchFormFound: mainSearchForm.length > 0,
locationInputHeaderFound: locationInputHeader.length > 0,
categorySelectHeaderFound: categorySelectHeader.length > 0,
subcategorySelectHeaderFound: subcategorySelectHeader.length > 0,
searchButtonHeaderFound: searchButtonHeader.length > 0,
sidebarFilterFormFound: sidebarFilterForm.length > 0,
sidebarLocationInputFound: sidebarLocationInput.length > 0,
sidebarCategorySelectFound: sidebarCategorySelect.length > 0,
sidebarSubcategorySelectFound: sidebarSubcategorySelect.length > 0,
filterApplyButtonFound: filterApplyButton.length > 0,
loadMoreButtonFound: loadMoreButton.length > 0
});
// Function to trigger the loading state
function startLoadingState() {
if (!document.body.classList.contains('searching')) {
document.body.classList.add('searching');
// Disable all relevant buttons and inputs
searchButtonHeader.prop('disabled', true).addClass('loading');
locationInputHeader.prop('disabled', true);
categorySelectHeader.prop('disabled', true);
subcategorySelectHeader.prop('disabled', true);
filterApplyButton.prop('disabled', true).addClass('loading');
sidebarLocationInput.prop('disabled', true);
sidebarCategorySelect.prop('disabled', true);
sidebarSubcategorySelect.prop('disabled', true);
loadMoreButton.prop('disabled', true).addClass('loading');
console.log("Get FDat: Loading state started. 'searching' class added.");
} else {
console.log("Get FDat: Loading state already active.");
}
}
// Function to end the loading state
function endLoadingState() {
if (document.body.classList.contains('searching')) {
document.body.classList.remove('searching');
// Re-enable all relevant buttons and inputs
searchButtonHeader.prop('disabled', false).removeClass('loading');
locationInputHeader.prop('disabled', false);
categorySelectHeader.prop('disabled', false);
subcategorySelectHeader.prop('disabled', false);
filterApplyButton.prop('disabled', false).removeClass('loading');
sidebarLocationInput.prop('disabled', false);
sidebarCategorySelect.prop('disabled', false);
sidebarSubcategorySelect.prop('disabled', false);
loadMoreButton.prop('disabled', false).removeClass('loading');
console.log("Get FDat: Loading state ended. 'searching' class removed.");
} else {
console.log("Get FDat: Loading state not active, no need to remove.");
}
}
// --- Event Listeners for the main search form (header) ---
if (mainSearchForm.length) {
mainSearchForm.on('submit', function(event) {
console.log("Get FDat: Main search form submitted.");
event.preventDefault(); // Prevent default form submission
startLoadingState();
// The global ajaxStart/ajaxStop handlers should pick up RTCL's AJAX.
});
}
// --- Event Listeners for sidebar filter form ---
if (sidebarFilterForm.length) {
// Listen for changes on filter inputs/selects within the sidebar form
// Using event delegation on the form itself for robustness
sidebarFilterForm.on('change', '#rtc-geo-search, select.rtcl-category-search, input[type="radio"][name^="filters[ad_type]"]', function() {
console.log("Get FDat: Sidebar filter input/select changed:", this.id || this.name);
startLoadingState();
// RTCL's own JS will likely trigger the AJAX search after this change.
});
// Listen for submission of the sidebar filter form (e.g., by pressing Enter or clicking a submit button)
sidebarFilterForm.on('submit', function(event) {
console.log("Get FDat: Sidebar filter form submitted.");
event.preventDefault(); // Prevent default form submission
startLoadingState();
// RTCL's own JS will likely trigger the AJAX submission after this.
});
// Handle click on the "Apply filters" button in the sidebar
if (filterApplyButton.length) {
filterApplyButton.on('click', function(event) {
console.log("Get FDat: 'Apply filters' button clicked.");
event.preventDefault(); // Prevent default button action if it's not already handled by RTCL
startLoadingState();
// RTCL's own JS will likely trigger the AJAX search after this.
});
}
}
// --- Event Listener for "Load More" button ---
if (loadMoreButton.length) {
loadMoreButton.on('click', function() {
console.log("Get FDat: 'Load More' button clicked.");
startLoadingState();
// The global ajaxStart/ajaxStop handlers should pick up RTCL's AJAX.
});
}
// --- Global AJAX Event Handlers ---
// These will catch any jQuery AJAX requests initiated by RTCL or other plugins.
// This is the most reliable way to detect when RTCL's dynamic content loading starts and stops.
$(document).ajaxStart(function() {
console.log("Get FDat: jQuery AJAX started.");
// Only start loading state if not already active to prevent flickering
if (!document.body.classList.contains('searching')) {
startLoadingState();
}
}).ajaxStop(function() {
console.log("Get FDat: jQuery AJAX stopped.");
// Ensure all AJAX requests are truly finished before removing the loading state.
// A small delay can prevent flickering if one AJAX call finishes just before another starts.
setTimeout(function() {
console.log("Get FDat: Checking loading state after AJAX stop delay.");
// Double-check if 'searching' class is still present before removing,
// in case another AJAX call started during the timeout.
if (document.body.classList.contains('searching')) {
endLoadingState();
}
}, 500); // Increased delay to 500ms for better visibility of spinner
});
});
})(jQuery);