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>
<div>
<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>
<label> Upload JSON: WARNING: WILL CLEAR DECK

View File

@ -26,15 +26,18 @@ window.addEventListener("load", () => {
document.querySelector('#saveButton').addEventListener('click', upload);
// download input JSON
document.querySelector('#jsonInputDownload').addEventListener('click', () => {
let dl = document.createElement('a');
dl.setAttribute('href', 'data:application/json;charset=utf-8,' +
encodeURIComponent(JSON.stringify(deckJSON)));
dl.setAttribute('download', deckName + '.input.json');
document.body.appendChild(dl);
dl.click();
document.body.removeChild(dl);
});
document.querySelector('#jsonInputDownload').addEventListener(
'click',
() => downloadFile('data:application/json;charset=utf-8,' +
encodeURIComponent(JSON.stringify(deckJSON)),
deckName + '.input.json'));
// download input JSON
document.querySelector('#outputDownload').addEventListener(
'click',
() => downloadFile('deck.json',
deckName + '.json'));
// handle changes to deck editor
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) {
let xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => {