@@ -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
93141window . downloadFile = downloadFile ;
142+ window . copyToClipboard = copyToClipboard ;
143+ window . emailFile = emailFile ;
144+ window . createUnsdgJson = createUnsdgJson
0 commit comments