(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
// Based on the 'all-ads' HTML structure
const mainSearchForm = $('form.rtcl-widget-search-form'); // Main search form in header
const locationInput = mainSearchForm.find('#rtc-geo-search'); // Location input in header form
const categorySelect = mainSearchForm.find('select.rtcl-category-search[name="rtcl_category"]'); // Category select in header form
const subcategorySelect = mainSearchForm.find('.rtcl-child-terms select.rtcl-category-search'); // Subcategory select in header form
const searchButton = mainSearchForm.find('.rtin-search-btn, .rtcl-btn'); // Search button in header form
// Selectors for the sidebar filter form and its elements
const sidebarFilterForm = $('.rtcl-filter-form'); // Sidebar filter form
const sidebarLocationInput = sidebarFilterForm.find('#rtc-geo-search'); // Location input in sidebar form
const sidebarCategorySelect = sidebarFilterForm.find('select.rtcl-category-search[name="rtcl_category"]'); // Category select in sidebar form
const sidebarSubcategorySelect = sidebarFilterForm.find('.rtcl-child-terms select.rtcl-category-search'); // Subcategory select in sidebar form
const filterApplyButton = sidebarFilterForm.find('.rtcl-filter-btn'); // "Apply filters" button in sidebar
console.log("Get FDat: Selected elements status:", {
mainSearchFormFound: mainSearchForm.length > 0,
locationInputFound: locationInput.length > 0,
categorySelectFound: categorySelect.length > 0,
subcategorySelectFound: subcategorySelect.length > 0,
searchButtonFound: searchButton.length > 0,
sidebarFilterFormFound: sidebarFilterForm.length > 0,
sidebarLocationInputFound: sidebarLocationInput.length > 0,
sidebarCategorySelectFound: sidebarCategorySelect.length > 0,
sidebarSubcategorySelectFound: sidebarSubcategorySelect.length > 0,
filterApplyButtonFound: filterApplyButton.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
searchButton.prop('disabled', true).addClass('loading');
locationInput.prop('disabled', true);
categorySelect.prop('disabled', true);
subcategorySelect.prop('disabled', true);
filterApplyButton.prop('disabled', true).addClass('loading');
sidebarLocationInput.prop('disabled', true);
sidebarCategorySelect.prop('disabled', true);
sidebarSubcategorySelect.prop('disabled', true);
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
searchButton.prop('disabled', false).removeClass('loading');
locationInput.prop('disabled', false);
categorySelect.prop('disabled', false);
subcategorySelect.prop('disabled', false);
filterApplyButton.prop('disabled', false).removeClass('loading');
sidebarLocationInput.prop('disabled', false);
sidebarCategorySelect.prop('disabled', false);
sidebarSubcategorySelect.prop('disabled', false);
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();
// Allow RTCL's own JS to handle the AJAX submission after this.
});
}
// --- Event Listeners for sidebar filter form ---
if (sidebarFilterForm.length) {
sidebarFilterForm.on('submit', function(event) {
console.log("Get FDat: Sidebar filter form submitted.");
event.preventDefault(); // Prevent default form submission
startLoadingState();
// Allow RTCL's own JS to handle the AJAX submission after this.
});
// Handle changes in filter dropdowns/inputs within the sidebar form
$(document).on('change', '.rtcl-filter-form #rtc-geo-search, .rtcl-filter-form select.rtcl-category-search', function() {
console.log("Get FDat: Sidebar filter dropdown/input changed:", this.id || this.name);
startLoadingState();
// RTCL's own JS will likely trigger the AJAX search after this change.
});
// Handle click on the "Apply filters" button in the sidebar
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();
// If RTCL has its own click handler for this button that triggers AJAX,
// the global ajaxStart/ajaxStop will manage the loading state.
// If not, you might need to manually trigger RTCL's filter application here.
// For now, we assume RTCL's JS will handle the actual filtering.
});
}
// --- 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);