🔧 Dashboard Dynamique Grist v2.7HYBRID

Système hybride en cours...
Hybrid Mode
', '', '', '', template.html, '', '', '' ]; return parts.join('\n'); } compileLegacyTemplate(template) { // Compilation avec échappement renforcé const escapeHtml = (str) => { return String(str) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); }; const escapeJs = (str) => { return String(str) .replace(/\\/g, '\\\\') .replace(/"/g, '\\"') .replace(/'/g, "\\'") .replace(/\n/g, '\\n') .replace(/\r/g, '\\r'); }; try { const parts = [ '', '', '', '', 'Legacy Template - ' + escapeHtml(template.id) + '', '', '', '', '', template.html || '
Template vide
', '', '', '' ]; return parts.join('\n'); } catch (error) { console.error('Erreur compilation legacy:', error); return this.buildErrorTemplate('Erreur compilation: ' + error.message); } } async renderInIframe(container, html) { // Nettoyage des anciens iframes const oldIframes = container.querySelectorAll('.iframe-container'); oldIframes.forEach(iframe => iframe.remove()); // Création du nouveau iframe const iframeContainer = document.createElement('div'); iframeContainer.className = 'iframe-container active'; const iframe = document.createElement('iframe'); iframe.className = 'dynamic-iframe'; iframe.sandbox = 'allow-scripts allow-same-origin'; iframeContainer.appendChild(iframe); container.appendChild(iframeContainer); // Chargement du contenu const blob = new Blob([html], { type: 'text/html; charset=utf-8' }); const url = URL.createObjectURL(blob); iframe.src = url; iframe.onload = () => { setTimeout(() => URL.revokeObjectURL(url), 1000); }; } buildErrorTemplate(message) { return ` Erreur Template

🚨 Erreur de Template

${message}

`; } } // GESTIONNAIRE PRINCIPAL HYBRIDE class HybridGristDashboard { constructor() { this.templates = new Map(); this.processor = new HybridTemplateProcessor(); this.currentTemplate = null; this.isReady = false; } async init() { try { console.log('🔧 Initialisation système hybride v2.7'); this.updateStatus('Connexion Grist...'); await grist.ready({ requiredAccess: 'read table' }); this.updateStatus('Chargement templates...'); await this.loadTemplates(); this.updateStatus('Configuration navigation...'); this.setupNavigation(); await this.loadDefaultTemplate(); this.updateStatus('Système hybride prêt'); this.hideLoading(); this.isReady = true; this.logSystemReport(); } catch (error) { console.error('Erreur initialisation:', error); this.showError('Erreur: ' + error.message); } } async loadTemplates() { try { const rawTemplates = await grist.docApi.fetchTable('Templates'); console.log(`📊 ${rawTemplates.length} templates trouvés`); for (const rawTemplate of rawTemplates) { try { const processedTemplate = await this.processor.processTemplate(rawTemplate); this.templates.set(processedTemplate.id, processedTemplate); console.log(`✅ Template ${processedTemplate.id} (${processedTemplate.format}) traité`); } catch (error) { console.warn(`❌ Erreur template ${rawTemplate.template_id}:`, error); } } } catch (error) { console.error('Erreur chargement templates:', error); throw error; } } setupNavigation() { const nav = document.getElementById('navigation'); if (!nav) return; nav.innerHTML = ''; // Compteur par format const formats = {}; this.templates.forEach(template => { formats[template.format] = (formats[template.format] || 0) + 1; }); // Indicateur de formats const indicator = document.createElement('span'); const formatText = Object.entries(formats) .map(([format, count]) => `${format.toUpperCase()}:${count}`) .join(' | '); indicator.style.cssText = 'background: #8b5cf6; color: white; padding: 4px 8px; border-radius: 4px; font-size: 11px; margin-right: 10px;'; indicator.textContent = `HYBRID (${formatText})`; nav.appendChild(indicator); // Boutons de navigation this.templates.forEach(template => { const btn = document.createElement('button'); btn.className = 'nav-btn'; btn.textContent = template.name; btn.title = `Format: ${template.format}`; btn.onclick = () => this.loadTemplate(template.id); nav.appendChild(btn); }); } async loadTemplate(templateId) { if (!this.isReady) return; try { const template = this.templates.get(templateId); if (!template) { throw new Error('Template non trouvé: ' + templateId); } console.log(`🔄 Chargement template ${templateId} (${template.format})`); this.updateStatus(`Rendu ${template.format}...`); const main = document.getElementById('main-content'); await template.render(main); // Mise à jour navigation document.querySelectorAll('.nav-btn').forEach(btn => { btn.classList.toggle('active', btn.textContent === template.name); }); this.currentTemplate = templateId; this.updateStatus(`Template ${template.format} actif`); } catch (error) { console.error('Erreur chargement template:', error); this.showError('Erreur: ' + error.message); } } async loadDefaultTemplate() { if (this.templates.size > 0) { const firstTemplate = Array.from(this.templates.keys())[0]; await this.loadTemplate(firstTemplate); } } updateStatus(message) { const status = document.getElementById('status'); if (status) status.textContent = message; const loadingText = document.getElementById('loading-text'); if (loadingText) loadingText.textContent = message; } hideLoading() { const loading = document.getElementById('loading'); if (loading) loading.style.display = 'none'; } showError(message) { const main = document.getElementById('main-content'); if (main) { main.innerHTML = `

🚨 Erreur

${message}

`; } } logSystemReport() { const formatStats = {}; this.templates.forEach(template => { formatStats[template.format] = (formatStats[template.format] || 0) + 1; }); console.log('🔧 RAPPORT SYSTÈME HYBRIDE v2.7'); console.log(' Templates total:', this.templates.size); console.log(' Formats supportés:', Object.keys(formatStats).join(', ')); Object.entries(formatStats).forEach(([format, count]) => { console.log(` ${format.toUpperCase()}:`, count); }); console.log(' Status: OPÉRATIONNEL'); } // MIGRATION AUTOMATIQUE Base64 async migrateToBase64() { console.log('🔄 Début migration vers Base64...'); let migrated = 0; try { for (const [templateId, template] of this.templates) { if (template.format === 'legacy') { console.log(`🔄 Migration ${templateId}...`); const updates = {}; if (template.html) updates.html_base64 = btoa(template.html); if (template.css) updates.css_base64 = btoa(template.css); if (template.js) updates.js_base64 = btoa(template.js); // Trouver l'ID du record const allTemplates = await grist.docApi.fetchTable('Templates'); const record = allTemplates.find(t => t.template_id === templateId); if (record && Object.keys(updates).length > 0) { await grist.docApi.applyUserActions([ ['UpdateRecord', 'Templates', record.id, updates] ]); migrated++; console.log(`✅ Template ${templateId} migré`); } } } alert(`✅ Migration réussie: ${migrated} templates migrés vers Base64`); location.reload(); // Recharger pour voir les changements } catch (error) { console.error('Erreur migration:', error); alert('❌ Erreur migration: ' + error.message); } } } // INITIALISATION document.addEventListener('DOMContentLoaded', () => { window.dashboard = new HybridGristDashboard(); window.dashboard.init(); }); // GESTIONNAIRES D'ÉVÉNEMENTS window.toggleDebug = function() { console.log('🔧 Système Hybride v2.7 - Formats supportés: Legacy, Base64, Component'); }; window.startMigration = function() { if (confirm('Migrer tous les templates legacy vers Base64 ?')) { window.dashboard.migrateToBase64(); } }; // Configuration des boutons setTimeout(() => { const debugBtn = document.getElementById('debug-btn'); const migrateBtn = document.getElementById('migrate-btn'); if (debugBtn) debugBtn.addEventListener('click', window.toggleDebug); if (migrateBtn) migrateBtn.addEventListener('click', window.startMigration); }, 100);