-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblems-loading.js
More file actions
159 lines (142 loc) · 5.45 KB
/
problems-loading.js
File metadata and controls
159 lines (142 loc) · 5.45 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
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
document.addEventListener("DOMContentLoaded", async function () {
const tbody = document.getElementById("problems-table");
try {
const response = await fetch("./questions.json");
const data = await response.json();
questions = data.questions;
tbody.innerHTML = questions
.map(
(question) => `
<tr>
<td>${question.title}</td>
<td>${question.date}</td>
<td>${question.level}</td>
<td>${question.tags.map((tag) => `<div class="table-tag">${tag}</div>`).join("")}</td>
<td><a href="${question.url}">${question.source}</a></td>
</tr>
`,
)
.join("");
} catch (error) {
console.error("Error loading JSON data:", error);
}
});
// Function to convert table data to CSV
function downloadCSV() {
const table = document.getElementById("problems-table");
let csvContent = "data:text/csv;charset=utf-8,";
// Add CSV headers
csvContent += "Title,Date,Level,Tags, Source,Link\n";
// Iterate over table rows
table.querySelectorAll("tr").forEach((row) => {
let rowData = [];
row.querySelectorAll("td").forEach((cell, index) => {
let text = cell.innerText.trim();
if (index === 3) {
// Tags column
const tags = Array.from(cell.querySelectorAll(".table-tag"))
.map((tag) => tag.innerText.trim())
.join("; "); // Join multiple tags with a semicolon
text = `"${tags.replace(/"/g, '""')}"`; // Escape quotes
}
if (index === 4) {
// Extract Source (anchor text) & Link (href)
const anchor = cell.querySelector("a");
const sourceText = anchor ? anchor.innerText.trim() : "";
const sourceLink = anchor ? anchor.href : "";
rowData.push(`"${sourceText}"`); // Source column
rowData.push(`"${sourceLink}"`); // Link column
return; // Skip pushing 'text' since we handle both source and link separately
}
rowData.push(`"${text.replace(/"/g, '""')}"`); // Escape quotes
});
csvContent += rowData.join(",") + "\n";
});
// Create a temporary download link
const encodedUri = encodeURI(csvContent);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "problems.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
// Attach event listener to the download button
document.getElementById("download-btn").addEventListener("click", downloadCSV);
let inorderSorting = true;
document
.getElementById("sortingLevel")
.addEventListener("click", async function () {
inorderSorting = !inorderSorting; // Toggle state
const ascIcon = document.getElementById("asc-icon");
const descIcon = document.getElementById("desc-icon");
const tbody = document.getElementById("problems-table");
try {
const response = await fetch("./questions.json");
const data = await response.json();
const levelOrder = { easy: 1, medium: 2, hard: 3 };
// Sort in ascending or descending order based on `inorderSorting`
const sortedQuestions = data.questions.sort((a, b) => {
return inorderSorting
? levelOrder[a.level.toLowerCase()] -
levelOrder[b.level.toLowerCase()]
: levelOrder[b.level.toLowerCase()] -
levelOrder[a.level.toLowerCase()];
});
tbody.innerHTML = sortedQuestions
.map(
(question) => `
<tr>
<td>${question.title}</td>
<td>${question.date}</td>
<td>${question.level}</td>
<td>${question.tags.map((tag) => `<div class="table-tag">${tag}</div>`).join("")}</td>
<td><a href="${question.url}">${question.source}</a></td>
</tr>
`,
)
.join("");
// Toggle SVG visibility
ascIcon.style.display = inorderSorting ? "inline" : "none";
descIcon.style.display = inorderSorting ? "none" : "inline";
} catch (error) {
console.error("Error loading JSON data:", error);
}
});
document
.getElementById("sorting-by-date")
.addEventListener("click", async function () {
inorderSorting = !inorderSorting; // Toggle state
const ascIcon = document.getElementById("asc-icon-for-date");
const descIcon = document.getElementById("desc-icon-for-date");
const tbody = document.getElementById("problems-table");
try {
const response = await fetch("./questions.json");
const data = await response.json();
const sortedQuestions = data.questions.sort((a, b) => {
const dateA = new Date(a.date);
const dateB = new Date(b.date);
return inorderSorting
? dateA - dateB // Ascending order
: dateB - dateA; // Descending order
});
tbody.innerHTML = sortedQuestions
.map(
(question) => `
<tr>
<td>${question.title}</td>
<td>${question.date}</td>
<td>${question.level}</td>
<td>${question.tags.map((tag) => `<div class="table-tag">${tag}</div>`).join("")}</td>
<td><a href="${question.url}">${question.source}</a></td>
</tr>
`,
)
.join("");
// Toggle SVG visibility
ascIcon.style.display = inorderSorting ? "inline" : "none";
descIcon.style.display = inorderSorting ? "none" : "inline";
} catch (error) {
console.error("Error loading JSON data:", error);
}
});