File size: 5,284 Bytes
d1a7111
 
 
 
 
 
4106f13
d1a7111
d91b022
 
 
 
 
 
 
2c21cf7
4106f13
d1a7111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd24d65
d1a7111
dd24d65
 
 
d1a7111
4106f13
dd24d65
d1a7111
 
 
 
 
 
 
dd24d65
 
d1a7111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd24d65
d1a7111
 
 
4106f13
 
 
 
 
 
 
 
d1a7111
 
 
 
4106f13
 
 
d1a7111
 
 
 
 
d91b022
 
 
 
9051509
d1a7111
5bcc69a
 
a3e21c6
d1a7111
4106f13
d1a7111
 
 
 
92d8154
d1a7111
 
 
 
dd24d65
4106f13
d1a7111
 
 
 
430bde6
d1a7111
 
 
 
 
4106f13
d1a7111
 
 
 
 
4106f13
d1a7111
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { DataTable } from 'primereact/datatable'
import { Column } from 'primereact/column'
import { FilterMatchMode } from 'primereact/api'
import { MultiSelect } from 'primereact/multiselect'
import { useState, useEffect } from 'react'
import { Slider } from 'primereact/slider'
import ScoreColumns from './ScoreColumns'

const LanguageTable = ({ data, selectedLanguages, setSelectedLanguages }) => {
  const [filters, setFilters] = useState({
    language_name: { value: null, matchMode: FilterMatchMode.EQUALS }, // via global search
    family: { value: null, matchMode: FilterMatchMode.IN },
    speakers: { value: null, matchMode: FilterMatchMode.BETWEEN }
  })

  const families = [...new Set(data.map(item => item.family))].slice(0, 10)
  families.push('Other')
  const familyRowFilterTemplate = options => {
    return (
      <MultiSelect
        value={options.value}
        options={families}
        onChange={e => {
          options.filterApplyCallback(e.value)
          setFilters(prevFilters => ({
            ...prevFilters,
            family: { value: e.value, matchMode: FilterMatchMode.IN }
          }))
        }}
        placeholder='All families'
      />
    )
  }

  const formatPopulation = population => {
    if (population === null) {
      return ''
    } else if (population < 1000) {
      return population.toFixed(0) + ''
    } else if (population < 10 * 1000) {
      return (population / 1000).toFixed(1) + 'K'
    } else if (population < 1000 * 1000) {
      return (population / 1000).toFixed(0) + 'K'
    } else if (population < 10 * 1000 * 1000) {
      return (population / 1000 / 1000).toFixed(1) + 'M'
    } else if (population < 1000 * 1000 * 1000) {
      return (population / 1000 / 1000).toFixed(0) + 'M'
    } else {
      return (population / 1000 / 1000 / 1000).toFixed(1) + 'B'
    }
  }

  const SliderWithLabel = ({ value, onChange }) => {
    const p = 10
    const min = 4
    const max = 9.3
    const start = value === null ? min : Math.log(value[0]) / Math.log(p)
    const stop = value === null ? max : Math.log(value[1]) / Math.log(p)
    const [_value, _setValue] = useState([start, stop])
    useEffect(() => {
      const timer = setTimeout(() => {
        onChange({
          value:
            _value[0] <= min + 0.1 && _value[1] >= max - 0.1
              ? null
              : [p ** _value[0], p ** _value[1]]
        })
      }, 1000)
      return () => clearTimeout(timer)
    }, [_value, onChange])
    return (
      <div style={{ minWidth: '20rem' }}>
        <div>{formatPopulation(p ** _value[0])}</div>
        <div>{formatPopulation(p ** _value[1])}</div>
        <Slider
          value={_value}
          onChange={e => _setValue(e.value)}
          placeholder='All sizes'
          min={min}
          max={max}
          step={0.01}
          range
          style={{ marginTop: '5rem' }}
        />
      </div>
    )
  }

  const speakerFilterTemplate = options => {
    return (
      <SliderWithLabel
        value={options.value}
        onChange={e => {
          options.filterApplyCallback(e.value)
          setFilters(prevFilters => ({
            ...prevFilters,
            speakers: { value: e.value, matchMode: FilterMatchMode.BETWEEN }
          }))
        }}
      />
    )
  }

  const speakerBodyTemplate = rowData => {
    const populationStr = formatPopulation(rowData.speakers)
    return <div style={{ textAlign: 'center' }}>{populationStr}</div>
  }

  const languageBodyTemplate = rowData => {
    return (
      <div>
        <div style={{ fontWeight: 'bold' }}>{rowData.autonym}</div>
        <div style={{ fontSize: '0.8rem', color: 'gray' }}>
          {rowData.language_name}
        </div>
      </div>
    )
  }

  return (
    <DataTable
      value={data.filter(
        item => !selectedLanguages.some(l => l.bcp_47 === item.bcp_47)
      )}
      header={<>Languages</>}
      sortField='speakers'
      removableSort
      filters={filters}
      filterDisplay='menu'
      selectionMode='checkbox'
      selection={selectedLanguages}
      onSelectionChange={e => setSelectedLanguages(e.value)}
      frozenValue={selectedLanguages}
      virtualScrollerOptions={{ itemSize: 60 }}
      scrollable
      scrollHeight='600px'
      id='language-table'
      style={{ width: '800px', minHeight: '650px' }}
    >
      <Column selectionMode='multiple' headerStyle={{ width: '3rem' }} />
      <Column
        field='language_name'
        header='Language'
        body={languageBodyTemplate}
        style={{ minWidth: '10rem' }}
        frozen
      />
      <Column
        field='speakers'
        header={<i className='pi pi-users' title='Speakers' />}
        headerTooltip='Number of speakers of the language (according to CLDR 2018)'
        body={speakerBodyTemplate}
        filter
        filterElement={speakerFilterTemplate}
        showFilterMatchModes={false}
        sortable
        style={{ minWidth: '5rem' }}
      />
      <Column
        field='family'
        header='Family'
        headerTooltip='Language family'
        filter
        showFilterMatchModes={false}
        filterElement={familyRowFilterTemplate}
        style={{ minWidth: '10rem' }}
      />
      {ScoreColumns}
    </DataTable>
  )
}

export default LanguageTable