From 4816876ff3d06e10523f6df627578a36e6da36e2 Mon Sep 17 00:00:00 2001 From: Johannes Baumeister Date: Sat, 4 Jul 2026 21:39:29 +0200 Subject: [PATCH] fix: move btnExportCoordsPDF logic inside DOMContentLoaded to access state --- app.js | 137 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 69 insertions(+), 68 deletions(-) diff --git a/app.js b/app.js index cdccbe0..b9287ea 100644 --- a/app.js +++ b/app.js @@ -2694,6 +2694,75 @@ document.addEventListener('DOMContentLoaded', async () => { updateLegend(); console.log("WindPlaner initialisiert."); + const btnExportCoordsPDF = document.getElementById('btnExportCoordsPDF'); + if (btnExportCoordsPDF) { + btnExportCoordsPDF.addEventListener('click', async () => { + const originalText = btnExportCoordsPDF.innerText; + btnExportCoordsPDF.innerText = 'Exportiere...'; + btnExportCoordsPDF.disabled = true; + + try { + // Finde aktive Variante + const activeVariantNode = document.querySelector('.variant-tab.active'); + const activeVariant = activeVariantNode ? activeVariantNode.dataset.variant : 'variant1'; + + // Filtere Anlagen, die zur aktuellen Variante gehören + const activeTurbines = state.turbines.filter(t => t.variant === activeVariant); + + if (activeTurbines.length === 0) { + alert('Keine Anlagen in der aktuellen Variante gefunden.'); + return; + } + + // Initialisiere jsPDF + const { jsPDF } = window.jspdf; + const doc = new jsPDF(); + + // Titel und Projektname + const projName = document.getElementById('projectSelector')?.options[document.getElementById('projectSelector')?.selectedIndex]?.text || 'Projekt unbekannt'; + + doc.setFontSize(16); + doc.text(`Koordinatenliste - ${projName}`, 14, 20); + + // Untertitel (Variante) + doc.setFontSize(12); + let variantName = activeVariant === 'variant1' ? 'Variante 1' : activeVariant === 'variant2' ? 'Variante 2' : 'Variante 3'; + doc.text(`Variante: ${variantName}`, 14, 30); + + // Daten vorbereiten (Rechtswert/Hochwert aus UTM32) + const tableData = activeTurbines.map(t => { + const latlng = t.layers.marker.getLatLng(); + const utmCoords = proj4(wgs84, utm32, [latlng.lng, latlng.lat]); + return [ + t.nr, + t.hersteller || '-', + t.type || '-', + utmCoords[0].toFixed(2), + utmCoords[1].toFixed(2) + ]; + }); + + // Tabelle einfügen + doc.autoTable({ + startY: 40, + head: [['WEA Nr.', 'Hersteller', 'Anlagentyp', 'Rechtswert (E)', 'Hochwert (N)']], + body: tableData, + theme: 'striped', + headStyles: { fillColor: [0, 200, 255], textColor: [255, 255, 255] } + }); + + doc.save(`Koordinatenliste_${projName.replace(/\s+/g, '_')}_${activeVariant}.pdf`); + + } catch (err) { + console.error('Fehler beim Koordinaten-PDF Export:', err); + alert('Fehler beim PDF Export: ' + err.message); + } finally { + btnExportCoordsPDF.innerText = originalText; + btnExportCoordsPDF.disabled = false; + } + }); + } + document.getElementById('statusInfo').innerText = "System bereit. Karte geladen."; }); @@ -3162,71 +3231,3 @@ async function resolveLayerConfig(isLocalFile, statusEl) { }); } - const btnExportCoordsPDF = document.getElementById('btnExportCoordsPDF'); - if (btnExportCoordsPDF) { - btnExportCoordsPDF.addEventListener('click', async () => { - const originalText = btnExportCoordsPDF.innerText; - btnExportCoordsPDF.innerText = 'Exportiere...'; - btnExportCoordsPDF.disabled = true; - - try { - // Finde aktive Variante - const activeVariantNode = document.querySelector('.variant-tab.active'); - const activeVariant = activeVariantNode ? activeVariantNode.dataset.variant : 'variant1'; - - // Filtere Anlagen, die zur aktuellen Variante gehören - const activeTurbines = state.turbines.filter(t => t.variant === activeVariant); - - if (activeTurbines.length === 0) { - alert('Keine Anlagen in der aktuellen Variante gefunden.'); - return; - } - - // Initialisiere jsPDF - const { jsPDF } = window.jspdf; - const doc = new jsPDF(); - - // Titel und Projektname - const projName = document.getElementById('projectSelector')?.options[document.getElementById('projectSelector')?.selectedIndex]?.text || 'Projekt unbekannt'; - - doc.setFontSize(16); - doc.text(`Koordinatenliste - ${projName}`, 14, 20); - - // Untertitel (Variante) - doc.setFontSize(12); - let variantName = activeVariant === 'variant1' ? 'Variante 1' : activeVariant === 'variant2' ? 'Variante 2' : 'Variante 3'; - doc.text(`Variante: ${variantName}`, 14, 30); - - // Daten vorbereiten (Rechtswert/Hochwert aus UTM32) - const tableData = activeTurbines.map(t => { - const latlng = t.layers.marker.getLatLng(); - const utmCoords = proj4(wgs84, utm32, [latlng.lng, latlng.lat]); - return [ - t.nr, - t.hersteller || '-', - t.type || '-', - utmCoords[0].toFixed(2), - utmCoords[1].toFixed(2) - ]; - }); - - // Tabelle einfügen - doc.autoTable({ - startY: 40, - head: [['WEA Nr.', 'Hersteller', 'Anlagentyp', 'Rechtswert (E)', 'Hochwert (N)']], - body: tableData, - theme: 'striped', - headStyles: { fillColor: [0, 200, 255], textColor: [255, 255, 255] } - }); - - doc.save(`Koordinatenliste_${projName.replace(/\s+/g, '_')}_${activeVariant}.pdf`); - - } catch (err) { - console.error('Fehler beim Koordinaten-PDF Export:', err); - alert('Fehler beim PDF Export: ' + err.message); - } finally { - btnExportCoordsPDF.innerText = originalText; - btnExportCoordsPDF.disabled = false; - } - }); - }