blob: b98334d50e43239b8ae11847f2309e3eacc0bf9e (
plain)
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
|
"use strict";
function sort_table_column(table, column) {
const minute = 60000;
const hour = 60 * minute;
const day = 24 * hour;
const week = 7 * day;
function is_date(s) {
if (s.match(/^\d{4}-\d{2}-\d{2}$/))
return true;
if (s.match(/^\d+ (minutes?|hours?|days|weeks) ago$/))
return true;
if (s.match(/^(Yesterday|now)$/))
return true;
return false;
}
function parse_date(s) {
if (s.match(/^\d{4}-\d{2}-\d{2}$/)) return new Date(s).valueOf();
if (s === 'now') return Date.now();
if (s === 'Yesterday') return Date.now() - day;
let [ _, value, unit ] = s.match(/^(\d+) (minutes?|hours?|days|weeks) ago$/);
switch (unit) {
default: unit = 0; break;
case 'minute': case 'minutes': unit = minute; break;
case 'hours': case 'hours': unit = hour; break;
case 'days': unit = day; break;
case 'weeks': unit = week; break;
}
return Date.now() - Number(value) * unit;
}
let tbody = table.querySelector("tbody");
let rows = Array.from(tbody.querySelectorAll("tr"));
rows.sort((row_a, row_b) => {
let cell_a = row_a.querySelectorAll("td")[column].textContent;
let cell_b = row_b.querySelectorAll("td")[column].textContent;
if (is_date(cell_a) && is_date(cell_b)) {
let age_a = parse_date(cell_a);
let age_b = parse_date(cell_b);
if (age_a > age_b) return -1;
if (age_a < age_b) return 1;
return 0;
} else if (cell_a.match(/^\d+$/) && cell_b.match(/^\d+$/)) {
cell_a = Number(cell_a);
cell_b = Number(cell_b);
if (cell_a > cell_b) return -1;
if (cell_a < cell_b) return 1;
return 0;
} else {
if (cell_a > cell_b) return 1;
if (cell_a < cell_b) return -1;
return 0;
}
});
rows.forEach(row => tbody.appendChild(row));
}
document.querySelectorAll("table.sort").forEach(table => {
table.querySelectorAll("th").forEach((th, column) => {
if (th.textContent !== "") {
th.addEventListener("click", evt => sort_table_column(table, column));
th.style.cursor = "pointer";
}
});
});
|