-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.js
More file actions
98 lines (78 loc) · 3.27 KB
/
sort.js
File metadata and controls
98 lines (78 loc) · 3.27 KB
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
function makeTablesSortable(tableSelector = 'table') {
const tables = document.querySelectorAll(tableSelector);
tables.forEach(table => {
const headers = table.querySelectorAll('thead th');
headers.forEach((header, index) => {
// Add sorting cursor
header.style.cursor = 'pointer';
// Track sort direction
header.setAttribute('data-sort-direction', 'none');
header.addEventListener('click', () => {
// Get current sort direction
const currentDirection = header.getAttribute('data-sort-direction');
// Reset all headers
headers.forEach(h => {
h.setAttribute('data-sort-direction', 'none');
});
// Set new sort direction
let nextDirection = (currentDirection === 'none' || currentDirection === 'desc') ? 'asc' : 'desc';
header.setAttribute('data-sort-direction', nextDirection);
// Get the tbody and rows
const tbody = table.querySelector('tbody');
const rows = Array.from(tbody.querySelectorAll('tr'));
// Sort rows
rows.sort((rowA, rowB) => {
const cellA = rowA.querySelectorAll('td')[index];
const cellB = rowB.querySelectorAll('td')[index];
if (!cellA || !cellB) return 0;
let valueA = cellA.innerText.trim();
let valueB = cellB.innerText.trim();
// Check if values are numbers
const numA = parseFloat(valueA);
const numB = parseFloat(valueB);
if (!isNaN(numA) && !isNaN(numB)) {
return nextDirection === 'asc' ? numA - numB : numB - numA;
} else {
return nextDirection === 'asc'
? valueA.localeCompare(valueB)
: valueB.localeCompare(valueA);
}
});
// Reappend rows in new order
rows.forEach(row => {
tbody.appendChild(row);
});
});
});
});
}
// Sort meal buttons by shade on load (darker shades first)
const mealContainer = document.querySelector('.grid.grid-cols-2.md\\:grid-cols-3.gap-3');
if (mealContainer) {
// Select the .relative wrapper divs that contain the forms
const wrappers = Array.from(mealContainer.querySelectorAll('.relative'));
wrappers.sort((a, b) => {
const buttonA = a.querySelector('.meal-form button');
const buttonB = b.querySelector('.meal-form button');
if (!buttonA || !buttonB) return 0;
// Extract color class from class list
const colorA = Array.from(buttonA.classList).find(cls =>
cls.startsWith('border-orange-') || cls.startsWith('border-amber-') || cls.startsWith('border-yellow-'));
const colorB = Array.from(buttonB.classList).find(cls =>
cls.startsWith('border-orange-') || cls.startsWith('border-amber-') || cls.startsWith('border-yellow-'));
// Define color order (darker shades first)
const colorOrder = {
'border-orange-500': 1,
'border-amber-500': 2,
'border-yellow-500': 3
};
return (colorOrder[colorA] || 5) - (colorOrder[colorB] || 5);
});
wrappers.forEach(wrapper => {
mealContainer.appendChild(wrapper);
});
}
// Initialize on document ready
document.addEventListener('DOMContentLoaded', () => {
makeTablesSortable();
});