summaryrefslogtreecommitdiff
path: root/client/tables.js
blob: 9eedbe18ab2d6c1b076d6f627ae55ed5b8ee5ff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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' },
  { title: 'Country', field: 'country', sorter: 'string', width: 140 },
  { title: 'Year', field: 'year', sorter: 'number', width: 70 },
  { title: 'PDF', field: 'pdf', formatter: 'link',
    formatterParams: { target: "_blank", urlField: 'pdf', },
    sorter: 'string', width: 100 },
]

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)
  console.log(dataset.citations.map(d => [d.pdf, d.doi]))
  return dataset.citations.map(citation => ({
    title: citation.title,
    institution: citation.addresses[0].name,
    country: citation.addresses[0].country,
    year: citation.year,
    pdf: (citation.pdf && citation.pdf.length)
      ? citation.pdf[0]
      : (citation.doi && citation.doi.length)
        ? citation.doi[0]
        : "",
  }))
}

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)

  if (payload.cmd === 'citations') {
    let { data } = payload
    if (!data) return null
    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 {
          // console.log(text)
          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 && fields.length > 1 && fields[1].indexOf('filter')) {
  //   const filter = fields[1].split(' ')
  // }
}