${parentId ? `` : ''}
`;
}
setupDelegatedEvents() {
console.log('[DEBUG] Setting up delegated events for container:', this.container);
if (!this.container) return;
// Ctrl+Enter to submit comment
this.container.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && e.ctrlKey) {
const textarea = e.target.closest('textarea');
if (!textarea) return;
const wrap = textarea.closest('.comment-input');
if (!wrap) return;
const submitBtn = wrap.querySelector('.submit-comment');
if (submitBtn) submitBtn.click();
} else if (e.key === 'Escape') {
const textarea = e.target.closest('textarea');
if (textarea) textarea.blur();
}
});
// Single Change Listener for Sort
this.container.addEventListener('change', (e) => {
if (e.target.id === 'comment-sort') {
this.sort = e.target.value;
this.loadComments();
}
});
// Single Click Listener for Everything
this.container.addEventListener('click', async (e) => {
console.log('[DEBUG] Click on container:', e.target);
const target = e.target;
// Handle legacy layout focus scroll
if (target.matches('textarea') && (document.body.classList.contains('legacy-view') || document.body.classList.contains('layout-legacy'))) {
target.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
// Toggling Scroll Action
const scrollBtn = target.closest('.scroll-to-bottom');
if (scrollBtn) {
if (scrollBtn.classList.contains('is-at-bottom')) {
// Scroll to Top of the page
window.scrollTo({
top: 0,
behavior: 'smooth'
});
} else {
// Scroll to Bottom of comments
const bottomElement = this.container.querySelector('.main-input') || this.container.querySelector('.lock-notice') || this.container.querySelector('.login-placeholder');
if (bottomElement) {
bottomElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
} else {
this.container.scrollIntoView({ behavior: 'smooth', block: 'end' });
}
}
return;
}
// Submit Comment
if (target.matches('.submit-comment')) {
this.handleSubmit(e);
return;
}
// Cancel Reply
if (target.matches('.cancel-reply')) {
const form = target.closest('.reply-input');
if (form) form.remove();
return;
}
// User Delete
const delBtn = target.closest('.delete-btn');
if (delBtn) {
if (delBtn.dataset.confirming !== 'true') {
delBtn.dataset.confirming = 'true';
const originalText = delBtn.innerHTML;
delBtn.innerHTML = 'Sure?';
delBtn.classList.add('btn-danger'); // Optional styling
setTimeout(() => {
delBtn.dataset.confirming = 'false';
delBtn.innerHTML = originalText;
delBtn.classList.remove('btn-danger');
}, 3000);
return;
}
const id = delBtn.dataset.id;
const res = await fetch(`/api/comments/${id}/delete`, {
method: 'POST',
headers: { 'X-CSRF-Token': window.f0ckSession?.csrf_token }
});
const json = await res.json();
if (json.success) this.loadComments();
else alert('Failed to delete: ' + (json.message || 'Error'));
return;
}
// Admin Delete
const adminDelBtn = target.closest('.admin-delete-btn');
if (adminDelBtn) {
if (typeof ModAction === 'undefined') return alert('Error: ModAction module not loaded. Are you a moderator?');
const id = adminDelBtn.dataset.id;
ModAction.confirm('Delete Comment', `Are you sure you want to delete comment