summaryrefslogtreecommitdiff
path: root/client/tables.js
diff options
context:
space:
mode:
authorAdam Harvey <adam@ahprojects.com>2018-12-23 01:37:03 +0100
committerAdam Harvey <adam@ahprojects.com>2018-12-23 01:37:03 +0100
commit4452e02e8b04f3476273574a875bb60cfbb4568b (patch)
tree3ffa44f9621b736250a8b94da14a187dc785c2fe /client/tables.js
parent2a65f7a157bd4bace970cef73529867b0e0a374d (diff)
parent5340bee951c18910fd764241945f1f136b5a22b4 (diff)
.
Diffstat (limited to 'client/tables.js')
-rw-r--r--client/tables.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/client/tables.js b/client/tables.js
new file mode 100644
index 00000000..b4c13887
--- /dev/null
+++ b/client/tables.js
@@ -0,0 +1,81 @@
+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)
+ el.classList.add('loaded')
+ } 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)
+ el.classList.add('loaded')
+ } catch (e) {
+ console.error("error making json:", payload.url)
+ console.error(e)
+ // console.log(text)
+ }
+ })
+ }
+
+ if (fields.length > 1 && fields[1].indexOf('filter')) {
+ const filter = fields[1].split(' ')
+ }
+}