Skip to content

Commit 2c4c04a

Browse files
authored
Merge pull request #14 from chaoss/sachin/apiMerge
Brought Features Back
2 parents 250da97 + 74124ed commit 2c4c04a

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

index.html

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<html>
22
<head>
3+
<meta charset="utf-8">
34
<!-- USWDS not working -->
45
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> -->
56
<!-- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> -->
@@ -12,7 +13,7 @@
1213
<!-- <script src="https://cdn.form.io/uswds/uswds.min.js"></script> -->
1314
<!-- <script src="https://cdn.form.io/formiojs/formio.full.js"></script> -->
1415
<!-- <script src="https://cdn.form.io/js/formio.embed.js"></script> -->
15-
16+
1617
<!-- Uses bootstrap for now -->
1718
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>
1819
<link rel="stylesheet" href="css/styles.css">
@@ -38,9 +39,10 @@
3839
},
3940
components: components,
4041
}).then(function (form) {
42+
window.formIOInstance = form;
4143
form.on("submit", function (submission) {
4244
console.log("form submission here:", submission);
43-
downloadFile(submission.data);
45+
createUnsdgJson(submission.data);
4446
});
4547
});
4648
})
@@ -51,6 +53,15 @@
5153
</head>
5254
<body>
5355
<div id="form-header"></div>
56+
5457
<div id="formio"></div>
58+
59+
<div id="output">
60+
<label for="json-result">Your Form Data </label>
61+
<textarea class="form-control" rows="10" id="json-result" readonly></textarea>
62+
<button type="button" class="btn btn-outline" href="#" onclick="copyToClipboard(event)">Copy</button>
63+
<button type="button" class="btn btn-outline" href="#" onclick="downloadFile(event)">Download</button>
64+
<button type="button" class="btn btn-outline" href="#" onclick="emailFile(event)">Email</button>
65+
</div>
5566
</body>
5667
</html>

js/formDataToJson.js

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,77 @@ function populateObject(data, schema) {
5656
return reorderedObject;
5757
}
5858

59-
async function populateCodeJson(data) {
59+
async function populateUnsdgJson(data) {
6060
const filePath = "schemas/input.json";
6161

6262
// Retrieves schema with fields in correct order
6363
const schema = await retrieveFile(filePath);
64-
let codeJson = {};
64+
let unsdgJson = {};
6565

6666
// Populates fields with form data
6767
if (schema) {
68-
codeJson = populateObject(data, schema);
68+
unsdgJson = populateObject(data, schema);
6969
} else {
7070
console.error("Failed to retrieve JSON data.");
7171
}
7272

73-
return codeJson;
73+
return unsdgJson;
7474
}
7575

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

81-
const jsonString = JSON.stringify(codeJson, null, 2);
81+
const jsonString = JSON.stringify(jsonData, null, 2);
82+
document.getElementById("json-result").value = jsonString;
83+
}
84+
85+
// Copies code.json to clipboard
86+
async function copyToClipboard(event){
87+
event.preventDefault();
88+
89+
var textArea = document.getElementById("json-result");
90+
textArea.select();
91+
document.execCommand("copy")
92+
}
93+
94+
// Triggers email(mailtolink)
95+
async function emailFile(event) {
96+
event.preventDefault();
97+
98+
const codeJson = document.getElementById("json-result").value
99+
const jsonObject = JSON.parse(codeJson);
100+
101+
try {
102+
const cleanData = {...jsonObject};
103+
delete cleanData.submit;
104+
105+
const jsonString = JSON.stringify(cleanData, null, 2);
106+
107+
const subject = "unsdg.json Generator Results";
108+
const body = `Hello,\n\nHere are the unsdg.json results:\n\n${jsonString}\n\nThank you!`;
109+
110+
const recipients = ["unsdg@chaoss.community"];
111+
112+
const mailtoLink = `mailto:${recipients}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
113+
114+
window.location.href = mailtoLink;
115+
116+
console.log("Email client opened");
117+
} catch {
118+
console.error("Error preparing email:", error);
119+
showNotificationModal("Error preparing email. Please try again or copy the data manually.", 'error');
120+
}
121+
}
122+
123+
// Triggers local file download
124+
async function downloadFile(event) {
125+
event.preventDefault();
126+
127+
const codeJson = document.getElementById("json-result").value
128+
const jsonObject = JSON.parse(codeJson);
129+
const jsonString = JSON.stringify(jsonObject, null, 2);
82130
const blob = new Blob([jsonString], { type: "application/json" });
83131

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

93141
window.downloadFile = downloadFile;
142+
window.copyToClipboard = copyToClipboard;
143+
window.emailFile = emailFile;
144+
window.createUnsdgJson = createUnsdgJson

0 commit comments

Comments
 (0)