-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
342 lines (286 loc) · 14 KB
/
scripts.js
File metadata and controls
342 lines (286 loc) · 14 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
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
// Helper function to fetch and parse CSV files
async function fetchAndParseCSV(filePath) {
const response = await fetch(filePath);
if (!response.ok) {
throw new Error(`Failed to fetch ${filePath}: ${response.statusText}`);
}
const csvText = await response.text();
return parseCSVText(csvText);
}
// CSV parser for semicolon-delimited files, handles RFC 4180 style quoting
function parseCSVText(csvText) {
const lines = csvText.trim().split(/\r?\n/);
if (lines.length === 0) return [];
const headers = lines[0].split(';').map(h => h.trim());
const data = [];
for (let i = 1; i < lines.length; i++) {
if (lines[i].trim() === '') continue; // Skip empty lines
const values = [];
let currentField = '';
let inQuotes = false;
for (let j = 0; j < lines[i].length; j++) {
const char = lines[i][j];
if (char === '"') {
if (inQuotes && j + 1 < lines[i].length && lines[i][j + 1] === '"') {
currentField += '"'; // Escaped double quote (e.g., "" becomes ")
j++; // Skip the next quote
} else {
inQuotes = !inQuotes; // Toggle quoted state (these quotes are structural)
}
} else if (char === ';' && !inQuotes) { // Semicolon as delimiter
values.push(currentField);
currentField = '';
} else {
currentField += char;
}
}
values.push(currentField); // Add the last field for the line
const entry = {};
headers.forEach((header, index) => {
// The currentField already has escaped quotes handled and structural quotes removed.
// We just need to trim it.
entry[header] = values[index] ? values[index].trim() : '';
});
data.push(entry);
}
return data;
}
// Process raw data from CSVs into a structured format
function processRawData(classesArray, modulesArray, unitsArray) {
// Group units by ModuleID for easy lookup
const unitsByModule = new Map();
unitsArray.forEach(unit => {
if (!unitsByModule.has(unit.ModuleID)) {
unitsByModule.set(unit.ModuleID, []);
}
unitsByModule.get(unit.ModuleID).push(unit);
});
// Enrich modules with their units and prepare accordion data
const processedModules = modulesArray.map(module => {
const moduleUnits = unitsByModule.get(module.ModuleID) || [];
const accordionDataString = moduleUnits
.map(u => `${u.UnitName}:${u.UnitDescription || 'Details not available.'}`)
.join('|');
// MajorClassIDs is read as a single string, e.g., "TüKITZmed+TüKITZlife"
// Then split here.
const majorClassIDs = module.MajorClassIDs ? module.MajorClassIDs.split('+').map(id => id.trim()) : [];
return {
...module, // Includes ModuleID, ModuleName, ModuleDescription, Category, Difficulty
majorClassIDs: majorClassIDs,
units: moduleUnits,
accordionDataString: accordionDataString
};
});
return {
classes: classesArray, // For rendering filter buttons
modules: processedModules,
};
}
// Render major class filter buttons
function renderFilterButtons(classesData, headerElement) {
const difficultyFilter = headerElement.querySelector('#difficulty-filter'); // Insert buttons before this
classesData.forEach(classInfo => {
const button = document.createElement('button');
button.classList.add('study-button', classInfo.ClassID);
button.textContent = classInfo.ButtonText;
if (classInfo.Color) {
button.style.backgroundColor = classInfo.Color;
}
button.dataset.filterClass = classInfo.ClassID; // Used for filtering logic
headerElement.insertBefore(button, difficultyFilter);
});
}
// Render course tiles into their respective topic containers
function renderCourseTiles(modulesData, classesData) {
// Clear any placeholder content if necessary (though HTML is now empty)
document.getElementById('math-container').innerHTML = '';
document.getElementById('ml-container').innerHTML = '';
document.getElementById('applications-container').innerHTML = '';
const classColorMap = new Map(classesData.map(c => [c.ClassID, c.Color]));
modulesData.forEach(module => {
const tile = document.createElement('div');
tile.classList.add('course-tile', module.Difficulty.toLowerCase());
module.majorClassIDs.forEach(classId => tile.classList.add(classId));
tile.dataset.topic = module.Category;
tile.dataset.accordion = module.accordionDataString;
tile.dataset.moduleId = module.ModuleID; // For easy data retrieval on click
// Course Header
const headerDiv = document.createElement('div');
headerDiv.classList.add('course-header');
headerDiv.textContent = module.ModuleName;
tile.appendChild(headerDiv);
// Course Labels (Units)
const labelsDiv = document.createElement('div');
labelsDiv.classList.add('course-labels');
module.units.forEach(unit => {
const label = document.createElement('div');
label.classList.add('label');
label.textContent = unit.UnitName;
labelsDiv.appendChild(label);
});
tile.appendChild(labelsDiv);
// Membership Indicators
const indicatorsDiv = document.createElement('div');
indicatorsDiv.classList.add('membership-indicators');
module.majorClassIDs.forEach(classId => {
const indicator = document.createElement('div');
indicator.classList.add('indicator', classId);
const color = classColorMap.get(classId);
if (color) {
indicator.style.backgroundColor = color; // Ensures color even if CSS is not specific enough
}
indicatorsDiv.appendChild(indicator);
});
tile.appendChild(indicatorsDiv);
// Skill Level Bars
const skillLevelDiv = document.createElement('div');
skillLevelDiv.classList.add('skill-level');
for (let i = 0; i < 3; i++) {
const bar = document.createElement('div');
bar.classList.add('bar');
// CSS handles filling based on .basic, .advanced, .expert class on tile
skillLevelDiv.appendChild(bar);
}
tile.appendChild(skillLevelDiv);
// Append tile to the correct topic container
let container;
if (module.Category.toLowerCase() === 'mathematics') container = document.getElementById('math-container');
else if (module.Category.toLowerCase() === 'machine learning') container = document.getElementById('ml-container');
else if (module.Category.toLowerCase() === 'applications') container = document.getElementById('applications-container');
if (container) {
container.appendChild(tile);
} else {
console.warn(`No container found for category: ${module.Category} (Module: ${module.ModuleName})`);
}
});
}
// Initialize all event listeners and interactivity
function initializePageInteractivity(appData) {
const modulesMap = new Map(appData.modules.map(m => [m.ModuleID, m]));
const studyButtons = document.querySelectorAll('.study-button'); // Includes "Show All" and generated buttons
const difficultyFilter = document.getElementById('difficulty-filter');
const allTiles = document.querySelectorAll('.course-tile'); // Tiles generated in renderCourseTiles
const downloadButton = document.getElementById('download-button');
function filterTiles() {
const activeStudyButton = document.querySelector('.study-button.active');
const selectedDifficulty = difficultyFilter.value;
allTiles.forEach(tile => {
let hasStudyFilter = true; // Default to true for "Show All"
if (activeStudyButton && !activeStudyButton.classList.contains('all')) {
const filterClass = activeStudyButton.dataset.filterClass;
hasStudyFilter = tile.classList.contains(filterClass);
}
const hasDifficultyFilter = selectedDifficulty === 'all' || tile.classList.contains(selectedDifficulty);
tile.style.display = (hasStudyFilter && hasDifficultyFilter) ? 'flex' : 'none';
});
}
studyButtons.forEach(button => {
button.addEventListener('click', function() {
if (this.classList.contains('active') && !this.classList.contains('all')) {
this.classList.remove('active');
document.querySelector('.study-button.all').classList.add('active');
} else {
studyButtons.forEach(btn => btn.classList.remove('active'));
this.classList.add('active');
}
filterTiles();
});
});
const showAllButton = document.querySelector('.study-button.all');
if (showAllButton) showAllButton.classList.add('active');
difficultyFilter.addEventListener('change', filterTiles);
allTiles.forEach(tile => {
tile.addEventListener('click', function() {
const popup = document.getElementById('popup');
const moduleId = this.dataset.moduleId;
const moduleData = modulesMap.get(moduleId);
if (!moduleData) {
console.error("Module data not found for tile:", this);
return;
}
popup.style.display = 'block';
document.getElementById('course-name').textContent = moduleData.ModuleName;
document.getElementById('popup-course-description').textContent = moduleData.ModuleDescription || 'No general description available.';
const accordionContainer = popup.querySelector('.accordion');
accordionContainer.innerHTML = '';
if (moduleData.accordionDataString) {
const accordionItems = moduleData.accordionDataString.split('|');
accordionItems.forEach(item => {
const [sectionName, ...sectionDetailsParts] = item.split(':');
const sectionDetails = sectionDetailsParts.join(':');
if (sectionName.trim()) {
const accButton = document.createElement('button');
accButton.classList.add('accordion-button');
accButton.textContent = sectionName;
const panel = document.createElement('div');
panel.classList.add('panel');
panel.innerHTML = `<p>${sectionDetails}</p>`;
accordionContainer.appendChild(accButton);
accordionContainer.appendChild(panel);
accButton.addEventListener('click', function() {
this.classList.toggle('active');
const currentPanel = this.nextElementSibling;
currentPanel.style.display = (currentPanel.style.display === 'block') ? 'none' : 'block';
});
}
});
}
});
});
downloadButton.addEventListener('click', () => {
const currentlyVisibleTiles = Array.from(allTiles)
.filter(tile => tile.style.display !== 'none');
if (currentlyVisibleTiles.length === 0) {
alert('No courses selected for download.');
return;
}
const selectedCourseNames = currentlyVisibleTiles
.map(tile => {
const moduleId = tile.dataset.moduleId;
const moduleData = modulesMap.get(moduleId);
return moduleData ? moduleData.ModuleName : "Unknown Course";
});
const csvContent = 'data:text/csv;charset=utf-8,' +
'Course Name\n' +
selectedCourseNames.join('\n');
const encodedUri = encodeURI(csvContent);
const link = document.createElement('a');
link.setAttribute('href', encodedUri);
link.setAttribute('download', 'selected_courses.txt'); // Note: saving as .txt but content is CSV-like
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
filterTiles();
}
window.closePopup = function() {
document.getElementById('popup').style.display = 'none';
const accordionContainer = document.querySelector('#popup .accordion');
if (accordionContainer) {
accordionContainer.querySelectorAll('.panel').forEach(panel => {
panel.style.display = 'none';
});
accordionContainer.querySelectorAll('.accordion-button').forEach(button => {
button.classList.remove('active');
});
}
}
document.addEventListener('DOMContentLoaded', async () => {
try {
const [classesArray, modulesArray, unitsArray] = await Promise.all([
fetchAndParseCSV('classes.csv'),
fetchAndParseCSV('modules.csv'),
fetchAndParseCSV('units.csv')
]);
const appData = processRawData(classesArray, modulesArray, unitsArray);
renderFilterButtons(appData.classes, document.querySelector('.header'));
renderCourseTiles(appData.modules, appData.classes);
initializePageInteractivity(appData);
} catch (error) {
console.error("Failed to initialize the page:", error);
const body = document.querySelector('body');
if (body) {
body.innerHTML = `<p style="color: red; text-align: center; padding: 20px;">Error loading course data. Please check the CSV files for correct formatting (semicolon-separated, quotes around fields containing semicolons or double quotes) and ensure the files are accessible. Details: ${error.message}</p>`;
}
}
});