File size: 9,973 Bytes
91073d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
document.addEventListener('DOMContentLoaded', function() {
    try {
        // Quote functionality
        setupQuoteButtons();
        
        // Reaction buttons
        setupReactionButtons();
        
        // Topic lock/pin confirmations
        setupModeratorActions();
        
        // Report form
        setupReportForms();
        
        // Search form validation
        setupSearchForm();
        
        // Confirmations for delete actions
        setupDeleteConfirmations();
    } catch (error) {
        console.log("Une erreur s'est produite lors de l'initialisation du JavaScript:", error);
    }
});

/**
 * Setup functionality for post quoting 
 */
function setupQuoteButtons() {
    const quoteButtons = document.querySelectorAll('.quote-button');
    
    quoteButtons.forEach(button => {
        button.addEventListener('click', function(e) {
            e.preventDefault();
            
            // If user has selected text, we'll quote only that part
            const selection = window.getSelection();
            if (selection && selection.toString().trim().length > 0) {
                // Get the selected text
                const selectedText = selection.toString().trim();
                
                // Get the post content element
                const postContent = this.closest('.post-content');
                if (postContent) {
                    // Get post author
                    const authorElement = this.closest('.post').querySelector('.post-author');
                    const author = authorElement ? authorElement.textContent.trim() : 'Someone';
                    
                    // Create a quote with the selected text
                    const quoteContent = `<blockquote><p>${selectedText}</p><footer>Posted by ${author}</footer></blockquote><p></p>`;
                    
                    // If we're on the topic page with a reply form
                    const replyForm = document.getElementById('reply-form');
                    if (replyForm) {
                        const textarea = replyForm.querySelector('textarea');
                        if (textarea) {
                            textarea.value += quoteContent;
                            textarea.focus();
                            // Scroll to the form
                            replyForm.scrollIntoView({ behavior: 'smooth' });
                        }
                    } else {
                        // We're not on a page with a reply form, store in session and redirect
                        sessionStorage.setItem('quoteContent', quoteContent);
                        window.location.href = this.getAttribute('href');
                    }
                }
            } else {
                // No text selected, just follow the link
                window.location.href = this.getAttribute('href');
            }
        });
    });
    
    // Check if we have stored quote content when loading a reply page
    const storedQuote = sessionStorage.getItem('quoteContent');
    if (storedQuote) {
        const textarea = document.querySelector('textarea[name="content"]');
        if (textarea) {
            textarea.value = storedQuote;
            textarea.focus();
            // Position cursor at the end
            textarea.selectionStart = textarea.selectionEnd = textarea.value.length;
        }
        // Clear the stored quote
        sessionStorage.removeItem('quoteContent');
    }
}

/**
 * Setup functionality for post reactions
 */
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');
            
            // Send AJAX request
            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 => {
                // Update the button state
                if (data.status === 'added' || data.status === 'updated') {
                    // Add active class to this button, remove from others
                    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');
                }
                
                // Update count
                if (countElement) {
                    countElement.textContent = data.count;
                    
                    // Hide count if zero
                    if (data.count === 0) {
                        countElement.classList.add('hidden');
                    } else {
                        countElement.classList.remove('hidden');
                    }
                }
            })
            .catch(error => {
                console.error('Error:', error);
            });
        });
    });
}

/**
 * Setup confirmation dialogs for moderator actions
 */
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();
            }
        });
    }
}

/**
 * Setup report forms
 */
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');
    
    // Show modal on report button click
    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;
            }
            
            // Set the appropriate ID in the form
            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 = '';
            }
            
            // Show the modal
            reportModal.classList.remove('hidden');
        });
    });
    
    // Close modal on close button click
    closeModalButtons.forEach(button => {
        button.addEventListener('click', function() {
            reportModal.classList.add('hidden');
        });
    });
    
    // Close modal when clicking outside
    reportModal.addEventListener('click', function(e) {
        if (e.target === reportModal) {
            reportModal.classList.add('hidden');
        }
    });
    
    // Validate report form
    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)');
            }
        });
    }
}

/**
 * Setup search form validation
 */
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');
            }
        });
    }
}

/**
 * Setup confirmation dialogs for delete actions
 */
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();
            }
        });
    });
}

/**
 * Get CSRF token from meta tag
 */
function getCsrfToken() {
    return document.querySelector('meta[name="csrf-token"]').getAttribute('content');
}