-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv-viewer.js
executable file
·165 lines (147 loc) · 4.71 KB
/
csv-viewer.js
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
import { CSV } from "https://code4sabae.github.io/js/CSV.js";
import IMIMojiConverter from "https://code4sabae.github.io/imi-moji-converter-es/IMIMojiConverter.mjs";
import { ArrayUtil } from "./ArrayUtil.js";
const std = (s) => IMIMojiConverter.toHalfWidth(s).toLowerCase();
//const EMBED_IMAGE_DEFAULT = true;
const EMBED_IMAGE_W = 100; // 300;
const EMBED_IMAGE_H = 100; // 225;
const USE_RESIZED_IMAGE = false; // if true, define getResizedImage
const create = (tag) => document.createElement(tag);
const clear = (ele) => ele.innerHTML = "";
const main = async (parent) => {
const showTable = function (p, csv, sfilter, sortidx, sortorder) {
sfilter = std(sfilter);
let array = [];
array.push(csv[0]);
if (sfilter.length == 0) {
for (let i = 1; i < csv.length; i++) {
array.push(csv[i]);
}
} else {
const afilter = sfilter.split(" ");
for (let i = 1; i < csv.length; i++) {
const ar = csv[i];
let flg = false;
for (let k = 0; k < afilter.length; k++) {
const af = afilter[k];
flg = false;
for (let j = 0; j < ar.length; j++) {
if (std(ar[j]).indexOf(af) >= 0) {
flg = true;
break;
}
}
if (!flg) {
break;
}
}
if (flg) {
array.push(ar);
}
}
}
const csvmode = true;
array = ArrayUtil.getSorted(array, sortidx, sortorder, csvmode);
const tbl = create("table");
const tr = create("tr");
const td = create("th");
td.textContent = "-";
tr.appendChild(td);
const dd = array[0];
for (let j = 0; j < dd.length; j++) {
const td = create("th");
const val = dd[j];
const lbl = create("span");
lbl.textContent = val;
td.appendChild(lbl);
const up = create("span");
up.idx = j;
up.className = "sort";
up.textContent = "▲";
up.onclick = function () {
showTable(p, array, sfilter, this.idx, true);
};
td.appendChild(up);
const down = create("span");
down.idx = j;
down.className = "sort";
down.textContent = "▼";
down.onclick = function () {
showTable(p, array, sfilter, this.idx, false);
};
td.appendChild(down);
tr.appendChild(td);
}
tbl.appendChild(tr);
for (let i = 1; i < array.length; i++) {
const dd = array[i];
const tr = create("tr");
const td = create("td");
td.textContent = i;
tr.appendChild(td);
for (let j = 0; j < dd.length; j++) {
const td = create("td");
let val = dd[j];
if (val.startsWith("http://") || val.startsWith("https://")) {
let s = "";
val = val.replace(/</g, "<");
val = val.replace(/>/g, ">");
// if (val.toLowerCase().endsWith(".jpg") || val.toLowerCase().endsWith(".png")) {
if (val.toLowerCase().endsWith(".jpg")) {
if (parent.getAttribute("embedimage") != "false") {
if (USE_RESIZED_IMAGE) {
s = "<img src='" +
getResizedImageURL(val, EMBED_IMAGE_W, EMBED_IMAGE_H) + "'><br>";
s += "<a href=" + val + ">" + val + "</a>";
} else {
s = "<img src='" + val + "'><br>";
s += "<a href=" + val + ">" + val + "</a>";
}
} else {
s += "<a href=" + val + ">" + val + "</a>";
}
} else {
s += "<a href=" + val + ">" + val + "</a>";
}
td.innerHTML = s;
} else {
val = val.replace(/</g, "<");
val = val.replace(/>/g, ">");
val = val.replace(/\\n/g, "<br>");
td.innerHTML = val;
}
tr.appendChild(td);
}
tbl.appendChild(tr);
}
clear(p);
p.appendChild(tbl);
};
const url = parent.getAttribute("src");
const name = parent.getAttribute("name") || url;
const q = parent.getAttribute("q") || "";
const csv = await CSV.fetch(url);
const filter = create("input");
filter.className = "filter";
filter.placeholder = "フィルター";
filter.value = q;
parent.appendChild(filter);
const tbl = create("div");
parent.appendChild(tbl);
showTable(tbl, csv, filter.value);
filter.onchange = filter.onkeyup = () => showTable(tbl, csv, filter.value);
const data = create("div");
data.innerHTML = `DATA: <a href=${url}>${name}</a>`;
parent.appendChild(data);
};
class CSVViewer extends HTMLElement {
constructor(param) {
super();
for (const name in param) {
this.setAttribute(name, param[name]);
}
main(this);
}
}
customElements.define("csv-viewer", CSVViewer);
export { CSVViewer };