Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,27 @@ async function syncClassSwitcher() {
renderSemesterTabs();
}

function prepareSearchIndex() {
if (!pdfDatabase || !Array.isArray(pdfDatabase)) return;

pdfDatabase.forEach(pdf => {
// Optimization: Pre-compute search string to avoid repetitive toLowerCase() calls in render loop
// We use a non-enumerable property so it doesn't get serialized to localStorage
const searchStr = [
pdf.title,
pdf.description,
pdf.category,
pdf.author
].map(s => (s || '').toLowerCase()).join(' ');

Object.defineProperty(pdf, '_searchStr', {
value: searchStr,
enumerable: false,
writable: true
});
});
}

async function loadPDFDatabase() {
if (isMaintenanceActive) return;

Expand Down Expand Up @@ -451,6 +472,7 @@ async function loadPDFDatabase() {

if (shouldUseCache) {
pdfDatabase = cachedData;
prepareSearchIndex();
// --- FIX: CALL THIS TO POPULATE UI ---
syncClassSwitcher();
renderSemesterTabs();
Expand All @@ -474,6 +496,8 @@ async function loadPDFDatabase() {
data: pdfDatabase
}));

prepareSearchIndex();

// --- FIX: CALL THIS TO POPULATE UI ---
syncClassSwitcher();
renderPDFs();
Expand Down Expand Up @@ -902,26 +926,24 @@ function renderPDFs() {

// Locate renderPDFs() in script.js and update the filter section
const filteredPdfs = pdfDatabase.filter(pdf => {
const matchesSemester = pdf.semester === currentSemester;

// NEW: Check if the PDF class matches the UI's current class selection
// Note: If old documents don't have this field, they will be hidden.
const matchesClass = pdf.class === currentClass;
// Optimization: Early returns to skip expensive checks
if (pdf.class !== currentClass) return false;
if (pdf.semester !== currentSemester) return false;

let matchesCategory = false;
if (currentCategory === 'favorites') {
matchesCategory = favorites.includes(pdf.id);
} else {
matchesCategory = currentCategory === 'all' || pdf.category === currentCategory;
if (!favorites.includes(pdf.id)) return false;
} else if (currentCategory !== 'all' && pdf.category !== currentCategory) {
return false;
}

const matchesSearch = pdf.title.toLowerCase().includes(searchTerm) ||
pdf.description.toLowerCase().includes(searchTerm) ||
pdf.category.toLowerCase().includes(searchTerm) ||
pdf.author.toLowerCase().includes(searchTerm);
// Use pre-computed search string
if (pdf._searchStr) {
return pdf._searchStr.includes(searchTerm);
}

// Update return statement to include matchesClass
return matchesSemester && matchesClass && matchesCategory && matchesSearch;
// Fallback for safety (should rarely happen if prepareSearchIndex ran)
const rawSearch = (pdf.title || '') + " " + (pdf.description || '') + " " + (pdf.category || '') + " " + (pdf.author || '');
return rawSearch.toLowerCase().includes(searchTerm);
});

updatePDFCount(filteredPdfs.length);
Expand Down
Binary file added verification_functions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added verification_initial.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added verification_search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.