-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
159 lines (144 loc) · 4.21 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>
value-format-table by Damien Seguin (https://github.com/dmnsgn)
</title>
<style>
:root {
--color-dark: #404040;
--color-light: #f2f2f2;
--color-accent: #fd5e62;
}
body {
margin: 0;
overscroll-behavior: none;
font-family: sans-serif;
color: var(--color-dark);
background-color: var(--color-light);
}
main {
padding: 0 20px;
}
table,
th,
td {
border: 1px solid;
}
th,
td {
padding: 10px;
}
th:not(:last-of-type) {
text-align: left;
}
</style>
</head>
<body>
<main>
<h1>value-format-table</h1>
<p>
Data:
<a
href="https://public.opendatasoft.com/explore/dataset/fromagescsv-fromagescsv/information/"
>List of french cheese by department</a
>. 🇫🇷 🧀
</p>
<label>
Department:
<select class="Select"></select>
</label>
<h2>HTML</h2>
<div class="ResultsHtml"></div>
<h2>Markdown</h2>
<pre class="ResultsMd"></pre>
<h2>Text</h2>
<pre class="ResultsText"></pre>
</main>
<script type="module">
import {
valueFormatTableAsText,
valueFormatTableAsMarkdown,
valueFormatTableAsHTML,
} from "./index.js";
const deparmentSelectElement = document.querySelector(".Select");
const resultsHtmlElement = document.querySelector(".ResultsHtml");
const resultsTextElement = document.querySelector(".ResultsText");
const resultsMdElement = document.querySelector(".ResultsMd");
// Load data
const dataset = await (
await fetch(
new URL("./examples/fromages-francais.json", import.meta.url),
)
).json();
// Parse data
const fromagesPerDepartment = Object.groupBy(
dataset,
({ departement }) => departement,
);
// Render
const toTitleCase = (str) =>
str
.replaceAll("_", " ")
.replace(
/\w\S*/g,
(text) =>
text.charAt(0).toUpperCase() + text.substring(1).toLowerCase(),
);
const toLink = (href) => (!!href ? `<a href="${href}">Link</a>` : "");
const toMarkdownLink = (href) => (!!href ? `[Link](${href})` : "");
const update = (data) => {
console.log(data);
const htmlTable = valueFormatTableAsHTML(data, {
formatHeader: toTitleCase,
columns: [
null,
null,
{ formatCell: toLink },
{ formatCell: toLink },
null,
null,
{
formatCell: (geoPoint) =>
`lat: ${geoPoint?.lat}, lon: ${geoPoint?.lon}`,
},
],
});
resultsHtmlElement.innerHTML = htmlTable;
console.log(htmlTable);
const mdTable = valueFormatTableAsMarkdown(data, {
formatHeader: toTitleCase,
columns: [
{ align: "right" },
{ align: "right" },
{ formatCell: toMarkdownLink },
{ formatCell: toMarkdownLink },
],
});
resultsMdElement.innerText = mdTable;
const textTable = valueFormatTableAsText(data, {
formatHeader: toTitleCase,
columns: [{ align: "right" }, { align: "right" }],
});
resultsTextElement.innerHTML = textTable;
console.log(textTable);
};
// Events
deparmentSelectElement.addEventListener("input", ({ target }) => {
const fromages = fromagesPerDepartment[target.value];
update(fromages);
});
deparmentSelectElement.innerHTML = Object.keys(fromagesPerDepartment)
.sort()
.map(
(department) =>
`<option value="${department}">${department}</option>`,
);
deparmentSelectElement.value = "Aube";
deparmentSelectElement.dispatchEvent(new InputEvent("input"));
</script>
</body>
</html>