Allow for downloading the input json from the editor

This commit is contained in:
Adam Goldsmith 2017-10-11 22:51:43 -04:00
parent ae1517a717
commit fe1d2ecd4a
2 changed files with 23 additions and 10 deletions

View File

@ -8,7 +8,8 @@
<body> <body>
<div> <div>
<button type="button" id="saveButton">Save Deck</button> <button type="button" id="saveButton">Save Deck</button>
<button type="button" id="jsonInputDownload">Download JSON</button> <button type="button" id="jsonInputDownload">Download Input JSON</button>
<button type="button" id="outputDownload">Download Tabletop Output JSON</button>
</div> </div>
<div> <div>
<label> Upload JSON: WARNING: WILL CLEAR DECK <label> Upload JSON: WARNING: WILL CLEAR DECK

View File

@ -26,15 +26,18 @@ window.addEventListener("load", () => {
document.querySelector('#saveButton').addEventListener('click', upload); document.querySelector('#saveButton').addEventListener('click', upload);
// download input JSON // download input JSON
document.querySelector('#jsonInputDownload').addEventListener('click', () => { document.querySelector('#jsonInputDownload').addEventListener(
let dl = document.createElement('a'); 'click',
dl.setAttribute('href', 'data:application/json;charset=utf-8,' + () => downloadFile('data:application/json;charset=utf-8,' +
encodeURIComponent(JSON.stringify(deckJSON))); encodeURIComponent(JSON.stringify(deckJSON)),
dl.setAttribute('download', deckName + '.input.json'); deckName + '.input.json'));
document.body.appendChild(dl);
dl.click(); // download input JSON
document.body.removeChild(dl); document.querySelector('#outputDownload').addEventListener(
}); 'click',
() => downloadFile('deck.json',
deckName + '.json'));
// handle changes to deck editor // handle changes to deck editor
document.querySelector('#deckForm').addEventListener('input', event => { document.querySelector('#deckForm').addEventListener('input', event => {
@ -67,6 +70,15 @@ window.addEventListener("load", () => {
}); });
}); });
function downloadFile(file, name) {
let dl = document.createElement('a');
dl.setAttribute('href', file);
dl.setAttribute('download', name);
document.body.appendChild(dl);
dl.click();
document.body.removeChild(dl);
}
function getJSON(filename, callback) { function getJSON(filename, callback) {
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => { xhr.addEventListener("load", () => {