Spaces:
Running
Running
File size: 16,038 Bytes
a1be761 |
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 |
// Main application JavaScript for the frontend
// Wait for the DOM to be loaded before executing
document.addEventListener('DOMContentLoaded', function() {
// Initialize theme
initTheme();
// Setup interactive elements
setupSubjectSelection();
setupCategorySelection();
setupTextSelection();
setupThemeToggle();
// Setup feedback form submission
const feedbackForm = document.getElementById('feedback-form');
if (feedbackForm) {
feedbackForm.addEventListener('submit', function(e) {
const feedbackMessage = document.getElementById('feedback-message');
if (!feedbackMessage.value.trim()) {
e.preventDefault();
alert('Veuillez entrer un message avant d\'envoyer votre feedback.');
}
});
}
});
// Initialize theme based on user preference
function initTheme() {
const userPreference = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', userPreference);
// Update theme icon
updateThemeIcon(userPreference);
}
// Setup theme toggle functionality
function setupThemeToggle() {
const themeToggle = document.getElementById('theme-toggle');
if (!themeToggle) return;
themeToggle.addEventListener('click', function() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
// Update theme attribute
document.documentElement.setAttribute('data-theme', newTheme);
// Save preference to localStorage
localStorage.setItem('theme', newTheme);
// Update icon
updateThemeIcon(newTheme);
// Send theme preference to server
saveThemePreference(newTheme);
});
}
// Update the theme toggle icon based on current theme
function updateThemeIcon(theme) {
const themeToggle = document.getElementById('theme-toggle');
if (!themeToggle) return;
// Update icon based on theme
if (theme === 'dark') {
themeToggle.innerHTML = '<i class="fas fa-sun"></i>';
themeToggle.setAttribute('title', 'Activer le mode clair');
} else {
themeToggle.innerHTML = '<i class="fas fa-moon"></i>';
themeToggle.setAttribute('title', 'Activer le mode sombre');
}
}
// Save theme preference to server
function saveThemePreference(theme) {
const formData = new FormData();
formData.append('theme', theme);
fetch('/set_theme', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.log('Theme preference saved:', data);
})
.catch(error => {
console.error('Error saving theme preference:', error);
});
}
// Setup subject selection functionality
function setupSubjectSelection() {
const subjectCards = document.querySelectorAll('.subject-card');
const subjectSelect = document.getElementById('matiere-select');
// Handle subject card clicks
subjectCards.forEach(card => {
card.addEventListener('click', function() {
const matiereId = this.getAttribute('data-matiere-id');
// Update select element if it exists
if (subjectSelect) {
subjectSelect.value = matiereId;
// Trigger change event to load subcategories
const event = new Event('change');
subjectSelect.dispatchEvent(event);
} else {
loadSubCategories(matiereId);
}
// Highlight the selected card
subjectCards.forEach(c => c.classList.remove('active'));
this.classList.add('active');
// Show the categories section
const categoriesSection = document.getElementById('sous-categories-section');
if (categoriesSection) {
categoriesSection.classList.remove('d-none');
categoriesSection.scrollIntoView({ behavior: 'smooth' });
}
});
});
// Handle subject select change
if (subjectSelect) {
subjectSelect.addEventListener('change', function() {
const matiereId = this.value;
if (matiereId) {
loadSubCategories(matiereId);
// Show the categories section
const categoriesSection = document.getElementById('sous-categories-section');
if (categoriesSection) {
categoriesSection.classList.remove('d-none');
}
}
});
}
}
// Load subcategories for the selected subject
function loadSubCategories(matiereId) {
fetch(`/get_sous_categories/${matiereId}`)
.then(response => response.json())
.then(data => {
// Update subcategories list
const sousCategoriesList = document.getElementById('sous-categories-list');
if (sousCategoriesList) {
sousCategoriesList.innerHTML = '';
data.forEach(category => {
const item = document.createElement('li');
item.className = 'selection-item';
item.setAttribute('data-category-id', category.id);
item.textContent = category.nom;
// Add click event
item.addEventListener('click', function() {
const categoryId = this.getAttribute('data-category-id');
loadTextes(categoryId);
// Highlight the selected category
const items = sousCategoriesList.querySelectorAll('.selection-item');
items.forEach(i => i.classList.remove('active'));
this.classList.add('active');
// Show the texts section
const textesSection = document.getElementById('textes-section');
if (textesSection) {
textesSection.classList.remove('d-none');
}
});
sousCategoriesList.appendChild(item);
});
// Show the subcategories section if it's hidden
const sousCategoriesSection = document.getElementById('sous-categories-section');
if (sousCategoriesSection) {
sousCategoriesSection.classList.remove('d-none');
}
}
})
.catch(error => {
console.error('Error loading subcategories:', error);
});
}
// Setup category selection functionality
function setupCategorySelection() {
const categorySelect = document.getElementById('sous-categorie-select');
if (categorySelect) {
categorySelect.addEventListener('change', function() {
const categoryId = this.value;
if (categoryId) {
loadTextes(categoryId);
// Show the texts section
const textesSection = document.getElementById('textes-section');
if (textesSection) {
textesSection.classList.remove('d-none');
}
}
});
}
}
// Load texts for the selected category
function loadTextes(categoryId) {
fetch(`/get_textes/${categoryId}`)
.then(response => response.json())
.then(data => {
// Update texts list
const textesList = document.getElementById('textes-list');
if (textesList) {
textesList.innerHTML = '';
data.forEach(texte => {
const item = document.createElement('li');
item.className = 'selection-item';
item.setAttribute('data-texte-id', texte.id);
item.textContent = texte.titre;
// Add click event
item.addEventListener('click', function() {
const texteId = this.getAttribute('data-texte-id');
displayTexte(texteId);
// Highlight the selected text
const items = textesList.querySelectorAll('.selection-item');
items.forEach(i => i.classList.remove('active'));
this.classList.add('active');
});
textesList.appendChild(item);
});
// Show the texts section
const textesSection = document.getElementById('textes-section');
if (textesSection) {
textesSection.classList.remove('d-none');
}
// Hide the content section since no text is selected yet
const contentSection = document.getElementById('content-section');
if (contentSection) {
contentSection.classList.add('d-none');
}
}
})
.catch(error => {
console.error('Error loading texts:', error);
});
}
// Setup text selection functionality
function setupTextSelection() {
const texteSelect = document.getElementById('texte-select');
if (texteSelect) {
texteSelect.addEventListener('change', function() {
const texteId = this.value;
if (texteId) {
displayTexte(texteId);
}
});
}
}
// Display the selected texte with content blocks
function displayTexte(texteId) {
fetch(`/get_texte/${texteId}`)
.then(response => response.json())
.then(data => {
const contentSection = document.getElementById('content-section');
const contentTitle = document.getElementById('content-title');
const contentBlocks = document.getElementById('content-blocks');
if (contentSection && contentTitle && contentBlocks) {
// Update content title
contentTitle.textContent = data.titre;
// Update content theme color based on matiere color
if (data.color_code) {
// Apply color to title underline
contentTitle.style.borderBottomColor = data.color_code;
// Apply color to all block titles
const style = document.createElement('style');
style.id = 'dynamic-block-styles';
const existingStyle = document.getElementById('dynamic-block-styles');
if (existingStyle) {
existingStyle.remove();
}
style.textContent = `
.content-block-title {
border-bottom-color: ${data.color_code} !important;
}
.content-block {
border-left: 4px solid ${data.color_code} !important;
}
`;
document.head.appendChild(style);
}
// Clear existing content
contentBlocks.innerHTML = '';
// Create content blocks
if (data.blocks && data.blocks.length > 0) {
data.blocks.forEach(block => {
// Create block container
const blockDiv = document.createElement('div');
blockDiv.className = 'content-block fade-in';
// Check if block has an image
if (block.image) {
blockDiv.classList.add('block-with-image');
blockDiv.classList.add(`image-${block.image_position || 'left'}`);
// Create image container
const imageDiv = document.createElement('div');
imageDiv.className = 'block-image-container';
// Create image element
const imageEl = document.createElement('img');
imageEl.className = 'block-image';
imageEl.src = block.image.src;
imageEl.alt = block.image.alt || 'Illustration';
imageDiv.appendChild(imageEl);
blockDiv.appendChild(imageDiv);
// Create content container
const contentDiv = document.createElement('div');
contentDiv.className = 'block-content-container';
// Add block title if present
if (block.title) {
const titleEl = document.createElement('h3');
titleEl.className = 'content-block-title';
titleEl.textContent = block.title;
contentDiv.appendChild(titleEl);
}
// Add block content
const contentEl = document.createElement('div');
contentEl.className = 'content-block-content';
contentEl.innerHTML = block.content.replace(/\n/g, '<br>');
contentDiv.appendChild(contentEl);
blockDiv.appendChild(contentDiv);
} else {
// No image - simple block
// Add block title if present
if (block.title) {
const titleEl = document.createElement('h3');
titleEl.className = 'content-block-title';
titleEl.textContent = block.title;
blockDiv.appendChild(titleEl);
}
// Add block content
const contentEl = document.createElement('div');
contentEl.className = 'content-block-content';
contentEl.innerHTML = block.content.replace(/\n/g, '<br>');
blockDiv.appendChild(contentEl);
}
// Add the block to the content area
contentBlocks.appendChild(blockDiv);
});
} else {
// Fallback to regular content if no blocks
const blockDiv = document.createElement('div');
blockDiv.className = 'content-block';
blockDiv.innerHTML = data.contenu.replace(/\n/g, '<br>');
contentBlocks.appendChild(blockDiv);
}
// Show the content section
contentSection.classList.remove('d-none');
contentSection.scrollIntoView({ behavior: 'smooth' });
}
})
.catch(error => {
console.error('Error loading texte:', error);
});
}
|