diff options
| -rw-r--r-- | csv-dump.mjs | 22 | ||||
| -rw-r--r-- | csv-parse.mjs (renamed from csv.mjs) | 0 | ||||
| -rw-r--r-- | index.html | 2 | ||||
| -rw-r--r-- | pnit-form.mjs | 13 | ||||
| -rw-r--r-- | worker.js | 2 |
5 files changed, 37 insertions, 2 deletions
diff --git a/csv-dump.mjs b/csv-dump.mjs new file mode 100644 index 0000000..76e1e08 --- /dev/null +++ b/csv-dump.mjs @@ -0,0 +1,22 @@ +export default class { + constructor(rows) { + this.rows = rows; + } + + download() { + // XXX: should escape various things, but the quick-n-dirty + // calls. + const data = this.rows.map(row => row.join(',')).join('\r\n'); + const file = new Blob([data], { type: 'text/csv' }) + let a = document.createElement('a'); + const url = URL.createObjectURL(file); + a.href = url; + a.download = 'filename.csv'; + document.body.appendChild(a); + a.click(); + setTimeout(_ => { + document.body.removeChild(a); + URL.revokeObjectURL(url); + }, 0); + } +} @@ -21,7 +21,7 @@ <input id='csv-input' name='csv-input' type='file' accept='text/csv' required> </form> - <button>download pnit csv</button> + <button id='csv-download'>download pnit csv</button> <table id='pnit-results'> <tr> <td>foo</td> diff --git a/pnit-form.mjs b/pnit-form.mjs index c1fa16c..f85457b 100644 --- a/pnit-form.mjs +++ b/pnit-form.mjs @@ -1,3 +1,5 @@ +import CSVDump from './csv-dump.mjs'; + const worker = new Worker('worker.js', { type: 'module' }); export default class extends HTMLElement { @@ -24,6 +26,10 @@ export default class extends HTMLElement { return inputs.flatMap(inp => Array.from(inp.files)); } + get downloadButton() { + return this.querySelector('#csv-download'); + } + get resultTable() { return document.getElementById('pnit-results'); } @@ -37,6 +43,11 @@ export default class extends HTMLElement { this.form.onchange = this.process.bind(this); this.form.onchange(); + + this.downloadButton.onclick = _ => { + const dumper = new CSVDump(this.results); + dumper.download(); + } } handleWorkerMessage(e) { @@ -46,6 +57,7 @@ export default class extends HTMLElement { col.innerText = v; row.appendChild(col); }); + this.results.push(e.data); this.resultTable.appendChild(row); } @@ -60,6 +72,7 @@ export default class extends HTMLElement { process() { console.debug('PNITForm#process', this); + this.results = []; this.resultTable.innerHTML = ''; if (isNaN(this.threshold)) { @@ -1,4 +1,4 @@ -import CSVParse from './csv.mjs'; +import CSVParse from './csv-parse.mjs'; let ignoreLines = 0; let sequenceNames = []; |
