document.addEventListener('DOMContentLoaded', () => {
// simple fallback in case wp.i18n isn't available for any reason
const __ = (typeof wp !== 'undefined' && wp.i18n && wp.i18n.__) ? wp.i18n.__ : (s => s);
const commentsButton = document.getElementById('comments_button');
const form = document.getElementById('respond');
if (commentsButton && form) {
commentsButton.addEventListener('click', () => {
form.classList.toggle('active');
});
}
const shareLink = document.getElementById('share-link');
if (shareLink) {
shareLink.addEventListener('click', async (event) => {
event.preventDefault();
const shareData = {
title: document.title,
text: __('Check this out:', 'rentcaffeine2'),
url: window.location.href
};
if (navigator.share) {
try {
await navigator.share(shareData);
} catch (err) {
console.warn(__('Share cancelled or failed', 'rentcaffeine2'), err);
}
return;
}
const dummy = document.createElement('input');
dummy.value = window.location.href;
document.body.appendChild(dummy);
dummy.select();
document.execCommand('copy');
document.body.removeChild(dummy);
alert(__('Link copied to clipboard!', 'rentcaffeine2'));
});
}
/**
* Custom handler for Category Dropdown to avoid conflict with ysi.js
* We use a custom data attribute to opt-out of ysi.js processing.
*/
const catToggles = document.querySelectorAll('[data-toggle="rentcaffeine-dropdown"]');
catToggles.forEach(toggle => {
toggle.addEventListener('click', function(e) {
e.preventDefault();
e.stopPropagation();
const parent = this.closest('.dropdown');
const menu = parent.querySelector('.dropdown-menu');
if (!parent || !menu) return;
const isExpanded = this.getAttribute('aria-expanded') === 'true';
if (isExpanded) {
parent.classList.remove('show');
menu.classList.remove('show');
this.setAttribute('aria-expanded', 'false');
} else {
// Close other opened dropdowns first if necessary
document.querySelectorAll('.dropdown.rentcaffeine-category.show').forEach(openDropdown => {
if (openDropdown !== parent) {
openDropdown.classList.remove('show');
openDropdown.querySelector('.dropdown-menu').classList.remove('show');
openDropdown.querySelector('[data-toggle="rentcaffeine-dropdown"]').setAttribute('aria-expanded', 'false');
}
});
parent.classList.add('show');
menu.classList.add('show');
this.setAttribute('aria-expanded', 'true');
}
});
});
// Close dropdowns when clicking outside
document.addEventListener('click', function(e) {
const openDropdowns = document.querySelectorAll('.dropdown.rentcaffeine-category.show');
openDropdowns.forEach(dropdown => {
if (!dropdown.contains(e.target)) {
dropdown.classList.remove('show');
const menu = dropdown.querySelector('.dropdown-menu');
if (menu) menu.classList.remove('show');
const toggle = dropdown.querySelector('[data-toggle="rentcaffeine-dropdown"]');
if (toggle) toggle.setAttribute('aria-expanded', 'false');
}
});
});
});