// ============================================ // LEGEND - Legenden-Funktionen // ============================================ // ========== LEGENDE AUSWAHL ========== function toggleLegendSelection(id) { if (selectedSymbols.has(id)) { selectedSymbols.delete(id); } else { selectedSymbols.add(id); const item = findSymbol(id); if (item && !legendItems.find(l => l.id === id)) { legendItems.push({ id: item.id, name: item.name, svg: item.svg, description: '' }); } } renderSymbols(); updateLegendCount(); saveLegendToStorage(); } function updateLegendCount() { const countEl = document.getElementById('legendCount'); if (countEl) { countEl.textContent = legendItems.length; } } // ========== LEGENDE MODAL ========== function openLegendModal() { const modal = document.getElementById('legendModal'); modal.classList.add('active'); renderLegendEditor(); updateLegendPreview(); } function closeLegendModal() { const modal = document.getElementById('legendModal'); modal.classList.remove('active'); } function renderLegendEditor() { const container = document.getElementById('legendItems'); if (legendItems.length === 0) { container.innerHTML = '
Keine Symbole in der Legende. Klicken Sie auf đź“‘ bei einem Symbol, um es hinzuzufĂĽgen.
'; updateLegendPreview(); return; } container.innerHTML = legendItems.map((item, index) => `
${item.svg}
`).join(''); updateLegendPreview(); } function updateLegendItem(index, field, value) { if (legendItems[index]) { legendItems[index][field] = value; saveLegendToStorage(); updateLegendPreview(); } } function moveLegendItem(index, direction) { const newIndex = index + direction; if (newIndex >= 0 && newIndex < legendItems.length) { const temp = legendItems[index]; legendItems[index] = legendItems[newIndex]; legendItems[newIndex] = temp; renderLegendEditor(); saveLegendToStorage(); } } function removeLegendItem(index) { const item = legendItems[index]; if (item) { selectedSymbols.delete(item.id); } legendItems.splice(index, 1); renderLegendEditor(); renderSymbols(); updateLegendCount(); saveLegendToStorage(); } function clearLegend() { if (confirm('Möchten Sie die Legende wirklich leeren?')) { legendItems = []; selectedSymbols.clear(); renderLegendEditor(); renderSymbols(); updateLegendCount(); saveLegendToStorage(); } } // ========== LEGENDE SPEICHERN/LADEN ========== function saveLegendToStorage() { try { localStorage.setItem('gutachter_legende', JSON.stringify(legendItems)); localStorage.setItem('gutachter_selected', JSON.stringify([...selectedSymbols])); } catch (e) { console.warn('LocalStorage nicht verfügbar'); } } function loadLegendFromStorage() { try { const saved = localStorage.getItem('gutachter_legende'); const savedSelected = localStorage.getItem('gutachter_selected'); if (saved) { legendItems = JSON.parse(saved); } if (savedSelected) { selectedSymbols = new Set(JSON.parse(savedSelected)); } updateLegendCount(); } catch (e) { console.warn('Fehler beim Laden der Legende'); } } // ========== LEGENDE VORSCHAU ========== function generateLegendSVG() { if (legendItems.length === 0) return null; const itemHeight = 50; const width = 320; const height = legendItems.length * itemHeight + 60; let svg = ` Legende `; legendItems.forEach((item, index) => { const y = 60 + index * itemHeight; svg += ` ${item.svg.replace(/]*>/, '').replace('', '')} ${escapeHtml(item.name)} ${item.description ? `${escapeHtml(item.description)}` : ''} `; }); svg += ''; return svg; } function updateLegendPreview() { const previewBox = document.getElementById('legendPreviewBox'); if (!previewBox) return; if (legendItems.length === 0) { previewBox.innerHTML = '
Keine Eintraege vorhanden
'; return; } const svg = generateLegendSVG(); if (svg) { previewBox.innerHTML = svg; } } async function copyLegendAsImage() { if (legendItems.length === 0) { showNotification('Legende ist leer', 'error'); return; } const svg = generateLegendSVG(); if (!svg) return; try { const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); const itemHeight = 50; const width = 320; const height = legendItems.length * itemHeight + 60; canvas.width = width * 2; canvas.height = height * 2; ctx.scale(2, 2); ctx.fillStyle = 'white'; ctx.fillRect(0, 0, width, height); const img = new Image(); const svgBlob = new Blob([svg], { type: 'image/svg+xml;charset=utf-8' }); const url = URL.createObjectURL(svgBlob); await new Promise((resolve, reject) => { img.onload = resolve; img.onerror = reject; img.src = url; }); ctx.drawImage(img, 0, 0, width, height); URL.revokeObjectURL(url); canvas.toBlob(async (blob) => { try { await navigator.clipboard.write([ new ClipboardItem({ 'image/png': blob }) ]); showNotification('Legende in Zwischenablage kopiert!'); } catch (err) { const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = 'legende.png'; link.click(); showNotification('Legende als PNG heruntergeladen'); } }, 'image/png'); } catch (err) { console.error('Fehler beim Kopieren:', err); showNotification('Fehler beim Kopieren', 'error'); } } // Export-Funktionen sind in legend-export.js