-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-able.js
157 lines (119 loc) · 4.15 KB
/
sort-able.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
class SortTable extends HTMLTableElement {
static #KEY_ENTER = 13;
static #KEY_SPACE = 32;
static #KEY_ARROW_UP = 38;
static #KEY_ARROW_DOWN = 40;
static #DIRECTION_OF_KEY = Object.entries({
"default": [SortTable.#KEY_ENTER, SortTable.#KEY_SPACE],
"asc": [SortTable.#KEY_ARROW_UP],
"desc": [SortTable.#KEY_ARROW_DOWN],
});
static #COMPARATOR = {
asc: (a, b) => a.value.localeCompare(b.value),
desc: (a, b) => this.#COMPARATOR.asc(b, a)
}
//---Custom Element Lifecycle Hooks----------------------------------------
constructor() {
super();
}
connectedCallback() {
this.#setUp();
}
//---Generic utils---------------------------------------------------------
#lookup({ inTable, valueOf }) {
let tableEntry = inTable.find(([returnValue, candidates]) => candidates.includes(valueOf))
if (tableEntry) {
return tableEntry[0];
}
}
//---DOM utils-------------------------------------------------------------
#hasHeaderRow() {
return this.getElementsByTagName('thead').length !== 0
}
#getHeaderRow() {
return this.tHead?.rows[0];
}
#getTableBody() {
return this.tBodies[0];
}
#getHeaderCells() {
let tr = this.#getHeaderRow();
return tr ? [...tr.cells] : [];
}
//---Setup-----------------------------------------------------------------
#setUp() {
this.#ensureHeaderRow();
this.#getHeaderCells().forEach((th, index) => {
th.tabIndex = 0;
th.setAttribute("role", "button");
th.style.cursor = "pointer";
this.#on(th, "click", () => this.sortBy(index, "default"))
const keydownHandler = (direction = "default") => this.sortBy(index, direction)
this.#on(th, "keydown", (e) => this.#onApplicableKeyDown(e, keydownHandler));
});
}
#ensureHeaderRow() {
if (!this.#hasHeaderRow()) {
let headerElement = document.createElement('thead');
let firstRow = this.rows[0];
headerElement.appendChild(firstRow);
this.insertBefore(headerElement, this.firstChild);
}
}
#on(element, eventName, handler) {
element.addEventListener(eventName, handler, false);
}
#onApplicableKeyDown(event, sortBy) {
const key = event.which;
const ordering =
this.#lookup({ valueOf: key, inTable: SortTable.#DIRECTION_OF_KEY });
if (ordering) {
event.preventDefault();
sortBy(ordering);
}
}
//---Sorting---------------------------------------------------------------
sortBy(column, direction = "asc") {
let orderedByDirection =
SortTable.#COMPARATOR[this.#getSortingOrder(direction, column)];
let tbody = this.#getTableBody();
let rows = [...tbody.rows];
let sortedRows = rows.map((row) => {
return {
element: row,
value: this.#getValue(row, column)
}
}).sort(orderedByDirection);
sortedRows.forEach(bag => {
tbody.appendChild(bag.element);
})
let headerCells = this.#getHeaderCells();
headerCells.forEach((cell, index) => {
if (index == column) {
cell.dataset.sortAbleSortOrder = this.#getSortingOrder(direction, column);
} else {
delete cell.dataset.sortAbleSortOrder;
}
});
}
#getSortingOrder(direction, column) {
if (direction == "default") {
let currentOrder =
this.#getHeaderCells()[column].dataset.sortAbleSortOrder;
if (currentOrder === "asc") {
return "desc";
}
if (currentOrder === "desc") {
return "asc";
}
if (!currentOrder) {
return "asc";
}
}
return direction;
}
#getValue(row, column) {
return row.cells[column].innerText.trim();
}
}
customElements.define("sort-able", SortTable, { extends: "table" });