-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
183 lines (182 loc) · 5.06 KB
/
index.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// node_modules/@vanillaes/interpolate/index.js
function interpolate(template, tags = {}) {
const keys = Object.keys(tags);
const values = Object.values(tags);
try {
return new Function(...keys, `return \`${template}\`;`)(...values);
} catch (e) {
throw new TemplateException(template, tags, e);
}
}
var TemplateException = class extends Error {
constructor(template, tags, message) {
super();
this.name = "TemplateError";
let msg = "\n------------------\n";
msg += `Template: \`${template}\``;
msg += "\n------------------\n";
msg += `Tags: ${JSON.stringify(tags, null, 2)}`;
msg += "\n------------------\n";
msg += message;
this.message = msg;
}
};
// src/wc-sortable-table.js
var WCSortableTable = class extends HTMLElement {
static get observedAttributes() {
return ["src"];
}
attributeChangedCallback(name, oldValue, newValue) {
if (!this.__initialized) {
return;
}
if (oldValue !== newValue) {
this[name] = newValue;
}
}
get src() {
return this.getAttribute("src");
}
set src(value) {
this.setAttribute("src", value);
this.setSrc(value);
}
get value() {
return this.__data;
}
set value(value) {
this.setValue(value);
}
constructor() {
super();
this.__initialized = false;
this.__theme = null;
this.__data = null;
this.__table = null;
this.__column = null;
this.__headers = null;
this.__selected = null;
}
async connectedCallback() {
this.__table = document.createElement("table");
if (this.hasAttribute("theme")) {
this.__theme = document.createElement("template");
this.__theme.innerHTML = await this.getTheme();
this.attachShadow({mode: "open"});
this.shadowRoot.appendChild(this.__theme.content.cloneNode(true));
this.shadowRoot.appendChild(this.__table);
} else {
this.appendChild(this.__table);
}
if (this.hasAttribute("src")) {
this.setSrc();
}
this.__initialized = true;
}
async getTheme() {
const path = this.getAttribute("theme");
const contents = await this.fetchTheme(path);
return interpolate(contents);
}
async fetchTheme(src) {
const response = await fetch(src);
if (response.status !== 200)
throw Error(`ERR ${response.status}: ${response.statusText}`);
return response.text();
}
async setSrc() {
if (this.hasAttribute("src")) {
this.__data = await this.fetchSrc(this.src);
this.render();
}
}
async fetchSrc(src) {
const response = await fetch(src);
if (response.status !== 200)
throw Error(`ERR ${response.status}: ${response.statusText}`);
return response.json();
}
setValue(value) {
this.__data = value;
this.render();
}
sortData(data) {
if (this.__column === null) {
return data;
}
const col = this.__column;
const dir = this.__direction;
const comp = dir ? (a, b) => a < b : (a, b) => a > b;
return data.sort((a, b) => {
return comp(a[col], b[col]) ? -1 : 1;
});
}
headerClicked(e) {
this.__selected = e.target.cellIndex;
const column = e.target.cellIndex;
this.__direction = column !== this.__column ? true : !this.__direction;
this.__column = column;
this.render();
}
styleHeaders() {
const dir = this.__direction ? "asc" : "desc";
const notDir = this.__direction ? "asc" : "desc";
for (const th of this.__headers.children) {
if (th.cellIndex === this.__selected) {
if (th.classList.contains(notDir)) {
th.classList.remove(notDir);
} else {
th.classList.add(dir);
}
} else {
if (th.classList.contains(dir)) {
th.classList.remove(dir);
}
if (th.classList.contains(notDir)) {
th.classList.remove(notDir);
}
}
}
}
render() {
let data = [...this.__data];
const table = document.createElement("table");
this.__headers = document.createElement("tr");
this.__headers.addEventListener("click", (e) => this.headerClicked(e));
const headers = data.shift();
headers.forEach((header) => {
const th = document.createElement("th");
th.innerText = header;
this.__headers.appendChild(th);
});
this.styleHeaders();
const thead = document.createElement("thead");
thead.appendChild(this.__headers);
table.appendChild(thead);
data = this.sortData(data);
const tbody = document.createElement("tbody");
data.forEach((row) => {
const tr = document.createElement("tr");
row.forEach((cell) => {
const td = document.createElement("td");
td.innerText = cell;
tr.appendChild(td);
});
tbody.appendChild(tr);
});
table.appendChild(tbody);
if (this.__theme) {
this.shadowRoot.removeChild(this.__table);
this.__table = table;
this.shadowRoot.appendChild(this.__table);
} else {
this.removeChild(this.__table);
this.__table = table;
this.appendChild(this.__table);
}
}
};
customElements.define("wc-sortable-table", WCSortableTable);
export {
WCSortableTable
};