|
document.addEventListener('DOMContentLoaded', function() { |
|
try { |
|
|
|
setupQuoteButtons(); |
|
|
|
|
|
setupReactionButtons(); |
|
|
|
|
|
setupModeratorActions(); |
|
|
|
|
|
setupReportForms(); |
|
|
|
|
|
setupSearchForm(); |
|
|
|
|
|
setupDeleteConfirmations(); |
|
} catch (error) { |
|
console.log("Une erreur s'est produite lors de l'initialisation du JavaScript:", error); |
|
} |
|
}); |
|
|
|
|
|
|
|
|
|
function setupQuoteButtons() { |
|
const quoteButtons = document.querySelectorAll('.quote-button'); |
|
|
|
quoteButtons.forEach(button => { |
|
button.addEventListener('click', function(e) { |
|
e.preventDefault(); |
|
|
|
|
|
const selection = window.getSelection(); |
|
if (selection && selection.toString().trim().length > 0) { |
|
|
|
const selectedText = selection.toString().trim(); |
|
|
|
|
|
const postContent = this.closest('.post-content'); |
|
if (postContent) { |
|
|
|
const authorElement = this.closest('.post').querySelector('.post-author'); |
|
const author = authorElement ? authorElement.textContent.trim() : 'Someone'; |
|
|
|
|
|
const quoteContent = `<blockquote><p>${selectedText}</p><footer>Posted by ${author}</footer></blockquote><p></p>`; |
|
|
|
|
|
const replyForm = document.getElementById('reply-form'); |
|
if (replyForm) { |
|
const textarea = replyForm.querySelector('textarea'); |
|
if (textarea) { |
|
textarea.value += quoteContent; |
|
textarea.focus(); |
|
|
|
replyForm.scrollIntoView({ behavior: 'smooth' }); |
|
} |
|
} else { |
|
|
|
sessionStorage.setItem('quoteContent', quoteContent); |
|
window.location.href = this.getAttribute('href'); |
|
} |
|
} |
|
} else { |
|
|
|
window.location.href = this.getAttribute('href'); |
|
} |
|
}); |
|
}); |
|
|
|
|
|
const storedQuote = sessionStorage.getItem('quoteContent'); |
|
if (storedQuote) { |
|
const textarea = document.querySelector('textarea[name="content"]'); |
|
if (textarea) { |
|
textarea.value = storedQuote; |
|
textarea.focus(); |
|
|
|
textarea.selectionStart = textarea.selectionEnd = textarea.value.length; |
|
} |
|
|
|
sessionStorage.removeItem('quoteContent'); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
function setupReactionButtons() { |
|
const reactionButtons = document.querySelectorAll('.reaction-btn'); |
|
|
|
reactionButtons.forEach(button => { |
|
button.addEventListener('click', function(e) { |
|
e.preventDefault(); |
|
|
|
if (!document.body.classList.contains('logged-in')) { |
|
alert('Vous devez être connecté pour réagir aux publications'); |
|
return; |
|
} |
|
|
|
const postId = this.dataset.postId; |
|
const reactionType = this.dataset.reactionType; |
|
const countElement = this.querySelector('.reaction-count'); |
|
|
|
|
|
fetch(`/post/${postId}/react`, { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/x-www-form-urlencoded', |
|
'X-CSRFToken': getCsrfToken() |
|
}, |
|
body: `reaction_type=${reactionType}` |
|
}) |
|
.then(response => response.json()) |
|
.then(data => { |
|
|
|
if (data.status === 'added' || data.status === 'updated') { |
|
|
|
const siblingButtons = this.parentNode.querySelectorAll('.reaction-btn'); |
|
siblingButtons.forEach(btn => btn.classList.remove('active')); |
|
this.classList.add('active'); |
|
} else if (data.status === 'removed') { |
|
this.classList.remove('active'); |
|
} |
|
|
|
|
|
if (countElement) { |
|
countElement.textContent = data.count; |
|
|
|
|
|
if (data.count === 0) { |
|
countElement.classList.add('hidden'); |
|
} else { |
|
countElement.classList.remove('hidden'); |
|
} |
|
} |
|
}) |
|
.catch(error => { |
|
console.error('Error:', error); |
|
}); |
|
}); |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
function setupModeratorActions() { |
|
const lockButton = document.getElementById('lock-topic-btn'); |
|
const pinButton = document.getElementById('pin-topic-btn'); |
|
|
|
if (lockButton) { |
|
lockButton.addEventListener('click', function(e) { |
|
const isLocked = this.dataset.isLocked === 'true'; |
|
const action = isLocked ? 'déverrouiller' : 'verrouiller'; |
|
|
|
if (!confirm(`Êtes-vous sûr de vouloir ${action} ce sujet ?`)) { |
|
e.preventDefault(); |
|
} |
|
}); |
|
} |
|
|
|
if (pinButton) { |
|
pinButton.addEventListener('click', function(e) { |
|
const isPinned = this.dataset.isPinned === 'true'; |
|
const action = isPinned ? 'détacher' : 'épingler'; |
|
|
|
if (!confirm(`Êtes-vous sûr de vouloir ${action} ce sujet ?`)) { |
|
e.preventDefault(); |
|
} |
|
}); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
function setupReportForms() { |
|
const reportButtons = document.querySelectorAll('.report-button'); |
|
const reportModal = document.getElementById('report-modal'); |
|
const reportForm = document.getElementById('report-form'); |
|
const closeModalButtons = document.querySelectorAll('.close-modal'); |
|
|
|
|
|
reportButtons.forEach(button => { |
|
button.addEventListener('click', function(e) { |
|
e.preventDefault(); |
|
|
|
if (!document.body.classList.contains('logged-in')) { |
|
alert('Vous devez être connecté pour signaler ce contenu'); |
|
return; |
|
} |
|
|
|
|
|
const postId = this.dataset.postId; |
|
const topicId = this.dataset.topicId; |
|
|
|
if (postId) { |
|
document.getElementById('post_id').value = postId; |
|
document.getElementById('topic_id').value = ''; |
|
} else if (topicId) { |
|
document.getElementById('topic_id').value = topicId; |
|
document.getElementById('post_id').value = ''; |
|
} |
|
|
|
|
|
reportModal.classList.remove('hidden'); |
|
}); |
|
}); |
|
|
|
|
|
closeModalButtons.forEach(button => { |
|
button.addEventListener('click', function() { |
|
reportModal.classList.add('hidden'); |
|
}); |
|
}); |
|
|
|
|
|
reportModal.addEventListener('click', function(e) { |
|
if (e.target === reportModal) { |
|
reportModal.classList.add('hidden'); |
|
} |
|
}); |
|
|
|
|
|
if (reportForm) { |
|
reportForm.addEventListener('submit', function(e) { |
|
const reasonField = document.getElementById('reason'); |
|
if (reasonField.value.trim().length < 10) { |
|
e.preventDefault(); |
|
alert('Veuillez fournir une raison détaillée pour votre signalement (au moins 10 caractères)'); |
|
} |
|
}); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
function setupSearchForm() { |
|
const searchForm = document.getElementById('search-form'); |
|
|
|
if (searchForm) { |
|
searchForm.addEventListener('submit', function(e) { |
|
const searchInput = document.getElementById('search-input'); |
|
if (searchInput.value.trim().length < 3) { |
|
e.preventDefault(); |
|
alert('La recherche doit contenir au moins 3 caractères'); |
|
} |
|
}); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
function setupDeleteConfirmations() { |
|
const deleteButtons = document.querySelectorAll('.delete-button'); |
|
|
|
deleteButtons.forEach(button => { |
|
button.addEventListener('click', function(e) { |
|
if (!confirm('Êtes-vous sûr de vouloir supprimer cet élément ? Cette action est irréversible.')) { |
|
e.preventDefault(); |
|
} |
|
}); |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
function getCsrfToken() { |
|
return document.querySelector('meta[name="csrf-token"]').getAttribute('content'); |
|
} |
|
|