diff options
| author | adamhrv <adam@ahprojects.com> | 2018-12-16 19:38:54 +0100 |
|---|---|---|
| committer | adamhrv <adam@ahprojects.com> | 2018-12-16 19:38:54 +0100 |
| commit | 23e9fef5dce8b0b15dd94713816b9d7d45f12356 (patch) | |
| tree | 3ca9ffe3adce76318450991bfc613073470b604c /client/tables.js | |
| parent | 759027d5fbfd6665082f72a3ceaeef68c2d2142e (diff) | |
| parent | 6431d06048791763f3644b3a0457cc9c4f1df6d3 (diff) | |
Merge branch 'master' of github.com:adamhrv/megapixels_dev
Diffstat (limited to 'client/tables.js')
| -rw-r--r-- | client/tables.js | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/client/tables.js b/client/tables.js new file mode 100644 index 00000000..a30abc32 --- /dev/null +++ b/client/tables.js @@ -0,0 +1,75 @@ +import Tabulator from 'tabulator-tables' +import csv from 'parse-csv' + +const datasetColumns = [ + { title: 'Title', field: 'title', sorter: 'string' }, + { title: 'Images', field: 'images', sorter: 'number' }, + { title: 'People', field: 'people', sorter: 'number' }, + { title: 'Year', field: 'year', sorter: 'number' }, + { title: 'Citations', field: 'citations', sorter: 'number' }, + { title: 'Influenced', field: 'influenced', sorter: 'number' }, + // { title: 'Origin', field: 'origin', sorter: 'string' }, +] +const citationsColumns = [ + { title: 'Title', field: 'title', sorter: 'string' }, + { title: 'Institution', field: 'institution', sorter: 'string' }, +] + +function getColumns(payload) { + let { cmd, url, fields } = payload + if (cmd === 'citations') { + return citationsColumns + } + if (url && url.match('datasets.csv')) { + return datasetColumns + } + return ((fields && fields.length) ? fields[0] : '').split(', ').map(field => { + switch (field) { + default: + return { title: field, field: field.toLowerCase(), sorter: 'string' } + } + }) +} + +function getCitations(dataset) { + console.log(dataset.citations) + return dataset.citations.map(c => ({ + title: c[0], + institution: c[2], + })) +} + +export default function append(el, payload) { + const columns = getColumns(payload) + console.log(columns) + const table = new Tabulator(el, { + height: '311px', + layout: 'fitColumns', + placeholder: 'No Data Set', + columns, + }) + // let path = payload.opt + // console.log(path, columns) + + console.log(payload.cmd, payload.url, payload.dataset) + if (payload.cmd === 'citations') { + let { data } = payload + const citations = getCitations(data) + console.log(citations) + table.setData(citations) + } else { + fetch(payload.url, { mode: 'cors' }) + .then(r => r.text()) + .then(text => { + try { + const data = csv.toJSON(text, { headers: { included: true } }) + // console.log(data) + table.setData(data) + } catch (e) { + console.error("error parsing json:", payload.url) + console.error(e) + // console.log(text) + } + }) + } +} |
