Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<html>
<head>
<meta charset="utf-8">
<!-- USWDS not working -->
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> -->
<!-- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> -->
Expand All @@ -12,7 +13,7 @@
<!-- <script src="https://cdn.form.io/uswds/uswds.min.js"></script> -->
<!-- <script src="https://cdn.form.io/formiojs/formio.full.js"></script> -->
<!-- <script src="https://cdn.form.io/js/formio.embed.js"></script> -->

<!-- Uses bootstrap for now -->
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>
<link rel="stylesheet" href="css/styles.css">
Expand All @@ -38,9 +39,10 @@
},
components: components,
}).then(function (form) {
window.formIOInstance = form;
form.on("submit", function (submission) {
console.log("form submission here:", submission);
downloadFile(submission.data);
createUnsdgJson(submission.data);
});
});
})
Expand All @@ -51,6 +53,15 @@
</head>
<body>
<div id="form-header"></div>

<div id="formio"></div>

<div id="output">
<label for="json-result">Your Form Data </label>
<textarea class="form-control" rows="10" id="json-result" readonly></textarea>
<button type="button" class="btn btn-outline" href="#" onclick="copyToClipboard(event)">Copy</button>
<button type="button" class="btn btn-outline" href="#" onclick="downloadFile(event)">Download</button>
<button type="button" class="btn btn-outline" href="#" onclick="emailFile(event)">Email</button>
</div>
</body>
</html>
67 changes: 59 additions & 8 deletions js/formDataToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,77 @@ function populateObject(data, schema) {
return reorderedObject;
}

async function populateCodeJson(data) {
async function populateUnsdgJson(data) {
const filePath = "schemas/input.json";

// Retrieves schema with fields in correct order
const schema = await retrieveFile(filePath);
let codeJson = {};
let unsdgJson = {};

// Populates fields with form data
if (schema) {
codeJson = populateObject(data, schema);
unsdgJson = populateObject(data, schema);
} else {
console.error("Failed to retrieve JSON data.");
}

return codeJson;
return unsdgJson;
}

// Creates code.json and triggers file download
async function downloadFile(data) {
// Creates code.json object
async function createUnsdgJson(data) {
delete data.submit;
const codeJson = await populateCodeJson(data);
const jsonData = await populateUnsdgJson(data);

const jsonString = JSON.stringify(codeJson, null, 2);
const jsonString = JSON.stringify(jsonData, null, 2);
document.getElementById("json-result").value = jsonString;
}

// Copies code.json to clipboard
async function copyToClipboard(event){
event.preventDefault();

var textArea = document.getElementById("json-result");
textArea.select();
document.execCommand("copy")
}

// Triggers email(mailtolink)
async function emailFile(event) {
event.preventDefault();

const codeJson = document.getElementById("json-result").value
const jsonObject = JSON.parse(codeJson);

try {
const cleanData = {...jsonObject};
delete cleanData.submit;

const jsonString = JSON.stringify(cleanData, null, 2);

const subject = "unsdg.json Generator Results";
const body = `Hello,\n\nHere are the unsdg.json results:\n\n${jsonString}\n\nThank you!`;

const recipients = ["unsdg@chaoss.community"];

const mailtoLink = `mailto:${recipients}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;

window.location.href = mailtoLink;

console.log("Email client opened");
} catch {
console.error("Error preparing email:", error);
showNotificationModal("Error preparing email. Please try again or copy the data manually.", 'error');
}
}

// Triggers local file download
async function downloadFile(event) {
event.preventDefault();

const codeJson = document.getElementById("json-result").value
const jsonObject = JSON.parse(codeJson);
const jsonString = JSON.stringify(jsonObject, null, 2);
const blob = new Blob([jsonString], { type: "application/json" });

// Create anchor element and create download link
Expand All @@ -91,3 +139,6 @@ async function downloadFile(data) {
}

window.downloadFile = downloadFile;
window.copyToClipboard = copyToClipboard;
window.emailFile = emailFile;
window.createUnsdgJson = createUnsdgJson
Loading