-
Notifications
You must be signed in to change notification settings - Fork 9
/
highlight the sorted column with background color change
385 lines (334 loc) · 11.5 KB
/
highlight the sorted column with background color change
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scaler Leaderboard</title>
<style>
/* General Styles */
body,
html {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
transition: background-color 0.3s, color 0.3s;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
opacity: 0;
transition: opacity 1s ease-in-out;
}
/* Header */
h1 {
text-align: center;
margin-bottom: 20px;
}
/* Toggle Button */
.dark-mode-toggle {
cursor: pointer;
padding: 10px 15px;
border: none;
background-color: #4caf50;
color: white;
border-radius: 4px;
transition: all 0.3s ease;
}
.dark-mode-toggle:hover {
background-color: #388e3c;
transform: scale(1.05);
}
/* Search Bar */
.search-container {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
.search-container input {
padding: 10px;
width: 70%;
font-size: 16px;
border: none;
border-radius: 4px;
}
/* Table */
table {
width: 100%;
border-collapse: collapse;
background-color: #1a1a1a;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
border-radius: 8px;
overflow: hidden;
transition: background-color 0.3s;
}
th,
td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #333;
}
th {
background-color: #2c2c2c;
font-weight: bold;
cursor: pointer;
position: relative;
}
th.sorted-asc::after,
th.sorted-desc::after {
content: "▲";
font-size: 10px;
position: absolute;
right: 10px;
transform: rotate(0deg);
transition: transform 0.2s;
}
th.sorted-desc::after {
transform: rotate(180deg);
}
/* Highlighted Column */
.highlighted {
background-color: #4caf50; /* Highlight color */
color: white; /* Text color for better contrast */
}
/* Row Hover Effect */
tr:hover {
background-color: #333;
transition: background-color 0.3s ease;
}
/* Tooltip */
.rating:hover::after {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
background-color: rgba(0, 0, 0, 0.7);
color: #fff;
padding: 5px;
border-radius: 4px;
white-space: nowrap;
font-size: 12px;
margin-bottom: 8px;
}
/* Dark Mode Styles */
body.dark-mode {
background-color: white;
color: black;
}
body.dark-mode .dark-mode-toggle {
background-color: #f44336;
color: black;
}
/* Rating Colors */
.gray {
color: #a0a0a0;
}
.green {
color: #4caf50;
}
.blue {
color: #2196f3;
}
.yellow {
color: #ffc107;
}
.red {
color: #f44336;
}
.purple {
color: #9c27b0;
}
/* Spinner Styles */
.spinner {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 8px solid #f3f3f3;
border-top: 8px solid #3498db;
border-radius: 50%;
width: 60px;
height: 60px;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/* Responsive Styles */
@media (max-width: 600px) {
.container {
padding: 10px;
}
h1 {
font-size: 1.5em;
}
.dark-mode-toggle,
button {
width: 100%;
margin-bottom: 10px;
}
.search-container {
flex-direction: column;
align-items: flex-start;
}
.search-container input {
width: 100%;
margin-bottom: 10px;
}
table {
font-size: 14px;
}
th, td {
padding: 8px;
}
th.sorted-asc::after,
th.sorted-desc::after {
right: 5px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Scaler Leaderboard</h1>
<div class="toggle-container">
<button class="dark-mode-toggle" onclick="toggleDarkMode()">Toggle Dark Mode</button>
<div class="search-container">
<input type="text" id="searchInput" placeholder="Search by name..." aria-label="Search by name" onkeyup="filterLeaderboard()">
</div>
<button onclick="resetScores()">Reset Scores</button>
</div>
<table aria-label="Leaderboard">
<thead>
<tr>
<th onclick="sortTable(0)">Name</th>
<th onclick="sortTable(1)">CodeChef</th>
<th onclick="sortTable(2)">Codeforces</th>
<th onclick="sortTable(3)">AtCoder</th>
<th onclick="sortTable(4)">LeetCode</th>
</tr>
</thead>
<tbody id="leaderboardBody"></tbody>
</table>
</div>
<!-- Spinner Element -->
<div class="spinner" id="loadingSpinner"></div>
<script>
// JavaScript Enhancements
const users = [
{ name: "Alice", codechef: 1500, codeforces: 1200, atcoder: 800, leetcode: 2000 },
{ name: "Bob", codechef: 2000, codeforces: 1800, atcoder: 1600, leetcode: 2500 },
{ name: "Charlie", codechef: 2500, codeforces: 2200, atcoder: 2000, leetcode: 3000 },
{ name: "David", codechef: 1800, codeforces: 1600, atcoder: 1400, leetcode: 2200 },
{ name: "Eve", codechef: 3000, codeforces: 2800, atcoder: 2400, leetcode: 3500 },
];
let ascendingOrder = true;
function showSpinner() {
document.getElementById("loadingSpinner").style.display = "block";
}
function hideSpinner() {
document.getElementById("loadingSpinner").style.display = "none";
}
function getRatingColor(rating) {
if (rating < 0) return "gray"; // Negative ratings
if (rating < 1200) return "gray";
if (rating < 1400) return "green";
if (rating < 1600) return "blue";
if (rating < 1900) return "yellow";
if (rating < 2100) return "red";
return "purple";
}
function populateLeaderboard() {
const tbody = document.getElementById("leaderboardBody");
tbody.innerHTML = "";
users.forEach(user => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${user.name}</td>
<td><span class="rating ${getRatingColor(user.codechef)}">${user.codechef}</span></td>
<td><span class="rating ${getRatingColor(user.codeforces)}">${user.codeforces}</span></td>
<td><span class="rating ${getRatingColor(user.atcoder)}">${user.atcoder}</span></td>
<td><span class="rating ${getRatingColor(user.leetcode)}">${user.leetcode}</span></td>
`;
tbody.appendChild(row);
});
hideSpinner();
}
function toggleDarkMode() {
document.body.classList.toggle("dark-mode");
}
function sortTable(columnIndex) {
showSpinner();
setTimeout(() => {
ascendingOrder = !ascendingOrder;
const fields = ["name", "codechef", "codeforces", "atcoder", "leetcode"];
// Remove highlight from all headers
const headers = document.querySelectorAll("th");
headers.forEach(header => header.classList.remove("highlighted"));
// Highlight the current header
headers[columnIndex].classList.add("highlighted");
// Sort the users array
users.sort((a, b) => {
const aValue = a[fields[columnIndex]];
const bValue = b[fields[columnIndex]];
return ascendingOrder ? (aValue < bValue ? -1 : 1) : (aValue > bValue ? -1 : 1);
});
populateLeaderboard();
}, 300);
}
function filterLeaderboard() {
showSpinner();
setTimeout(() => {
const query = document.getElementById("searchInput").value.toLowerCase();
const tbody = document.getElementById("leaderboardBody");
tbody.innerHTML = "";
users
.filter(user => user.name.toLowerCase().includes(query))
.forEach(user => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${user.name}</td>
<td><span class="rating ${getRatingColor(user.codechef)}">${user.codechef}</span></td>
<td><span class="rating ${getRatingColor(user.codeforces)}">${user.codeforces}</span></td>
<td><span class="rating ${getRatingColor(user.atcoder)}">${user.atcoder}</span></td>
<td><span class="rating ${getRatingColor(user.leetcode)}">${user.leetcode}</span></td>
`;
tbody.appendChild(row);
});
hideSpinner();
}, 300);
}
// Function to Add Test Cases
function addTestCases() {
users.push(
{ name: "Frank", codechef: -100, codeforces: 1200, atcoder: 1500, leetcode: 1300 }, // Negative score
{ name: "Grace", codechef: 0, codeforces: 0, atcoder: 0, leetcode: 0 }, // Edge case with zero
{ name: "Hannah", codechef: 1500, codeforces: 1500, atcoder: 1500, leetcode: 1500 } // All ratings the same
);
populateLeaderboard();
}
// Function to Reset Scores
function resetScores() {
users.forEach(user => {
user.codechef = 0;
user.codeforces = 0;
user.atcoder = 0;
user.leetcode = 0;
});
populateLeaderboard();
}
window.addEventListener("load", () => {
document.querySelector(".container").style.opacity = "1";
populateLeaderboard();
addTestCases(); // Populate with test cases on load
});
</script>
</body>
</html>