File size: 23,328 Bytes
da893ba
a1be761
da893ba
 
a1be761
da893ba
a1be761
da893ba
 
a1be761
 
da893ba
 
 
 
 
 
 
 
 
 
 
a1be761
 
da893ba
 
 
 
 
 
 
 
a1be761
da893ba
a1be761
 
da893ba
a1be761
 
da893ba
 
 
a1be761
 
da893ba
 
 
 
 
a1be761
 
 
da893ba
 
a1be761
da893ba
a1be761
 
da893ba
 
a1be761
da893ba
 
 
a1be761
 
 
da893ba
 
 
 
a1be761
 
da893ba
 
a1be761
da893ba
a1be761
 
da893ba
a1be761
 
 
 
da893ba
 
 
 
 
a1be761
 
 
da893ba
 
a1be761
 
 
da893ba
 
 
 
 
 
 
a1be761
da893ba
a1be761
 
da893ba
a1be761
 
 
da893ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
da893ba
 
 
 
 
 
 
 
 
 
 
 
a1be761
da893ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
 
 
da893ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
 
 
da893ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
da893ba
 
a1be761
 
 
da893ba
 
 
 
 
 
 
 
a1be761
da893ba
 
a1be761
 
 
da893ba
 
a1be761
 
 
da893ba
 
 
 
 
 
 
 
 
a1be761
da893ba
 
a1be761
da893ba
 
 
 
 
 
a1be761
da893ba
 
 
 
a1be761
da893ba
 
 
a1be761
 
 
da893ba
 
 
 
 
 
 
 
a1be761
 
 
 
da893ba
 
 
 
 
 
 
 
a1be761
da893ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
da893ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
da893ba
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
da893ba
 
 
 
 
 
a1be761
da893ba
 
 
 
 
 
 
 
 
 
a1be761
da893ba
 
 
 
 
 
 
 
a1be761
 
da893ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
da893ba
a1be761
da893ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// ==================================================================
// Main application JavaScript for the frontend
// Handles theme switching, sidebar navigation, and content display.
// ==================================================================

// Wait for the DOM to be fully loaded before executing scripts
document.addEventListener('DOMContentLoaded', function() {

    // 1. Initialize Theme (Dark/Light Mode)
    initTheme();
    setupThemeToggle();

    // 2. Setup Sidebar Navigation System
    setupSidebarNavigation();

    // 3. Setup Feedback Form Validation (Basic)
    setupFeedbackForm();

    // NOTE: Old setup functions for direct page selection are removed/commented out
    // setupSubjectSelection(); // Replaced by sidebar logic
    // setupCategorySelection(); // Replaced by sidebar logic
    // setupTextSelection(); // Replaced by sidebar logic
});


// ==================================================================
// THEME SWITCHING FUNCTIONS
// ==================================================================

/**
 * Initializes the theme (dark/light) based on localStorage preference or system default.
 */
function initTheme() {
    // Default to 'light' if no preference is found
    const userPreference = localStorage.getItem('theme') || 'light';
    document.documentElement.setAttribute('data-theme', userPreference);
    updateThemeIcon(userPreference); // Set the correct icon on load
}

/**
 * Sets up the event listener for the theme toggle button.
 */
function setupThemeToggle() {
    const themeToggle = document.getElementById('theme-toggle');
    if (!themeToggle) {
        console.warn("Theme toggle button (#theme-toggle) not found.");
        return;
    }

    themeToggle.addEventListener('click', function() {
        const currentTheme = document.documentElement.getAttribute('data-theme');
        const newTheme = currentTheme === 'dark' ? 'light' : 'dark';

        // Apply the new theme
        document.documentElement.setAttribute('data-theme', newTheme);

        // Save preference to localStorage
        localStorage.setItem('theme', newTheme);

        // Update the button icon
        updateThemeIcon(newTheme);

        // Optional: Send theme preference to the server (if needed)
        // saveThemePreference(newTheme);
    });
}

/**
 * Updates the icon (sun/moon) inside the theme toggle button.
 * @param {string} theme - The current theme ('light' or 'dark').
 */
function updateThemeIcon(theme) {
    const themeToggle = document.getElementById('theme-toggle');
    if (!themeToggle) return; // Exit if button not found

    if (theme === 'dark') {
        themeToggle.innerHTML = '<i class="fas fa-sun"></i>'; // Show sun icon
        themeToggle.setAttribute('title', 'Activer le mode clair');
    } else {
        themeToggle.innerHTML = '<i class="fas fa-moon"></i>'; // Show moon icon
        themeToggle.setAttribute('title', 'Activer le mode sombre');
    }
}

/**
 * Optional: Sends the chosen theme preference to the server.
 * Uncomment the call in setupThemeToggle if needed.
 * @param {string} theme - The theme to save ('light' or 'dark').
 */
function saveThemePreference(theme) {
    const formData = new FormData();
    formData.append('theme', theme);

    fetch('/set_theme', { // Ensure this endpoint exists in your Flask app
        method: 'POST',
        body: formData
    })
    .then(response => {
        if (!response.ok) {
            console.error(`Error saving theme: ${response.statusText}`);
            return response.json().catch(() => ({})); // Try to parse error body
        }
        return response.json();
    })
    .then(data => {
        console.log('Theme preference saved on server:', data);
    })
    .catch(error => {
        console.error('Error sending theme preference to server:', error);
    });
}


// ==================================================================
// SIDEBAR NAVIGATION FUNCTIONS
// ==================================================================

/**
 * Sets up all event listeners and logic for the sidebar navigation.
 */
function setupSidebarNavigation() {
    // Get references to all necessary DOM elements
    const burgerMenu = document.getElementById('burger-menu');
    const sidebarMatieres = document.getElementById('sidebar-matieres');
    const sidebarSousCategories = document.getElementById('sidebar-sous-categories');
    const sidebarOverlay = document.getElementById('sidebar-overlay');
    const matieresList = document.getElementById('matieres-list-sidebar');
    const sousCategoriesList = document.getElementById('sous-categories-list-sidebar');
    const backButton = document.getElementById('sidebar-back-button');
    const closeButtons = document.querySelectorAll('.close-sidebar-btn');
    const initialInstructions = document.getElementById('initial-instructions');
    const contentSection = document.getElementById('content-section');

    // --- Helper Function to Close All Sidebars ---
    const closeAllSidebars = () => {
        if (sidebarMatieres) sidebarMatieres.classList.remove('active');
        if (sidebarSousCategories) sidebarSousCategories.classList.remove('active');
        if (sidebarOverlay) sidebarOverlay.classList.remove('active');
        // Optional: Reset scroll position of lists when closing
        // if (matieresList) matieresList.scrollTop = 0;
        // if (sousCategoriesList) sousCategoriesList.scrollTop = 0;
    };

    // --- Event Listeners ---

    // 1. Open Sidebar 1 (Matières) with Burger Menu
    if (burgerMenu && sidebarMatieres && sidebarOverlay) {
        burgerMenu.addEventListener('click', (e) => {
            e.stopPropagation(); // Prevent immediate closing if overlay listener fires
            closeAllSidebars(); // Ensure only one sidebar is open initially
            sidebarMatieres.classList.add('active');
            sidebarOverlay.classList.add('active');
        });
    } else {
        console.warn("Burger menu, matières sidebar, or overlay not found.");
    }

    // 2. Close sidebars via Overlay click
    if (sidebarOverlay) {
        sidebarOverlay.addEventListener('click', closeAllSidebars);
    }

    // 3. Close sidebars via dedicated Close buttons (X)
    closeButtons.forEach(button => {
        button.addEventListener('click', closeAllSidebars);
    });

    // 4. Handle click on a Matière in Sidebar 1
    if (matieresList && sidebarSousCategories && sidebarMatieres) {
        matieresList.addEventListener('click', (e) => {
            // Use closest to handle clicks even if icon is clicked
            const listItem = e.target.closest('li');
            if (listItem) {
                const matiereId = listItem.getAttribute('data-matiere-id');
                const matiereNom = listItem.getAttribute('data-matiere-nom') || 'Inconnu'; // Fallback name

                if (matiereId) {
                    // Update Sidebar 2 title
                    const titleElement = document.getElementById('sidebar-sous-categories-title');
                    if (titleElement) {
                        titleElement.textContent = `Sous-catégories (${matiereNom})`;
                    }
                    // Load sous-categories into Sidebar 2's list
                    loadSubCategoriesForSidebar(matiereId, sousCategoriesList);

                    // Switch Sidebars: Hide 1, Show 2
                    sidebarMatieres.classList.remove('active'); // Slide out sidebar 1
                    sidebarSousCategories.classList.add('active'); // Slide in sidebar 2
                    // Keep overlay active
                    if (sidebarOverlay) sidebarOverlay.classList.add('active');
                }
            }
        });
    } else {
        console.warn("Matières list, sous-catégories sidebar, or matières sidebar not found.");
    }

    // 5. Handle click on Back Button in Sidebar 2
    if (backButton && sidebarMatieres && sidebarSousCategories) {
        backButton.addEventListener('click', () => {
            sidebarSousCategories.classList.remove('active'); // Slide out sidebar 2
            sidebarMatieres.classList.add('active'); // Slide in sidebar 1
            // Keep overlay active
            if (sidebarOverlay) sidebarOverlay.classList.add('active');
        });
    } else {
        console.warn("Sidebar back button, matières sidebar, or sous-catégories sidebar not found.");
    }

    // 6. Handle click on a Sous-catégorie in Sidebar 2
    if (sousCategoriesList && initialInstructions && contentSection) {
        sousCategoriesList.addEventListener('click', (e) => {
            const listItem = e.target.closest('li');
            if (listItem && listItem.getAttribute('data-category-id')) { // Ensure it's a valid category item
                const categoryId = listItem.getAttribute('data-category-id');

                // Load and display the content for the first text in this category
                loadAndDisplayFirstTexte(categoryId);

                // Hide initial instructions, show content section
                if (initialInstructions) initialInstructions.classList.add('d-none');
                if (contentSection) contentSection.classList.remove('d-none');

                // Close both sidebars and overlay after selection
                closeAllSidebars();
            }
        });
    } else {
        console.warn("Sous-catégories list, initial instructions, or content section not found.");
    }
}

/**
 * Fetches and loads subcategories for a given matiereId into the specified list element.
 * @param {string} matiereId - The ID of the selected matière.
 * @param {HTMLElement} listElement - The UL element to populate.
 */
function loadSubCategoriesForSidebar(matiereId, listElement) {
    if (!listElement) {
        console.error("Target list element for subcategories is missing.");
        return;
    }
    listElement.innerHTML = '<li>Chargement...</li>'; // Show loading state

    fetch(`/get_sous_categories/${matiereId}`) // Ensure this endpoint exists in Flask
        .then(response => {
            if (!response.ok) {
                throw new Error(`Erreur HTTP: ${response.status} ${response.statusText}`);
            }
            return response.json();
        })
        .then(data => {
            listElement.innerHTML = ''; // Clear loading/previous items
            if (data && data.length > 0) {
                data.forEach(category => {
                    const item = document.createElement('li');
                    item.setAttribute('data-category-id', category.id);
                    item.textContent = category.nom; // Use category name

                    // Add chevron icon for visual cue
                    const icon = document.createElement('i');
                    icon.className = 'fas fa-chevron-right float-end';
                    item.appendChild(icon);

                    listElement.appendChild(item);
                });
            } else {
                listElement.innerHTML = '<li>Aucune sous-catégorie trouvée.</li>';
            }
        })
        .catch(error => {
            console.error('Erreur lors du chargement des sous-catégories:', error);
            listElement.innerHTML = `<li>Erreur: ${error.message}</li>`;
        });
}

/**
 * Fetches the list of texts for a category and displays the first one found.
 * @param {string} categoryId - The ID of the selected sous-catégorie.
 */
function loadAndDisplayFirstTexte(categoryId) {
    fetch(`/get_textes/${categoryId}`) // Ensure this endpoint exists and returns a list of texts [{id, titre}, ...]
        .then(response => {
            if (!response.ok) {
                throw new Error(`Erreur HTTP: ${response.status} ${response.statusText}`);
            }
            return response.json();
        })
        .then(data => {
            if (data && data.length > 0) {
                const firstTexteId = data[0].id; // Get the ID of the very first text
                if (firstTexteId) {
                    displayTexte(firstTexteId); // Call displayTexte with this ID
                } else {
                     throw new Error("Le premier texte reçu n'a pas d'ID.");
                }
            } else {
                // Handle case where a category has no texts associated with it
                const contentTitle = document.getElementById('content-title');
                const contentBlocks = document.getElementById('content-blocks');
                const contentSection = document.getElementById('content-section');
                if (contentTitle) contentTitle.textContent = "Contenu non disponible";
                if (contentBlocks) contentBlocks.innerHTML = '<div class="alert alert-info">Aucun texte trouvé pour cette sous-catégorie.</div>';
                if (contentSection) contentSection.classList.remove('d-none'); // Ensure section is visible
            }
        })
        .catch(error => {
            console.error('Erreur lors du chargement des textes pour la catégorie:', error);
            // Display error message in the content area
            const contentTitle = document.getElementById('content-title');
            const contentBlocks = document.getElementById('content-blocks');
            const contentSection = document.getElementById('content-section');
            if (contentTitle) contentTitle.textContent = "Erreur";
            if (contentBlocks) contentBlocks.innerHTML = `<div class="alert alert-danger">Impossible de charger le contenu. ${error.message}</div>`;
            if (contentSection) contentSection.classList.remove('d-none'); // Ensure section is visible
        });
}


// ==================================================================
// CONTENT DISPLAY FUNCTION
// ==================================================================

/**
 * Fetches and displays the content (title and blocks) for a specific texteId.
 * @param {string} texteId - The ID of the text to display.
 */
function displayTexte(texteId) {
    const contentSection = document.getElementById('content-section');
    const contentTitle = document.getElementById('content-title');
    const contentBlocks = document.getElementById('content-blocks');

    // Check if essential elements exist
    if (!contentSection || !contentTitle || !contentBlocks) {
        console.error("Éléments d'affichage du contenu (#content-section, #content-title, #content-blocks) introuvables.");
        alert("Erreur interne: Impossible d'afficher le contenu.");
        return;
    }

    // Indicate loading state visually
    contentTitle.textContent = "Chargement du contenu...";
    contentBlocks.innerHTML = '<div class="text-center p-3"><i class="fas fa-spinner fa-spin fa-2x"></i></div>'; // Simple spinner

    fetch(`/get_texte/${texteId}`) // Ensure this endpoint exists and returns detailed text object {titre, contenu, blocks, color_code, ...}
        .then(response => {
            if (!response.ok) {
                throw new Error(`Erreur HTTP: ${response.status} ${response.statusText}`);
            }
            return response.json();
        })
        .then(data => {
            // --- Update Content Title ---
            contentTitle.textContent = data.titre || "Titre non disponible";

            // --- Update Theme/Color Styling ---
            const dynamicStyleId = 'dynamic-block-styles';
            let style = document.getElementById(dynamicStyleId);
            if (!style) { // Create style tag if it doesn't exist
                style = document.createElement('style');
                style.id = dynamicStyleId;
                document.head.appendChild(style);
            }

            if (data.color_code) {
                // Apply color to title underline and block border/title
                contentTitle.style.borderBottomColor = data.color_code;
                style.textContent = `
                    #content-section .content-block-title { border-bottom-color: ${data.color_code} !important; }
                    #content-section .content-block { border-left: 4px solid ${data.color_code} !important; }
                `;
            } else {
                // Reset styles if no color code is provided (use CSS defaults)
                contentTitle.style.borderBottomColor = ''; // Reset specific style
                style.textContent = ''; // Clear dynamic rules
            }

            // --- Render Content Blocks ---
            contentBlocks.innerHTML = ''; // Clear loading indicator/previous content

            if (data.blocks && Array.isArray(data.blocks) && data.blocks.length > 0) {
                // Sort blocks by 'order' property if it exists, otherwise render as received
                const sortedBlocks = data.blocks.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));

                sortedBlocks.forEach(block => {
                    const blockDiv = document.createElement('div');
                    blockDiv.className = 'content-block fade-in'; // Add animation class

                    // Block with Image
                    if (block.image && block.image.src) {
                        blockDiv.classList.add('block-with-image', `image-${block.image_position || 'left'}`);

                        // Image Container
                        const imageDiv = document.createElement('div');
                        imageDiv.className = 'block-image-container';
                        const imageEl = document.createElement('img');
                        imageEl.className = 'block-image';
                        imageEl.src = block.image.src; // Ensure backend provides full URL if needed
                        imageEl.alt = block.image.alt || 'Illustration';
                        imageEl.loading = 'lazy'; // Add lazy loading for images
                        imageDiv.appendChild(imageEl);
                        blockDiv.appendChild(imageDiv);

                        // Content Container (Text part)
                        const contentDiv = document.createElement('div');
                        contentDiv.className = 'block-content-container';
                        if (block.title) {
                            const titleEl = document.createElement('h3');
                            titleEl.className = 'content-block-title';
                            titleEl.textContent = block.title;
                            contentDiv.appendChild(titleEl);
                        }
                        const contentEl = document.createElement('div');
                        contentEl.className = 'content-block-content';
                        // IMPORTANT: Sanitize HTML content if it comes from user input or untrusted sources
                        // For now, assuming safe HTML from backend:
                        contentEl.innerHTML = block.content ? block.content.replace(/\n/g, '<br>') : '';
                        contentDiv.appendChild(contentEl);
                        blockDiv.appendChild(contentDiv);

                    } else { // Block without Image
                        if (block.title) {
                            const titleEl = document.createElement('h3');
                            titleEl.className = 'content-block-title';
                            titleEl.textContent = block.title;
                            blockDiv.appendChild(titleEl);
                        }
                        const contentEl = document.createElement('div');
                        contentEl.className = 'content-block-content';
                         // IMPORTANT: Sanitize HTML content
                        contentEl.innerHTML = block.content ? block.content.replace(/\n/g, '<br>') : '';
                        blockDiv.appendChild(contentEl);
                    }
                    contentBlocks.appendChild(blockDiv);
                });
            } else if (data.contenu) { // Fallback: Use simple 'contenu' field if no blocks
                const blockDiv = document.createElement('div');
                blockDiv.className = 'content-block';
                 // IMPORTANT: Sanitize HTML content
                blockDiv.innerHTML = data.contenu.replace(/\n/g, '<br>');
                contentBlocks.appendChild(blockDiv);
            } else {
                // No blocks and no simple content
                contentBlocks.innerHTML = '<div class="alert alert-warning">Le contenu de ce texte est vide ou non structuré.</div>';
            }

            // --- Final Steps ---
            // Ensure the content section is visible
            contentSection.classList.remove('d-none');

            // Scroll to the top of the content title for better UX
            contentTitle.scrollIntoView({ behavior: 'smooth', block: 'start' });

        })
        .catch(error => {
            console.error(`Erreur lors du chargement du texte ID ${texteId}:`, error);
             // Display error message in the content area
            contentTitle.textContent = "Erreur de chargement";
            contentBlocks.innerHTML = `<div class="alert alert-danger">Impossible de charger le contenu demandé. ${error.message}</div>`;
            // Ensure section is visible even on error
            contentSection.classList.remove('d-none');
        });
}


// ==================================================================
// FEEDBACK FORM SETUP
// ==================================================================

/**
 * Sets up basic validation for the feedback form.
 */
function setupFeedbackForm() {
    const feedbackForm = document.getElementById('feedback-form');
    if (feedbackForm) {
        feedbackForm.addEventListener('submit', function(e) {
            const feedbackMessage = document.getElementById('feedback-message');
            // Simple check if message textarea is empty or only whitespace
            if (!feedbackMessage || !feedbackMessage.value.trim()) {
                e.preventDefault(); // Stop form submission
                alert('Veuillez entrer un message avant d\'envoyer votre avis.');
                if (feedbackMessage) feedbackMessage.focus(); // Focus the textarea
            }
            // Add more complex validation here if needed
        });
    }
}


// ==================================================================
// OLD FUNCTIONS (Removed/Commented Out) - Kept for reference only
// ==================================================================

/*
function setupSubjectSelection() { // No longer used directly on page
    // ... old logic targeting .subject-card elements on the main page ...
}

function loadSubCategories(matiereId) { // Replaced by loadSubCategoriesForSidebar
    // ... old logic targeting #sous-categories-list on the main page ...
}

function setupCategorySelection() { // No longer used directly on page
    // ... old logic targeting #sous-categorie-select or similar ...
}

function loadTextes(categoryId) { // Logic integrated into loadAndDisplayFirstTexte
    // ... old logic targeting #textes-list on the main page ...
}

function setupTextSelection() { // No longer used directly on page
    // ... old logic targeting #texte-select or similar ...
}
*/

// ==================================================================
// END OF FILE
// ==================================================================