Skip to content

Commit

Permalink
Merge pull request #344 from cidgoh/save-as-json
Browse files Browse the repository at this point in the history
Added save as JSON option
  • Loading branch information
pkalita-lbl authored Aug 3, 2022
2 parents 73668dd + 508a512 commit d881095
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'bootstrap/js/dist/modal';
import '@selectize/selectize';
import '@selectize/selectize/dist/css/selectize.bootstrap4.css';

import { exportFile } from './utils/files';
import { exportFile, exportJsonFile } from './utils/files';

import template from './toolbar.html';
import { getGettingStartedMarkup } from './toolbarGettingStarted';
Expand Down Expand Up @@ -106,8 +106,13 @@ class Toolbar {
try {
const baseName = $('#base-name-save-as-input').val();
const ext = $('#file-ext-save-as-select').val();
const matrix = [...dh.getFlatHeaders(), ...dh.getTrimmedData()];
dh.runBehindLoadingScreen(exportFile, [matrix, baseName, ext]);
if (ext == 'json') {
let data = dh.getDataObjects();
dh.runBehindLoadingScreen(exportJsonFile, [data, baseName, ext]);
} else {
let matrix = [...dh.getFlatHeaders(), ...dh.getTrimmedData()];
dh.runBehindLoadingScreen(exportFile, [matrix, baseName, ext]);
}
$('#save-as-modal').modal('hide');
} catch (err) {
$('#save-as-err-msg').text(err.message);
Expand Down
1 change: 1 addition & 0 deletions lib/toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
<option value="xls">.xls</option>
<option value="tsv">.tsv</option>
<option value="csv">.csv</option>
<option value="json">.json</option>
</select>
</div>
</div>
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,14 @@ export function exportFile(matrix, baseName, ext) {
break;
}
}

/**
* Download Object to file as JSON.
* @param {Object} data Object to download as JSON.
* @param {String} baseName Basename of downloaded file.
* @param {String} ext Extension of downloaded file.
*/
export function exportJsonFile(data, baseName) {
let blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
saveAs(blob, `${baseName}.json`);
}

0 comments on commit d881095

Please sign in to comment.