From 869561fdcef086846b89d8f5c3c1e5e38e17afc2 Mon Sep 17 00:00:00 2001 From: Patrick Joyce Date: Wed, 7 Feb 2024 19:48:06 -0500 Subject: [PATCH] clean up css and url generation --- assets/css/main.css | 15 +++++++++++++++ assets/js/main.js | 32 ++++++++++++++++++++++++-------- index.html | 18 +++++++++++------- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/assets/css/main.css b/assets/css/main.css index e5cda30..5b7582c 100644 --- a/assets/css/main.css +++ b/assets/css/main.css @@ -61,3 +61,18 @@ a { a:hover { text-decoration: underline; } + +#inner-box { + padding: 20px; + background-color: #fff; + border: 1px solid #ddd; + border-radius: 3px; + box-sizing: border-box; +} + +#description { + text-align: center; + margin-bottom: 20px; + font-size: 1em; + color: #333; +} diff --git a/assets/js/main.js b/assets/js/main.js index 9ada941..92ad775 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -80,9 +80,11 @@ class MeetingDataProcessor { // CSV export functionality exportCSV(meetings) { - const csvContent = `data:text/csv;charset=utf-8,${encodeURIComponent( - this.constructor.convertToCSV(meetings) - )}`; + const convertedCSV = this.constructor.convertToCSV(meetings); + const csvContent = MeetingDataProcessor.createDownloadLink( + convertedCSV, + "text/csv" + ); const downloadLink = document.getElementById("downloadLink"); downloadLink.href = csvContent; downloadLink.style.display = "block"; @@ -90,14 +92,21 @@ class MeetingDataProcessor { // KML export functionality exportKML(meetings) { - const kmlContent = `data:text/xml;charset=utf-8,${encodeURIComponent( - this.constructor.convertToKML(meetings) - )}`; + const convertedKML = this.constructor.convertToKML(meetings); + const kmlContent = MeetingDataProcessor.createDownloadLink( + convertedKML, + "application/vnd.google-earth.kml+xml" + ); const kmlDownloadLink = document.getElementById("kmlDownloadLink"); kmlDownloadLink.href = kmlContent; kmlDownloadLink.style.display = "block"; } + static createDownloadLink(data, type) { + const blob = new Blob([data], { type }); + return URL.createObjectURL(blob); + } + static hideLinks() { const downloadLink = document.getElementById("downloadLink"); downloadLink.style.display = "none"; @@ -107,16 +116,23 @@ class MeetingDataProcessor { // Convert data to CSV static convertToCSV(data) { + if (!Array.isArray(data) || data.length === 0) { + MeetingDataProcessor.displayError("No data found"); + throw new Error("No data found"); + } const csvRows = []; const keys = Object.keys(data[0]); csvRows.push(keys.join(",")); data.forEach((row) => { const values = keys.map((key) => { - let value = row[key]; + let value = row[key] === null ? "" : row[key]; if ( typeof value === "string" && - (value.includes(",") || value.includes('"')) + (value.includes(",") || + value.includes('"') || + value.includes("\n") || + value.includes("\r")) ) { value = `"${value.replace(/"/g, '""')}"`; } diff --git a/index.html b/index.html index 4c28398..12ab977 100644 --- a/index.html +++ b/index.html @@ -9,14 +9,18 @@

BMLT Data Converter

- - -
- - - - +
Converts BMLT JSON meeting data to CSV or KML
+
+ + +
+ + + + +
+