-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
124 lines (117 loc) · 4.27 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Books</title>
<style type="text/css">
html,
input {
font-family: Menlo, monospace;
font-size: 16px;
}
.sr-hidden {
clip: rect(1px, 1px, 1px, 1px);
clip-path: inset(50%);
height: 1px;
width: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
}
header {
display: flex;
align-items: center;
padding: 0.25rem 0.5rem;
}
input[name=filter] {
margin-left: 2rem;
}
table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}
tbody,
tr {
display: grid;
}
tr {
grid-template-columns: 1fr 5fr 3fr;
}
tr:nth-child(odd) {
background-color: #eee;
}
td {
padding: 0.25rem 0.5rem;
}
</style>
</head>
<body>
<header>
<h1>Books read since 1998</h1>
<label for="filter" class="sr-hidden">Filter books</label>
<input type="search" name="filter" placeholder="Filter" style="display: none">
</header>
<script src="https://d3js.org/d3-dsv.v1.min.js"></script>
<script src="https://d3js.org/d3-fetch.v1.min.js"></script>
<script type="text/javascript">
const FIRST_YEAR = 1998
const CURRENT_YEAR = 2024
const YEARS = Array(CURRENT_YEAR - FIRST_YEAR + 1).fill().map((_, i) => FIRST_YEAR + i).reverse()
const COLUMNS = ['Year', 'Title', 'Author']
Promise.all(
YEARS.map(year => {
return new Promise((resolve) => {
d3.csv(`${year}.csv`)
.then(rows => {
resolve({year, rows})
})
})
})
)
.then(years => {
return years.map(year => {
return year.rows.map(row => {
row.Year = year.year
return row
})
})
})
.then(years => years.map(year => year.reverse()))
.then(years => years.flat())
.then(rows => {
const table = document.createElement('table')
rows.forEach(row => {
const tr = table.insertRow(-1)
COLUMNS.forEach(key => {
const td = tr.insertCell(-1)
td.innerText = row[key]
td.classList.add(key)
})
})
const filter = document.querySelector('input[name=filter]')
filter.value = ''
filter.addEventListener('change', ({ target: { value } }) => {
if (value.trim() === '') {
table.querySelectorAll('tr').forEach(tr => {
tr.removeAttribute('style')
})
return
}
table.querySelectorAll('tr').forEach(tr => {
const isVisible = [...tr.querySelectorAll('td')]
.find(td => {
return td.innerText.toLowerCase()
.includes(value.toLowerCase())
})
tr.style.display = !isVisible ? 'none' : ''
})
})
document.body.appendChild(table)
filter.removeAttribute('style')
})
</script>
</body>
</html>